blob: fbc33dd9b6c03e16f581b0e26b714d5699f5e3ea [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 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
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 Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000111
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000112/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000113 * All user-defined global variables are stored in dictionary "globvardict".
114 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000116static dict_T globvardict;
117static dictitem_T globvars_var;
118#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
123 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000124static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125
126/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
129 */
130static int current_copyID = 0;
131
132/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000133 * Array to hold the hashtab with variables local to each sourced script.
134 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000136typedef struct
137{
138 dictitem_T sv_var;
139 dict_T sv_dict;
140} scriptvar_T;
141
142static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
143#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
144#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146static int echo_attr = 0; /* attributes used for ":echo" */
147
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000148/* Values for trans_function_name() argument: */
149#define TFN_INT 1 /* internal function name OK */
150#define TFN_QUIET 2 /* no error messages */
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Structure to hold info for a user function.
154 */
155typedef struct ufunc ufunc_T;
156
157struct ufunc
158{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000159 int uf_varargs; /* variable nr of arguments */
160 int uf_flags;
161 int uf_calls; /* nr of active calls */
162 garray_T uf_args; /* arguments */
163 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000164#ifdef FEAT_PROFILE
165 int uf_profiling; /* TRUE when func is being profiled */
166 /* profiling the function as a whole */
167 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000168 proftime_T uf_tm_total; /* time spent in function + children */
169 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 proftime_T *uf_tml_total; /* time spent in a line + children */
174 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000195 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000197static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000199/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000200static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
201
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000202/* list heads for garbage collection */
203static dict_T *first_dict = NULL; /* list of all dicts */
204static list_T *first_list = NULL; /* list of all lists */
205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000206/* From user function to hashitem and back. */
207static ufunc_T dumuf;
208#define UF2HIKEY(fp) ((fp)->uf_name)
209#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
210#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
211
212#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
213#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar33570922005-01-25 22:26:29 +0000215#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
216#define VAR_SHORT_LEN 20 /* short variable name length */
217#define FIXVAR_CNT 12 /* number of fixed variables */
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000220typedef struct funccall_S funccall_T;
221
222struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 ufunc_T *func; /* function being called */
225 int linenr; /* next line to be executed */
226 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000227 struct /* fixed variables for arguments */
228 {
229 dictitem_T var; /* variable (without room for name) */
230 char_u room[VAR_SHORT_LEN]; /* room for the name */
231 } fixvar[FIXVAR_CNT];
232 dict_T l_vars; /* l: local function variables */
233 dictitem_T l_vars_var; /* variable for l: scope */
234 dict_T l_avars; /* a: argument variables */
235 dictitem_T l_avars_var; /* variable for a: scope */
236 list_T l_varlist; /* list for a:000 */
237 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
238 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 linenr_T breakpoint; /* next line with breakpoint or zero */
240 int dbg_tick; /* debug_tick when breakpoint was set */
241 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000242#ifdef FEAT_PROFILE
243 proftime_T prof_child; /* time spent in a child */
244#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000245 funccall_T *caller; /* calling function or NULL */
246};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000249 * Info used by a ":for" loop.
250 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000251typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000252{
253 int fi_semicolon; /* TRUE if ending in '; var]' */
254 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 listwatch_T fi_lw; /* keep an eye on the item used. */
256 list_T *fi_list; /* list being used */
257} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000260 * Struct used by trans_function_name()
261 */
262typedef struct
263{
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000265 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dictitem_T *fd_di; /* Dictionary item used */
267} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268
Bram Moolenaara7043832005-01-21 11:56:39 +0000269
270/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 * Array to hold the value of v: variables.
272 * The value is in a dictitem, so that it can also be used in the v: scope.
273 * The reason to use this table anyway is for very quick access to the
274 * variables with the VV_ defines.
275 */
276#include "version.h"
277
278/* values for vv_flags: */
279#define VV_COMPAT 1 /* compatible, also used without "v:" */
280#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000281#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
285static struct vimvar
286{
287 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288 dictitem_T vv_di; /* value and name for key */
289 char vv_filler[16]; /* space for LONGEST name below!!! */
290 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
291} vimvars[VV_LEN] =
292{
293 /*
294 * The order here must match the VV_ defines in vim.h!
295 * Initializing a union does not work, leave tv.vval empty to get zero's.
296 */
297 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
298 {VV_NAME("count1", VAR_NUMBER), VV_RO},
299 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
300 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
301 {VV_NAME("warningmsg", VAR_STRING), 0},
302 {VV_NAME("statusmsg", VAR_STRING), 0},
303 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
305 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
307 {VV_NAME("termresponse", VAR_STRING), VV_RO},
308 {VV_NAME("fname", VAR_STRING), VV_RO},
309 {VV_NAME("lang", VAR_STRING), VV_RO},
310 {VV_NAME("lc_time", VAR_STRING), VV_RO},
311 {VV_NAME("ctype", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
313 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
314 {VV_NAME("fname_in", VAR_STRING), VV_RO},
315 {VV_NAME("fname_out", VAR_STRING), VV_RO},
316 {VV_NAME("fname_new", VAR_STRING), VV_RO},
317 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
318 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
319 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
322 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("progname", VAR_STRING), VV_RO},
324 {VV_NAME("servername", VAR_STRING), VV_RO},
325 {VV_NAME("dying", VAR_NUMBER), VV_RO},
326 {VV_NAME("exception", VAR_STRING), VV_RO},
327 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
328 {VV_NAME("register", VAR_STRING), VV_RO},
329 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
330 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000331 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
332 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000333 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000334 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
335 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000336 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000341 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000342 {VV_NAME("swapname", VAR_STRING), VV_RO},
343 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000344 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000346 {VV_NAME("mouse_win", VAR_NUMBER), 0},
347 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
348 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000349 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000350 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000351};
352
353/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000354#define vv_type vv_di.di_tv.v_type
355#define vv_nr vv_di.di_tv.vval.v_number
356#define vv_float vv_di.di_tv.vval.v_float
357#define vv_str vv_di.di_tv.vval.v_string
358#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000359
360/*
361 * The v: variables are stored in dictionary "vimvardict".
362 * "vimvars_var" is the variable that is used for the "l:" scope.
363 */
364static dict_T vimvardict;
365static dictitem_T vimvars_var;
366#define vimvarht vimvardict.dv_hashtab
367
Bram Moolenaara40058a2005-07-11 22:42:07 +0000368static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
369static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
370#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
371static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
372#endif
373static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
374static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
375static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000376static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
377static void list_glob_vars __ARGS((int *first));
378static void list_buf_vars __ARGS((int *first));
379static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000380#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_vim_vars __ARGS((int *first));
384static void list_script_vars __ARGS((int *first));
385static void list_func_vars __ARGS((int *first));
386static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
388static int check_changedtick __ARGS((char_u *arg));
389static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
390static void clear_lval __ARGS((lval_T *lp));
391static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
392static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
393static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
394static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
395static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
396static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
397static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
398static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
399static void item_lock __ARGS((typval_T *tv, int deep, int lock));
400static int tv_islocked __ARGS((typval_T *tv));
401
Bram Moolenaar33570922005-01-25 22:26:29 +0000402static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
403static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000408static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
409static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000411static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000416static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static listitem_T *listitem_alloc __ARGS((void));
418static void listitem_free __ARGS((listitem_T *item));
419static void listitem_remove __ARGS((list_T *l, listitem_T *item));
420static long list_len __ARGS((list_T *l));
421static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
422static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
423static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void list_append __ARGS((list_T *l, listitem_T *item));
428static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000429static int list_append_string __ARGS((list_T *l, char_u *str, int len));
430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
432static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
433static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *list2string __ARGS((typval_T *tv, int copyID));
437static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000438static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
439static void set_ref_in_list __ARGS((list_T *l, int copyID));
440static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000442static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static dictitem_T *dictitem_alloc __ARGS((char_u *key));
444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
446static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int dict_add __ARGS((dict_T *d, dictitem_T *item));
449static long dict_len __ARGS((dict_T *d));
450static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000453static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
454static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static int string2float __ARGS((char_u *text, float_T *value));
458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
460static int find_internal_func __ARGS((char_u *name));
461static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
462static 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));
463static 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 Moolenaar89d40322006-08-29 15:30:07 +0000464static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000465static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
468static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
490static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000492static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000495static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000497#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000498static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000499static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#ifdef FEAT_FLOAT
505static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
506#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
510static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000523static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000524static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
530static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
532#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000533static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000542static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000544static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000558static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000559static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000560static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000561static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000564static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000572static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000586static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000592static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000604#ifdef FEAT_FLOAT
605static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
606#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000611static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000612static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000613static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000615static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000619#ifdef vim_mkdir
620static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
621#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000625static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626#ifdef FEAT_FLOAT
627static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
628#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000630static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000631static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000633static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000634static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000646#ifdef FEAT_FLOAT
647static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000650static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000652static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000659static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000660static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000661static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000662static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000664static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000666static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000672static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000673static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000676#ifdef FEAT_FLOAT
677static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
679#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000680static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681#ifdef HAVE_STRFTIME
682static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
684static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000695static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000697static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000698static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000699static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000700static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000701static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000703static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#ifdef FEAT_FLOAT
708static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
709#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000720static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000723static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000725static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000726static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static int get_env_len __ARGS((char_u **arg));
728static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000729static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000730static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
731#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
732#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
733 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000734static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000736static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000737static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
738static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static typval_T *alloc_tv __ARGS((void));
740static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void init_tv __ARGS((typval_T *varp));
742static long get_tv_number __ARGS((typval_T *varp));
743static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000744static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static char_u *get_tv_string __ARGS((typval_T *varp));
746static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000747static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000749static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
751static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
752static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000753static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
754static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
756static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000757static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000758static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000760static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
762static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
763static int eval_fname_script __ARGS((char_u *p));
764static int eval_fname_sid __ARGS((char_u *p));
765static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766static ufunc_T *find_func __ARGS((char_u *name));
767static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000768static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#ifdef FEAT_PROFILE
770static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000771static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
772static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
773static int
774# ifdef __BORLANDC__
775 _RTLENTRYF
776# endif
777 prof_total_cmp __ARGS((const void *s1, const void *s2));
778static int
779# ifdef __BORLANDC__
780 _RTLENTRYF
781# endif
782 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000784static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000785static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000786static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static void func_free __ARGS((ufunc_T *fp));
788static void func_unref __ARGS((char_u *name));
789static void func_ref __ARGS((char_u *name));
790static 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));
791static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000792static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
793static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000794static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000795static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000796static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000798/* Character used as separated in autoload function/variable names. */
799#define AUTOLOAD_CHAR '#'
800
Bram Moolenaar33570922005-01-25 22:26:29 +0000801/*
802 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000803 */
804 void
805eval_init()
806{
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 int i;
808 struct vimvar *p;
809
810 init_var_dict(&globvardict, &globvars_var);
811 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000812 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000813 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000814
815 for (i = 0; i < VV_LEN; ++i)
816 {
817 p = &vimvars[i];
818 STRCPY(p->vv_di.di_key, p->vv_name);
819 if (p->vv_flags & VV_RO)
820 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
821 else if (p->vv_flags & VV_RO_SBX)
822 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
823 else
824 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000825
826 /* add to v: scope dict, unless the value is not always available */
827 if (p->vv_type != VAR_UNKNOWN)
828 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000829 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000830 /* add to compat scope dict */
831 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000833 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000834}
835
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000836#if defined(EXITFREE) || defined(PROTO)
837 void
838eval_clear()
839{
840 int i;
841 struct vimvar *p;
842
843 for (i = 0; i < VV_LEN; ++i)
844 {
845 p = &vimvars[i];
846 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000847 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000848 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000849 p->vv_di.di_tv.vval.v_string = NULL;
850 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000851 }
852 hash_clear(&vimvarht);
853 hash_clear(&compat_hashtab);
854
855 /* script-local variables */
856 for (i = 1; i <= ga_scripts.ga_len; ++i)
857 vars_clear(&SCRIPT_VARS(i));
858 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000859 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000860
861 /* global variables */
862 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000863
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000864 /* autoloaded script names */
865 ga_clear_strings(&ga_loaded);
866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000867 /* unreferenced lists and dicts */
868 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000869
870 /* functions */
871 free_all_functions();
872 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000873}
874#endif
875
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * Return the name of the executed function.
878 */
879 char_u *
880func_name(cookie)
881 void *cookie;
882{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884}
885
886/*
887 * Return the address holding the next breakpoint line for a funccall cookie.
888 */
889 linenr_T *
890func_breakpoint(cookie)
891 void *cookie;
892{
Bram Moolenaar33570922005-01-25 22:26:29 +0000893 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894}
895
896/*
897 * Return the address holding the debug tick for a funccall cookie.
898 */
899 int *
900func_dbg_tick(cookie)
901 void *cookie;
902{
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904}
905
906/*
907 * Return the nesting level for a funccall cookie.
908 */
909 int
910func_level(cookie)
911 void *cookie;
912{
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914}
915
916/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000917funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919/*
920 * Return TRUE when a function was ended by a ":return" command.
921 */
922 int
923current_func_returned()
924{
925 return current_funccal->returned;
926}
927
928
929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 * Set an internal variable to a string value. Creates the variable if it does
931 * not already exist.
932 */
933 void
934set_internal_string_var(name, value)
935 char_u *name;
936 char_u *value;
937{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000938 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 val = vim_strsave(value);
942 if (val != NULL)
943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000945 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000947 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000948 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950 }
951}
952
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000953static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000954static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955static char_u *redir_endp = NULL;
956static char_u *redir_varname = NULL;
957
958/*
959 * Start recording command output to a variable
960 * Returns OK if successfully completed the setup. FAIL otherwise.
961 */
962 int
963var_redir_start(name, append)
964 char_u *name;
965 int append; /* append to an existing variable */
966{
967 int save_emsg;
968 int err;
969 typval_T tv;
970
971 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000972 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000973 {
974 EMSG(_(e_invarg));
975 return FAIL;
976 }
977
978 redir_varname = vim_strsave(name);
979 if (redir_varname == NULL)
980 return FAIL;
981
982 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
983 if (redir_lval == NULL)
984 {
985 var_redir_stop();
986 return FAIL;
987 }
988
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000989 /* The output is stored in growarray "redir_ga" until redirection ends. */
990 ga_init2(&redir_ga, (int)sizeof(char), 500);
991
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000992 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000993 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
994 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000995 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
996 {
997 if (redir_endp != NULL && *redir_endp != NUL)
998 /* Trailing characters are present after the variable name */
999 EMSG(_(e_trailing));
1000 else
1001 EMSG(_(e_invarg));
1002 var_redir_stop();
1003 return FAIL;
1004 }
1005
1006 /* check if we can write to the variable: set it to or append an empty
1007 * string */
1008 save_emsg = did_emsg;
1009 did_emsg = FALSE;
1010 tv.v_type = VAR_STRING;
1011 tv.vval.v_string = (char_u *)"";
1012 if (append)
1013 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1014 else
1015 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1016 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001017 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018 if (err)
1019 {
1020 var_redir_stop();
1021 return FAIL;
1022 }
1023 if (redir_lval->ll_newkey != NULL)
1024 {
1025 /* Dictionary item was created, don't do it again. */
1026 vim_free(redir_lval->ll_newkey);
1027 redir_lval->ll_newkey = NULL;
1028 }
1029
1030 return OK;
1031}
1032
1033/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001034 * Append "value[value_len]" to the variable set by var_redir_start().
1035 * The actual appending is postponed until redirection ends, because the value
1036 * appended may in fact be the string we write to, changing it may cause freed
1037 * memory to be used:
1038 * :redir => foo
1039 * :let foo
1040 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 */
1042 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001043var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001045 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001047 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001048
1049 if (redir_lval == NULL)
1050 return;
1051
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001052 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001053 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001055 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001057 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001058 {
1059 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001060 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001061 }
1062 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064}
1065
1066/*
1067 * Stop redirecting command output to a variable.
1068 */
1069 void
1070var_redir_stop()
1071{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001072 typval_T tv;
1073
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074 if (redir_lval != NULL)
1075 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001076 /* Append the trailing NUL. */
1077 ga_append(&redir_ga, NUL);
1078
1079 /* Assign the text to the variable. */
1080 tv.v_type = VAR_STRING;
1081 tv.vval.v_string = redir_ga.ga_data;
1082 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1083 vim_free(tv.vval.v_string);
1084
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 clear_lval(redir_lval);
1086 vim_free(redir_lval);
1087 redir_lval = NULL;
1088 }
1089 vim_free(redir_varname);
1090 redir_varname = NULL;
1091}
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093# if defined(FEAT_MBYTE) || defined(PROTO)
1094 int
1095eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1096 char_u *enc_from;
1097 char_u *enc_to;
1098 char_u *fname_from;
1099 char_u *fname_to;
1100{
1101 int err = FALSE;
1102
1103 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1104 set_vim_var_string(VV_CC_TO, enc_to, -1);
1105 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1106 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1107 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1108 err = TRUE;
1109 set_vim_var_string(VV_CC_FROM, NULL, -1);
1110 set_vim_var_string(VV_CC_TO, NULL, -1);
1111 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113
1114 if (err)
1115 return FAIL;
1116 return OK;
1117}
1118# endif
1119
1120# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1121 int
1122eval_printexpr(fname, args)
1123 char_u *fname;
1124 char_u *args;
1125{
1126 int err = FALSE;
1127
1128 set_vim_var_string(VV_FNAME_IN, fname, -1);
1129 set_vim_var_string(VV_CMDARG, args, -1);
1130 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1131 err = TRUE;
1132 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1133 set_vim_var_string(VV_CMDARG, NULL, -1);
1134
1135 if (err)
1136 {
1137 mch_remove(fname);
1138 return FAIL;
1139 }
1140 return OK;
1141}
1142# endif
1143
1144# if defined(FEAT_DIFF) || defined(PROTO)
1145 void
1146eval_diff(origfile, newfile, outfile)
1147 char_u *origfile;
1148 char_u *newfile;
1149 char_u *outfile;
1150{
1151 int err = FALSE;
1152
1153 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1154 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1155 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1156 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1157 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1158 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1159 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1160}
1161
1162 void
1163eval_patch(origfile, difffile, outfile)
1164 char_u *origfile;
1165 char_u *difffile;
1166 char_u *outfile;
1167{
1168 int err;
1169
1170 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1171 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1172 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1173 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1174 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1175 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1176 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1177}
1178# endif
1179
1180/*
1181 * Top level evaluation function, returning a boolean.
1182 * Sets "error" to TRUE if there was an error.
1183 * Return TRUE or FALSE.
1184 */
1185 int
1186eval_to_bool(arg, error, nextcmd, skip)
1187 char_u *arg;
1188 int *error;
1189 char_u **nextcmd;
1190 int skip; /* only parse, don't execute */
1191{
Bram Moolenaar33570922005-01-25 22:26:29 +00001192 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 int retval = FALSE;
1194
1195 if (skip)
1196 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else
1200 {
1201 *error = FALSE;
1202 if (!skip)
1203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001204 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 }
1208 if (skip)
1209 --emsg_skip;
1210
1211 return retval;
1212}
1213
1214/*
1215 * Top level evaluation function, returning a string. If "skip" is TRUE,
1216 * only parsing to "nextcmd" is done, without reporting errors. Return
1217 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1218 */
1219 char_u *
1220eval_to_string_skip(arg, nextcmd, skip)
1221 char_u *arg;
1222 char_u **nextcmd;
1223 int skip; /* only parse, don't execute */
1224{
Bram Moolenaar33570922005-01-25 22:26:29 +00001225 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 char_u *retval;
1227
1228 if (skip)
1229 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 retval = NULL;
1232 else
1233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001234 retval = vim_strsave(get_tv_string(&tv));
1235 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
1237 if (skip)
1238 --emsg_skip;
1239
1240 return retval;
1241}
1242
1243/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001244 * Skip over an expression at "*pp".
1245 * Return FAIL for an error, OK otherwise.
1246 */
1247 int
1248skip_expr(pp)
1249 char_u **pp;
1250{
Bram Moolenaar33570922005-01-25 22:26:29 +00001251 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001252
1253 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001255}
1256
1257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 * Top level evaluation function, returning a string.
1259 * Return pointer to allocated memory, or NULL for failure.
1260 */
1261 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001262eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 char_u *arg;
1264 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001265 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001269 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 retval = NULL;
1273 else
1274 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001275 if (dolist && tv.v_type == VAR_LIST)
1276 {
1277 ga_init2(&ga, (int)sizeof(char), 80);
1278 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1279 ga_append(&ga, NUL);
1280 retval = (char_u *)ga.ga_data;
1281 }
1282 else
1283 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286
1287 return retval;
1288}
1289
1290/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001291 * Call eval_to_string() without using current local variables and using
1292 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 */
1294 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001295eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 char_u *arg;
1297 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001298 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 char_u *retval;
1301 void *save_funccalp;
1302
1303 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001304 if (use_sandbox)
1305 ++sandbox;
1306 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001307 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001308 if (use_sandbox)
1309 --sandbox;
1310 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 restore_funccal(save_funccalp);
1312 return retval;
1313}
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315/*
1316 * Top level evaluation function, returning a number.
1317 * Evaluates "expr" silently.
1318 * Returns -1 for an error.
1319 */
1320 int
1321eval_to_number(expr)
1322 char_u *expr;
1323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001326 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
1328 ++emsg_off;
1329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 retval = -1;
1332 else
1333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001334 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 --emsg_off;
1338
1339 return retval;
1340}
1341
Bram Moolenaara40058a2005-07-11 22:42:07 +00001342/*
1343 * Prepare v: variable "idx" to be used.
1344 * Save the current typeval in "save_tv".
1345 * When not used yet add the variable to the v: hashtable.
1346 */
1347 static void
1348prepare_vimvar(idx, save_tv)
1349 int idx;
1350 typval_T *save_tv;
1351{
1352 *save_tv = vimvars[idx].vv_tv;
1353 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1354 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1355}
1356
1357/*
1358 * Restore v: variable "idx" to typeval "save_tv".
1359 * When no longer defined, remove the variable from the v: hashtable.
1360 */
1361 static void
1362restore_vimvar(idx, save_tv)
1363 int idx;
1364 typval_T *save_tv;
1365{
1366 hashitem_T *hi;
1367
Bram Moolenaara40058a2005-07-11 22:42:07 +00001368 vimvars[idx].vv_tv = *save_tv;
1369 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1370 {
1371 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1372 if (HASHITEM_EMPTY(hi))
1373 EMSG2(_(e_intern2), "restore_vimvar()");
1374 else
1375 hash_remove(&vimvarht, hi);
1376 }
1377}
1378
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001379#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001380/*
1381 * Evaluate an expression to a list with suggestions.
1382 * For the "expr:" part of 'spellsuggest'.
1383 */
1384 list_T *
1385eval_spell_expr(badword, expr)
1386 char_u *badword;
1387 char_u *expr;
1388{
1389 typval_T save_val;
1390 typval_T rettv;
1391 list_T *list = NULL;
1392 char_u *p = skipwhite(expr);
1393
1394 /* Set "v:val" to the bad word. */
1395 prepare_vimvar(VV_VAL, &save_val);
1396 vimvars[VV_VAL].vv_type = VAR_STRING;
1397 vimvars[VV_VAL].vv_str = badword;
1398 if (p_verbose == 0)
1399 ++emsg_off;
1400
1401 if (eval1(&p, &rettv, TRUE) == OK)
1402 {
1403 if (rettv.v_type != VAR_LIST)
1404 clear_tv(&rettv);
1405 else
1406 list = rettv.vval.v_list;
1407 }
1408
1409 if (p_verbose == 0)
1410 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001411 restore_vimvar(VV_VAL, &save_val);
1412
1413 return list;
1414}
1415
1416/*
1417 * "list" is supposed to contain two items: a word and a number. Return the
1418 * word in "pp" and the number as the return value.
1419 * Return -1 if anything isn't right.
1420 * Used to get the good word and score from the eval_spell_expr() result.
1421 */
1422 int
1423get_spellword(list, pp)
1424 list_T *list;
1425 char_u **pp;
1426{
1427 listitem_T *li;
1428
1429 li = list->lv_first;
1430 if (li == NULL)
1431 return -1;
1432 *pp = get_tv_string(&li->li_tv);
1433
1434 li = li->li_next;
1435 if (li == NULL)
1436 return -1;
1437 return get_tv_number(&li->li_tv);
1438}
1439#endif
1440
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001441/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001442 * Top level evaluation function.
1443 * Returns an allocated typval_T with the result.
1444 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001445 */
1446 typval_T *
1447eval_expr(arg, nextcmd)
1448 char_u *arg;
1449 char_u **nextcmd;
1450{
1451 typval_T *tv;
1452
1453 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001454 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001455 {
1456 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001457 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001458 }
1459
1460 return tv;
1461}
1462
1463
Bram Moolenaar4f688582007-07-24 12:34:30 +00001464#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1465 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001467 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001468 * Uses argv[argc] for the function arguments. Only Number and String
1469 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472 static int
1473call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 char_u *func;
1475 int argc;
1476 char_u **argv;
1477 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar33570922005-01-25 22:26:29 +00001480 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 long n;
1482 int len;
1483 int i;
1484 int doesrange;
1485 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001486 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001488 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001490 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 for (i = 0; i < argc; i++)
1493 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001494 /* Pass a NULL or empty argument as an empty string */
1495 if (argv[i] == NULL || *argv[i] == NUL)
1496 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001497 argvars[i].v_type = VAR_STRING;
1498 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001499 continue;
1500 }
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 /* Recognize a number argument, the others must be strings. */
1503 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1504 if (len != 0 && len == (int)STRLEN(argv[i]))
1505 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001506 argvars[i].v_type = VAR_NUMBER;
1507 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 else
1510 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001511 argvars[i].v_type = VAR_STRING;
1512 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514 }
1515
1516 if (safe)
1517 {
1518 save_funccalp = save_funccal();
1519 ++sandbox;
1520 }
1521
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001522 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1523 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001525 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (safe)
1527 {
1528 --sandbox;
1529 restore_funccal(save_funccalp);
1530 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001531 vim_free(argvars);
1532
1533 if (ret == FAIL)
1534 clear_tv(rettv);
1535
1536 return ret;
1537}
1538
Bram Moolenaar4f688582007-07-24 12:34:30 +00001539# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001540/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001541 * Call vimL function "func" and return the result as a string.
1542 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001543 * Uses argv[argc] for the function arguments.
1544 */
1545 void *
1546call_func_retstr(func, argc, argv, safe)
1547 char_u *func;
1548 int argc;
1549 char_u **argv;
1550 int safe; /* use the sandbox */
1551{
1552 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001553 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001554
1555 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1556 return NULL;
1557
1558 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 return retval;
1561}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001562# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001563
Bram Moolenaar4f688582007-07-24 12:34:30 +00001564# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001566 * Call vimL function "func" and return the result as a number.
1567 * Returns -1 when calling the function fails.
1568 * Uses argv[argc] for the function arguments.
1569 */
1570 long
1571call_func_retnr(func, argc, argv, safe)
1572 char_u *func;
1573 int argc;
1574 char_u **argv;
1575 int safe; /* use the sandbox */
1576{
1577 typval_T rettv;
1578 long retval;
1579
1580 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1581 return -1;
1582
1583 retval = get_tv_number_chk(&rettv, NULL);
1584 clear_tv(&rettv);
1585 return retval;
1586}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001587# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001588
1589/*
1590 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 * Uses argv[argc] for the function arguments.
1592 */
1593 void *
1594call_func_retlist(func, argc, argv, safe)
1595 char_u *func;
1596 int argc;
1597 char_u **argv;
1598 int safe; /* use the sandbox */
1599{
1600 typval_T rettv;
1601
1602 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1603 return NULL;
1604
1605 if (rettv.v_type != VAR_LIST)
1606 {
1607 clear_tv(&rettv);
1608 return NULL;
1609 }
1610
1611 return rettv.vval.v_list;
1612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#endif
1614
Bram Moolenaar4f688582007-07-24 12:34:30 +00001615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
1617 * Save the current function call pointer, and set it to NULL.
1618 * Used when executing autocommands and for ":source".
1619 */
1620 void *
1621save_funccal()
1622{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001623 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 current_funccal = NULL;
1626 return (void *)fc;
1627}
1628
1629 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001630restore_funccal(vfc)
1631 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001633 funccall_T *fc = (funccall_T *)vfc;
1634
1635 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636}
1637
Bram Moolenaar05159a02005-02-26 23:04:13 +00001638#if defined(FEAT_PROFILE) || defined(PROTO)
1639/*
1640 * Prepare profiling for entering a child or something else that is not
1641 * counted for the script/function itself.
1642 * Should always be called in pair with prof_child_exit().
1643 */
1644 void
1645prof_child_enter(tm)
1646 proftime_T *tm; /* place to store waittime */
1647{
1648 funccall_T *fc = current_funccal;
1649
1650 if (fc != NULL && fc->func->uf_profiling)
1651 profile_start(&fc->prof_child);
1652 script_prof_save(tm);
1653}
1654
1655/*
1656 * Take care of time spent in a child.
1657 * Should always be called after prof_child_enter().
1658 */
1659 void
1660prof_child_exit(tm)
1661 proftime_T *tm; /* where waittime was stored */
1662{
1663 funccall_T *fc = current_funccal;
1664
1665 if (fc != NULL && fc->func->uf_profiling)
1666 {
1667 profile_end(&fc->prof_child);
1668 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1669 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1670 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1671 }
1672 script_prof_restore(tm);
1673}
1674#endif
1675
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#ifdef FEAT_FOLDING
1678/*
1679 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1680 * it in "*cp". Doesn't give error messages.
1681 */
1682 int
1683eval_foldexpr(arg, cp)
1684 char_u *arg;
1685 int *cp;
1686{
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 int retval;
1689 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001690 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1691 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001694 if (use_sandbox)
1695 ++sandbox;
1696 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 retval = 0;
1700 else
1701 {
1702 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001703 if (tv.v_type == VAR_NUMBER)
1704 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001705 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 retval = 0;
1707 else
1708 {
1709 /* If the result is a string, check if there is a non-digit before
1710 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001711 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if (!VIM_ISDIGIT(*s) && *s != '-')
1713 *cp = *s++;
1714 retval = atol((char *)s);
1715 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
1718 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001719 if (use_sandbox)
1720 --sandbox;
1721 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 return retval;
1724}
1725#endif
1726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001728 * ":let" list all variable values
1729 * ":let var1 var2" list variable values
1730 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 * ":let var += expr" assignment command.
1732 * ":let var -= expr" assignment command.
1733 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 */
1736 void
1737ex_let(eap)
1738 exarg_T *eap;
1739{
1740 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001741 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001742 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 int var_count = 0;
1745 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001747 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001748 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaardb552d602006-03-23 22:59:57 +00001750 argend = skip_var_list(arg, &var_count, &semicolon);
1751 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001753 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1754 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001755 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001758 /*
1759 * ":let" without "=": list variables
1760 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761 if (*arg == '[')
1762 EMSG(_(e_invarg));
1763 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001764 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001765 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001769 list_glob_vars(&first);
1770 list_buf_vars(&first);
1771 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001772#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001773 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001774#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001775 list_script_vars(&first);
1776 list_func_vars(&first);
1777 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 eap->nextcmd = check_nextcmd(arg);
1780 }
1781 else
1782 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 op[0] = '=';
1784 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001785 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001786 {
1787 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1788 op[0] = expr[-1]; /* +=, -= or .= */
1789 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (eap->skip)
1793 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (eap->skip)
1796 {
1797 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 --emsg_skip;
1800 }
1801 else if (i != FAIL)
1802 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
1807 }
1808}
1809
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810/*
1811 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1812 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001813 * When "nextchars" is not NULL it points to a string with characters that
1814 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1815 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 * Returns OK or FAIL;
1817 */
1818 static int
1819ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1820 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001821 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 int copy; /* copy values from "tv", don't move */
1823 int semicolon; /* from skip_var_list() */
1824 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826{
1827 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001828 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001830 listitem_T *item;
1831 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832
1833 if (*arg != '[')
1834 {
1835 /*
1836 * ":let var = expr" or ":for var in list"
1837 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001838 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 return FAIL;
1840 return OK;
1841 }
1842
1843 /*
1844 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1845 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001846 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 {
1848 EMSG(_(e_listreq));
1849 return FAIL;
1850 }
1851
1852 i = list_len(l);
1853 if (semicolon == 0 && var_count < i)
1854 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001855 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 return FAIL;
1857 }
1858 if (var_count - semicolon > i)
1859 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001860 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return FAIL;
1862 }
1863
1864 item = l->lv_first;
1865 while (*arg != ']')
1866 {
1867 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 item = item->li_next;
1870 if (arg == NULL)
1871 return FAIL;
1872
1873 arg = skipwhite(arg);
1874 if (*arg == ';')
1875 {
1876 /* Put the rest of the list (may be empty) in the var after ';'.
1877 * Create a new list for this. */
1878 l = list_alloc();
1879 if (l == NULL)
1880 return FAIL;
1881 while (item != NULL)
1882 {
1883 list_append_tv(l, &item->li_tv);
1884 item = item->li_next;
1885 }
1886
1887 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001888 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 ltv.vval.v_list = l;
1890 l->lv_refcount = 1;
1891
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1893 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 clear_tv(&ltv);
1895 if (arg == NULL)
1896 return FAIL;
1897 break;
1898 }
1899 else if (*arg != ',' && *arg != ']')
1900 {
1901 EMSG2(_(e_intern2), "ex_let_vars()");
1902 return FAIL;
1903 }
1904 }
1905
1906 return OK;
1907}
1908
1909/*
1910 * Skip over assignable variable "var" or list of variables "[var, var]".
1911 * Used for ":let varvar = expr" and ":for varvar in expr".
1912 * For "[var, var]" increment "*var_count" for each variable.
1913 * for "[var, var; var]" set "semicolon".
1914 * Return NULL for an error.
1915 */
1916 static char_u *
1917skip_var_list(arg, var_count, semicolon)
1918 char_u *arg;
1919 int *var_count;
1920 int *semicolon;
1921{
1922 char_u *p, *s;
1923
1924 if (*arg == '[')
1925 {
1926 /* "[var, var]": find the matching ']'. */
1927 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001928 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 {
1930 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1931 s = skip_var_one(p);
1932 if (s == p)
1933 {
1934 EMSG2(_(e_invarg2), p);
1935 return NULL;
1936 }
1937 ++*var_count;
1938
1939 p = skipwhite(s);
1940 if (*p == ']')
1941 break;
1942 else if (*p == ';')
1943 {
1944 if (*semicolon == 1)
1945 {
1946 EMSG(_("Double ; in list of variables"));
1947 return NULL;
1948 }
1949 *semicolon = 1;
1950 }
1951 else if (*p != ',')
1952 {
1953 EMSG2(_(e_invarg2), p);
1954 return NULL;
1955 }
1956 }
1957 return p + 1;
1958 }
1959 else
1960 return skip_var_one(arg);
1961}
1962
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001963/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001964 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001965 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 static char_u *
1968skip_var_one(arg)
1969 char_u *arg;
1970{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001971 if (*arg == '@' && arg[1] != NUL)
1972 return arg + 2;
1973 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1974 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975}
1976
Bram Moolenaara7043832005-01-21 11:56:39 +00001977/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001978 * List variables for hashtab "ht" with prefix "prefix".
1979 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001982list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001983 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001984 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001986 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001987{
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 hashitem_T *hi;
1989 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001990 int todo;
1991
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001992 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1994 {
1995 if (!HASHITEM_EMPTY(hi))
1996 {
1997 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001998 di = HI2DI(hi);
1999 if (empty || di->di_tv.v_type != VAR_STRING
2000 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002001 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002002 }
2003 }
2004}
2005
2006/*
2007 * List global variables.
2008 */
2009 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002010list_glob_vars(first)
2011 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002012{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002013 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002014}
2015
2016/*
2017 * List buffer variables.
2018 */
2019 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002020list_buf_vars(first)
2021 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002022{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002023 char_u numbuf[NUMBUFLEN];
2024
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002025 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2026 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002027
2028 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002029 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2030 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002031}
2032
2033/*
2034 * List window variables.
2035 */
2036 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002037list_win_vars(first)
2038 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002039{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002040 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2041 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002042}
2043
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002044#ifdef FEAT_WINDOWS
2045/*
2046 * List tab page variables.
2047 */
2048 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002049list_tab_vars(first)
2050 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002051{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002052 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2053 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054}
2055#endif
2056
Bram Moolenaara7043832005-01-21 11:56:39 +00002057/*
2058 * List Vim variables.
2059 */
2060 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002061list_vim_vars(first)
2062 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002064 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065}
2066
2067/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002068 * List script-local variables, if there is a script.
2069 */
2070 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071list_script_vars(first)
2072 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002073{
2074 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002075 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2076 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002077}
2078
2079/*
2080 * List function variables, if there is a function.
2081 */
2082 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083list_func_vars(first)
2084 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002085{
2086 if (current_funccal != NULL)
2087 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002088 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002089}
2090
2091/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 * List variables in "arg".
2093 */
2094 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 exarg_T *eap;
2097 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099{
2100 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103 char_u *name_start;
2104 char_u *arg_subsc;
2105 char_u *tofree;
2106 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107
2108 while (!ends_excmd(*arg) && !got_int)
2109 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002112 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2114 {
2115 emsg_severe = TRUE;
2116 EMSG(_(e_trailing));
2117 break;
2118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 /* get_name_len() takes care of expanding curly braces */
2123 name_start = name = arg;
2124 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2125 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002127 /* This is mainly to keep test 49 working: when expanding
2128 * curly braces fails overrule the exception error message. */
2129 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002131 emsg_severe = TRUE;
2132 EMSG2(_(e_invarg2), arg);
2133 break;
2134 }
2135 error = TRUE;
2136 }
2137 else
2138 {
2139 if (tofree != NULL)
2140 name = tofree;
2141 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 else
2144 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145 /* handle d.key, l[idx], f(expr) */
2146 arg_subsc = arg;
2147 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002148 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002150 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002151 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002153 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002154 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 case 'g': list_glob_vars(first); break;
2156 case 'b': list_buf_vars(first); break;
2157 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002158#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 case 'v': list_vim_vars(first); break;
2162 case 's': list_script_vars(first); break;
2163 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002164 default:
2165 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002167 }
2168 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002169 {
2170 char_u numbuf[NUMBUFLEN];
2171 char_u *tf;
2172 int c;
2173 char_u *s;
2174
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002175 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176 c = *arg;
2177 *arg = NUL;
2178 list_one_var_a((char_u *)"",
2179 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 tv.v_type,
2181 s == NULL ? (char_u *)"" : s,
2182 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 *arg = c;
2184 vim_free(tf);
2185 }
2186 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 }
2189 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190
2191 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193
2194 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 }
2196
2197 return arg;
2198}
2199
2200/*
2201 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2202 * Returns a pointer to the char just after the var name.
2203 * Returns NULL if there is an error.
2204 */
2205 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002208 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002210 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002211 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212{
2213 int c1;
2214 char_u *name;
2215 char_u *p;
2216 char_u *arg_end = NULL;
2217 int len;
2218 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002219 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220
2221 /*
2222 * ":let $VAR = expr": Set environment variable.
2223 */
2224 if (*arg == '$')
2225 {
2226 /* Find the end of the name. */
2227 ++arg;
2228 name = arg;
2229 len = get_env_len(&arg);
2230 if (len == 0)
2231 EMSG2(_(e_invarg2), name - 1);
2232 else
2233 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002234 if (op != NULL && (*op == '+' || *op == '-'))
2235 EMSG2(_(e_letwrong), op);
2236 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002237 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 EMSG(_(e_letunexp));
2239 else
2240 {
2241 c1 = name[len];
2242 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243 p = get_tv_string_chk(tv);
2244 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002245 {
2246 int mustfree = FALSE;
2247 char_u *s = vim_getenv(name, &mustfree);
2248
2249 if (s != NULL)
2250 {
2251 p = tofree = concat_str(s, p);
2252 if (mustfree)
2253 vim_free(s);
2254 }
2255 }
2256 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002257 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259 if (STRICMP(name, "HOME") == 0)
2260 init_homedir();
2261 else if (didset_vim && STRICMP(name, "VIM") == 0)
2262 didset_vim = FALSE;
2263 else if (didset_vimruntime
2264 && STRICMP(name, "VIMRUNTIME") == 0)
2265 didset_vimruntime = FALSE;
2266 arg_end = arg;
2267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 }
2271 }
2272 }
2273
2274 /*
2275 * ":let &option = expr": Set option value.
2276 * ":let &l:option = expr": Set local option value.
2277 * ":let &g:option = expr": Set global option value.
2278 */
2279 else if (*arg == '&')
2280 {
2281 /* Find the end of the name. */
2282 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002283 if (p == NULL || (endchars != NULL
2284 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 EMSG(_(e_letunexp));
2286 else
2287 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 long n;
2289 int opt_type;
2290 long numval;
2291 char_u *stringval = NULL;
2292 char_u *s;
2293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 c1 = *p;
2295 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296
2297 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002298 s = get_tv_string_chk(tv); /* != NULL if number or string */
2299 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 {
2301 opt_type = get_option_value(arg, &numval,
2302 &stringval, opt_flags);
2303 if ((opt_type == 1 && *op == '.')
2304 || (opt_type == 0 && *op != '.'))
2305 EMSG2(_(e_letwrong), op);
2306 else
2307 {
2308 if (opt_type == 1) /* number */
2309 {
2310 if (*op == '+')
2311 n = numval + n;
2312 else
2313 n = numval - n;
2314 }
2315 else if (opt_type == 0 && stringval != NULL) /* string */
2316 {
2317 s = concat_str(stringval, s);
2318 vim_free(stringval);
2319 stringval = s;
2320 }
2321 }
2322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002323 if (s != NULL)
2324 {
2325 set_option_value(arg, n, s, opt_flags);
2326 arg_end = p;
2327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
2331 }
2332
2333 /*
2334 * ":let @r = expr": Set register contents.
2335 */
2336 else if (*arg == '@')
2337 {
2338 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 if (op != NULL && (*op == '+' || *op == '-'))
2340 EMSG2(_(e_letwrong), op);
2341 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 EMSG(_(e_letunexp));
2344 else
2345 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002346 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 char_u *s;
2348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 p = get_tv_string_chk(tv);
2350 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002352 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (s != NULL)
2354 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002355 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 vim_free(s);
2357 }
2358 }
2359 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 arg_end = arg + 1;
2363 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002364 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
2367
2368 /*
2369 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002372 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002374 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002376 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2380 EMSG(_(e_letunexp));
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 arg_end = p;
2385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 }
2389
2390 else
2391 EMSG2(_(e_invarg2), arg);
2392
2393 return arg_end;
2394}
2395
2396/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2398 */
2399 static int
2400check_changedtick(arg)
2401 char_u *arg;
2402{
2403 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2404 {
2405 EMSG2(_(e_readonlyvar), arg);
2406 return TRUE;
2407 }
2408 return FALSE;
2409}
2410
2411/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 * Get an lval: variable, Dict item or List item that can be assigned a value
2413 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2414 * "name.key", "name.key[expr]" etc.
2415 * Indexing only works if "name" is an existing List or Dictionary.
2416 * "name" points to the start of the name.
2417 * If "rettv" is not NULL it points to the value to be assigned.
2418 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2419 * wrong; must end in space or cmd separator.
2420 *
2421 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002422 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 */
2425 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002426get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002428 typval_T *rettv;
2429 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 int unlet;
2431 int skip;
2432 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002433 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 char_u *expr_start, *expr_end;
2437 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002438 dictitem_T *v;
2439 typval_T var1;
2440 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002442 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002445 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002448 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449
2450 if (skip)
2451 {
2452 /* When skipping just find the end of the name. */
2453 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002454 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 }
2456
2457 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002458 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (expr_start != NULL)
2460 {
2461 /* Don't expand the name when we already know there is an error. */
2462 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2463 && *p != '[' && *p != '.')
2464 {
2465 EMSG(_(e_trailing));
2466 return NULL;
2467 }
2468
2469 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2470 if (lp->ll_exp_name == NULL)
2471 {
2472 /* Report an invalid expression in braces, unless the
2473 * expression evaluation has been cancelled due to an
2474 * aborting error, an interrupt, or an exception. */
2475 if (!aborting() && !quiet)
2476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002477 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 EMSG2(_(e_invarg2), name);
2479 return NULL;
2480 }
2481 }
2482 lp->ll_name = lp->ll_exp_name;
2483 }
2484 else
2485 lp->ll_name = name;
2486
2487 /* Without [idx] or .key we are done. */
2488 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2489 return p;
2490
2491 cc = *p;
2492 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002493 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (v == NULL && !quiet)
2495 EMSG2(_(e_undefvar), lp->ll_name);
2496 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 if (v == NULL)
2498 return NULL;
2499
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 /*
2501 * Loop until no more [idx] or .key is following.
2502 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002503 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2507 && !(lp->ll_tv->v_type == VAR_DICT
2508 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (!quiet)
2511 EMSG(_("E689: Can only index a List or Dictionary"));
2512 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (!quiet)
2517 EMSG(_("E708: [:] must come last"));
2518 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 len = -1;
2522 if (*p == '.')
2523 {
2524 key = p + 1;
2525 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2526 ;
2527 if (len == 0)
2528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (!quiet)
2530 EMSG(_(e_emptykey));
2531 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 }
2533 p = key + len;
2534 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002535 else
2536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002538 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 if (*p == ':')
2540 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002541 else
2542 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 empty1 = FALSE;
2544 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546 if (get_tv_string_chk(&var1) == NULL)
2547 {
2548 /* not a number or string */
2549 clear_tv(&var1);
2550 return NULL;
2551 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 }
2553
2554 /* Optionally get the second index [ :expr]. */
2555 if (*p == ':')
2556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002560 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002561 if (!empty1)
2562 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002564 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 if (rettv != NULL && (rettv->v_type != VAR_LIST
2566 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (!quiet)
2569 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 if (!empty1)
2571 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 }
2574 p = skipwhite(p + 1);
2575 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 else
2578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2581 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 if (!empty1)
2583 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002586 if (get_tv_string_chk(&var2) == NULL)
2587 {
2588 /* not a number or string */
2589 if (!empty1)
2590 clear_tv(&var1);
2591 clear_tv(&var2);
2592 return NULL;
2593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002599
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!quiet)
2603 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 if (!empty1)
2605 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002607 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 }
2610
2611 /* Skip to past ']'. */
2612 ++p;
2613 }
2614
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 {
2617 if (len == -1)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002620 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 if (*key == NUL)
2622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!quiet)
2624 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 }
2628 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002629 lp->ll_list = NULL;
2630 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002631 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002634 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002638 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 if (len == -1)
2640 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 }
2643 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (len == -1)
2648 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 p = NULL;
2651 break;
2652 }
2653 if (len == -1)
2654 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 else
2658 {
2659 /*
2660 * Get the number and item for the only or first index of the List.
2661 */
2662 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 else
2665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002666 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 clear_tv(&var1);
2668 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_list = lp->ll_tv->vval.v_list;
2671 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2672 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002674 if (lp->ll_n1 < 0)
2675 {
2676 lp->ll_n1 = 0;
2677 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2678 }
2679 }
2680 if (lp->ll_li == NULL)
2681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686
2687 /*
2688 * May need to find the item or absolute index for the second
2689 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 * When no index given: "lp->ll_empty2" is TRUE.
2691 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2706 if (lp->ll_n1 < 0)
2707 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2708 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
2711
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714 }
2715
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return p;
2717}
2718
2719/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002720 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 */
2722 static void
2723clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002724 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725{
2726 vim_free(lp->ll_exp_name);
2727 vim_free(lp->ll_newkey);
2728}
2729
2730/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002731 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002733 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 */
2735 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002737 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002739 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002741 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742{
2743 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002744 listitem_T *ri;
2745 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746
2747 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 cc = *endp;
2752 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 if (op != NULL && *op != '=')
2754 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002757 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002758 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002759 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 {
2761 if (tv_op(&tv, rettv, op) == OK)
2762 set_var(lp->ll_name, &tv, FALSE);
2763 clear_tv(&tv);
2764 }
2765 }
2766 else
2767 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002769 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002771 else if (tv_check_lock(lp->ll_newkey == NULL
2772 ? lp->ll_tv->v_lock
2773 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2774 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 else if (lp->ll_range)
2776 {
2777 /*
2778 * Assign the List values to the list items.
2779 */
2780 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002781 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 if (op != NULL && *op != '=')
2783 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2784 else
2785 {
2786 clear_tv(&lp->ll_li->li_tv);
2787 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2788 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 ri = ri->li_next;
2790 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2791 break;
2792 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002795 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002796 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 ri = NULL;
2798 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_li = lp->ll_li->li_next;
2802 ++lp->ll_n1;
2803 }
2804 if (ri != NULL)
2805 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002806 else if (lp->ll_empty2
2807 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 : lp->ll_n1 != lp->ll_n2)
2809 EMSG(_("E711: List value has not enough items"));
2810 }
2811 else
2812 {
2813 /*
2814 * Assign to a List or Dictionary item.
2815 */
2816 if (lp->ll_newkey != NULL)
2817 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 if (op != NULL && *op != '=')
2819 {
2820 EMSG2(_(e_letwrong), op);
2821 return;
2822 }
2823
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002825 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (di == NULL)
2827 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002828 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2829 {
2830 vim_free(di);
2831 return;
2832 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002834 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002835 else if (op != NULL && *op != '=')
2836 {
2837 tv_op(lp->ll_tv, rettv, op);
2838 return;
2839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002840 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 /*
2844 * Assign the value to the variable or list item.
2845 */
2846 if (copy)
2847 copy_tv(rettv, lp->ll_tv);
2848 else
2849 {
2850 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002851 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002853 }
2854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002855}
2856
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002858 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2859 * Returns OK or FAIL.
2860 */
2861 static int
2862tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 typval_T *tv1;
2864 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 char_u *op;
2866{
2867 long n;
2868 char_u numbuf[NUMBUFLEN];
2869 char_u *s;
2870
2871 /* Can't do anything with a Funcref or a Dict on the right. */
2872 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2873 {
2874 switch (tv1->v_type)
2875 {
2876 case VAR_DICT:
2877 case VAR_FUNC:
2878 break;
2879
2880 case VAR_LIST:
2881 if (*op != '+' || tv2->v_type != VAR_LIST)
2882 break;
2883 /* List += List */
2884 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2885 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2886 return OK;
2887
2888 case VAR_NUMBER:
2889 case VAR_STRING:
2890 if (tv2->v_type == VAR_LIST)
2891 break;
2892 if (*op == '+' || *op == '-')
2893 {
2894 /* nr += nr or nr -= nr*/
2895 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002896#ifdef FEAT_FLOAT
2897 if (tv2->v_type == VAR_FLOAT)
2898 {
2899 float_T f = n;
2900
2901 if (*op == '+')
2902 f += tv2->vval.v_float;
2903 else
2904 f -= tv2->vval.v_float;
2905 clear_tv(tv1);
2906 tv1->v_type = VAR_FLOAT;
2907 tv1->vval.v_float = f;
2908 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002909 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002910#endif
2911 {
2912 if (*op == '+')
2913 n += get_tv_number(tv2);
2914 else
2915 n -= get_tv_number(tv2);
2916 clear_tv(tv1);
2917 tv1->v_type = VAR_NUMBER;
2918 tv1->vval.v_number = n;
2919 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 }
2921 else
2922 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002923 if (tv2->v_type == VAR_FLOAT)
2924 break;
2925
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 /* str .= str */
2927 s = get_tv_string(tv1);
2928 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2929 clear_tv(tv1);
2930 tv1->v_type = VAR_STRING;
2931 tv1->vval.v_string = s;
2932 }
2933 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002934
2935#ifdef FEAT_FLOAT
2936 case VAR_FLOAT:
2937 {
2938 float_T f;
2939
2940 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2941 && tv2->v_type != VAR_NUMBER
2942 && tv2->v_type != VAR_STRING))
2943 break;
2944 if (tv2->v_type == VAR_FLOAT)
2945 f = tv2->vval.v_float;
2946 else
2947 f = get_tv_number(tv2);
2948 if (*op == '+')
2949 tv1->vval.v_float += f;
2950 else
2951 tv1->vval.v_float -= f;
2952 }
2953 return OK;
2954#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 }
2956 }
2957
2958 EMSG2(_(e_letwrong), op);
2959 return FAIL;
2960}
2961
2962/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002963 * Add a watcher to a list.
2964 */
2965 static void
2966list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002967 list_T *l;
2968 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969{
2970 lw->lw_next = l->lv_watch;
2971 l->lv_watch = lw;
2972}
2973
2974/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002975 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 * No warning when it isn't found...
2977 */
2978 static void
2979list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002980 list_T *l;
2981 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982{
Bram Moolenaar33570922005-01-25 22:26:29 +00002983 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984
2985 lwp = &l->lv_watch;
2986 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2987 {
2988 if (lw == lwrem)
2989 {
2990 *lwp = lw->lw_next;
2991 break;
2992 }
2993 lwp = &lw->lw_next;
2994 }
2995}
2996
2997/*
2998 * Just before removing an item from a list: advance watchers to the next
2999 * item.
3000 */
3001 static void
3002list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 list_T *l;
3004 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005{
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007
3008 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3009 if (lw->lw_item == item)
3010 lw->lw_item = item->li_next;
3011}
3012
3013/*
3014 * Evaluate the expression used in a ":for var in expr" command.
3015 * "arg" points to "var".
3016 * Set "*errp" to TRUE for an error, FALSE otherwise;
3017 * Return a pointer that holds the info. Null when there is an error.
3018 */
3019 void *
3020eval_for_line(arg, errp, nextcmdp, skip)
3021 char_u *arg;
3022 int *errp;
3023 char_u **nextcmdp;
3024 int skip;
3025{
Bram Moolenaar33570922005-01-25 22:26:29 +00003026 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 typval_T tv;
3029 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030
3031 *errp = TRUE; /* default: there is an error */
3032
Bram Moolenaar33570922005-01-25 22:26:29 +00003033 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003034 if (fi == NULL)
3035 return NULL;
3036
3037 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3038 if (expr == NULL)
3039 return fi;
3040
3041 expr = skipwhite(expr);
3042 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3043 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003044 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045 return fi;
3046 }
3047
3048 if (skip)
3049 ++emsg_skip;
3050 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3051 {
3052 *errp = FALSE;
3053 if (!skip)
3054 {
3055 l = tv.vval.v_list;
3056 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003057 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003059 clear_tv(&tv);
3060 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061 else
3062 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003063 /* No need to increment the refcount, it's already set for the
3064 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065 fi->fi_list = l;
3066 list_add_watch(l, &fi->fi_lw);
3067 fi->fi_lw.lw_item = l->lv_first;
3068 }
3069 }
3070 }
3071 if (skip)
3072 --emsg_skip;
3073
3074 return fi;
3075}
3076
3077/*
3078 * Use the first item in a ":for" list. Advance to the next.
3079 * Assign the values to the variable (list). "arg" points to the first one.
3080 * Return TRUE when a valid item was found, FALSE when at end of list or
3081 * something wrong.
3082 */
3083 int
3084next_for_item(fi_void, arg)
3085 void *fi_void;
3086 char_u *arg;
3087{
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091
3092 item = fi->fi_lw.lw_item;
3093 if (item == NULL)
3094 result = FALSE;
3095 else
3096 {
3097 fi->fi_lw.lw_item = item->li_next;
3098 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3099 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3100 }
3101 return result;
3102}
3103
3104/*
3105 * Free the structure used to store info used by ":for".
3106 */
3107 void
3108free_for_info(fi_void)
3109 void *fi_void;
3110{
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003113 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003114 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003116 list_unref(fi->fi_list);
3117 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 vim_free(fi);
3119}
3120
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3122
3123 void
3124set_context_for_expression(xp, arg, cmdidx)
3125 expand_T *xp;
3126 char_u *arg;
3127 cmdidx_T cmdidx;
3128{
3129 int got_eq = FALSE;
3130 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133 if (cmdidx == CMD_let)
3134 {
3135 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003136 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137 {
3138 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003139 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 {
3141 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143 if (vim_iswhite(*p))
3144 break;
3145 }
3146 return;
3147 }
3148 }
3149 else
3150 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3151 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 while ((xp->xp_pattern = vim_strpbrk(arg,
3153 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3154 {
3155 c = *xp->xp_pattern;
3156 if (c == '&')
3157 {
3158 c = xp->xp_pattern[1];
3159 if (c == '&')
3160 {
3161 ++xp->xp_pattern;
3162 xp->xp_context = cmdidx != CMD_let || got_eq
3163 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3164 }
3165 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003168 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3169 xp->xp_pattern += 2;
3170
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 }
3173 else if (c == '$')
3174 {
3175 /* environment variable */
3176 xp->xp_context = EXPAND_ENV_VARS;
3177 }
3178 else if (c == '=')
3179 {
3180 got_eq = TRUE;
3181 xp->xp_context = EXPAND_EXPRESSION;
3182 }
3183 else if (c == '<'
3184 && xp->xp_context == EXPAND_FUNCTIONS
3185 && vim_strchr(xp->xp_pattern, '(') == NULL)
3186 {
3187 /* Function name can start with "<SNR>" */
3188 break;
3189 }
3190 else if (cmdidx != CMD_let || got_eq)
3191 {
3192 if (c == '"') /* string */
3193 {
3194 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3195 if (c == '\\' && xp->xp_pattern[1] != NUL)
3196 ++xp->xp_pattern;
3197 xp->xp_context = EXPAND_NOTHING;
3198 }
3199 else if (c == '\'') /* literal string */
3200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3203 /* skip */ ;
3204 xp->xp_context = EXPAND_NOTHING;
3205 }
3206 else if (c == '|')
3207 {
3208 if (xp->xp_pattern[1] == '|')
3209 {
3210 ++xp->xp_pattern;
3211 xp->xp_context = EXPAND_EXPRESSION;
3212 }
3213 else
3214 xp->xp_context = EXPAND_COMMANDS;
3215 }
3216 else
3217 xp->xp_context = EXPAND_EXPRESSION;
3218 }
3219 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 /* Doesn't look like something valid, expand as an expression
3221 * anyway. */
3222 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 arg = xp->xp_pattern;
3224 if (*arg != NUL)
3225 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3226 /* skip */ ;
3227 }
3228 xp->xp_pattern = arg;
3229}
3230
3231#endif /* FEAT_CMDL_COMPL */
3232
3233/*
3234 * ":1,25call func(arg1, arg2)" function call.
3235 */
3236 void
3237ex_call(eap)
3238 exarg_T *eap;
3239{
3240 char_u *arg = eap->arg;
3241 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003243 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 linenr_T lnum;
3247 int doesrange;
3248 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003251 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003252 if (fudi.fd_newkey != NULL)
3253 {
3254 /* Still need to give an error message for missing key. */
3255 EMSG2(_(e_dictkey), fudi.fd_newkey);
3256 vim_free(fudi.fd_newkey);
3257 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003258 if (tofree == NULL)
3259 return;
3260
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003261 /* Increase refcount on dictionary, it could get deleted when evaluating
3262 * the arguments. */
3263 if (fudi.fd_dict != NULL)
3264 ++fudi.fd_dict->dv_refcount;
3265
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003266 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003267 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003268 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaar532c7802005-01-27 14:44:31 +00003270 /* Skip white space to allow ":call func ()". Not good, but required for
3271 * backward compatibility. */
3272 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 if (*startarg != '(')
3276 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003277 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 goto end;
3279 }
3280
3281 /*
3282 * When skipping, evaluate the function once, to find the end of the
3283 * arguments.
3284 * When the function takes a range, this is discovered after the first
3285 * call, and the loop is broken.
3286 */
3287 if (eap->skip)
3288 {
3289 ++emsg_skip;
3290 lnum = eap->line2; /* do it once, also with an invalid range */
3291 }
3292 else
3293 lnum = eap->line1;
3294 for ( ; lnum <= eap->line2; ++lnum)
3295 {
3296 if (!eap->skip && eap->addr_count > 0)
3297 {
3298 curwin->w_cursor.lnum = lnum;
3299 curwin->w_cursor.col = 0;
3300 }
3301 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003302 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003303 eap->line1, eap->line2, &doesrange,
3304 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 failed = TRUE;
3307 break;
3308 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003309
3310 /* Handle a function returning a Funcref, Dictionary or List. */
3311 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3312 {
3313 failed = TRUE;
3314 break;
3315 }
3316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003317 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (doesrange || eap->skip)
3319 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003322 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003323 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003324 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (aborting())
3326 break;
3327 }
3328 if (eap->skip)
3329 --emsg_skip;
3330
3331 if (!failed)
3332 {
3333 /* Check for trailing illegal characters and a following command. */
3334 if (!ends_excmd(*arg))
3335 {
3336 emsg_severe = TRUE;
3337 EMSG(_(e_trailing));
3338 }
3339 else
3340 eap->nextcmd = check_nextcmd(arg);
3341 }
3342
3343end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003344 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003345 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346}
3347
3348/*
3349 * ":unlet[!] var1 ... " command.
3350 */
3351 void
3352ex_unlet(eap)
3353 exarg_T *eap;
3354{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355 ex_unletlock(eap, eap->arg, 0);
3356}
3357
3358/*
3359 * ":lockvar" and ":unlockvar" commands
3360 */
3361 void
3362ex_lockvar(eap)
3363 exarg_T *eap;
3364{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 int deep = 2;
3367
3368 if (eap->forceit)
3369 deep = -1;
3370 else if (vim_isdigit(*arg))
3371 {
3372 deep = getdigits(&arg);
3373 arg = skipwhite(arg);
3374 }
3375
3376 ex_unletlock(eap, arg, deep);
3377}
3378
3379/*
3380 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3381 */
3382 static void
3383ex_unletlock(eap, argstart, deep)
3384 exarg_T *eap;
3385 char_u *argstart;
3386 int deep;
3387{
3388 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003391 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 do
3394 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003395 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003396 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3397 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003398 if (lv.ll_name == NULL)
3399 error = TRUE; /* error but continue parsing */
3400 if (name_end == NULL || (!vim_iswhite(*name_end)
3401 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003403 if (name_end != NULL)
3404 {
3405 emsg_severe = TRUE;
3406 EMSG(_(e_trailing));
3407 }
3408 if (!(eap->skip || error))
3409 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 break;
3411 }
3412
3413 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003414 {
3415 if (eap->cmdidx == CMD_unlet)
3416 {
3417 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3418 error = TRUE;
3419 }
3420 else
3421 {
3422 if (do_lock_var(&lv, name_end, deep,
3423 eap->cmdidx == CMD_lockvar) == FAIL)
3424 error = TRUE;
3425 }
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003428 if (!eap->skip)
3429 clear_lval(&lv);
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 arg = skipwhite(name_end);
3432 } while (!ends_excmd(*arg));
3433
3434 eap->nextcmd = check_nextcmd(arg);
3435}
3436
Bram Moolenaar8c711452005-01-14 21:53:12 +00003437 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003438do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003439 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003440 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003441 int forceit;
3442{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003443 int ret = OK;
3444 int cc;
3445
3446 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003448 cc = *name_end;
3449 *name_end = NUL;
3450
3451 /* Normal name or expanded name. */
3452 if (check_changedtick(lp->ll_name))
3453 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003454 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003455 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003456 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003457 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003458 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3459 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003460 else if (lp->ll_range)
3461 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003463
3464 /* Delete a range of List items. */
3465 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3466 {
3467 li = lp->ll_li->li_next;
3468 listitem_remove(lp->ll_list, lp->ll_li);
3469 lp->ll_li = li;
3470 ++lp->ll_n1;
3471 }
3472 }
3473 else
3474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003476 /* unlet a List item. */
3477 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003481 }
3482
3483 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003484}
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486/*
3487 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003488 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 */
3490 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003491do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003493 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
Bram Moolenaar33570922005-01-25 22:26:29 +00003495 hashtab_T *ht;
3496 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003497 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003498 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 ht = find_var_ht(name, &varname);
3501 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003503 hi = hash_find(ht, varname);
3504 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003505 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003506 di = HI2DI(hi);
3507 if (var_check_fixed(di->di_flags, name)
3508 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003509 return FAIL;
3510 delete_var(ht, hi);
3511 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003514 if (forceit)
3515 return OK;
3516 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 return FAIL;
3518}
3519
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003520/*
3521 * Lock or unlock variable indicated by "lp".
3522 * "deep" is the levels to go (-1 for unlimited);
3523 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3524 */
3525 static int
3526do_lock_var(lp, name_end, deep, lock)
3527 lval_T *lp;
3528 char_u *name_end;
3529 int deep;
3530 int lock;
3531{
3532 int ret = OK;
3533 int cc;
3534 dictitem_T *di;
3535
3536 if (deep == 0) /* nothing to do */
3537 return OK;
3538
3539 if (lp->ll_tv == NULL)
3540 {
3541 cc = *name_end;
3542 *name_end = NUL;
3543
3544 /* Normal name or expanded name. */
3545 if (check_changedtick(lp->ll_name))
3546 ret = FAIL;
3547 else
3548 {
3549 di = find_var(lp->ll_name, NULL);
3550 if (di == NULL)
3551 ret = FAIL;
3552 else
3553 {
3554 if (lock)
3555 di->di_flags |= DI_FLAGS_LOCK;
3556 else
3557 di->di_flags &= ~DI_FLAGS_LOCK;
3558 item_lock(&di->di_tv, deep, lock);
3559 }
3560 }
3561 *name_end = cc;
3562 }
3563 else if (lp->ll_range)
3564 {
3565 listitem_T *li = lp->ll_li;
3566
3567 /* (un)lock a range of List items. */
3568 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3569 {
3570 item_lock(&li->li_tv, deep, lock);
3571 li = li->li_next;
3572 ++lp->ll_n1;
3573 }
3574 }
3575 else if (lp->ll_list != NULL)
3576 /* (un)lock a List item. */
3577 item_lock(&lp->ll_li->li_tv, deep, lock);
3578 else
3579 /* un(lock) a Dictionary item. */
3580 item_lock(&lp->ll_di->di_tv, deep, lock);
3581
3582 return ret;
3583}
3584
3585/*
3586 * Lock or unlock an item. "deep" is nr of levels to go.
3587 */
3588 static void
3589item_lock(tv, deep, lock)
3590 typval_T *tv;
3591 int deep;
3592 int lock;
3593{
3594 static int recurse = 0;
3595 list_T *l;
3596 listitem_T *li;
3597 dict_T *d;
3598 hashitem_T *hi;
3599 int todo;
3600
3601 if (recurse >= DICT_MAXNEST)
3602 {
3603 EMSG(_("E743: variable nested too deep for (un)lock"));
3604 return;
3605 }
3606 if (deep == 0)
3607 return;
3608 ++recurse;
3609
3610 /* lock/unlock the item itself */
3611 if (lock)
3612 tv->v_lock |= VAR_LOCKED;
3613 else
3614 tv->v_lock &= ~VAR_LOCKED;
3615
3616 switch (tv->v_type)
3617 {
3618 case VAR_LIST:
3619 if ((l = tv->vval.v_list) != NULL)
3620 {
3621 if (lock)
3622 l->lv_lock |= VAR_LOCKED;
3623 else
3624 l->lv_lock &= ~VAR_LOCKED;
3625 if (deep < 0 || deep > 1)
3626 /* recursive: lock/unlock the items the List contains */
3627 for (li = l->lv_first; li != NULL; li = li->li_next)
3628 item_lock(&li->li_tv, deep - 1, lock);
3629 }
3630 break;
3631 case VAR_DICT:
3632 if ((d = tv->vval.v_dict) != NULL)
3633 {
3634 if (lock)
3635 d->dv_lock |= VAR_LOCKED;
3636 else
3637 d->dv_lock &= ~VAR_LOCKED;
3638 if (deep < 0 || deep > 1)
3639 {
3640 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003641 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3643 {
3644 if (!HASHITEM_EMPTY(hi))
3645 {
3646 --todo;
3647 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3648 }
3649 }
3650 }
3651 }
3652 }
3653 --recurse;
3654}
3655
Bram Moolenaara40058a2005-07-11 22:42:07 +00003656/*
3657 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3658 * it refers to a List or Dictionary that is locked.
3659 */
3660 static int
3661tv_islocked(tv)
3662 typval_T *tv;
3663{
3664 return (tv->v_lock & VAR_LOCKED)
3665 || (tv->v_type == VAR_LIST
3666 && tv->vval.v_list != NULL
3667 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3668 || (tv->v_type == VAR_DICT
3669 && tv->vval.v_dict != NULL
3670 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3671}
3672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3674/*
3675 * Delete all "menutrans_" variables.
3676 */
3677 void
3678del_menutrans_vars()
3679{
Bram Moolenaar33570922005-01-25 22:26:29 +00003680 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003684 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003686 {
3687 if (!HASHITEM_EMPTY(hi))
3688 {
3689 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003690 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3691 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003692 }
3693 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003694 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695}
3696#endif
3697
3698#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3699
3700/*
3701 * Local string buffer for the next two functions to store a variable name
3702 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3703 * get_user_var_name().
3704 */
3705
3706static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3707
3708static char_u *varnamebuf = NULL;
3709static int varnamebuflen = 0;
3710
3711/*
3712 * Function to concatenate a prefix and a variable name.
3713 */
3714 static char_u *
3715cat_prefix_varname(prefix, name)
3716 int prefix;
3717 char_u *name;
3718{
3719 int len;
3720
3721 len = (int)STRLEN(name) + 3;
3722 if (len > varnamebuflen)
3723 {
3724 vim_free(varnamebuf);
3725 len += 10; /* some additional space */
3726 varnamebuf = alloc(len);
3727 if (varnamebuf == NULL)
3728 {
3729 varnamebuflen = 0;
3730 return NULL;
3731 }
3732 varnamebuflen = len;
3733 }
3734 *varnamebuf = prefix;
3735 varnamebuf[1] = ':';
3736 STRCPY(varnamebuf + 2, name);
3737 return varnamebuf;
3738}
3739
3740/*
3741 * Function given to ExpandGeneric() to obtain the list of user defined
3742 * (global/buffer/window/built-in) variable names.
3743 */
3744/*ARGSUSED*/
3745 char_u *
3746get_user_var_name(xp, idx)
3747 expand_T *xp;
3748 int idx;
3749{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003750 static long_u gdone;
3751 static long_u bdone;
3752 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003753#ifdef FEAT_WINDOWS
3754 static long_u tdone;
3755#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003756 static int vidx;
3757 static hashitem_T *hi;
3758 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003761 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003762 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003763#ifdef FEAT_WINDOWS
3764 tdone = 0;
3765#endif
3766 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003767
3768 /* Global variables */
3769 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003772 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003773 else
3774 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003775 while (HASHITEM_EMPTY(hi))
3776 ++hi;
3777 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3778 return cat_prefix_varname('g', hi->hi_key);
3779 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003781
3782 /* b: variables */
3783 ht = &curbuf->b_vars.dv_hashtab;
3784 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003786 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003787 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003788 else
3789 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003790 while (HASHITEM_EMPTY(hi))
3791 ++hi;
3792 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003794 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003796 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return (char_u *)"b:changedtick";
3798 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003799
3800 /* w: variables */
3801 ht = &curwin->w_vars.dv_hashtab;
3802 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003804 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003806 else
3807 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003808 while (HASHITEM_EMPTY(hi))
3809 ++hi;
3810 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003812
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003813#ifdef FEAT_WINDOWS
3814 /* t: variables */
3815 ht = &curtab->tp_vars.dv_hashtab;
3816 if (tdone < ht->ht_used)
3817 {
3818 if (tdone++ == 0)
3819 hi = ht->ht_array;
3820 else
3821 ++hi;
3822 while (HASHITEM_EMPTY(hi))
3823 ++hi;
3824 return cat_prefix_varname('t', hi->hi_key);
3825 }
3826#endif
3827
Bram Moolenaar33570922005-01-25 22:26:29 +00003828 /* v: variables */
3829 if (vidx < VV_LEN)
3830 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 vim_free(varnamebuf);
3833 varnamebuf = NULL;
3834 varnamebuflen = 0;
3835 return NULL;
3836}
3837
3838#endif /* FEAT_CMDL_COMPL */
3839
3840/*
3841 * types for expressions.
3842 */
3843typedef enum
3844{
3845 TYPE_UNKNOWN = 0
3846 , TYPE_EQUAL /* == */
3847 , TYPE_NEQUAL /* != */
3848 , TYPE_GREATER /* > */
3849 , TYPE_GEQUAL /* >= */
3850 , TYPE_SMALLER /* < */
3851 , TYPE_SEQUAL /* <= */
3852 , TYPE_MATCH /* =~ */
3853 , TYPE_NOMATCH /* !~ */
3854} exptype_T;
3855
3856/*
3857 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003858 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3860 */
3861
3862/*
3863 * Handle zero level expression.
3864 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003866 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * Return OK or FAIL.
3868 */
3869 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003870eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u **nextcmd;
3874 int evaluate;
3875{
3876 int ret;
3877 char_u *p;
3878
3879 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003880 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (ret == FAIL || !ends_excmd(*p))
3882 {
3883 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003884 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 /*
3886 * Report the invalid expression unless the expression evaluation has
3887 * been cancelled due to an aborting error, an interrupt, or an
3888 * exception.
3889 */
3890 if (!aborting())
3891 EMSG2(_(e_invexpr2), arg);
3892 ret = FAIL;
3893 }
3894 if (nextcmd != NULL)
3895 *nextcmd = check_nextcmd(p);
3896
3897 return ret;
3898}
3899
3900/*
3901 * Handle top level expression:
3902 * expr1 ? expr0 : expr0
3903 *
3904 * "arg" must point to the first non-white of the expression.
3905 * "arg" is advanced to the next non-white after the recognized expression.
3906 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003907 * Note: "rettv.v_lock" is not set.
3908 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 * Return OK or FAIL.
3910 */
3911 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 int evaluate;
3916{
3917 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003918 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 /*
3921 * Get the first variable.
3922 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003923 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 return FAIL;
3925
3926 if ((*arg)[0] == '?')
3927 {
3928 result = FALSE;
3929 if (evaluate)
3930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003931 int error = FALSE;
3932
3933 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003935 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003936 if (error)
3937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939
3940 /*
3941 * Get the second variable.
3942 */
3943 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 return FAIL;
3946
3947 /*
3948 * Check for the ":".
3949 */
3950 if ((*arg)[0] != ':')
3951 {
3952 EMSG(_("E109: Missing ':' after '?'"));
3953 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return FAIL;
3956 }
3957
3958 /*
3959 * Get the third variable.
3960 */
3961 *arg = skipwhite(*arg + 1);
3962 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3963 {
3964 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return FAIL;
3967 }
3968 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971
3972 return OK;
3973}
3974
3975/*
3976 * Handle first level expression:
3977 * expr2 || expr2 || expr2 logical OR
3978 *
3979 * "arg" must point to the first non-white of the expression.
3980 * "arg" is advanced to the next non-white after the recognized expression.
3981 *
3982 * Return OK or FAIL.
3983 */
3984 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003985eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int evaluate;
3989{
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 long result;
3992 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995 /*
3996 * Get the first variable.
3997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return FAIL;
4000
4001 /*
4002 * Repeat until there is no following "||".
4003 */
4004 first = TRUE;
4005 result = FALSE;
4006 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4007 {
4008 if (evaluate && first)
4009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 if (error)
4014 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 first = FALSE;
4016 }
4017
4018 /*
4019 * Get the second variable.
4020 */
4021 *arg = skipwhite(*arg + 2);
4022 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4023 return FAIL;
4024
4025 /*
4026 * Compute the result.
4027 */
4028 if (evaluate && !result)
4029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004030 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004033 if (error)
4034 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
4036 if (evaluate)
4037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 rettv->v_type = VAR_NUMBER;
4039 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041 }
4042
4043 return OK;
4044}
4045
4046/*
4047 * Handle second level expression:
4048 * expr3 && expr3 && expr3 logical AND
4049 *
4050 * "arg" must point to the first non-white of the expression.
4051 * "arg" is advanced to the next non-white after the recognized expression.
4052 *
4053 * Return OK or FAIL.
4054 */
4055 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 int evaluate;
4060{
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 long result;
4063 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004064 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 /*
4067 * Get the first variable.
4068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return FAIL;
4071
4072 /*
4073 * Repeat until there is no following "&&".
4074 */
4075 first = TRUE;
4076 result = TRUE;
4077 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4078 {
4079 if (evaluate && first)
4080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004084 if (error)
4085 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 first = FALSE;
4087 }
4088
4089 /*
4090 * Get the second variable.
4091 */
4092 *arg = skipwhite(*arg + 2);
4093 if (eval4(arg, &var2, evaluate && result) == FAIL)
4094 return FAIL;
4095
4096 /*
4097 * Compute the result.
4098 */
4099 if (evaluate && result)
4100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004101 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 if (error)
4105 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 if (evaluate)
4108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 rettv->v_type = VAR_NUMBER;
4110 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 }
4113
4114 return OK;
4115}
4116
4117/*
4118 * Handle third level expression:
4119 * var1 == var2
4120 * var1 =~ var2
4121 * var1 != var2
4122 * var1 !~ var2
4123 * var1 > var2
4124 * var1 >= var2
4125 * var1 < var2
4126 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004127 * var1 is var2
4128 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 *
4130 * "arg" must point to the first non-white of the expression.
4131 * "arg" is advanced to the next non-white after the recognized expression.
4132 *
4133 * Return OK or FAIL.
4134 */
4135 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 int evaluate;
4140{
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 char_u *p;
4143 int i;
4144 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004145 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int len = 2;
4147 long n1, n2;
4148 char_u *s1, *s2;
4149 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4150 regmatch_T regmatch;
4151 int ic;
4152 char_u *save_cpo;
4153
4154 /*
4155 * Get the first variable.
4156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 p = *arg;
4161 switch (p[0])
4162 {
4163 case '=': if (p[1] == '=')
4164 type = TYPE_EQUAL;
4165 else if (p[1] == '~')
4166 type = TYPE_MATCH;
4167 break;
4168 case '!': if (p[1] == '=')
4169 type = TYPE_NEQUAL;
4170 else if (p[1] == '~')
4171 type = TYPE_NOMATCH;
4172 break;
4173 case '>': if (p[1] != '=')
4174 {
4175 type = TYPE_GREATER;
4176 len = 1;
4177 }
4178 else
4179 type = TYPE_GEQUAL;
4180 break;
4181 case '<': if (p[1] != '=')
4182 {
4183 type = TYPE_SMALLER;
4184 len = 1;
4185 }
4186 else
4187 type = TYPE_SEQUAL;
4188 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004189 case 'i': if (p[1] == 's')
4190 {
4191 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4192 len = 5;
4193 if (!vim_isIDc(p[len]))
4194 {
4195 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4196 type_is = TRUE;
4197 }
4198 }
4199 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201
4202 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004203 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
4205 if (type != TYPE_UNKNOWN)
4206 {
4207 /* extra question mark appended: ignore case */
4208 if (p[len] == '?')
4209 {
4210 ic = TRUE;
4211 ++len;
4212 }
4213 /* extra '#' appended: match case */
4214 else if (p[len] == '#')
4215 {
4216 ic = FALSE;
4217 ++len;
4218 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004219 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 else
4221 ic = p_ic;
4222
4223 /*
4224 * Get the second variable.
4225 */
4226 *arg = skipwhite(p + len);
4227 if (eval5(arg, &var2, evaluate) == FAIL)
4228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231 }
4232
4233 if (evaluate)
4234 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004235 if (type_is && rettv->v_type != var2.v_type)
4236 {
4237 /* For "is" a different type always means FALSE, for "notis"
4238 * it means TRUE. */
4239 n1 = (type == TYPE_NEQUAL);
4240 }
4241 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4242 {
4243 if (type_is)
4244 {
4245 n1 = (rettv->v_type == var2.v_type
4246 && rettv->vval.v_list == var2.vval.v_list);
4247 if (type == TYPE_NEQUAL)
4248 n1 = !n1;
4249 }
4250 else if (rettv->v_type != var2.v_type
4251 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4252 {
4253 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004254 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004255 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004256 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004257 clear_tv(rettv);
4258 clear_tv(&var2);
4259 return FAIL;
4260 }
4261 else
4262 {
4263 /* Compare two Lists for being equal or unequal. */
4264 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4265 if (type == TYPE_NEQUAL)
4266 n1 = !n1;
4267 }
4268 }
4269
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004270 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4271 {
4272 if (type_is)
4273 {
4274 n1 = (rettv->v_type == var2.v_type
4275 && rettv->vval.v_dict == var2.vval.v_dict);
4276 if (type == TYPE_NEQUAL)
4277 n1 = !n1;
4278 }
4279 else if (rettv->v_type != var2.v_type
4280 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4281 {
4282 if (rettv->v_type != var2.v_type)
4283 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4284 else
4285 EMSG(_("E736: Invalid operation for Dictionary"));
4286 clear_tv(rettv);
4287 clear_tv(&var2);
4288 return FAIL;
4289 }
4290 else
4291 {
4292 /* Compare two Dictionaries for being equal or unequal. */
4293 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4294 if (type == TYPE_NEQUAL)
4295 n1 = !n1;
4296 }
4297 }
4298
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004299 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4300 {
4301 if (rettv->v_type != var2.v_type
4302 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4303 {
4304 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004305 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004306 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004307 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004308 clear_tv(rettv);
4309 clear_tv(&var2);
4310 return FAIL;
4311 }
4312 else
4313 {
4314 /* Compare two Funcrefs for being equal or unequal. */
4315 if (rettv->vval.v_string == NULL
4316 || var2.vval.v_string == NULL)
4317 n1 = FALSE;
4318 else
4319 n1 = STRCMP(rettv->vval.v_string,
4320 var2.vval.v_string) == 0;
4321 if (type == TYPE_NEQUAL)
4322 n1 = !n1;
4323 }
4324 }
4325
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004326#ifdef FEAT_FLOAT
4327 /*
4328 * If one of the two variables is a float, compare as a float.
4329 * When using "=~" or "!~", always compare as string.
4330 */
4331 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4332 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4333 {
4334 float_T f1, f2;
4335
4336 if (rettv->v_type == VAR_FLOAT)
4337 f1 = rettv->vval.v_float;
4338 else
4339 f1 = get_tv_number(rettv);
4340 if (var2.v_type == VAR_FLOAT)
4341 f2 = var2.vval.v_float;
4342 else
4343 f2 = get_tv_number(&var2);
4344 n1 = FALSE;
4345 switch (type)
4346 {
4347 case TYPE_EQUAL: n1 = (f1 == f2); break;
4348 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4349 case TYPE_GREATER: n1 = (f1 > f2); break;
4350 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4351 case TYPE_SMALLER: n1 = (f1 < f2); break;
4352 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4353 case TYPE_UNKNOWN:
4354 case TYPE_MATCH:
4355 case TYPE_NOMATCH: break; /* avoid gcc warning */
4356 }
4357 }
4358#endif
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /*
4361 * If one of the two variables is a number, compare as a number.
4362 * When using "=~" or "!~", always compare as string.
4363 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004364 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004367 n1 = get_tv_number(rettv);
4368 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 switch (type)
4370 {
4371 case TYPE_EQUAL: n1 = (n1 == n2); break;
4372 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4373 case TYPE_GREATER: n1 = (n1 > n2); break;
4374 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4375 case TYPE_SMALLER: n1 = (n1 < n2); break;
4376 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4377 case TYPE_UNKNOWN:
4378 case TYPE_MATCH:
4379 case TYPE_NOMATCH: break; /* avoid gcc warning */
4380 }
4381 }
4382 else
4383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 s1 = get_tv_string_buf(rettv, buf1);
4385 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4387 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4388 else
4389 i = 0;
4390 n1 = FALSE;
4391 switch (type)
4392 {
4393 case TYPE_EQUAL: n1 = (i == 0); break;
4394 case TYPE_NEQUAL: n1 = (i != 0); break;
4395 case TYPE_GREATER: n1 = (i > 0); break;
4396 case TYPE_GEQUAL: n1 = (i >= 0); break;
4397 case TYPE_SMALLER: n1 = (i < 0); break;
4398 case TYPE_SEQUAL: n1 = (i <= 0); break;
4399
4400 case TYPE_MATCH:
4401 case TYPE_NOMATCH:
4402 /* avoid 'l' flag in 'cpoptions' */
4403 save_cpo = p_cpo;
4404 p_cpo = (char_u *)"";
4405 regmatch.regprog = vim_regcomp(s2,
4406 RE_MAGIC + RE_STRING);
4407 regmatch.rm_ic = ic;
4408 if (regmatch.regprog != NULL)
4409 {
4410 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4411 vim_free(regmatch.regprog);
4412 if (type == TYPE_NOMATCH)
4413 n1 = !n1;
4414 }
4415 p_cpo = save_cpo;
4416 break;
4417
4418 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4419 }
4420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004421 clear_tv(rettv);
4422 clear_tv(&var2);
4423 rettv->v_type = VAR_NUMBER;
4424 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 }
4427
4428 return OK;
4429}
4430
4431/*
4432 * Handle fourth level expression:
4433 * + number addition
4434 * - number subtraction
4435 * . string concatenation
4436 *
4437 * "arg" must point to the first non-white of the expression.
4438 * "arg" is advanced to the next non-white after the recognized expression.
4439 *
4440 * Return OK or FAIL.
4441 */
4442 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 int evaluate;
4447{
Bram Moolenaar33570922005-01-25 22:26:29 +00004448 typval_T var2;
4449 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 int op;
4451 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452#ifdef FEAT_FLOAT
4453 float_T f1 = 0, f2 = 0;
4454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 char_u *s1, *s2;
4456 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4457 char_u *p;
4458
4459 /*
4460 * Get the first variable.
4461 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004462 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 return FAIL;
4464
4465 /*
4466 * Repeat computing, until no '+', '-' or '.' is following.
4467 */
4468 for (;;)
4469 {
4470 op = **arg;
4471 if (op != '+' && op != '-' && op != '.')
4472 break;
4473
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004474 if ((op != '+' || rettv->v_type != VAR_LIST)
4475#ifdef FEAT_FLOAT
4476 && (op == '.' || rettv->v_type != VAR_FLOAT)
4477#endif
4478 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004479 {
4480 /* For "list + ...", an illegal use of the first operand as
4481 * a number cannot be determined before evaluating the 2nd
4482 * operand: if this is also a list, all is ok.
4483 * For "something . ...", "something - ..." or "non-list + ...",
4484 * we know that the first operand needs to be a string or number
4485 * without evaluating the 2nd operand. So check before to avoid
4486 * side effects after an error. */
4487 if (evaluate && get_tv_string_chk(rettv) == NULL)
4488 {
4489 clear_tv(rettv);
4490 return FAIL;
4491 }
4492 }
4493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /*
4495 * Get the second variable.
4496 */
4497 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004498 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 return FAIL;
4502 }
4503
4504 if (evaluate)
4505 {
4506 /*
4507 * Compute the result.
4508 */
4509 if (op == '.')
4510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004511 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4512 s2 = get_tv_string_buf_chk(&var2, buf2);
4513 if (s2 == NULL) /* type error ? */
4514 {
4515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004519 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004520 clear_tv(rettv);
4521 rettv->v_type = VAR_STRING;
4522 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004524 else if (op == '+' && rettv->v_type == VAR_LIST
4525 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004526 {
4527 /* concatenate Lists */
4528 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4529 &var3) == FAIL)
4530 {
4531 clear_tv(rettv);
4532 clear_tv(&var2);
4533 return FAIL;
4534 }
4535 clear_tv(rettv);
4536 *rettv = var3;
4537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
4539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004540 int error = FALSE;
4541
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004542#ifdef FEAT_FLOAT
4543 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004544 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004545 f1 = rettv->vval.v_float;
4546 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004548 else
4549#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004550 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004551 n1 = get_tv_number_chk(rettv, &error);
4552 if (error)
4553 {
4554 /* This can only happen for "list + non-list". For
4555 * "non-list + ..." or "something - ...", we returned
4556 * before evaluating the 2nd operand. */
4557 clear_tv(rettv);
4558 return FAIL;
4559 }
4560#ifdef FEAT_FLOAT
4561 if (var2.v_type == VAR_FLOAT)
4562 f1 = n1;
4563#endif
4564 }
4565#ifdef FEAT_FLOAT
4566 if (var2.v_type == VAR_FLOAT)
4567 {
4568 f2 = var2.vval.v_float;
4569 n2 = 0;
4570 }
4571 else
4572#endif
4573 {
4574 n2 = get_tv_number_chk(&var2, &error);
4575 if (error)
4576 {
4577 clear_tv(rettv);
4578 clear_tv(&var2);
4579 return FAIL;
4580 }
4581#ifdef FEAT_FLOAT
4582 if (rettv->v_type == VAR_FLOAT)
4583 f2 = n2;
4584#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004586 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004587
4588#ifdef FEAT_FLOAT
4589 /* If there is a float on either side the result is a float. */
4590 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4591 {
4592 if (op == '+')
4593 f1 = f1 + f2;
4594 else
4595 f1 = f1 - f2;
4596 rettv->v_type = VAR_FLOAT;
4597 rettv->vval.v_float = f1;
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004600#endif
4601 {
4602 if (op == '+')
4603 n1 = n1 + n2;
4604 else
4605 n1 = n1 - n2;
4606 rettv->v_type = VAR_NUMBER;
4607 rettv->vval.v_number = n1;
4608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004610 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612 }
4613 return OK;
4614}
4615
4616/*
4617 * Handle fifth level expression:
4618 * * number multiplication
4619 * / number division
4620 * % number modulo
4621 *
4622 * "arg" must point to the first non-white of the expression.
4623 * "arg" is advanced to the next non-white after the recognized expression.
4624 *
4625 * Return OK or FAIL.
4626 */
4627 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004628eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004632 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
Bram Moolenaar33570922005-01-25 22:26:29 +00004634 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 int op;
4636 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004637#ifdef FEAT_FLOAT
4638 int use_float = FALSE;
4639 float_T f1 = 0, f2;
4640#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
4643 /*
4644 * Get the first variable.
4645 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004646 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 return FAIL;
4648
4649 /*
4650 * Repeat computing, until no '*', '/' or '%' is following.
4651 */
4652 for (;;)
4653 {
4654 op = **arg;
4655 if (op != '*' && op != '/' && op != '%')
4656 break;
4657
4658 if (evaluate)
4659 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004660#ifdef FEAT_FLOAT
4661 if (rettv->v_type == VAR_FLOAT)
4662 {
4663 f1 = rettv->vval.v_float;
4664 use_float = TRUE;
4665 n1 = 0;
4666 }
4667 else
4668#endif
4669 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 if (error)
4672 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 else
4675 n1 = 0;
4676
4677 /*
4678 * Get the second variable.
4679 */
4680 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004681 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 return FAIL;
4683
4684 if (evaluate)
4685 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004686#ifdef FEAT_FLOAT
4687 if (var2.v_type == VAR_FLOAT)
4688 {
4689 if (!use_float)
4690 {
4691 f1 = n1;
4692 use_float = TRUE;
4693 }
4694 f2 = var2.vval.v_float;
4695 n2 = 0;
4696 }
4697 else
4698#endif
4699 {
4700 n2 = get_tv_number_chk(&var2, &error);
4701 clear_tv(&var2);
4702 if (error)
4703 return FAIL;
4704#ifdef FEAT_FLOAT
4705 if (use_float)
4706 f2 = n2;
4707#endif
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
4710 /*
4711 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714#ifdef FEAT_FLOAT
4715 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 if (op == '*')
4718 f1 = f1 * f2;
4719 else if (op == '/')
4720 {
4721 /* We rely on the floating point library to handle divide
4722 * by zero to result in "inf" and not a crash. */
4723 f1 = f1 / f2;
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004726 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004727 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 return FAIL;
4729 }
4730 rettv->v_type = VAR_FLOAT;
4731 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
4733 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004736 if (op == '*')
4737 n1 = n1 * n2;
4738 else if (op == '/')
4739 {
4740 if (n2 == 0) /* give an error message? */
4741 {
4742 if (n1 == 0)
4743 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4744 else if (n1 < 0)
4745 n1 = -0x7fffffffL;
4746 else
4747 n1 = 0x7fffffffL;
4748 }
4749 else
4750 n1 = n1 / n2;
4751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753 {
4754 if (n2 == 0) /* give an error message? */
4755 n1 = 0;
4756 else
4757 n1 = n1 % n2;
4758 }
4759 rettv->v_type = VAR_NUMBER;
4760 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 }
4764
4765 return OK;
4766}
4767
4768/*
4769 * Handle sixth level expression:
4770 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004771 * "string" string constant
4772 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 * &option-name option value
4774 * @r register contents
4775 * identifier variable value
4776 * function() function call
4777 * $VAR environment variable
4778 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004779 * [expr, expr] List
4780 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 *
4782 * Also handle:
4783 * ! in front logical NOT
4784 * - in front unary minus
4785 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 * trailing [] subscript in String or List
4787 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 *
4789 * "arg" must point to the first non-white of the expression.
4790 * "arg" is advanced to the next non-white after the recognized expression.
4791 *
4792 * Return OK or FAIL.
4793 */
4794 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004795eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004799 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 long n;
4802 int len;
4803 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 char_u *start_leader, *end_leader;
4805 int ret = OK;
4806 char_u *alias;
4807
4808 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 /*
4815 * Skip '!' and '-' characters. They are handled later.
4816 */
4817 start_leader = *arg;
4818 while (**arg == '!' || **arg == '-' || **arg == '+')
4819 *arg = skipwhite(*arg + 1);
4820 end_leader = *arg;
4821
4822 switch (**arg)
4823 {
4824 /*
4825 * Number constant.
4826 */
4827 case '0':
4828 case '1':
4829 case '2':
4830 case '3':
4831 case '4':
4832 case '5':
4833 case '6':
4834 case '7':
4835 case '8':
4836 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837 {
4838#ifdef FEAT_FLOAT
4839 char_u *p = skipdigits(*arg + 1);
4840 int get_float = FALSE;
4841
4842 /* We accept a float when the format matches
4843 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004844 * strict to avoid backwards compatibility problems.
4845 * Don't look for a float after the "." operator, so that
4846 * ":let vers = 1.2.3" doesn't fail. */
4847 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004849 get_float = TRUE;
4850 p = skipdigits(p + 2);
4851 if (*p == 'e' || *p == 'E')
4852 {
4853 ++p;
4854 if (*p == '-' || *p == '+')
4855 ++p;
4856 if (!vim_isdigit(*p))
4857 get_float = FALSE;
4858 else
4859 p = skipdigits(p + 1);
4860 }
4861 if (ASCII_ISALPHA(*p) || *p == '.')
4862 get_float = FALSE;
4863 }
4864 if (get_float)
4865 {
4866 float_T f;
4867
4868 *arg += string2float(*arg, &f);
4869 if (evaluate)
4870 {
4871 rettv->v_type = VAR_FLOAT;
4872 rettv->vval.v_float = f;
4873 }
4874 }
4875 else
4876#endif
4877 {
4878 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4879 *arg += len;
4880 if (evaluate)
4881 {
4882 rettv->v_type = VAR_NUMBER;
4883 rettv->vval.v_number = n;
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
4889 /*
4890 * String constant: "string".
4891 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 break;
4894
4895 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004896 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 break;
4900
4901 /*
4902 * List: [expr, expr]
4903 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 break;
4906
4907 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004908 * Dictionary: {key: val, key: val}
4909 */
4910 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4911 break;
4912
4913 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004914 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004916 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 break;
4918
4919 /*
4920 * Environment variable: $VAR.
4921 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004922 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 break;
4924
4925 /*
4926 * Register contents: @r.
4927 */
4928 case '@': ++*arg;
4929 if (evaluate)
4930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004932 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 if (**arg != NUL)
4935 ++*arg;
4936 break;
4937
4938 /*
4939 * nested expression: (expression).
4940 */
4941 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (**arg == ')')
4944 ++*arg;
4945 else if (ret == OK)
4946 {
4947 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004948 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 ret = FAIL;
4950 }
4951 break;
4952
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 break;
4955 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956
4957 if (ret == NOTDONE)
4958 {
4959 /*
4960 * Must be a variable or function name.
4961 * Can also be a curly-braces kind of name: {expr}.
4962 */
4963 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004964 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 if (alias != NULL)
4966 s = alias;
4967
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004968 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 ret = FAIL;
4970 else
4971 {
4972 if (**arg == '(') /* recursive! */
4973 {
4974 /* If "s" is the name of a variable of type VAR_FUNC
4975 * use its contents. */
4976 s = deref_func_name(s, &len);
4977
4978 /* Invoke the function. */
4979 ret = get_func_tv(s, len, rettv, arg,
4980 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004981 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 /* Stop the expression evaluation when immediately
4983 * aborting on error, or when an interrupt occurred or
4984 * an exception was thrown but not caught. */
4985 if (aborting())
4986 {
4987 if (ret == OK)
4988 clear_tv(rettv);
4989 ret = FAIL;
4990 }
4991 }
4992 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004994 else
4995 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 }
4997
4998 if (alias != NULL)
4999 vim_free(alias);
5000 }
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 *arg = skipwhite(*arg);
5003
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005004 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5005 * expr(expr). */
5006 if (ret == OK)
5007 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 /*
5010 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5011 */
5012 if (ret == OK && evaluate && end_leader > start_leader)
5013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005014 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015 int val = 0;
5016#ifdef FEAT_FLOAT
5017 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005018
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 if (rettv->v_type == VAR_FLOAT)
5020 f = rettv->vval.v_float;
5021 else
5022#endif
5023 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005024 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005026 clear_tv(rettv);
5027 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005029 else
5030 {
5031 while (end_leader > start_leader)
5032 {
5033 --end_leader;
5034 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 {
5036#ifdef FEAT_FLOAT
5037 if (rettv->v_type == VAR_FLOAT)
5038 f = !f;
5039 else
5040#endif
5041 val = !val;
5042 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005043 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 {
5045#ifdef FEAT_FLOAT
5046 if (rettv->v_type == VAR_FLOAT)
5047 f = -f;
5048 else
5049#endif
5050 val = -val;
5051 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005052 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005053#ifdef FEAT_FLOAT
5054 if (rettv->v_type == VAR_FLOAT)
5055 {
5056 clear_tv(rettv);
5057 rettv->vval.v_float = f;
5058 }
5059 else
5060#endif
5061 {
5062 clear_tv(rettv);
5063 rettv->v_type = VAR_NUMBER;
5064 rettv->vval.v_number = val;
5065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068
5069 return ret;
5070}
5071
5072/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005073 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5074 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005075 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5076 */
5077 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005078eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005081 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005082 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005083{
5084 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005085 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 long len = -1;
5088 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005089 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005092 if (rettv->v_type == VAR_FUNC
5093#ifdef FEAT_FLOAT
5094 || rettv->v_type == VAR_FLOAT
5095#endif
5096 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005098 if (verbose)
5099 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 return FAIL;
5101 }
5102
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 /*
5106 * dict.name
5107 */
5108 key = *arg + 1;
5109 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5110 ;
5111 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005112 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 }
5115 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 /*
5118 * something[idx]
5119 *
5120 * Get the (first) variable from inside the [].
5121 */
5122 *arg = skipwhite(*arg + 1);
5123 if (**arg == ':')
5124 empty1 = TRUE;
5125 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5126 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5128 {
5129 /* not a number or string */
5130 clear_tv(&var1);
5131 return FAIL;
5132 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133
5134 /*
5135 * Get the second variable from inside the [:].
5136 */
5137 if (**arg == ':')
5138 {
5139 range = TRUE;
5140 *arg = skipwhite(*arg + 1);
5141 if (**arg == ']')
5142 empty2 = TRUE;
5143 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005145 if (!empty1)
5146 clear_tv(&var1);
5147 return FAIL;
5148 }
5149 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5150 {
5151 /* not a number or string */
5152 if (!empty1)
5153 clear_tv(&var1);
5154 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 return FAIL;
5156 }
5157 }
5158
5159 /* Check for the ']'. */
5160 if (**arg != ']')
5161 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005162 if (verbose)
5163 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 clear_tv(&var1);
5165 if (range)
5166 clear_tv(&var2);
5167 return FAIL;
5168 }
5169 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 }
5171
5172 if (evaluate)
5173 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 n1 = 0;
5175 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 n1 = get_tv_number(&var1);
5178 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 }
5180 if (range)
5181 {
5182 if (empty2)
5183 n2 = -1;
5184 else
5185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 n2 = get_tv_number(&var2);
5187 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 }
5189 }
5190
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 {
5193 case VAR_NUMBER:
5194 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005196 len = (long)STRLEN(s);
5197 if (range)
5198 {
5199 /* The resulting variable is a substring. If the indexes
5200 * are out of range the result is empty. */
5201 if (n1 < 0)
5202 {
5203 n1 = len + n1;
5204 if (n1 < 0)
5205 n1 = 0;
5206 }
5207 if (n2 < 0)
5208 n2 = len + n2;
5209 else if (n2 >= len)
5210 n2 = len;
5211 if (n1 >= len || n2 < 0 || n1 > n2)
5212 s = NULL;
5213 else
5214 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5215 }
5216 else
5217 {
5218 /* The resulting variable is a string of a single
5219 * character. If the index is too big or negative the
5220 * result is empty. */
5221 if (n1 >= len || n1 < 0)
5222 s = NULL;
5223 else
5224 s = vim_strnsave(s + n1, 1);
5225 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005226 clear_tv(rettv);
5227 rettv->v_type = VAR_STRING;
5228 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 break;
5230
5231 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005232 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 if (n1 < 0)
5234 n1 = len + n1;
5235 if (!empty1 && (n1 < 0 || n1 >= len))
5236 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005237 /* For a range we allow invalid values and return an empty
5238 * list. A list index out of range is an error. */
5239 if (!range)
5240 {
5241 if (verbose)
5242 EMSGN(_(e_listidx), n1);
5243 return FAIL;
5244 }
5245 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 }
5247 if (range)
5248 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005249 list_T *l;
5250 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251
5252 if (n2 < 0)
5253 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005254 else if (n2 >= len)
5255 n2 = len - 1;
5256 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005257 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 l = list_alloc();
5259 if (l == NULL)
5260 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 n1 <= n2; ++n1)
5263 {
5264 if (list_append_tv(l, &item->li_tv) == FAIL)
5265 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005266 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 return FAIL;
5268 }
5269 item = item->li_next;
5270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005271 clear_tv(rettv);
5272 rettv->v_type = VAR_LIST;
5273 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005274 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 }
5276 else
5277 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005278 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005279 clear_tv(rettv);
5280 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 }
5282 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283
5284 case VAR_DICT:
5285 if (range)
5286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005287 if (verbose)
5288 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 if (len == -1)
5290 clear_tv(&var1);
5291 return FAIL;
5292 }
5293 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005294 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295
5296 if (len == -1)
5297 {
5298 key = get_tv_string(&var1);
5299 if (*key == NUL)
5300 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 if (verbose)
5302 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 clear_tv(&var1);
5304 return FAIL;
5305 }
5306 }
5307
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005308 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005310 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005311 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 if (len == -1)
5313 clear_tv(&var1);
5314 if (item == NULL)
5315 return FAIL;
5316
5317 copy_tv(&item->di_tv, &var1);
5318 clear_tv(rettv);
5319 *rettv = var1;
5320 }
5321 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 }
5323 }
5324
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 return OK;
5326}
5327
5328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 * Get an option value.
5330 * "arg" points to the '&' or '+' before the option name.
5331 * "arg" is advanced to character after the option name.
5332 * Return OK or FAIL.
5333 */
5334 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005337 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 int evaluate;
5339{
5340 char_u *option_end;
5341 long numval;
5342 char_u *stringval;
5343 int opt_type;
5344 int c;
5345 int working = (**arg == '+'); /* has("+option") */
5346 int ret = OK;
5347 int opt_flags;
5348
5349 /*
5350 * Isolate the option name and find its value.
5351 */
5352 option_end = find_option_end(arg, &opt_flags);
5353 if (option_end == NULL)
5354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005355 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 EMSG2(_("E112: Option name missing: %s"), *arg);
5357 return FAIL;
5358 }
5359
5360 if (!evaluate)
5361 {
5362 *arg = option_end;
5363 return OK;
5364 }
5365
5366 c = *option_end;
5367 *option_end = NUL;
5368 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005369 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371 if (opt_type == -3) /* invalid name */
5372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 EMSG2(_("E113: Unknown option: %s"), *arg);
5375 ret = FAIL;
5376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 if (opt_type == -2) /* hidden string option */
5380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 rettv->v_type = VAR_STRING;
5382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384 else if (opt_type == -1) /* hidden number option */
5385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 rettv->v_type = VAR_NUMBER;
5387 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 else if (opt_type == 1) /* number option */
5390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 rettv->v_type = VAR_NUMBER;
5392 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 else /* string option */
5395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 rettv->v_type = VAR_STRING;
5397 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 }
5400 else if (working && (opt_type == -2 || opt_type == -1))
5401 ret = FAIL;
5402
5403 *option_end = c; /* put back for error messages */
5404 *arg = option_end;
5405
5406 return ret;
5407}
5408
5409/*
5410 * Allocate a variable for a string constant.
5411 * Return OK or FAIL.
5412 */
5413 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005414get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 int evaluate;
5418{
5419 char_u *p;
5420 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 int extra = 0;
5422
5423 /*
5424 * Find the end of the string, skipping backslashed characters.
5425 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005426 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
5428 if (*p == '\\' && p[1] != NUL)
5429 {
5430 ++p;
5431 /* A "\<x>" form occupies at least 4 characters, and produces up
5432 * to 6 characters: reserve space for 2 extra */
5433 if (*p == '<')
5434 extra += 2;
5435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437
5438 if (*p != '"')
5439 {
5440 EMSG2(_("E114: Missing quote: %s"), *arg);
5441 return FAIL;
5442 }
5443
5444 /* If only parsing, set *arg and return here */
5445 if (!evaluate)
5446 {
5447 *arg = p + 1;
5448 return OK;
5449 }
5450
5451 /*
5452 * Copy the string into allocated memory, handling backslashed
5453 * characters.
5454 */
5455 name = alloc((unsigned)(p - *arg + extra));
5456 if (name == NULL)
5457 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 rettv->v_type = VAR_STRING;
5459 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 if (*p == '\\')
5464 {
5465 switch (*++p)
5466 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 case 'b': *name++ = BS; ++p; break;
5468 case 'e': *name++ = ESC; ++p; break;
5469 case 'f': *name++ = FF; ++p; break;
5470 case 'n': *name++ = NL; ++p; break;
5471 case 'r': *name++ = CAR; ++p; break;
5472 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
5474 case 'X': /* hex: "\x1", "\x12" */
5475 case 'x':
5476 case 'u': /* Unicode: "\u0023" */
5477 case 'U':
5478 if (vim_isxdigit(p[1]))
5479 {
5480 int n, nr;
5481 int c = toupper(*p);
5482
5483 if (c == 'X')
5484 n = 2;
5485 else
5486 n = 4;
5487 nr = 0;
5488 while (--n >= 0 && vim_isxdigit(p[1]))
5489 {
5490 ++p;
5491 nr = (nr << 4) + hex2nr(*p);
5492 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494#ifdef FEAT_MBYTE
5495 /* For "\u" store the number according to
5496 * 'encoding'. */
5497 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 else
5500#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 break;
5504
5505 /* octal: "\1", "\12", "\123" */
5506 case '0':
5507 case '1':
5508 case '2':
5509 case '3':
5510 case '4':
5511 case '5':
5512 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005513 case '7': *name = *p++ - '0';
5514 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 *name = (*name << 3) + *p++ - '0';
5517 if (*p >= '0' && *p <= '7')
5518 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 break;
5522
5523 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 if (extra != 0)
5526 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 break;
5529 }
5530 /* FALLTHROUGH */
5531
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 break;
5534 }
5535 }
5536 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 *arg = p + 1;
5542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 return OK;
5544}
5545
5546/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 * Return OK or FAIL.
5549 */
5550 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 int evaluate;
5555{
5556 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005557 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005558 int reduce = 0;
5559
5560 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005562 */
5563 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5564 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005565 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005566 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005568 break;
5569 ++reduce;
5570 ++p;
5571 }
5572 }
5573
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005575 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005577 return FAIL;
5578 }
5579
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005581 if (!evaluate)
5582 {
5583 *arg = p + 1;
5584 return OK;
5585 }
5586
5587 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005589 */
5590 str = alloc((unsigned)((p - *arg) - reduce));
5591 if (str == NULL)
5592 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 rettv->v_type = VAR_STRING;
5594 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005595
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005599 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005601 break;
5602 ++p;
5603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005607 *arg = p + 1;
5608
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005609 return OK;
5610}
5611
5612/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 * Allocate a variable for a List and fill it from "*arg".
5614 * Return OK or FAIL.
5615 */
5616 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005619 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 int evaluate;
5621{
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 list_T *l = NULL;
5623 typval_T tv;
5624 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625
5626 if (evaluate)
5627 {
5628 l = list_alloc();
5629 if (l == NULL)
5630 return FAIL;
5631 }
5632
5633 *arg = skipwhite(*arg + 1);
5634 while (**arg != ']' && **arg != NUL)
5635 {
5636 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5637 goto failret;
5638 if (evaluate)
5639 {
5640 item = listitem_alloc();
5641 if (item != NULL)
5642 {
5643 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005644 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 list_append(l, item);
5646 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005647 else
5648 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 }
5650
5651 if (**arg == ']')
5652 break;
5653 if (**arg != ',')
5654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 goto failret;
5657 }
5658 *arg = skipwhite(*arg + 1);
5659 }
5660
5661 if (**arg != ']')
5662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664failret:
5665 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005666 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 return FAIL;
5668 }
5669
5670 *arg = skipwhite(*arg + 1);
5671 if (evaluate)
5672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 rettv->v_type = VAR_LIST;
5674 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 ++l->lv_refcount;
5676 }
5677
5678 return OK;
5679}
5680
5681/*
5682 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005683 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005684 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005685 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686list_alloc()
5687{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005688 list_T *l;
5689
5690 l = (list_T *)alloc_clear(sizeof(list_T));
5691 if (l != NULL)
5692 {
5693 /* Prepend the list to the list of lists for garbage collection. */
5694 if (first_list != NULL)
5695 first_list->lv_used_prev = l;
5696 l->lv_used_prev = NULL;
5697 l->lv_used_next = first_list;
5698 first_list = l;
5699 }
5700 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701}
5702
5703/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005704 * Allocate an empty list for a return value.
5705 * Returns OK or FAIL.
5706 */
5707 static int
5708rettv_list_alloc(rettv)
5709 typval_T *rettv;
5710{
5711 list_T *l = list_alloc();
5712
5713 if (l == NULL)
5714 return FAIL;
5715
5716 rettv->vval.v_list = l;
5717 rettv->v_type = VAR_LIST;
5718 ++l->lv_refcount;
5719 return OK;
5720}
5721
5722/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 * Unreference a list: decrement the reference count and free it when it
5724 * becomes zero.
5725 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005726 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005730 if (l != NULL && --l->lv_refcount <= 0)
5731 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732}
5733
5734/*
5735 * Free a list, including all items it points to.
5736 * Ignores the reference count.
5737 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005738 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005739list_free(l, recurse)
5740 list_T *l;
5741 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742{
Bram Moolenaar33570922005-01-25 22:26:29 +00005743 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005745 /* Remove the list from the list of lists for garbage collection. */
5746 if (l->lv_used_prev == NULL)
5747 first_list = l->lv_used_next;
5748 else
5749 l->lv_used_prev->lv_used_next = l->lv_used_next;
5750 if (l->lv_used_next != NULL)
5751 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5752
Bram Moolenaard9fba312005-06-26 22:34:35 +00005753 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005755 /* Remove the item before deleting it. */
5756 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005757 if (recurse || (item->li_tv.v_type != VAR_LIST
5758 && item->li_tv.v_type != VAR_DICT))
5759 clear_tv(&item->li_tv);
5760 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 }
5762 vim_free(l);
5763}
5764
5765/*
5766 * Allocate a list item.
5767 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769listitem_alloc()
5770{
Bram Moolenaar33570922005-01-25 22:26:29 +00005771 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772}
5773
5774/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005775 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776 */
5777 static void
5778listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005779 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005781 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 vim_free(item);
5783}
5784
5785/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005786 * Remove a list item from a List and free it. Also clears the value.
5787 */
5788 static void
5789listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005790 list_T *l;
5791 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005792{
5793 list_remove(l, item, item);
5794 listitem_free(item);
5795}
5796
5797/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 * Get the number of items in a list.
5799 */
5800 static long
5801list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 if (l == NULL)
5805 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005806 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807}
5808
5809/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005810 * Return TRUE when two lists have exactly the same values.
5811 */
5812 static int
5813list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005814 list_T *l1;
5815 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005816 int ic; /* ignore case for strings */
5817{
Bram Moolenaar33570922005-01-25 22:26:29 +00005818 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005819
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005820 if (l1 == l2)
5821 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005822 if (list_len(l1) != list_len(l2))
5823 return FALSE;
5824
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005825 for (item1 = l1->lv_first, item2 = l2->lv_first;
5826 item1 != NULL && item2 != NULL;
5827 item1 = item1->li_next, item2 = item2->li_next)
5828 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5829 return FALSE;
5830 return item1 == NULL && item2 == NULL;
5831}
5832
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005833#if defined(FEAT_PYTHON) || defined(PROTO)
5834/*
5835 * Return the dictitem that an entry in a hashtable points to.
5836 */
5837 dictitem_T *
5838dict_lookup(hi)
5839 hashitem_T *hi;
5840{
5841 return HI2DI(hi);
5842}
5843#endif
5844
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005845/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005846 * Return TRUE when two dictionaries have exactly the same key/values.
5847 */
5848 static int
5849dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 dict_T *d1;
5851 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005852 int ic; /* ignore case for strings */
5853{
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 hashitem_T *hi;
5855 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005856 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005857
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005858 if (d1 == d2)
5859 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005860 if (dict_len(d1) != dict_len(d2))
5861 return FALSE;
5862
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005863 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005866 if (!HASHITEM_EMPTY(hi))
5867 {
5868 item2 = dict_find(d2, hi->hi_key, -1);
5869 if (item2 == NULL)
5870 return FALSE;
5871 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5872 return FALSE;
5873 --todo;
5874 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005875 }
5876 return TRUE;
5877}
5878
5879/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005880 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005881 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005882 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 */
5884 static int
5885tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005886 typval_T *tv1;
5887 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888 int ic; /* ignore case */
5889{
5890 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005891 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005892 static int recursive = 0; /* cach recursive loops */
5893 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005894
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005895 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005896 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005897 /* Catch lists and dicts that have an endless loop by limiting
5898 * recursiveness to 1000. We guess they are equal then. */
5899 if (recursive >= 1000)
5900 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005901
5902 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005904 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005905 ++recursive;
5906 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5907 --recursive;
5908 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005909
5910 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005911 ++recursive;
5912 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5913 --recursive;
5914 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005915
5916 case VAR_FUNC:
5917 return (tv1->vval.v_string != NULL
5918 && tv2->vval.v_string != NULL
5919 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5920
5921 case VAR_NUMBER:
5922 return tv1->vval.v_number == tv2->vval.v_number;
5923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005924#ifdef FEAT_FLOAT
5925 case VAR_FLOAT:
5926 return tv1->vval.v_float == tv2->vval.v_float;
5927#endif
5928
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005929 case VAR_STRING:
5930 s1 = get_tv_string_buf(tv1, buf1);
5931 s2 = get_tv_string_buf(tv2, buf2);
5932 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005933 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005934
5935 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005936 return TRUE;
5937}
5938
5939/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 * Locate item with index "n" in list "l" and return it.
5941 * A negative index is counted from the end; -1 is the last item.
5942 * Returns NULL when "n" is out of range.
5943 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 long n;
5948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 long idx;
5951
5952 if (l == NULL)
5953 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005954
5955 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005957 n = l->lv_len + n;
5958
5959 /* Check for index out of range. */
5960 if (n < 0 || n >= l->lv_len)
5961 return NULL;
5962
5963 /* When there is a cached index may start search from there. */
5964 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005966 if (n < l->lv_idx / 2)
5967 {
5968 /* closest to the start of the list */
5969 item = l->lv_first;
5970 idx = 0;
5971 }
5972 else if (n > (l->lv_idx + l->lv_len) / 2)
5973 {
5974 /* closest to the end of the list */
5975 item = l->lv_last;
5976 idx = l->lv_len - 1;
5977 }
5978 else
5979 {
5980 /* closest to the cached index */
5981 item = l->lv_idx_item;
5982 idx = l->lv_idx;
5983 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 }
5985 else
5986 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005987 if (n < l->lv_len / 2)
5988 {
5989 /* closest to the start of the list */
5990 item = l->lv_first;
5991 idx = 0;
5992 }
5993 else
5994 {
5995 /* closest to the end of the list */
5996 item = l->lv_last;
5997 idx = l->lv_len - 1;
5998 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006000
6001 while (n > idx)
6002 {
6003 /* search forward */
6004 item = item->li_next;
6005 ++idx;
6006 }
6007 while (n < idx)
6008 {
6009 /* search backward */
6010 item = item->li_prev;
6011 --idx;
6012 }
6013
6014 /* cache the used index */
6015 l->lv_idx = idx;
6016 l->lv_idx_item = item;
6017
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 return item;
6019}
6020
6021/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006022 * Get list item "l[idx]" as a number.
6023 */
6024 static long
6025list_find_nr(l, idx, errorp)
6026 list_T *l;
6027 long idx;
6028 int *errorp; /* set to TRUE when something wrong */
6029{
6030 listitem_T *li;
6031
6032 li = list_find(l, idx);
6033 if (li == NULL)
6034 {
6035 if (errorp != NULL)
6036 *errorp = TRUE;
6037 return -1L;
6038 }
6039 return get_tv_number_chk(&li->li_tv, errorp);
6040}
6041
6042/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006043 * Locate "item" list "l" and return its index.
6044 * Returns -1 when "item" is not in the list.
6045 */
6046 static long
6047list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 list_T *l;
6049 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006050{
6051 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006053
6054 if (l == NULL)
6055 return -1;
6056 idx = 0;
6057 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6058 ++idx;
6059 if (li == NULL)
6060 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006061 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006062}
6063
6064/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 * Append item "item" to the end of list "l".
6066 */
6067 static void
6068list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 list_T *l;
6070 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071{
6072 if (l->lv_last == NULL)
6073 {
6074 /* empty list */
6075 l->lv_first = item;
6076 l->lv_last = item;
6077 item->li_prev = NULL;
6078 }
6079 else
6080 {
6081 l->lv_last->li_next = item;
6082 item->li_prev = l->lv_last;
6083 l->lv_last = item;
6084 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006085 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 item->li_next = NULL;
6087}
6088
6089/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 */
6093 static int
6094list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 list_T *l;
6096 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006098 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099
Bram Moolenaar05159a02005-02-26 23:04:13 +00006100 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006102 copy_tv(tv, &li->li_tv);
6103 list_append(l, li);
6104 return OK;
6105}
6106
6107/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006108 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006109 * Return FAIL when out of memory.
6110 */
6111 int
6112list_append_dict(list, dict)
6113 list_T *list;
6114 dict_T *dict;
6115{
6116 listitem_T *li = listitem_alloc();
6117
6118 if (li == NULL)
6119 return FAIL;
6120 li->li_tv.v_type = VAR_DICT;
6121 li->li_tv.v_lock = 0;
6122 li->li_tv.vval.v_dict = dict;
6123 list_append(list, li);
6124 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 return OK;
6126}
6127
6128/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006129 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006130 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006131 * Returns FAIL when out of memory.
6132 */
6133 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006134list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006135 list_T *l;
6136 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006137 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006138{
6139 listitem_T *li = listitem_alloc();
6140
6141 if (li == NULL)
6142 return FAIL;
6143 list_append(l, li);
6144 li->li_tv.v_type = VAR_STRING;
6145 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006146 if (str == NULL)
6147 li->li_tv.vval.v_string = NULL;
6148 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006149 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006150 return FAIL;
6151 return OK;
6152}
6153
6154/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006155 * Append "n" to list "l".
6156 * Returns FAIL when out of memory.
6157 */
6158 static int
6159list_append_number(l, n)
6160 list_T *l;
6161 varnumber_T n;
6162{
6163 listitem_T *li;
6164
6165 li = listitem_alloc();
6166 if (li == NULL)
6167 return FAIL;
6168 li->li_tv.v_type = VAR_NUMBER;
6169 li->li_tv.v_lock = 0;
6170 li->li_tv.vval.v_number = n;
6171 list_append(l, li);
6172 return OK;
6173}
6174
6175/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177 * If "item" is NULL append at the end.
6178 * Return FAIL when out of memory.
6179 */
6180 static int
6181list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 list_T *l;
6183 typval_T *tv;
6184 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185{
Bram Moolenaar33570922005-01-25 22:26:29 +00006186 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006187
6188 if (ni == NULL)
6189 return FAIL;
6190 copy_tv(tv, &ni->li_tv);
6191 if (item == NULL)
6192 /* Append new item at end of list. */
6193 list_append(l, ni);
6194 else
6195 {
6196 /* Insert new item before existing item. */
6197 ni->li_prev = item->li_prev;
6198 ni->li_next = item;
6199 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006202 ++l->lv_idx;
6203 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006207 l->lv_idx_item = NULL;
6208 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006209 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006210 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006211 }
6212 return OK;
6213}
6214
6215/*
6216 * Extend "l1" with "l2".
6217 * If "bef" is NULL append at the end, otherwise insert before this item.
6218 * Returns FAIL when out of memory.
6219 */
6220 static int
6221list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006222 list_T *l1;
6223 list_T *l2;
6224 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225{
Bram Moolenaar33570922005-01-25 22:26:29 +00006226 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227
6228 for (item = l2->lv_first; item != NULL; item = item->li_next)
6229 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6230 return FAIL;
6231 return OK;
6232}
6233
6234/*
6235 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6236 * Return FAIL when out of memory.
6237 */
6238 static int
6239list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 list_T *l1;
6241 list_T *l2;
6242 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243{
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245
6246 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006247 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 if (l == NULL)
6249 return FAIL;
6250 tv->v_type = VAR_LIST;
6251 tv->vval.v_list = l;
6252
6253 /* append all items from the second list */
6254 return list_extend(l, l2, NULL);
6255}
6256
6257/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006258 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006260 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 * Returns NULL when out of memory.
6262 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006264list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006267 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268{
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 list_T *copy;
6270 listitem_T *item;
6271 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272
6273 if (orig == NULL)
6274 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275
6276 copy = list_alloc();
6277 if (copy != NULL)
6278 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006279 if (copyID != 0)
6280 {
6281 /* Do this before adding the items, because one of the items may
6282 * refer back to this list. */
6283 orig->lv_copyID = copyID;
6284 orig->lv_copylist = copy;
6285 }
6286 for (item = orig->lv_first; item != NULL && !got_int;
6287 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 {
6289 ni = listitem_alloc();
6290 if (ni == NULL)
6291 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006293 {
6294 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6295 {
6296 vim_free(ni);
6297 break;
6298 }
6299 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006301 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 list_append(copy, ni);
6303 }
6304 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006305 if (item != NULL)
6306 {
6307 list_unref(copy);
6308 copy = NULL;
6309 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 }
6311
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 return copy;
6313}
6314
6315/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006317 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006319 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006320list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 list_T *l;
6322 listitem_T *item;
6323 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006324{
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006327 /* notify watchers */
6328 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331 list_fix_watch(l, ip);
6332 if (ip == item2)
6333 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335
6336 if (item2->li_next == NULL)
6337 l->lv_last = item->li_prev;
6338 else
6339 item2->li_next->li_prev = item->li_prev;
6340 if (item->li_prev == NULL)
6341 l->lv_first = item2->li_next;
6342 else
6343 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006344 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345}
6346
6347/*
6348 * Return an allocated string with the string representation of a list.
6349 * May return NULL.
6350 */
6351 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006352list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006354 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355{
6356 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357
6358 if (tv->vval.v_list == NULL)
6359 return NULL;
6360 ga_init2(&ga, (int)sizeof(char), 80);
6361 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006362 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006363 {
6364 vim_free(ga.ga_data);
6365 return NULL;
6366 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006367 ga_append(&ga, ']');
6368 ga_append(&ga, NUL);
6369 return (char_u *)ga.ga_data;
6370}
6371
6372/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006373 * Join list "l" into a string in "*gap", using separator "sep".
6374 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006375 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006376 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006378list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006379 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006381 char_u *sep;
6382 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006383 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006384{
6385 int first = TRUE;
6386 char_u *tofree;
6387 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006389 char_u *s;
6390
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006391 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006392 {
6393 if (first)
6394 first = FALSE;
6395 else
6396 ga_concat(gap, sep);
6397
6398 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006399 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006400 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006401 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006402 if (s != NULL)
6403 ga_concat(gap, s);
6404 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006405 if (s == NULL)
6406 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006407 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006408 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006409}
6410
6411/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006412 * Garbage collection for lists and dictionaries.
6413 *
6414 * We use reference counts to be able to free most items right away when they
6415 * are no longer used. But for composite items it's possible that it becomes
6416 * unused while the reference count is > 0: When there is a recursive
6417 * reference. Example:
6418 * :let l = [1, 2, 3]
6419 * :let d = {9: l}
6420 * :let l[1] = d
6421 *
6422 * Since this is quite unusual we handle this with garbage collection: every
6423 * once in a while find out which lists and dicts are not referenced from any
6424 * variable.
6425 *
6426 * Here is a good reference text about garbage collection (refers to Python
6427 * but it applies to all reference-counting mechanisms):
6428 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006429 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006430
6431/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006432 * Do garbage collection for lists and dicts.
6433 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006434 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006435 int
6436garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006437{
6438 dict_T *dd;
6439 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006440 int copyID = ++current_copyID;
6441 buf_T *buf;
6442 win_T *wp;
6443 int i;
6444 funccall_T *fc;
6445 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006446#ifdef FEAT_WINDOWS
6447 tabpage_T *tp;
6448#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006449
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006450 /* Only do this once. */
6451 want_garbage_collect = FALSE;
6452 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006453 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006454
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006455 /*
6456 * 1. Go through all accessible variables and mark all lists and dicts
6457 * with copyID.
6458 */
6459 /* script-local variables */
6460 for (i = 1; i <= ga_scripts.ga_len; ++i)
6461 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6462
6463 /* buffer-local variables */
6464 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6465 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6466
6467 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006468 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006469 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6470
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006471#ifdef FEAT_WINDOWS
6472 /* tabpage-local variables */
6473 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6474 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6475#endif
6476
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006477 /* global variables */
6478 set_ref_in_ht(&globvarht, copyID);
6479
6480 /* function-local variables */
6481 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6482 {
6483 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6484 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6485 }
6486
6487 /*
6488 * 2. Go through the list of dicts and free items without the copyID.
6489 */
6490 for (dd = first_dict; dd != NULL; )
6491 if (dd->dv_copyID != copyID)
6492 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006493 /* Free the Dictionary and ordinary items it contains, but don't
6494 * recurse into Lists and Dictionaries, they will be in the list
6495 * of dicts or list of lists. */
6496 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006497 did_free = TRUE;
6498
6499 /* restart, next dict may also have been freed */
6500 dd = first_dict;
6501 }
6502 else
6503 dd = dd->dv_used_next;
6504
6505 /*
6506 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006507 * But don't free a list that has a watcher (used in a for loop), these
6508 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006509 */
6510 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006511 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006512 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006513 /* Free the List and ordinary items it contains, but don't recurse
6514 * into Lists and Dictionaries, they will be in the list of dicts
6515 * or list of lists. */
6516 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006517 did_free = TRUE;
6518
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006519 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006520 ll = first_list;
6521 }
6522 else
6523 ll = ll->lv_used_next;
6524
6525 return did_free;
6526}
6527
6528/*
6529 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6530 */
6531 static void
6532set_ref_in_ht(ht, copyID)
6533 hashtab_T *ht;
6534 int copyID;
6535{
6536 int todo;
6537 hashitem_T *hi;
6538
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006539 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006540 for (hi = ht->ht_array; todo > 0; ++hi)
6541 if (!HASHITEM_EMPTY(hi))
6542 {
6543 --todo;
6544 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6545 }
6546}
6547
6548/*
6549 * Mark all lists and dicts referenced through list "l" with "copyID".
6550 */
6551 static void
6552set_ref_in_list(l, copyID)
6553 list_T *l;
6554 int copyID;
6555{
6556 listitem_T *li;
6557
6558 for (li = l->lv_first; li != NULL; li = li->li_next)
6559 set_ref_in_item(&li->li_tv, copyID);
6560}
6561
6562/*
6563 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6564 */
6565 static void
6566set_ref_in_item(tv, copyID)
6567 typval_T *tv;
6568 int copyID;
6569{
6570 dict_T *dd;
6571 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006572
6573 switch (tv->v_type)
6574 {
6575 case VAR_DICT:
6576 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006577 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006578 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006579 /* Didn't see this dict yet. */
6580 dd->dv_copyID = copyID;
6581 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006582 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006583 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006584
6585 case VAR_LIST:
6586 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006587 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006588 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006589 /* Didn't see this list yet. */
6590 ll->lv_copyID = copyID;
6591 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006592 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006594 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006595 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006596}
6597
6598/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006599 * Allocate an empty header for a dictionary.
6600 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006601 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006602dict_alloc()
6603{
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006605
Bram Moolenaar33570922005-01-25 22:26:29 +00006606 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006607 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006608 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006609 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006610 if (first_dict != NULL)
6611 first_dict->dv_used_prev = d;
6612 d->dv_used_next = first_dict;
6613 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006614 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006615
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006617 d->dv_lock = 0;
6618 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006620 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006621 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006622}
6623
6624/*
6625 * Unreference a Dictionary: decrement the reference count and free it when it
6626 * becomes zero.
6627 */
6628 static void
6629dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006630 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006631{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006632 if (d != NULL && --d->dv_refcount <= 0)
6633 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006634}
6635
6636/*
6637 * Free a Dictionary, including all items it contains.
6638 * Ignores the reference count.
6639 */
6640 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006641dict_free(d, recurse)
6642 dict_T *d;
6643 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006645 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006647 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006649 /* Remove the dict from the list of dicts for garbage collection. */
6650 if (d->dv_used_prev == NULL)
6651 first_dict = d->dv_used_next;
6652 else
6653 d->dv_used_prev->dv_used_next = d->dv_used_next;
6654 if (d->dv_used_next != NULL)
6655 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6656
6657 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006658 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006659 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006662 if (!HASHITEM_EMPTY(hi))
6663 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006664 /* Remove the item before deleting it, just in case there is
6665 * something recursive causing trouble. */
6666 di = HI2DI(hi);
6667 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006668 if (recurse || (di->di_tv.v_type != VAR_LIST
6669 && di->di_tv.v_type != VAR_DICT))
6670 clear_tv(&di->di_tv);
6671 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006672 --todo;
6673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006674 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006675 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 vim_free(d);
6677}
6678
6679/*
6680 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006681 * The "key" is copied to the new item.
6682 * Note that the value of the item "di_tv" still needs to be initialized!
6683 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006686dictitem_alloc(key)
6687 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688{
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006690
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006691 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006692 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006693 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006694 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006695 di->di_flags = 0;
6696 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006697 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006698}
6699
6700/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701 * Make a copy of a Dictionary item.
6702 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006703 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006704dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006705 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006706{
Bram Moolenaar33570922005-01-25 22:26:29 +00006707 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006708
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006709 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6710 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 if (di != NULL)
6712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006713 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006714 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006715 copy_tv(&org->di_tv, &di->di_tv);
6716 }
6717 return di;
6718}
6719
6720/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006721 * Remove item "item" from Dictionary "dict" and free it.
6722 */
6723 static void
6724dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 dict_T *dict;
6726 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006727{
Bram Moolenaar33570922005-01-25 22:26:29 +00006728 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006729
Bram Moolenaar33570922005-01-25 22:26:29 +00006730 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006731 if (HASHITEM_EMPTY(hi))
6732 EMSG2(_(e_intern2), "dictitem_remove()");
6733 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006734 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006735 dictitem_free(item);
6736}
6737
6738/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739 * Free a dict item. Also clears the value.
6740 */
6741 static void
6742dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006744{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006745 clear_tv(&item->di_tv);
6746 vim_free(item);
6747}
6748
6749/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006750 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6751 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006752 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006753 * Returns NULL when out of memory.
6754 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006755 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006756dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006758 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006759 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760{
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 dict_T *copy;
6762 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006763 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006764 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006765
6766 if (orig == NULL)
6767 return NULL;
6768
6769 copy = dict_alloc();
6770 if (copy != NULL)
6771 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006772 if (copyID != 0)
6773 {
6774 orig->dv_copyID = copyID;
6775 orig->dv_copydict = copy;
6776 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006777 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006778 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006779 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006780 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006781 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006782 --todo;
6783
6784 di = dictitem_alloc(hi->hi_key);
6785 if (di == NULL)
6786 break;
6787 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006788 {
6789 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6790 copyID) == FAIL)
6791 {
6792 vim_free(di);
6793 break;
6794 }
6795 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006796 else
6797 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6798 if (dict_add(copy, di) == FAIL)
6799 {
6800 dictitem_free(di);
6801 break;
6802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006804 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006805
Bram Moolenaare9a41262005-01-15 22:18:47 +00006806 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006807 if (todo > 0)
6808 {
6809 dict_unref(copy);
6810 copy = NULL;
6811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 }
6813
6814 return copy;
6815}
6816
6817/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006818 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006819 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006821 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006823 dict_T *d;
6824 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006825{
Bram Moolenaar33570922005-01-25 22:26:29 +00006826 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006827}
6828
Bram Moolenaar8c711452005-01-14 21:53:12 +00006829/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006830 * Add a number or string entry to dictionary "d".
6831 * When "str" is NULL use number "nr", otherwise use "str".
6832 * Returns FAIL when out of memory and when key already exists.
6833 */
6834 int
6835dict_add_nr_str(d, key, nr, str)
6836 dict_T *d;
6837 char *key;
6838 long nr;
6839 char_u *str;
6840{
6841 dictitem_T *item;
6842
6843 item = dictitem_alloc((char_u *)key);
6844 if (item == NULL)
6845 return FAIL;
6846 item->di_tv.v_lock = 0;
6847 if (str == NULL)
6848 {
6849 item->di_tv.v_type = VAR_NUMBER;
6850 item->di_tv.vval.v_number = nr;
6851 }
6852 else
6853 {
6854 item->di_tv.v_type = VAR_STRING;
6855 item->di_tv.vval.v_string = vim_strsave(str);
6856 }
6857 if (dict_add(d, item) == FAIL)
6858 {
6859 dictitem_free(item);
6860 return FAIL;
6861 }
6862 return OK;
6863}
6864
6865/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006866 * Get the number of items in a Dictionary.
6867 */
6868 static long
6869dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006870 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872 if (d == NULL)
6873 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006874 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006875}
6876
6877/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006878 * Find item "key[len]" in Dictionary "d".
6879 * If "len" is negative use strlen(key).
6880 * Returns NULL when not found.
6881 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006882 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006883dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006884 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006885 char_u *key;
6886 int len;
6887{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006888#define AKEYLEN 200
6889 char_u buf[AKEYLEN];
6890 char_u *akey;
6891 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006893
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006894 if (len < 0)
6895 akey = key;
6896 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898 tofree = akey = vim_strnsave(key, len);
6899 if (akey == NULL)
6900 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006901 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006902 else
6903 {
6904 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006905 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006906 akey = buf;
6907 }
6908
Bram Moolenaar33570922005-01-25 22:26:29 +00006909 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006910 vim_free(tofree);
6911 if (HASHITEM_EMPTY(hi))
6912 return NULL;
6913 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006914}
6915
6916/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006917 * Get a string item from a dictionary.
6918 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006919 * Returns NULL if the entry doesn't exist or out of memory.
6920 */
6921 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006922get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006923 dict_T *d;
6924 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006925 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006926{
6927 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006928 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006929
6930 di = dict_find(d, key, -1);
6931 if (di == NULL)
6932 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006933 s = get_tv_string(&di->di_tv);
6934 if (save && s != NULL)
6935 s = vim_strsave(s);
6936 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006937}
6938
6939/*
6940 * Get a number item from a dictionary.
6941 * Returns 0 if the entry doesn't exist or out of memory.
6942 */
6943 long
6944get_dict_number(d, key)
6945 dict_T *d;
6946 char_u *key;
6947{
6948 dictitem_T *di;
6949
6950 di = dict_find(d, key, -1);
6951 if (di == NULL)
6952 return 0;
6953 return get_tv_number(&di->di_tv);
6954}
6955
6956/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006957 * Return an allocated string with the string representation of a Dictionary.
6958 * May return NULL.
6959 */
6960 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006961dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006962 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006963 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006964{
6965 garray_T ga;
6966 int first = TRUE;
6967 char_u *tofree;
6968 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006973
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975 return NULL;
6976 ga_init2(&ga, (int)sizeof(char), 80);
6977 ga_append(&ga, '{');
6978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006979 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006980 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006981 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006982 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006984 --todo;
6985
6986 if (first)
6987 first = FALSE;
6988 else
6989 ga_concat(&ga, (char_u *)", ");
6990
6991 tofree = string_quote(hi->hi_key, FALSE);
6992 if (tofree != NULL)
6993 {
6994 ga_concat(&ga, tofree);
6995 vim_free(tofree);
6996 }
6997 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006998 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999 if (s != NULL)
7000 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007002 if (s == NULL)
7003 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007005 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007006 if (todo > 0)
7007 {
7008 vim_free(ga.ga_data);
7009 return NULL;
7010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011
7012 ga_append(&ga, '}');
7013 ga_append(&ga, NUL);
7014 return (char_u *)ga.ga_data;
7015}
7016
7017/*
7018 * Allocate a variable for a Dictionary and fill it from "*arg".
7019 * Return OK or FAIL. Returns NOTDONE for {expr}.
7020 */
7021 static int
7022get_dict_tv(arg, rettv, evaluate)
7023 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025 int evaluate;
7026{
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 dict_T *d = NULL;
7028 typval_T tvkey;
7029 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007030 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034
7035 /*
7036 * First check if it's not a curly-braces thing: {expr}.
7037 * Must do this without evaluating, otherwise a function may be called
7038 * twice. Unfortunately this means we need to call eval1() twice for the
7039 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007040 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 if (*start != '}')
7043 {
7044 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7045 return FAIL;
7046 if (*start == '}')
7047 return NOTDONE;
7048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049
7050 if (evaluate)
7051 {
7052 d = dict_alloc();
7053 if (d == NULL)
7054 return FAIL;
7055 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 tvkey.v_type = VAR_UNKNOWN;
7057 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058
7059 *arg = skipwhite(*arg + 1);
7060 while (**arg != '}' && **arg != NUL)
7061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063 goto failret;
7064 if (**arg != ':')
7065 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007066 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007067 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068 goto failret;
7069 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007070 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007071 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007072 key = get_tv_string_buf_chk(&tvkey, buf);
7073 if (key == NULL || *key == NUL)
7074 {
7075 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7076 if (key != NULL)
7077 EMSG(_(e_emptykey));
7078 clear_tv(&tvkey);
7079 goto failret;
7080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007082
7083 *arg = skipwhite(*arg + 1);
7084 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7085 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007086 if (evaluate)
7087 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 goto failret;
7089 }
7090 if (evaluate)
7091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 if (item != NULL)
7094 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007095 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097 clear_tv(&tv);
7098 goto failret;
7099 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007100 item = dictitem_alloc(key);
7101 clear_tv(&tvkey);
7102 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007105 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106 if (dict_add(d, item) == FAIL)
7107 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 }
7109 }
7110
7111 if (**arg == '}')
7112 break;
7113 if (**arg != ',')
7114 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007115 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 goto failret;
7117 }
7118 *arg = skipwhite(*arg + 1);
7119 }
7120
7121 if (**arg != '}')
7122 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007123 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124failret:
7125 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007126 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127 return FAIL;
7128 }
7129
7130 *arg = skipwhite(*arg + 1);
7131 if (evaluate)
7132 {
7133 rettv->v_type = VAR_DICT;
7134 rettv->vval.v_dict = d;
7135 ++d->dv_refcount;
7136 }
7137
7138 return OK;
7139}
7140
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007142 * Return a string with the string representation of a variable.
7143 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007144 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007145 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007146 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007147 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007148 */
7149 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007150echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007151 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007152 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007153 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007154 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007155{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007156 static int recurse = 0;
7157 char_u *r = NULL;
7158
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007161 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162 *tofree = NULL;
7163 return NULL;
7164 }
7165 ++recurse;
7166
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007167 switch (tv->v_type)
7168 {
7169 case VAR_FUNC:
7170 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 r = tv->vval.v_string;
7172 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007173
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007174 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007175 if (tv->vval.v_list == NULL)
7176 {
7177 *tofree = NULL;
7178 r = NULL;
7179 }
7180 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7181 {
7182 *tofree = NULL;
7183 r = (char_u *)"[...]";
7184 }
7185 else
7186 {
7187 tv->vval.v_list->lv_copyID = copyID;
7188 *tofree = list2string(tv, copyID);
7189 r = *tofree;
7190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007192
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007194 if (tv->vval.v_dict == NULL)
7195 {
7196 *tofree = NULL;
7197 r = NULL;
7198 }
7199 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7200 {
7201 *tofree = NULL;
7202 r = (char_u *)"{...}";
7203 }
7204 else
7205 {
7206 tv->vval.v_dict->dv_copyID = copyID;
7207 *tofree = dict2string(tv, copyID);
7208 r = *tofree;
7209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007210 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007211
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007212 case VAR_STRING:
7213 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 *tofree = NULL;
7215 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007216 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007217
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007218#ifdef FEAT_FLOAT
7219 case VAR_FLOAT:
7220 *tofree = NULL;
7221 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7222 r = numbuf;
7223 break;
7224#endif
7225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007226 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007227 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007229 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230
7231 --recurse;
7232 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007233}
7234
7235/*
7236 * Return a string with the string representation of a variable.
7237 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7238 * "numbuf" is used for a number.
7239 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007240 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007241 */
7242 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007243tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007245 char_u **tofree;
7246 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007247 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007248{
7249 switch (tv->v_type)
7250 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007251 case VAR_FUNC:
7252 *tofree = string_quote(tv->vval.v_string, TRUE);
7253 return *tofree;
7254 case VAR_STRING:
7255 *tofree = string_quote(tv->vval.v_string, FALSE);
7256 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007257#ifdef FEAT_FLOAT
7258 case VAR_FLOAT:
7259 *tofree = NULL;
7260 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7261 return numbuf;
7262#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007263 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007264 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007266 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007267 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007268 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007269 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007270 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007271}
7272
7273/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 * Return string "str" in ' quotes, doubling ' characters.
7275 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007277 */
7278 static char_u *
7279string_quote(str, function)
7280 char_u *str;
7281 int function;
7282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007284 char_u *p, *r, *s;
7285
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 len = (function ? 13 : 3);
7287 if (str != NULL)
7288 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007289 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 for (p = str; *p != NUL; mb_ptr_adv(p))
7291 if (*p == '\'')
7292 ++len;
7293 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007294 s = r = alloc(len);
7295 if (r != NULL)
7296 {
7297 if (function)
7298 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007300 r += 10;
7301 }
7302 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 if (str != NULL)
7305 for (p = str; *p != NUL; )
7306 {
7307 if (*p == '\'')
7308 *r++ = '\'';
7309 MB_COPY_CHAR(p, r);
7310 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007312 if (function)
7313 *r++ = ')';
7314 *r++ = NUL;
7315 }
7316 return s;
7317}
7318
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007319#ifdef FEAT_FLOAT
7320/*
7321 * Convert the string "text" to a floating point number.
7322 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7323 * this always uses a decimal point.
7324 * Returns the length of the text that was consumed.
7325 */
7326 static int
7327string2float(text, value)
7328 char_u *text;
7329 float_T *value; /* result stored here */
7330{
7331 char *s = (char *)text;
7332 float_T f;
7333
7334 f = strtod(s, &s);
7335 *value = f;
7336 return (int)((char_u *)s - text);
7337}
7338#endif
7339
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 * Get the value of an environment variable.
7342 * "arg" is pointing to the '$'. It is advanced to after the name.
7343 * If the environment variable was not set, silently assume it is empty.
7344 * Always return OK.
7345 */
7346 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007347get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 int evaluate;
7351{
7352 char_u *string = NULL;
7353 int len;
7354 int cc;
7355 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007356 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 ++*arg;
7359 name = *arg;
7360 len = get_env_len(arg);
7361 if (evaluate)
7362 {
7363 if (len != 0)
7364 {
7365 cc = name[len];
7366 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007367 /* first try vim_getenv(), fast for normal environment vars */
7368 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007370 {
7371 if (!mustfree)
7372 string = vim_strsave(string);
7373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 else
7375 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007376 if (mustfree)
7377 vim_free(string);
7378
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 /* next try expanding things like $VIM and ${HOME} */
7380 string = expand_env_save(name - 1);
7381 if (string != NULL && *string == '$')
7382 {
7383 vim_free(string);
7384 string = NULL;
7385 }
7386 }
7387 name[len] = cc;
7388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389 rettv->v_type = VAR_STRING;
7390 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 }
7392
7393 return OK;
7394}
7395
7396/*
7397 * Array with names and number of arguments of all internal functions
7398 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7399 */
7400static struct fst
7401{
7402 char *f_name; /* function name */
7403 char f_min_argc; /* minimal number of arguments */
7404 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007406 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407} functions[] =
7408{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007409#ifdef FEAT_FLOAT
7410 {"abs", 1, 1, f_abs},
7411#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007412 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {"append", 2, 2, f_append},
7414 {"argc", 0, 0, f_argc},
7415 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007416 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007417#ifdef FEAT_FLOAT
7418 {"atan", 1, 1, f_atan},
7419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007421 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {"bufexists", 1, 1, f_bufexists},
7423 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7424 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7425 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7426 {"buflisted", 1, 1, f_buflisted},
7427 {"bufloaded", 1, 1, f_bufloaded},
7428 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007429 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 {"bufwinnr", 1, 1, f_bufwinnr},
7431 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007432 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007434#ifdef FEAT_FLOAT
7435 {"ceil", 1, 1, f_ceil},
7436#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007437 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 {"char2nr", 1, 1, f_char2nr},
7439 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007440 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007442#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007443 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007444 {"complete_add", 1, 1, f_complete_add},
7445 {"complete_check", 0, 0, f_complete_check},
7446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007448 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007449#ifdef FEAT_FLOAT
7450 {"cos", 1, 1, f_cos},
7451#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007452 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007454 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007455 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {"delete", 1, 1, f_delete},
7457 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007458 {"diff_filler", 1, 1, f_diff_filler},
7459 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007460 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007462 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {"eventhandler", 0, 0, f_eventhandler},
7464 {"executable", 1, 1, f_executable},
7465 {"exists", 1, 1, f_exists},
7466 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007467 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007468 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7470 {"filereadable", 1, 1, f_filereadable},
7471 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007472 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007473 {"finddir", 1, 3, f_finddir},
7474 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007475#ifdef FEAT_FLOAT
7476 {"float2nr", 1, 1, f_float2nr},
7477 {"floor", 1, 1, f_floor},
7478#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007479 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 {"fnamemodify", 2, 2, f_fnamemodify},
7481 {"foldclosed", 1, 1, f_foldclosed},
7482 {"foldclosedend", 1, 1, f_foldclosedend},
7483 {"foldlevel", 1, 1, f_foldlevel},
7484 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007485 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007487 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007488 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007489 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007490 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {"getbufvar", 2, 2, f_getbufvar},
7492 {"getchar", 0, 1, f_getchar},
7493 {"getcharmod", 0, 0, f_getcharmod},
7494 {"getcmdline", 0, 0, f_getcmdline},
7495 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007496 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007498 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007499 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {"getfsize", 1, 1, f_getfsize},
7501 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007502 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007503 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007504 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007505 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007506 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007507 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007508 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007509 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007511 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 {"getwinposx", 0, 0, f_getwinposx},
7513 {"getwinposy", 0, 0, f_getwinposy},
7514 {"getwinvar", 2, 2, f_getwinvar},
7515 {"glob", 1, 1, f_glob},
7516 {"globpath", 2, 2, f_globpath},
7517 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007518 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007519 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007520 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7522 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7523 {"histadd", 2, 2, f_histadd},
7524 {"histdel", 1, 2, f_histdel},
7525 {"histget", 1, 2, f_histget},
7526 {"histnr", 1, 1, f_histnr},
7527 {"hlID", 1, 1, f_hlID},
7528 {"hlexists", 1, 1, f_hlexists},
7529 {"hostname", 0, 0, f_hostname},
7530 {"iconv", 3, 3, f_iconv},
7531 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007532 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007533 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007535 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {"inputrestore", 0, 0, f_inputrestore},
7537 {"inputsave", 0, 0, f_inputsave},
7538 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007539 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007541 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007543 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007546 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {"libcall", 3, 3, f_libcall},
7548 {"libcallnr", 3, 3, f_libcallnr},
7549 {"line", 1, 1, f_line},
7550 {"line2byte", 1, 1, f_line2byte},
7551 {"lispindent", 1, 1, f_lispindent},
7552 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007553#ifdef FEAT_FLOAT
7554 {"log10", 1, 1, f_log10},
7555#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007556 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007557 {"maparg", 1, 3, f_maparg},
7558 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007559 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007560 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007561 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007562 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007563 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007564 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007565 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007566 {"max", 1, 1, f_max},
7567 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007568#ifdef vim_mkdir
7569 {"mkdir", 1, 3, f_mkdir},
7570#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007571 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {"nextnonblank", 1, 1, f_nextnonblank},
7573 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007574 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007575#ifdef FEAT_FLOAT
7576 {"pow", 2, 2, f_pow},
7577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007579 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007580 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007582 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007583 {"reltime", 0, 2, f_reltime},
7584 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {"remote_expr", 2, 3, f_remote_expr},
7586 {"remote_foreground", 1, 1, f_remote_foreground},
7587 {"remote_peek", 1, 2, f_remote_peek},
7588 {"remote_read", 1, 1, f_remote_read},
7589 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007590 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007592 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007594 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007595#ifdef FEAT_FLOAT
7596 {"round", 1, 1, f_round},
7597#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007598 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007599 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007600 {"searchpair", 3, 7, f_searchpair},
7601 {"searchpairpos", 3, 7, f_searchpairpos},
7602 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {"server2client", 2, 2, f_server2client},
7604 {"serverlist", 0, 0, f_serverlist},
7605 {"setbufvar", 3, 3, f_setbufvar},
7606 {"setcmdpos", 1, 1, f_setcmdpos},
7607 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007608 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007609 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007610 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007611 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007613 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007615 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007617#ifdef FEAT_FLOAT
7618 {"sin", 1, 1, f_sin},
7619#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007620 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007621 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007622 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007623 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007624 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007625#ifdef FEAT_FLOAT
7626 {"sqrt", 1, 1, f_sqrt},
7627 {"str2float", 1, 1, f_str2float},
7628#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007629 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630#ifdef HAVE_STRFTIME
7631 {"strftime", 1, 2, f_strftime},
7632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007633 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {"strlen", 1, 1, f_strlen},
7636 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007637 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {"strtrans", 1, 1, f_strtrans},
7639 {"submatch", 1, 1, f_submatch},
7640 {"substitute", 4, 4, f_substitute},
7641 {"synID", 3, 3, f_synID},
7642 {"synIDattr", 2, 3, f_synIDattr},
7643 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007644 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007645 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007646 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007647 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007648 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007649 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007650 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007652 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {"tolower", 1, 1, f_tolower},
7654 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007655 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007656#ifdef FEAT_FLOAT
7657 {"trunc", 1, 1, f_trunc},
7658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {"virtcol", 1, 1, f_virtcol},
7662 {"visualmode", 0, 1, f_visualmode},
7663 {"winbufnr", 1, 1, f_winbufnr},
7664 {"wincol", 0, 0, f_wincol},
7665 {"winheight", 1, 1, f_winheight},
7666 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007667 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007669 {"winrestview", 1, 1, f_winrestview},
7670 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007672 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673};
7674
7675#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7676
7677/*
7678 * Function given to ExpandGeneric() to obtain the list of internal
7679 * or user defined function names.
7680 */
7681 char_u *
7682get_function_name(xp, idx)
7683 expand_T *xp;
7684 int idx;
7685{
7686 static int intidx = -1;
7687 char_u *name;
7688
7689 if (idx == 0)
7690 intidx = -1;
7691 if (intidx < 0)
7692 {
7693 name = get_user_func_name(xp, idx);
7694 if (name != NULL)
7695 return name;
7696 }
7697 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7698 {
7699 STRCPY(IObuff, functions[intidx].f_name);
7700 STRCAT(IObuff, "(");
7701 if (functions[intidx].f_max_argc == 0)
7702 STRCAT(IObuff, ")");
7703 return IObuff;
7704 }
7705
7706 return NULL;
7707}
7708
7709/*
7710 * Function given to ExpandGeneric() to obtain the list of internal or
7711 * user defined variable or function names.
7712 */
7713/*ARGSUSED*/
7714 char_u *
7715get_expr_name(xp, idx)
7716 expand_T *xp;
7717 int idx;
7718{
7719 static int intidx = -1;
7720 char_u *name;
7721
7722 if (idx == 0)
7723 intidx = -1;
7724 if (intidx < 0)
7725 {
7726 name = get_function_name(xp, idx);
7727 if (name != NULL)
7728 return name;
7729 }
7730 return get_user_var_name(xp, ++intidx);
7731}
7732
7733#endif /* FEAT_CMDL_COMPL */
7734
7735/*
7736 * Find internal function in table above.
7737 * Return index, or -1 if not found
7738 */
7739 static int
7740find_internal_func(name)
7741 char_u *name; /* name of the function */
7742{
7743 int first = 0;
7744 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7745 int cmp;
7746 int x;
7747
7748 /*
7749 * Find the function name in the table. Binary search.
7750 */
7751 while (first <= last)
7752 {
7753 x = first + ((unsigned)(last - first) >> 1);
7754 cmp = STRCMP(name, functions[x].f_name);
7755 if (cmp < 0)
7756 last = x - 1;
7757 else if (cmp > 0)
7758 first = x + 1;
7759 else
7760 return x;
7761 }
7762 return -1;
7763}
7764
7765/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7767 * name it contains, otherwise return "name".
7768 */
7769 static char_u *
7770deref_func_name(name, lenp)
7771 char_u *name;
7772 int *lenp;
7773{
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 int cc;
7776
7777 cc = name[*lenp];
7778 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007779 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007780 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007783 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007784 {
7785 *lenp = 0;
7786 return (char_u *)""; /* just in case */
7787 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007788 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007790 }
7791
7792 return name;
7793}
7794
7795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 * Allocate a variable for the result of a function.
7797 * Return OK or FAIL.
7798 */
7799 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7801 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 char_u *name; /* name of the function */
7803 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 char_u **arg; /* argument, pointing to the '(' */
7806 linenr_T firstline; /* first line of range */
7807 linenr_T lastline; /* last line of range */
7808 int *doesrange; /* return: function handled range */
7809 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 char_u *argp;
7813 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007814 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 int argcount = 0; /* number of arguments found */
7816
7817 /*
7818 * Get the arguments.
7819 */
7820 argp = *arg;
7821 while (argcount < MAX_FUNC_ARGS)
7822 {
7823 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7824 if (*argp == ')' || *argp == ',' || *argp == NUL)
7825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7827 {
7828 ret = FAIL;
7829 break;
7830 }
7831 ++argcount;
7832 if (*argp != ',')
7833 break;
7834 }
7835 if (*argp == ')')
7836 ++argp;
7837 else
7838 ret = FAIL;
7839
7840 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 {
7845 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007846 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007848 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850
7851 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
7854 *arg = skipwhite(argp);
7855 return ret;
7856}
7857
7858
7859/*
7860 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007861 * Return OK when the function can't be called, FAIL otherwise.
7862 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 */
7864 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 char_u *name; /* name of the function */
7868 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007869 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007871 typval_T *argvars; /* vars for arguments, must have "argcount"
7872 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 linenr_T firstline; /* first line of range */
7874 linenr_T lastline; /* last line of range */
7875 int *doesrange; /* return: function handled range */
7876 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
7879 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880#define ERROR_UNKNOWN 0
7881#define ERROR_TOOMANY 1
7882#define ERROR_TOOFEW 2
7883#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884#define ERROR_DICT 4
7885#define ERROR_NONE 5
7886#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 int error = ERROR_NONE;
7888 int i;
7889 int llen;
7890 ufunc_T *fp;
7891 int cc;
7892#define FLEN_FIXED 40
7893 char_u fname_buf[FLEN_FIXED + 1];
7894 char_u *fname;
7895
7896 /*
7897 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7898 * Change <SNR>123_name() to K_SNR 123_name().
7899 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7900 */
7901 cc = name[len];
7902 name[len] = NUL;
7903 llen = eval_fname_script(name);
7904 if (llen > 0)
7905 {
7906 fname_buf[0] = K_SPECIAL;
7907 fname_buf[1] = KS_EXTRA;
7908 fname_buf[2] = (int)KE_SNR;
7909 i = 3;
7910 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7911 {
7912 if (current_SID <= 0)
7913 error = ERROR_SCRIPT;
7914 else
7915 {
7916 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7917 i = (int)STRLEN(fname_buf);
7918 }
7919 }
7920 if (i + STRLEN(name + llen) < FLEN_FIXED)
7921 {
7922 STRCPY(fname_buf + i, name + llen);
7923 fname = fname_buf;
7924 }
7925 else
7926 {
7927 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7928 if (fname == NULL)
7929 error = ERROR_OTHER;
7930 else
7931 {
7932 mch_memmove(fname, fname_buf, (size_t)i);
7933 STRCPY(fname + i, name + llen);
7934 }
7935 }
7936 }
7937 else
7938 fname = name;
7939
7940 *doesrange = FALSE;
7941
7942
7943 /* execute the function if no errors detected and executing */
7944 if (evaluate && error == ERROR_NONE)
7945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 error = ERROR_UNKNOWN;
7948
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007949 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {
7951 /*
7952 * User defined function.
7953 */
7954 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007957 /* Trigger FuncUndefined event, may load the function. */
7958 if (fp == NULL
7959 && apply_autocmds(EVENT_FUNCUNDEFINED,
7960 fname, fname, TRUE, NULL)
7961 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007963 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 fp = find_func(fname);
7965 }
7966#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007967 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007968 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007969 {
7970 /* loaded a package, search for the function again */
7971 fp = find_func(fname);
7972 }
7973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (fp != NULL)
7975 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007976 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007978 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007980 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007982 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 else
7985 {
7986 /*
7987 * Call the user function.
7988 * Save and restore search patterns, script variables and
7989 * redo buffer.
7990 */
7991 save_search_patterns();
7992 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007993 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007995 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007996 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7997 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7998 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007999 /* Function was unreferenced while being used, free it
8000 * now. */
8001 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 restoreRedobuff();
8003 restore_search_patterns();
8004 error = ERROR_NONE;
8005 }
8006 }
8007 }
8008 else
8009 {
8010 /*
8011 * Find the function name in the table, call its implementation.
8012 */
8013 i = find_internal_func(fname);
8014 if (i >= 0)
8015 {
8016 if (argcount < functions[i].f_min_argc)
8017 error = ERROR_TOOFEW;
8018 else if (argcount > functions[i].f_max_argc)
8019 error = ERROR_TOOMANY;
8020 else
8021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008022 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 error = ERROR_NONE;
8025 }
8026 }
8027 }
8028 /*
8029 * The function call (or "FuncUndefined" autocommand sequence) might
8030 * have been aborted by an error, an interrupt, or an explicitly thrown
8031 * exception that has not been caught so far. This situation can be
8032 * tested for by calling aborting(). For an error in an internal
8033 * function or for the "E132" error in call_user_func(), however, the
8034 * throw point at which the "force_abort" flag (temporarily reset by
8035 * emsg()) is normally updated has not been reached yet. We need to
8036 * update that flag first to make aborting() reliable.
8037 */
8038 update_force_abort();
8039 }
8040 if (error == ERROR_NONE)
8041 ret = OK;
8042
8043 /*
8044 * Report an error unless the argument evaluation or function call has been
8045 * cancelled due to an aborting error, an interrupt, or an exception.
8046 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008047 if (!aborting())
8048 {
8049 switch (error)
8050 {
8051 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008052 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 break;
8054 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008055 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 break;
8057 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008058 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008059 name);
8060 break;
8061 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008062 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008063 name);
8064 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008065 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008066 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008067 name);
8068 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 }
8070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071
8072 name[len] = cc;
8073 if (fname != name && fname != fname_buf)
8074 vim_free(fname);
8075
8076 return ret;
8077}
8078
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008079/*
8080 * Give an error message with a function name. Handle <SNR> things.
8081 */
8082 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008083emsg_funcname(ermsg, name)
8084 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008085 char_u *name;
8086{
8087 char_u *p;
8088
8089 if (*name == K_SPECIAL)
8090 p = concat_str((char_u *)"<SNR>", name + 3);
8091 else
8092 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008093 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008094 if (p != name)
8095 vim_free(p);
8096}
8097
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008098/*
8099 * Return TRUE for a non-zero Number and a non-empty String.
8100 */
8101 static int
8102non_zero_arg(argvars)
8103 typval_T *argvars;
8104{
8105 return ((argvars[0].v_type == VAR_NUMBER
8106 && argvars[0].vval.v_number != 0)
8107 || (argvars[0].v_type == VAR_STRING
8108 && argvars[0].vval.v_string != NULL
8109 && *argvars[0].vval.v_string != NUL));
8110}
8111
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112/*********************************************
8113 * Implementation of the built-in functions
8114 */
8115
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117/*
8118 * "abs(expr)" function
8119 */
8120 static void
8121f_abs(argvars, rettv)
8122 typval_T *argvars;
8123 typval_T *rettv;
8124{
8125 if (argvars[0].v_type == VAR_FLOAT)
8126 {
8127 rettv->v_type = VAR_FLOAT;
8128 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8129 }
8130 else
8131 {
8132 varnumber_T n;
8133 int error = FALSE;
8134
8135 n = get_tv_number_chk(&argvars[0], &error);
8136 if (error)
8137 rettv->vval.v_number = -1;
8138 else if (n > 0)
8139 rettv->vval.v_number = n;
8140 else
8141 rettv->vval.v_number = -n;
8142 }
8143}
8144#endif
8145
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008147 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 */
8149 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008150f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008151 typval_T *argvars;
8152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
Bram Moolenaar33570922005-01-25 22:26:29 +00008154 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008159 if ((l = argvars[0].vval.v_list) != NULL
8160 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8161 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008163 }
8164 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008165 EMSG(_(e_listreq));
8166}
8167
8168/*
8169 * "append(lnum, string/list)" function
8170 */
8171 static void
8172f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008173 typval_T *argvars;
8174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008175{
8176 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008177 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008178 list_T *l = NULL;
8179 listitem_T *li = NULL;
8180 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008181 long added = 0;
8182
Bram Moolenaar0d660222005-01-07 21:51:51 +00008183 lnum = get_tv_lnum(argvars);
8184 if (lnum >= 0
8185 && lnum <= curbuf->b_ml.ml_line_count
8186 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008187 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008188 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008189 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008190 l = argvars[1].vval.v_list;
8191 if (l == NULL)
8192 return;
8193 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008195 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008196 for (;;)
8197 {
8198 if (l == NULL)
8199 tv = &argvars[1]; /* append a string */
8200 else if (li == NULL)
8201 break; /* end of list */
8202 else
8203 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008204 line = get_tv_string_chk(tv);
8205 if (line == NULL) /* type error */
8206 {
8207 rettv->vval.v_number = 1; /* Failed */
8208 break;
8209 }
8210 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008211 ++added;
8212 if (l == NULL)
8213 break;
8214 li = li->li_next;
8215 }
8216
8217 appended_lines_mark(lnum, added);
8218 if (curwin->w_cursor.lnum > lnum)
8219 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 else
8222 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223}
8224
8225/*
8226 * "argc()" function
8227 */
8228/* ARGSUSED */
8229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008230f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 typval_T *argvars;
8232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235}
8236
8237/*
8238 * "argidx()" function
8239 */
8240/* ARGSUSED */
8241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 typval_T *argvars;
8244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247}
8248
8249/*
8250 * "argv(nr)" function
8251 */
8252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *argvars;
8255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257 int idx;
8258
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008259 if (argvars[0].v_type != VAR_UNKNOWN)
8260 {
8261 idx = get_tv_number_chk(&argvars[0], NULL);
8262 if (idx >= 0 && idx < ARGCOUNT)
8263 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8264 else
8265 rettv->vval.v_string = NULL;
8266 rettv->v_type = VAR_STRING;
8267 }
8268 else if (rettv_list_alloc(rettv) == OK)
8269 for (idx = 0; idx < ARGCOUNT; ++idx)
8270 list_append_string(rettv->vval.v_list,
8271 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272}
8273
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008274#ifdef FEAT_FLOAT
8275static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8276
8277/*
8278 * Get the float value of "argvars[0]" into "f".
8279 * Returns FAIL when the argument is not a Number or Float.
8280 */
8281 static int
8282get_float_arg(argvars, f)
8283 typval_T *argvars;
8284 float_T *f;
8285{
8286 if (argvars[0].v_type == VAR_FLOAT)
8287 {
8288 *f = argvars[0].vval.v_float;
8289 return OK;
8290 }
8291 if (argvars[0].v_type == VAR_NUMBER)
8292 {
8293 *f = (float_T)argvars[0].vval.v_number;
8294 return OK;
8295 }
8296 EMSG(_("E808: Number or Float required"));
8297 return FAIL;
8298}
8299
8300/*
8301 * "atan()" function
8302 */
8303 static void
8304f_atan(argvars, rettv)
8305 typval_T *argvars;
8306 typval_T *rettv;
8307{
8308 float_T f;
8309
8310 rettv->v_type = VAR_FLOAT;
8311 if (get_float_arg(argvars, &f) == OK)
8312 rettv->vval.v_float = atan(f);
8313 else
8314 rettv->vval.v_float = 0.0;
8315}
8316#endif
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318/*
8319 * "browse(save, title, initdir, default)" function
8320 */
8321/* ARGSUSED */
8322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008323f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 typval_T *argvars;
8325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326{
8327#ifdef FEAT_BROWSE
8328 int save;
8329 char_u *title;
8330 char_u *initdir;
8331 char_u *defname;
8332 char_u buf[NUMBUFLEN];
8333 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008334 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008336 save = get_tv_number_chk(&argvars[0], &error);
8337 title = get_tv_string_chk(&argvars[1]);
8338 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8339 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008341 if (error || title == NULL || initdir == NULL || defname == NULL)
8342 rettv->vval.v_string = NULL;
8343 else
8344 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008345 do_browse(save ? BROWSE_SAVE : 0,
8346 title, defname, NULL, initdir, NULL, curbuf);
8347#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008349#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008351}
8352
8353/*
8354 * "browsedir(title, initdir)" function
8355 */
8356/* ARGSUSED */
8357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 typval_T *argvars;
8360 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008361{
8362#ifdef FEAT_BROWSE
8363 char_u *title;
8364 char_u *initdir;
8365 char_u buf[NUMBUFLEN];
8366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008367 title = get_tv_string_chk(&argvars[0]);
8368 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008370 if (title == NULL || initdir == NULL)
8371 rettv->vval.v_string = NULL;
8372 else
8373 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008374 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008378 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379}
8380
Bram Moolenaar33570922005-01-25 22:26:29 +00008381static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008382
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383/*
8384 * Find a buffer by number or exact name.
8385 */
8386 static buf_T *
8387find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008388 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389{
8390 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008392 if (avar->v_type == VAR_NUMBER)
8393 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008394 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008396 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008397 if (buf == NULL)
8398 {
8399 /* No full path name match, try a match with a URL or a "nofile"
8400 * buffer, these don't use the full path. */
8401 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8402 if (buf->b_fname != NULL
8403 && (path_with_url(buf->b_fname)
8404#ifdef FEAT_QUICKFIX
8405 || bt_nofile(buf)
8406#endif
8407 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008408 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008409 break;
8410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
8412 return buf;
8413}
8414
8415/*
8416 * "bufexists(expr)" function
8417 */
8418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008419f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 typval_T *argvars;
8421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424}
8425
8426/*
8427 * "buflisted(expr)" function
8428 */
8429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008430f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 typval_T *argvars;
8432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433{
8434 buf_T *buf;
8435
8436 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008437 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438}
8439
8440/*
8441 * "bufloaded(expr)" function
8442 */
8443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008444f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008445 typval_T *argvars;
8446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 buf_T *buf;
8449
8450 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452}
8453
Bram Moolenaar33570922005-01-25 22:26:29 +00008454static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456/*
8457 * Get buffer by number or pattern.
8458 */
8459 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008460get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008461 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 int save_magic;
8465 char_u *save_cpo;
8466 buf_T *buf;
8467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 if (tv->v_type == VAR_NUMBER)
8469 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008470 if (tv->v_type != VAR_STRING)
8471 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 if (name == NULL || *name == NUL)
8473 return curbuf;
8474 if (name[0] == '$' && name[1] == NUL)
8475 return lastbuf;
8476
8477 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8478 save_magic = p_magic;
8479 p_magic = TRUE;
8480 save_cpo = p_cpo;
8481 p_cpo = (char_u *)"";
8482
8483 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8484 TRUE, FALSE));
8485
8486 p_magic = save_magic;
8487 p_cpo = save_cpo;
8488
8489 /* If not found, try expanding the name, like done for bufexists(). */
8490 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
8493 return buf;
8494}
8495
8496/*
8497 * "bufname(expr)" function
8498 */
8499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008500f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 typval_T *argvars;
8502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
8504 buf_T *buf;
8505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008506 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 buf = get_buf_tv(&argvars[0]);
8509 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 --emsg_off;
8515}
8516
8517/*
8518 * "bufnr(expr)" function
8519 */
8520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008522 typval_T *argvars;
8523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008526 int error = FALSE;
8527 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008529 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008532 --emsg_off;
8533
8534 /* If the buffer isn't found and the second argument is not zero create a
8535 * new buffer. */
8536 if (buf == NULL
8537 && argvars[1].v_type != VAR_UNKNOWN
8538 && get_tv_number_chk(&argvars[1], &error) != 0
8539 && !error
8540 && (name = get_tv_string_chk(&argvars[0])) != NULL
8541 && !error)
8542 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8543
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008547 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
8549
8550/*
8551 * "bufwinnr(nr)" function
8552 */
8553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008554f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008555 typval_T *argvars;
8556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558#ifdef FEAT_WINDOWS
8559 win_T *wp;
8560 int winnr = 0;
8561#endif
8562 buf_T *buf;
8563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008564 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008566 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567#ifdef FEAT_WINDOWS
8568 for (wp = firstwin; wp; wp = wp->w_next)
8569 {
8570 ++winnr;
8571 if (wp->w_buffer == buf)
8572 break;
8573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008574 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577#endif
8578 --emsg_off;
8579}
8580
8581/*
8582 * "byte2line(byte)" function
8583 */
8584/*ARGSUSED*/
8585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008587 typval_T *argvars;
8588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592#else
8593 long boff = 0;
8594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008595 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 (linenr_T)0, &boff);
8601#endif
8602}
8603
8604/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008605 * "byteidx()" function
8606 */
8607/*ARGSUSED*/
8608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 typval_T *argvars;
8611 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008612{
8613#ifdef FEAT_MBYTE
8614 char_u *t;
8615#endif
8616 char_u *str;
8617 long idx;
8618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619 str = get_tv_string_chk(&argvars[0]);
8620 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008622 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008623 return;
8624
8625#ifdef FEAT_MBYTE
8626 t = str;
8627 for ( ; idx > 0; idx--)
8628 {
8629 if (*t == NUL) /* EOL reached */
8630 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008631 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008632 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008633 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008634#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008635 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008636 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008637#endif
8638}
8639
8640/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008641 * "call(func, arglist)" function
8642 */
8643 static void
8644f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 typval_T *argvars;
8646 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008647{
8648 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008649 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008650 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008652 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008654
8655 rettv->vval.v_number = 0;
8656 if (argvars[1].v_type != VAR_LIST)
8657 {
8658 EMSG(_(e_listreq));
8659 return;
8660 }
8661 if (argvars[1].vval.v_list == NULL)
8662 return;
8663
8664 if (argvars[0].v_type == VAR_FUNC)
8665 func = argvars[0].vval.v_string;
8666 else
8667 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008668 if (*func == NUL)
8669 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008670
Bram Moolenaare9a41262005-01-15 22:18:47 +00008671 if (argvars[2].v_type != VAR_UNKNOWN)
8672 {
8673 if (argvars[2].v_type != VAR_DICT)
8674 {
8675 EMSG(_(e_dictreq));
8676 return;
8677 }
8678 selfdict = argvars[2].vval.v_dict;
8679 }
8680
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008681 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8682 item = item->li_next)
8683 {
8684 if (argc == MAX_FUNC_ARGS)
8685 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008686 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008687 break;
8688 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008689 /* Make a copy of each argument. This is needed to be able to set
8690 * v_lock to VAR_FIXED in the copy without changing the original list.
8691 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008692 copy_tv(&item->li_tv, &argv[argc++]);
8693 }
8694
8695 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008696 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8698 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008699
8700 /* Free the arguments. */
8701 while (argc > 0)
8702 clear_tv(&argv[--argc]);
8703}
8704
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008705#ifdef FEAT_FLOAT
8706/*
8707 * "ceil({float})" function
8708 */
8709 static void
8710f_ceil(argvars, rettv)
8711 typval_T *argvars;
8712 typval_T *rettv;
8713{
8714 float_T f;
8715
8716 rettv->v_type = VAR_FLOAT;
8717 if (get_float_arg(argvars, &f) == OK)
8718 rettv->vval.v_float = ceil(f);
8719 else
8720 rettv->vval.v_float = 0.0;
8721}
8722#endif
8723
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008724/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008725 * "changenr()" function
8726 */
8727/*ARGSUSED*/
8728 static void
8729f_changenr(argvars, rettv)
8730 typval_T *argvars;
8731 typval_T *rettv;
8732{
8733 rettv->vval.v_number = curbuf->b_u_seq_cur;
8734}
8735
8736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 * "char2nr(string)" function
8738 */
8739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008741 typval_T *argvars;
8742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743{
8744#ifdef FEAT_MBYTE
8745 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008746 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 else
8748#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750}
8751
8752/*
8753 * "cindent(lnum)" function
8754 */
8755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 typval_T *argvars;
8758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760#ifdef FEAT_CINDENT
8761 pos_T pos;
8762 linenr_T lnum;
8763
8764 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8767 {
8768 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 curwin->w_cursor = pos;
8771 }
8772 else
8773#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
8777/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008778 * "clearmatches()" function
8779 */
8780/*ARGSUSED*/
8781 static void
8782f_clearmatches(argvars, rettv)
8783 typval_T *argvars;
8784 typval_T *rettv;
8785{
8786#ifdef FEAT_SEARCH_EXTRA
8787 clear_matches(curwin);
8788#endif
8789}
8790
8791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 * "col(string)" function
8793 */
8794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T *argvars;
8797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
8799 colnr_T col = 0;
8800 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008801 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008803 fp = var2fpos(&argvars[0], FALSE, &fnum);
8804 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 {
8806 if (fp->col == MAXCOL)
8807 {
8808 /* '> can be MAXCOL, get the length of the line then */
8809 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008810 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 else
8812 col = MAXCOL;
8813 }
8814 else
8815 {
8816 col = fp->col + 1;
8817#ifdef FEAT_VIRTUALEDIT
8818 /* col(".") when the cursor is on the NUL at the end of the line
8819 * because of "coladd" can be seen as an extra column. */
8820 if (virtual_active() && fp == &curwin->w_cursor)
8821 {
8822 char_u *p = ml_get_cursor();
8823
8824 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8825 curwin->w_virtcol - curwin->w_cursor.coladd))
8826 {
8827# ifdef FEAT_MBYTE
8828 int l;
8829
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008830 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 col += l;
8832# else
8833 if (*p != NUL && p[1] == NUL)
8834 ++col;
8835# endif
8836 }
8837 }
8838#endif
8839 }
8840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843
Bram Moolenaar572cb562005-08-05 21:35:02 +00008844#if defined(FEAT_INS_EXPAND)
8845/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008846 * "complete()" function
8847 */
8848/*ARGSUSED*/
8849 static void
8850f_complete(argvars, rettv)
8851 typval_T *argvars;
8852 typval_T *rettv;
8853{
8854 int startcol;
8855
8856 if ((State & INSERT) == 0)
8857 {
8858 EMSG(_("E785: complete() can only be used in Insert mode"));
8859 return;
8860 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008861
8862 /* Check for undo allowed here, because if something was already inserted
8863 * the line was already saved for undo and this check isn't done. */
8864 if (!undo_allowed())
8865 return;
8866
Bram Moolenaarade00832006-03-10 21:46:58 +00008867 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8868 {
8869 EMSG(_(e_invarg));
8870 return;
8871 }
8872
8873 startcol = get_tv_number_chk(&argvars[0], NULL);
8874 if (startcol <= 0)
8875 return;
8876
8877 set_completion(startcol - 1, argvars[1].vval.v_list);
8878}
8879
8880/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008881 * "complete_add()" function
8882 */
8883/*ARGSUSED*/
8884 static void
8885f_complete_add(argvars, rettv)
8886 typval_T *argvars;
8887 typval_T *rettv;
8888{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008889 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008890}
8891
8892/*
8893 * "complete_check()" function
8894 */
8895/*ARGSUSED*/
8896 static void
8897f_complete_check(argvars, rettv)
8898 typval_T *argvars;
8899 typval_T *rettv;
8900{
8901 int saved = RedrawingDisabled;
8902
8903 RedrawingDisabled = 0;
8904 ins_compl_check_keys(0);
8905 rettv->vval.v_number = compl_interrupted;
8906 RedrawingDisabled = saved;
8907}
8908#endif
8909
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910/*
8911 * "confirm(message, buttons[, default [, type]])" function
8912 */
8913/*ARGSUSED*/
8914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 typval_T *argvars;
8917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8920 char_u *message;
8921 char_u *buttons = NULL;
8922 char_u buf[NUMBUFLEN];
8923 char_u buf2[NUMBUFLEN];
8924 int def = 1;
8925 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008926 char_u *typestr;
8927 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008929 message = get_tv_string_chk(&argvars[0]);
8930 if (message == NULL)
8931 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008932 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8935 if (buttons == NULL)
8936 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008937 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008940 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8943 if (typestr == NULL)
8944 error = TRUE;
8945 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 switch (TOUPPER_ASC(*typestr))
8948 {
8949 case 'E': type = VIM_ERROR; break;
8950 case 'Q': type = VIM_QUESTION; break;
8951 case 'I': type = VIM_INFO; break;
8952 case 'W': type = VIM_WARNING; break;
8953 case 'G': type = VIM_GENERIC; break;
8954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 }
8956 }
8957 }
8958 }
8959
8960 if (buttons == NULL || *buttons == NUL)
8961 buttons = (char_u *)_("&Ok");
8962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 if (error)
8964 rettv->vval.v_number = 0;
8965 else
8966 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 def, NULL);
8968#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970#endif
8971}
8972
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008973/*
8974 * "copy()" function
8975 */
8976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008980{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008981 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008984#ifdef FEAT_FLOAT
8985/*
8986 * "cos()" function
8987 */
8988 static void
8989f_cos(argvars, rettv)
8990 typval_T *argvars;
8991 typval_T *rettv;
8992{
8993 float_T f;
8994
8995 rettv->v_type = VAR_FLOAT;
8996 if (get_float_arg(argvars, &f) == OK)
8997 rettv->vval.v_float = cos(f);
8998 else
8999 rettv->vval.v_float = 0.0;
9000}
9001#endif
9002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009004 * "count()" function
9005 */
9006 static void
9007f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 typval_T *argvars;
9009 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009011 long n = 0;
9012 int ic = FALSE;
9013
Bram Moolenaare9a41262005-01-15 22:18:47 +00009014 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009015 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009016 listitem_T *li;
9017 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009019
Bram Moolenaare9a41262005-01-15 22:18:47 +00009020 if ((l = argvars[0].vval.v_list) != NULL)
9021 {
9022 li = l->lv_first;
9023 if (argvars[2].v_type != VAR_UNKNOWN)
9024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 int error = FALSE;
9026
9027 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009028 if (argvars[3].v_type != VAR_UNKNOWN)
9029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009030 idx = get_tv_number_chk(&argvars[3], &error);
9031 if (!error)
9032 {
9033 li = list_find(l, idx);
9034 if (li == NULL)
9035 EMSGN(_(e_listidx), idx);
9036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 if (error)
9039 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009040 }
9041
9042 for ( ; li != NULL; li = li->li_next)
9043 if (tv_equal(&li->li_tv, &argvars[1], ic))
9044 ++n;
9045 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009046 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 else if (argvars[0].v_type == VAR_DICT)
9048 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 int todo;
9050 dict_T *d;
9051 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009053 if ((d = argvars[0].vval.v_dict) != NULL)
9054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 int error = FALSE;
9056
Bram Moolenaare9a41262005-01-15 22:18:47 +00009057 if (argvars[2].v_type != VAR_UNKNOWN)
9058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009060 if (argvars[3].v_type != VAR_UNKNOWN)
9061 EMSG(_(e_invarg));
9062 }
9063
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009064 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009066 {
9067 if (!HASHITEM_EMPTY(hi))
9068 {
9069 --todo;
9070 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9071 ++n;
9072 }
9073 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009074 }
9075 }
9076 else
9077 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009078 rettv->vval.v_number = n;
9079}
9080
9081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9083 *
9084 * Checks the existence of a cscope connection.
9085 */
9086/*ARGSUSED*/
9087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 typval_T *argvars;
9090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091{
9092#ifdef FEAT_CSCOPE
9093 int num = 0;
9094 char_u *dbpath = NULL;
9095 char_u *prepend = NULL;
9096 char_u buf[NUMBUFLEN];
9097
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009098 if (argvars[0].v_type != VAR_UNKNOWN
9099 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 num = (int)get_tv_number(&argvars[0]);
9102 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009103 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 }
9106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110#endif
9111}
9112
9113/*
9114 * "cursor(lnum, col)" function
9115 *
9116 * Moves the cursor to the specified line and column
9117 */
9118/*ARGSUSED*/
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009125#ifdef FEAT_VIRTUALEDIT
9126 long coladd = 0;
9127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128
Bram Moolenaara5525202006-03-02 22:52:09 +00009129 if (argvars[1].v_type == VAR_UNKNOWN)
9130 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009131 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009132
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009133 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009134 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009135 line = pos.lnum;
9136 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009137#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009138 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009139#endif
9140 }
9141 else
9142 {
9143 line = get_tv_lnum(argvars);
9144 col = get_tv_number_chk(&argvars[1], NULL);
9145#ifdef FEAT_VIRTUALEDIT
9146 if (argvars[2].v_type != VAR_UNKNOWN)
9147 coladd = get_tv_number_chk(&argvars[2], NULL);
9148#endif
9149 }
9150 if (line < 0 || col < 0
9151#ifdef FEAT_VIRTUALEDIT
9152 || coladd < 0
9153#endif
9154 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009155 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 if (line > 0)
9157 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 if (col > 0)
9159 curwin->w_cursor.col = col - 1;
9160#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009161 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162#endif
9163
9164 /* Make sure the cursor is in a valid position. */
9165 check_cursor();
9166#ifdef FEAT_MBYTE
9167 /* Correct cursor for multi-byte character. */
9168 if (has_mbyte)
9169 mb_adjust_cursor();
9170#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009171
9172 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173}
9174
9175/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009176 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 */
9178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *argvars;
9181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009183 int noref = 0;
9184
9185 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009187 if (noref < 0 || noref > 1)
9188 EMSG(_(e_invarg));
9189 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009190 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191}
9192
9193/*
9194 * "delete()" function
9195 */
9196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009198 typval_T *argvars;
9199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205}
9206
9207/*
9208 * "did_filetype()" function
9209 */
9210/*ARGSUSED*/
9211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009213 typval_T *argvars;
9214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
9216#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220#endif
9221}
9222
9223/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009224 * "diff_filler()" function
9225 */
9226/*ARGSUSED*/
9227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009228f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009229 typval_T *argvars;
9230 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009231{
9232#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009234#endif
9235}
9236
9237/*
9238 * "diff_hlID()" function
9239 */
9240/*ARGSUSED*/
9241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009243 typval_T *argvars;
9244 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009245{
9246#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009247 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009248 static linenr_T prev_lnum = 0;
9249 static int changedtick = 0;
9250 static int fnum = 0;
9251 static int change_start = 0;
9252 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009253 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009254 int filler_lines;
9255 int col;
9256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 if (lnum < 0) /* ignore type error in {lnum} arg */
9258 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009259 if (lnum != prev_lnum
9260 || changedtick != curbuf->b_changedtick
9261 || fnum != curbuf->b_fnum)
9262 {
9263 /* New line, buffer, change: need to get the values. */
9264 filler_lines = diff_check(curwin, lnum);
9265 if (filler_lines < 0)
9266 {
9267 if (filler_lines == -1)
9268 {
9269 change_start = MAXCOL;
9270 change_end = -1;
9271 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9272 hlID = HLF_ADD; /* added line */
9273 else
9274 hlID = HLF_CHD; /* changed line */
9275 }
9276 else
9277 hlID = HLF_ADD; /* added line */
9278 }
9279 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009280 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009281 prev_lnum = lnum;
9282 changedtick = curbuf->b_changedtick;
9283 fnum = curbuf->b_fnum;
9284 }
9285
9286 if (hlID == HLF_CHD || hlID == HLF_TXD)
9287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009289 if (col >= change_start && col <= change_end)
9290 hlID = HLF_TXD; /* changed text */
9291 else
9292 hlID = HLF_CHD; /* changed line */
9293 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009294 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009295#endif
9296}
9297
9298/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009299 * "empty({expr})" function
9300 */
9301 static void
9302f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009303 typval_T *argvars;
9304 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009305{
9306 int n;
9307
9308 switch (argvars[0].v_type)
9309 {
9310 case VAR_STRING:
9311 case VAR_FUNC:
9312 n = argvars[0].vval.v_string == NULL
9313 || *argvars[0].vval.v_string == NUL;
9314 break;
9315 case VAR_NUMBER:
9316 n = argvars[0].vval.v_number == 0;
9317 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009318#ifdef FEAT_FLOAT
9319 case VAR_FLOAT:
9320 n = argvars[0].vval.v_float == 0.0;
9321 break;
9322#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009323 case VAR_LIST:
9324 n = argvars[0].vval.v_list == NULL
9325 || argvars[0].vval.v_list->lv_first == NULL;
9326 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009327 case VAR_DICT:
9328 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009330 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009331 default:
9332 EMSG2(_(e_intern2), "f_empty()");
9333 n = 0;
9334 }
9335
9336 rettv->vval.v_number = n;
9337}
9338
9339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 * "escape({string}, {chars})" function
9341 */
9342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009344 typval_T *argvars;
9345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346{
9347 char_u buf[NUMBUFLEN];
9348
Bram Moolenaar758711c2005-02-02 23:11:38 +00009349 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9350 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352}
9353
9354/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009355 * "eval()" function
9356 */
9357/*ARGSUSED*/
9358 static void
9359f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009360 typval_T *argvars;
9361 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009362{
9363 char_u *s;
9364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 s = get_tv_string_chk(&argvars[0]);
9366 if (s != NULL)
9367 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9370 {
9371 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009372 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009374 else if (*s != NUL)
9375 EMSG(_(e_trailing));
9376}
9377
9378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 * "eventhandler()" function
9380 */
9381/*ARGSUSED*/
9382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T *argvars;
9385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388}
9389
9390/*
9391 * "executable()" function
9392 */
9393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399}
9400
9401/*
9402 * "exists()" function
9403 */
9404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009405f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 typval_T *argvars;
9407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408{
9409 char_u *p;
9410 char_u *name;
9411 int n = FALSE;
9412 int len = 0;
9413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 if (*p == '$') /* environment variable */
9416 {
9417 /* first try "normal" environment variables (fast) */
9418 if (mch_getenv(p + 1) != NULL)
9419 n = TRUE;
9420 else
9421 {
9422 /* try expanding things like $VIM and ${HOME} */
9423 p = expand_env_save(p);
9424 if (p != NULL && *p != '$')
9425 n = TRUE;
9426 vim_free(p);
9427 }
9428 }
9429 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009432 if (*skipwhite(p) != NUL)
9433 n = FALSE; /* trailing garbage */
9434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 else if (*p == '*') /* internal or user defined function */
9436 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009437 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 }
9439 else if (*p == ':')
9440 {
9441 n = cmd_exists(p + 1);
9442 }
9443 else if (*p == '#')
9444 {
9445#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009446 if (p[1] == '#')
9447 n = autocmd_supported(p + 2);
9448 else
9449 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#endif
9451 }
9452 else /* internal variable */
9453 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009454 char_u *tofree;
9455 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009457 /* get_name_len() takes care of expanding curly braces */
9458 name = p;
9459 len = get_name_len(&p, &tofree, TRUE, FALSE);
9460 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009462 if (tofree != NULL)
9463 name = tofree;
9464 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9465 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009467 /* handle d.key, l[idx], f(expr) */
9468 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9469 if (n)
9470 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 }
9472 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009473 if (*p != NUL)
9474 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009476 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 }
9478
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480}
9481
9482/*
9483 * "expand()" function
9484 */
9485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009487 typval_T *argvars;
9488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
9490 char_u *s;
9491 int len;
9492 char_u *errormsg;
9493 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9494 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009495 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->v_type = VAR_STRING;
9498 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (*s == '%' || *s == '#' || *s == '<')
9500 {
9501 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009502 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 --emsg_off;
9504 }
9505 else
9506 {
9507 /* When the optional second argument is non-zero, don't remove matches
9508 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509 if (argvars[1].v_type != VAR_UNKNOWN
9510 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 if (!error)
9513 {
9514 ExpandInit(&xpc);
9515 xpc.xp_context = EXPAND_FILES;
9516 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 }
9518 else
9519 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 }
9521}
9522
9523/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009524 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009525 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009526 */
9527 static void
9528f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009531{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009532 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009533 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009535 list_T *l1, *l2;
9536 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009537 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009539
Bram Moolenaare9a41262005-01-15 22:18:47 +00009540 l1 = argvars[0].vval.v_list;
9541 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009542 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9543 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009544 {
9545 if (argvars[2].v_type != VAR_UNKNOWN)
9546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 before = get_tv_number_chk(&argvars[2], &error);
9548 if (error)
9549 return; /* type error; errmsg already given */
9550
Bram Moolenaar758711c2005-02-02 23:11:38 +00009551 if (before == l1->lv_len)
9552 item = NULL;
9553 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009554 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009555 item = list_find(l1, before);
9556 if (item == NULL)
9557 {
9558 EMSGN(_(e_listidx), before);
9559 return;
9560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009561 }
9562 }
9563 else
9564 item = NULL;
9565 list_extend(l1, l2, item);
9566
Bram Moolenaare9a41262005-01-15 22:18:47 +00009567 copy_tv(&argvars[0], rettv);
9568 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009569 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009570 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009572 dict_T *d1, *d2;
9573 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009574 char_u *action;
9575 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009576 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009577 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009578
9579 d1 = argvars[0].vval.v_dict;
9580 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009581 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9582 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009583 {
9584 /* Check the third argument. */
9585 if (argvars[2].v_type != VAR_UNKNOWN)
9586 {
9587 static char *(av[]) = {"keep", "force", "error"};
9588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 action = get_tv_string_chk(&argvars[2]);
9590 if (action == NULL)
9591 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 for (i = 0; i < 3; ++i)
9593 if (STRCMP(action, av[i]) == 0)
9594 break;
9595 if (i == 3)
9596 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009597 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009598 return;
9599 }
9600 }
9601 else
9602 action = (char_u *)"force";
9603
9604 /* Go over all entries in the second dict and add them to the
9605 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009606 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009607 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009609 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009611 --todo;
9612 di1 = dict_find(d1, hi2->hi_key, -1);
9613 if (di1 == NULL)
9614 {
9615 di1 = dictitem_copy(HI2DI(hi2));
9616 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9617 dictitem_free(di1);
9618 }
9619 else if (*action == 'e')
9620 {
9621 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9622 break;
9623 }
9624 else if (*action == 'f')
9625 {
9626 clear_tv(&di1->di_tv);
9627 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 }
9630 }
9631
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 copy_tv(&argvars[0], rettv);
9633 }
9634 }
9635 else
9636 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009637}
9638
9639/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009640 * "feedkeys()" function
9641 */
9642/*ARGSUSED*/
9643 static void
9644f_feedkeys(argvars, rettv)
9645 typval_T *argvars;
9646 typval_T *rettv;
9647{
9648 int remap = TRUE;
9649 char_u *keys, *flags;
9650 char_u nbuf[NUMBUFLEN];
9651 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009652 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009653
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009654 /* This is not allowed in the sandbox. If the commands would still be
9655 * executed in the sandbox it would be OK, but it probably happens later,
9656 * when "sandbox" is no longer set. */
9657 if (check_secure())
9658 return;
9659
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009660 rettv->vval.v_number = 0;
9661 keys = get_tv_string(&argvars[0]);
9662 if (*keys != NUL)
9663 {
9664 if (argvars[1].v_type != VAR_UNKNOWN)
9665 {
9666 flags = get_tv_string_buf(&argvars[1], nbuf);
9667 for ( ; *flags != NUL; ++flags)
9668 {
9669 switch (*flags)
9670 {
9671 case 'n': remap = FALSE; break;
9672 case 'm': remap = TRUE; break;
9673 case 't': typed = TRUE; break;
9674 }
9675 }
9676 }
9677
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009678 /* Need to escape K_SPECIAL and CSI before putting the string in the
9679 * typeahead buffer. */
9680 keys_esc = vim_strsave_escape_csi(keys);
9681 if (keys_esc != NULL)
9682 {
9683 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009684 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009685 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009686 if (vgetc_busy)
9687 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009688 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009689 }
9690}
9691
9692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 * "filereadable()" function
9694 */
9695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009697 typval_T *argvars;
9698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009700 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 char_u *p;
9702 int n;
9703
Bram Moolenaarc236c162008-07-13 17:41:49 +00009704#ifndef O_NONBLOCK
9705# define O_NONBLOCK 0
9706#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009708 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9709 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 {
9711 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009712 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 }
9714 else
9715 n = FALSE;
9716
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009721 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 * rights to write into.
9723 */
9724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 typval_T *argvars;
9727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009729 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730}
9731
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009732static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009733
9734 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009735findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009736 typval_T *argvars;
9737 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009738 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009739{
9740#ifdef FEAT_SEARCHPATH
9741 char_u *fname;
9742 char_u *fresult = NULL;
9743 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9744 char_u *p;
9745 char_u pathbuf[NUMBUFLEN];
9746 int count = 1;
9747 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009748 int error = FALSE;
9749#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009750
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009751 rettv->vval.v_string = NULL;
9752 rettv->v_type = VAR_STRING;
9753
9754#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009757 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9760 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009761 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009762 else
9763 {
9764 if (*p != NUL)
9765 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009768 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009770 }
9771
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009772 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9773 error = TRUE;
9774
9775 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 do
9778 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009779 if (rettv->v_type == VAR_STRING)
9780 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 fresult = find_file_in_path_option(first ? fname : NULL,
9782 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009783 0, first, path,
9784 find_what,
9785 curbuf->b_ffname,
9786 find_what == FINDFILE_DIR
9787 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009788 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009789
9790 if (fresult != NULL && rettv->v_type == VAR_LIST)
9791 list_append_string(rettv->vval.v_list, fresult, -1);
9792
9793 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009795
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009796 if (rettv->v_type == VAR_STRING)
9797 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009798#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009799}
9800
Bram Moolenaar33570922005-01-25 22:26:29 +00009801static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9802static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009803
9804/*
9805 * Implementation of map() and filter().
9806 */
9807 static void
9808filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 typval_T *argvars;
9810 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009811 int map;
9812{
9813 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009814 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009815 listitem_T *li, *nli;
9816 list_T *l = NULL;
9817 dictitem_T *di;
9818 hashtab_T *ht;
9819 hashitem_T *hi;
9820 dict_T *d = NULL;
9821 typval_T save_val;
9822 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009823 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009824 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009825 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009826 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009827
9828 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009829 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009830 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009831 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009832 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009833 return;
9834 }
9835 else if (argvars[0].v_type == VAR_DICT)
9836 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009837 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009838 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009839 return;
9840 }
9841 else
9842 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009843 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009844 return;
9845 }
9846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009847 expr = get_tv_string_buf_chk(&argvars[1], buf);
9848 /* On type errors, the preceding call has already displayed an error
9849 * message. Avoid a misleading error message for an empty string that
9850 * was not passed as argument. */
9851 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 prepare_vimvar(VV_VAL, &save_val);
9854 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009855
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009856 /* We reset "did_emsg" to be able to detect whether an error
9857 * occurred during evaluation of the expression. */
9858 save_did_emsg = did_emsg;
9859 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 prepare_vimvar(VV_KEY, &save_key);
9864 vimvars[VV_KEY].vv_type = VAR_STRING;
9865
9866 ht = &d->dv_hashtab;
9867 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009868 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 if (!HASHITEM_EMPTY(hi))
9872 {
9873 --todo;
9874 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009875 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 break;
9877 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009878 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009879 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009880 break;
9881 if (!map && rem)
9882 dictitem_remove(d, di);
9883 clear_tv(&vimvars[VV_KEY].vv_tv);
9884 }
9885 }
9886 hash_unlock(ht);
9887
9888 restore_vimvar(VV_KEY, &save_key);
9889 }
9890 else
9891 {
9892 for (li = l->lv_first; li != NULL; li = nli)
9893 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009894 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009895 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009896 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009897 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009898 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009899 break;
9900 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009902 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009903 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009905 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009906
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009907 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009908 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909
9910 copy_tv(&argvars[0], rettv);
9911}
9912
9913 static int
9914filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916 char_u *expr;
9917 int map;
9918 int *remp;
9919{
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009922 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923
Bram Moolenaar33570922005-01-25 22:26:29 +00009924 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925 s = expr;
9926 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009927 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 if (*s != NUL) /* check for trailing chars after expr */
9929 {
9930 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009931 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009932 }
9933 if (map)
9934 {
9935 /* map(): replace the list item value */
9936 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009937 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 *tv = rettv;
9939 }
9940 else
9941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009942 int error = FALSE;
9943
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009946 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 /* On type error, nothing has been removed; return FAIL to stop the
9948 * loop. The error message was given by get_tv_number_chk(). */
9949 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009950 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009951 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009952 retval = OK;
9953theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009954 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009955 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009956}
9957
9958/*
9959 * "filter()" function
9960 */
9961 static void
9962f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 typval_T *argvars;
9964 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009965{
9966 filter_map(argvars, rettv, FALSE);
9967}
9968
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009969/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009970 * "finddir({fname}[, {path}[, {count}]])" function
9971 */
9972 static void
9973f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 typval_T *argvars;
9975 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009976{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009977 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009978}
9979
9980/*
9981 * "findfile({fname}[, {path}[, {count}]])" function
9982 */
9983 static void
9984f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 typval_T *argvars;
9986 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009987{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009988 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009989}
9990
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009991#ifdef FEAT_FLOAT
9992/*
9993 * "float2nr({float})" function
9994 */
9995 static void
9996f_float2nr(argvars, rettv)
9997 typval_T *argvars;
9998 typval_T *rettv;
9999{
10000 float_T f;
10001
10002 if (get_float_arg(argvars, &f) == OK)
10003 {
10004 if (f < -0x7fffffff)
10005 rettv->vval.v_number = -0x7fffffff;
10006 else if (f > 0x7fffffff)
10007 rettv->vval.v_number = 0x7fffffff;
10008 else
10009 rettv->vval.v_number = (varnumber_T)f;
10010 }
10011 else
10012 rettv->vval.v_number = 0;
10013}
10014
10015/*
10016 * "floor({float})" function
10017 */
10018 static void
10019f_floor(argvars, rettv)
10020 typval_T *argvars;
10021 typval_T *rettv;
10022{
10023 float_T f;
10024
10025 rettv->v_type = VAR_FLOAT;
10026 if (get_float_arg(argvars, &f) == OK)
10027 rettv->vval.v_float = floor(f);
10028 else
10029 rettv->vval.v_float = 0.0;
10030}
10031#endif
10032
Bram Moolenaar0d660222005-01-07 21:51:51 +000010033/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010034 * "fnameescape({string})" function
10035 */
10036 static void
10037f_fnameescape(argvars, rettv)
10038 typval_T *argvars;
10039 typval_T *rettv;
10040{
10041 rettv->vval.v_string = vim_strsave_fnameescape(
10042 get_tv_string(&argvars[0]), FALSE);
10043 rettv->v_type = VAR_STRING;
10044}
10045
10046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 * "fnamemodify({fname}, {mods})" function
10048 */
10049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010051 typval_T *argvars;
10052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 char_u *fname;
10055 char_u *mods;
10056 int usedlen = 0;
10057 int len;
10058 char_u *fbuf = NULL;
10059 char_u buf[NUMBUFLEN];
10060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010061 fname = get_tv_string_chk(&argvars[0]);
10062 mods = get_tv_string_buf_chk(&argvars[1], buf);
10063 if (fname == NULL || mods == NULL)
10064 fname = NULL;
10065 else
10066 {
10067 len = (int)STRLEN(fname);
10068 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 vim_free(fbuf);
10077}
10078
Bram Moolenaar33570922005-01-25 22:26:29 +000010079static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080
10081/*
10082 * "foldclosed()" function
10083 */
10084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010086 typval_T *argvars;
10087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 int end;
10089{
10090#ifdef FEAT_FOLDING
10091 linenr_T lnum;
10092 linenr_T first, last;
10093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10096 {
10097 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10098 {
10099 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 return;
10104 }
10105 }
10106#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108}
10109
10110/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010111 * "foldclosed()" function
10112 */
10113 static void
10114f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010115 typval_T *argvars;
10116 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010117{
10118 foldclosed_both(argvars, rettv, FALSE);
10119}
10120
10121/*
10122 * "foldclosedend()" function
10123 */
10124 static void
10125f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010126 typval_T *argvars;
10127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010128{
10129 foldclosed_both(argvars, rettv, TRUE);
10130}
10131
10132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 * "foldlevel()" function
10134 */
10135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010137 typval_T *argvars;
10138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140#ifdef FEAT_FOLDING
10141 linenr_T lnum;
10142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 else
10147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149}
10150
10151/*
10152 * "foldtext()" function
10153 */
10154/*ARGSUSED*/
10155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 typval_T *argvars;
10158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159{
10160#ifdef FEAT_FOLDING
10161 linenr_T lnum;
10162 char_u *s;
10163 char_u *r;
10164 int len;
10165 char *txt;
10166#endif
10167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->v_type = VAR_STRING;
10169 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010171 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10172 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10173 <= curbuf->b_ml.ml_line_count
10174 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 {
10176 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010177 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10178 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 {
10180 if (!linewhite(lnum))
10181 break;
10182 ++lnum;
10183 }
10184
10185 /* Find interesting text in this line. */
10186 s = skipwhite(ml_get(lnum));
10187 /* skip C comment-start */
10188 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010191 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010193 {
10194 s = skipwhite(ml_get(lnum + 1));
10195 if (*s == '*')
10196 s = skipwhite(s + 1);
10197 }
10198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 txt = _("+-%s%3ld lines: ");
10200 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 + 20 /* for %3ld */
10203 + STRLEN(s))); /* concatenated */
10204 if (r != NULL)
10205 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010206 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10207 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10208 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 len = (int)STRLEN(r);
10210 STRCAT(r, s);
10211 /* remove 'foldmarker' and 'commentstring' */
10212 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 }
10215 }
10216#endif
10217}
10218
10219/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010220 * "foldtextresult(lnum)" function
10221 */
10222/*ARGSUSED*/
10223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010225 typval_T *argvars;
10226 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010227{
10228#ifdef FEAT_FOLDING
10229 linenr_T lnum;
10230 char_u *text;
10231 char_u buf[51];
10232 foldinfo_T foldinfo;
10233 int fold_count;
10234#endif
10235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010236 rettv->v_type = VAR_STRING;
10237 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010238#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 /* treat illegal types and illegal string values for {lnum} the same */
10241 if (lnum < 0)
10242 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010243 fold_count = foldedCount(curwin, lnum, &foldinfo);
10244 if (fold_count > 0)
10245 {
10246 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10247 &foldinfo, buf);
10248 if (text == buf)
10249 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010251 }
10252#endif
10253}
10254
10255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 * "foreground()" function
10257 */
10258/*ARGSUSED*/
10259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010261 typval_T *argvars;
10262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265#ifdef FEAT_GUI
10266 if (gui.in_use)
10267 gui_mch_set_foreground();
10268#else
10269# ifdef WIN32
10270 win32_set_foreground();
10271# endif
10272#endif
10273}
10274
10275/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010276 * "function()" function
10277 */
10278/*ARGSUSED*/
10279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010281 typval_T *argvars;
10282 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010283{
10284 char_u *s;
10285
Bram Moolenaara7043832005-01-21 11:56:39 +000010286 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010288 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010289 EMSG2(_(e_invarg2), s);
10290 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010291 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010292 else
10293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->vval.v_string = vim_strsave(s);
10295 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010296 }
10297}
10298
10299/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010300 * "garbagecollect()" function
10301 */
10302/*ARGSUSED*/
10303 static void
10304f_garbagecollect(argvars, rettv)
10305 typval_T *argvars;
10306 typval_T *rettv;
10307{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010308 /* This is postponed until we are back at the toplevel, because we may be
10309 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10310 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010311
10312 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10313 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010314}
10315
10316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317 * "get()" function
10318 */
10319 static void
10320f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010323{
Bram Moolenaar33570922005-01-25 22:26:29 +000010324 listitem_T *li;
10325 list_T *l;
10326 dictitem_T *di;
10327 dict_T *d;
10328 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010331 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010334 int error = FALSE;
10335
10336 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10337 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010339 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010340 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010341 else if (argvars[0].v_type == VAR_DICT)
10342 {
10343 if ((d = argvars[0].vval.v_dict) != NULL)
10344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010345 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010346 if (di != NULL)
10347 tv = &di->di_tv;
10348 }
10349 }
10350 else
10351 EMSG2(_(e_listdictarg), "get()");
10352
10353 if (tv == NULL)
10354 {
10355 if (argvars[2].v_type == VAR_UNKNOWN)
10356 rettv->vval.v_number = 0;
10357 else
10358 copy_tv(&argvars[2], rettv);
10359 }
10360 else
10361 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010362}
10363
Bram Moolenaar342337a2005-07-21 21:11:17 +000010364static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010365
10366/*
10367 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010368 * Return a range (from start to end) of lines in rettv from the specified
10369 * buffer.
10370 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010371 */
10372 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010373get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010374 buf_T *buf;
10375 linenr_T start;
10376 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010377 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010378 typval_T *rettv;
10379{
10380 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010381
Bram Moolenaar342337a2005-07-21 21:11:17 +000010382 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010383 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010384 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010385 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010386 }
10387 else
10388 rettv->vval.v_number = 0;
10389
10390 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10391 return;
10392
10393 if (!retlist)
10394 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010395 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10396 p = ml_get_buf(buf, start, FALSE);
10397 else
10398 p = (char_u *)"";
10399
10400 rettv->v_type = VAR_STRING;
10401 rettv->vval.v_string = vim_strsave(p);
10402 }
10403 else
10404 {
10405 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010406 return;
10407
10408 if (start < 1)
10409 start = 1;
10410 if (end > buf->b_ml.ml_line_count)
10411 end = buf->b_ml.ml_line_count;
10412 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010413 if (list_append_string(rettv->vval.v_list,
10414 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010415 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010416 }
10417}
10418
10419/*
10420 * "getbufline()" function
10421 */
10422 static void
10423f_getbufline(argvars, rettv)
10424 typval_T *argvars;
10425 typval_T *rettv;
10426{
10427 linenr_T lnum;
10428 linenr_T end;
10429 buf_T *buf;
10430
10431 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10432 ++emsg_off;
10433 buf = get_buf_tv(&argvars[0]);
10434 --emsg_off;
10435
Bram Moolenaar661b1822005-07-28 22:36:45 +000010436 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010437 if (argvars[2].v_type == VAR_UNKNOWN)
10438 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010439 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010440 end = get_tv_lnum_buf(&argvars[2], buf);
10441
Bram Moolenaar342337a2005-07-21 21:11:17 +000010442 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010443}
10444
Bram Moolenaar0d660222005-01-07 21:51:51 +000010445/*
10446 * "getbufvar()" function
10447 */
10448 static void
10449f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010450 typval_T *argvars;
10451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010452{
10453 buf_T *buf;
10454 buf_T *save_curbuf;
10455 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010456 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10459 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010460 ++emsg_off;
10461 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010462
10463 rettv->v_type = VAR_STRING;
10464 rettv->vval.v_string = NULL;
10465
10466 if (buf != NULL && varname != NULL)
10467 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010468 /* set curbuf to be our buf, temporarily */
10469 save_curbuf = curbuf;
10470 curbuf = buf;
10471
Bram Moolenaar0d660222005-01-07 21:51:51 +000010472 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010473 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010474 else
10475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 if (*varname == NUL)
10477 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10478 * scope prefix before the NUL byte is required by
10479 * find_var_in_ht(). */
10480 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010481 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010482 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010483 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010485 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010486
10487 /* restore previous notion of curbuf */
10488 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010489 }
10490
10491 --emsg_off;
10492}
10493
10494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 * "getchar()" function
10496 */
10497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010499 typval_T *argvars;
10500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010505 /* Position the cursor. Needed after a message that ends in a space. */
10506 windgoto(msg_row, msg_col);
10507
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 ++no_mapping;
10509 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010510 for (;;)
10511 {
10512 if (argvars[0].v_type == VAR_UNKNOWN)
10513 /* getchar(): blocking wait. */
10514 n = safe_vgetc();
10515 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10516 /* getchar(1): only check if char avail */
10517 n = vpeekc();
10518 else if (error || vpeekc() == NUL)
10519 /* illegal argument or getchar(0) and no char avail: return zero */
10520 n = 0;
10521 else
10522 /* getchar(0) and char avail: return char */
10523 n = safe_vgetc();
10524 if (n == K_IGNORE)
10525 continue;
10526 break;
10527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 --no_mapping;
10529 --allow_keys;
10530
Bram Moolenaar219b8702006-11-01 14:32:36 +000010531 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10532 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10533 vimvars[VV_MOUSE_COL].vv_nr = 0;
10534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 if (IS_SPECIAL(n) || mod_mask != 0)
10537 {
10538 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10539 int i = 0;
10540
10541 /* Turn a special key into three bytes, plus modifier. */
10542 if (mod_mask != 0)
10543 {
10544 temp[i++] = K_SPECIAL;
10545 temp[i++] = KS_MODIFIER;
10546 temp[i++] = mod_mask;
10547 }
10548 if (IS_SPECIAL(n))
10549 {
10550 temp[i++] = K_SPECIAL;
10551 temp[i++] = K_SECOND(n);
10552 temp[i++] = K_THIRD(n);
10553 }
10554#ifdef FEAT_MBYTE
10555 else if (has_mbyte)
10556 i += (*mb_char2bytes)(n, temp + i);
10557#endif
10558 else
10559 temp[i++] = n;
10560 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->v_type = VAR_STRING;
10562 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010563
10564#ifdef FEAT_MOUSE
10565 if (n == K_LEFTMOUSE
10566 || n == K_LEFTMOUSE_NM
10567 || n == K_LEFTDRAG
10568 || n == K_LEFTRELEASE
10569 || n == K_LEFTRELEASE_NM
10570 || n == K_MIDDLEMOUSE
10571 || n == K_MIDDLEDRAG
10572 || n == K_MIDDLERELEASE
10573 || n == K_RIGHTMOUSE
10574 || n == K_RIGHTDRAG
10575 || n == K_RIGHTRELEASE
10576 || n == K_X1MOUSE
10577 || n == K_X1DRAG
10578 || n == K_X1RELEASE
10579 || n == K_X2MOUSE
10580 || n == K_X2DRAG
10581 || n == K_X2RELEASE
10582 || n == K_MOUSEDOWN
10583 || n == K_MOUSEUP)
10584 {
10585 int row = mouse_row;
10586 int col = mouse_col;
10587 win_T *win;
10588 linenr_T lnum;
10589# ifdef FEAT_WINDOWS
10590 win_T *wp;
10591# endif
10592 int n = 1;
10593
10594 if (row >= 0 && col >= 0)
10595 {
10596 /* Find the window at the mouse coordinates and compute the
10597 * text position. */
10598 win = mouse_find_win(&row, &col);
10599 (void)mouse_comp_pos(win, &row, &col, &lnum);
10600# ifdef FEAT_WINDOWS
10601 for (wp = firstwin; wp != win; wp = wp->w_next)
10602 ++n;
10603# endif
10604 vimvars[VV_MOUSE_WIN].vv_nr = n;
10605 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10606 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10607 }
10608 }
10609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 }
10611}
10612
10613/*
10614 * "getcharmod()" function
10615 */
10616/*ARGSUSED*/
10617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010619 typval_T *argvars;
10620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623}
10624
10625/*
10626 * "getcmdline()" function
10627 */
10628/*ARGSUSED*/
10629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 rettv->v_type = VAR_STRING;
10635 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636}
10637
10638/*
10639 * "getcmdpos()" function
10640 */
10641/*ARGSUSED*/
10642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010644 typval_T *argvars;
10645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648}
10649
10650/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010651 * "getcmdtype()" function
10652 */
10653/*ARGSUSED*/
10654 static void
10655f_getcmdtype(argvars, rettv)
10656 typval_T *argvars;
10657 typval_T *rettv;
10658{
10659 rettv->v_type = VAR_STRING;
10660 rettv->vval.v_string = alloc(2);
10661 if (rettv->vval.v_string != NULL)
10662 {
10663 rettv->vval.v_string[0] = get_cmdline_type();
10664 rettv->vval.v_string[1] = NUL;
10665 }
10666}
10667
10668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 * "getcwd()" function
10670 */
10671/*ARGSUSED*/
10672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010674 typval_T *argvars;
10675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676{
10677 char_u cwd[MAXPATHL];
10678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 else
10683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010686 if (rettv->vval.v_string != NULL)
10687 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688#endif
10689 }
10690}
10691
10692/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010693 * "getfontname()" function
10694 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010695/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010698 typval_T *argvars;
10699 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010700{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 rettv->v_type = VAR_STRING;
10702 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010703#ifdef FEAT_GUI
10704 if (gui.in_use)
10705 {
10706 GuiFont font;
10707 char_u *name = NULL;
10708
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010709 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010710 {
10711 /* Get the "Normal" font. Either the name saved by
10712 * hl_set_font_name() or from the font ID. */
10713 font = gui.norm_font;
10714 name = hl_get_font_name();
10715 }
10716 else
10717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010719 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10720 return;
10721 font = gui_mch_get_font(name, FALSE);
10722 if (font == NOFONT)
10723 return; /* Invalid font name, return empty string. */
10724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010726 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010727 gui_mch_free_font(font);
10728 }
10729#endif
10730}
10731
10732/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010733 * "getfperm({fname})" function
10734 */
10735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *argvars;
10738 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010739{
10740 char_u *fname;
10741 struct stat st;
10742 char_u *perm = NULL;
10743 char_u flags[] = "rwx";
10744 int i;
10745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010749 if (mch_stat((char *)fname, &st) >= 0)
10750 {
10751 perm = vim_strsave((char_u *)"---------");
10752 if (perm != NULL)
10753 {
10754 for (i = 0; i < 9; i++)
10755 {
10756 if (st.st_mode & (1 << (8 - i)))
10757 perm[i] = flags[i % 3];
10758 }
10759 }
10760 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010762}
10763
10764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 * "getfsize({fname})" function
10766 */
10767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 typval_T *argvars;
10770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771{
10772 char_u *fname;
10773 struct stat st;
10774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778
10779 if (mch_stat((char *)fname, &st) >= 0)
10780 {
10781 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010786
10787 /* non-perfect check for overflow */
10788 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10789 rettv->vval.v_number = -2;
10790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 }
10792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794}
10795
10796/*
10797 * "getftime({fname})" function
10798 */
10799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *argvars;
10802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803{
10804 char_u *fname;
10805 struct stat st;
10806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808
10809 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813}
10814
10815/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010816 * "getftype({fname})" function
10817 */
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010822{
10823 char_u *fname;
10824 struct stat st;
10825 char_u *type = NULL;
10826 char *t;
10827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010831 if (mch_lstat((char *)fname, &st) >= 0)
10832 {
10833#ifdef S_ISREG
10834 if (S_ISREG(st.st_mode))
10835 t = "file";
10836 else if (S_ISDIR(st.st_mode))
10837 t = "dir";
10838# ifdef S_ISLNK
10839 else if (S_ISLNK(st.st_mode))
10840 t = "link";
10841# endif
10842# ifdef S_ISBLK
10843 else if (S_ISBLK(st.st_mode))
10844 t = "bdev";
10845# endif
10846# ifdef S_ISCHR
10847 else if (S_ISCHR(st.st_mode))
10848 t = "cdev";
10849# endif
10850# ifdef S_ISFIFO
10851 else if (S_ISFIFO(st.st_mode))
10852 t = "fifo";
10853# endif
10854# ifdef S_ISSOCK
10855 else if (S_ISSOCK(st.st_mode))
10856 t = "fifo";
10857# endif
10858 else
10859 t = "other";
10860#else
10861# ifdef S_IFMT
10862 switch (st.st_mode & S_IFMT)
10863 {
10864 case S_IFREG: t = "file"; break;
10865 case S_IFDIR: t = "dir"; break;
10866# ifdef S_IFLNK
10867 case S_IFLNK: t = "link"; break;
10868# endif
10869# ifdef S_IFBLK
10870 case S_IFBLK: t = "bdev"; break;
10871# endif
10872# ifdef S_IFCHR
10873 case S_IFCHR: t = "cdev"; break;
10874# endif
10875# ifdef S_IFIFO
10876 case S_IFIFO: t = "fifo"; break;
10877# endif
10878# ifdef S_IFSOCK
10879 case S_IFSOCK: t = "socket"; break;
10880# endif
10881 default: t = "other";
10882 }
10883# else
10884 if (mch_isdir(fname))
10885 t = "dir";
10886 else
10887 t = "file";
10888# endif
10889#endif
10890 type = vim_strsave((char_u *)t);
10891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010893}
10894
10895/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010896 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897 */
10898 static void
10899f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010900 typval_T *argvars;
10901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010902{
10903 linenr_T lnum;
10904 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010905 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010906
10907 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010908 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010909 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010910 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010911 retlist = FALSE;
10912 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010913 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010914 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010915 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010916 retlist = TRUE;
10917 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010918
Bram Moolenaar342337a2005-07-21 21:11:17 +000010919 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010920}
10921
10922/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010923 * "getmatches()" function
10924 */
10925/*ARGSUSED*/
10926 static void
10927f_getmatches(argvars, rettv)
10928 typval_T *argvars;
10929 typval_T *rettv;
10930{
10931#ifdef FEAT_SEARCH_EXTRA
10932 dict_T *dict;
10933 matchitem_T *cur = curwin->w_match_head;
10934
10935 rettv->vval.v_number = 0;
10936
10937 if (rettv_list_alloc(rettv) == OK)
10938 {
10939 while (cur != NULL)
10940 {
10941 dict = dict_alloc();
10942 if (dict == NULL)
10943 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010944 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10945 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10946 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10947 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10948 list_append_dict(rettv->vval.v_list, dict);
10949 cur = cur->next;
10950 }
10951 }
10952#endif
10953}
10954
10955/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010956 * "getpid()" function
10957 */
10958/*ARGSUSED*/
10959 static void
10960f_getpid(argvars, rettv)
10961 typval_T *argvars;
10962 typval_T *rettv;
10963{
10964 rettv->vval.v_number = mch_get_pid();
10965}
10966
10967/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010968 * "getpos(string)" function
10969 */
10970 static void
10971f_getpos(argvars, rettv)
10972 typval_T *argvars;
10973 typval_T *rettv;
10974{
10975 pos_T *fp;
10976 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010977 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010978
10979 if (rettv_list_alloc(rettv) == OK)
10980 {
10981 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010982 fp = var2fpos(&argvars[0], TRUE, &fnum);
10983 if (fnum != -1)
10984 list_append_number(l, (varnumber_T)fnum);
10985 else
10986 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010987 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10988 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010989 list_append_number(l, (fp != NULL)
10990 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010991 : (varnumber_T)0);
10992 list_append_number(l,
10993#ifdef FEAT_VIRTUALEDIT
10994 (fp != NULL) ? (varnumber_T)fp->coladd :
10995#endif
10996 (varnumber_T)0);
10997 }
10998 else
10999 rettv->vval.v_number = FALSE;
11000}
11001
11002/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011003 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011004 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000011005/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000011006 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011007f_getqflist(argvars, rettv)
11008 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011009 typval_T *rettv;
11010{
11011#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011012 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011013#endif
11014
Bram Moolenaar77f66d62007-02-20 02:16:18 +000011015 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011016#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011017 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011018 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011019 wp = NULL;
11020 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11021 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011022 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011023 if (wp == NULL)
11024 return;
11025 }
11026
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011027 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011028 }
11029#endif
11030}
11031
11032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 * "getreg()" function
11034 */
11035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 typval_T *argvars;
11038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
11040 char_u *strregname;
11041 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011042 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011045 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 strregname = get_tv_string_chk(&argvars[0]);
11048 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011053 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 regname = (strregname == NULL ? '"' : *strregname);
11055 if (regname == 0)
11056 regname = '"';
11057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 rettv->vval.v_string = error ? NULL :
11060 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061}
11062
11063/*
11064 * "getregtype()" function
11065 */
11066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011068 typval_T *argvars;
11069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070{
11071 char_u *strregname;
11072 int regname;
11073 char_u buf[NUMBUFLEN + 2];
11074 long reglen = 0;
11075
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011076 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 {
11078 strregname = get_tv_string_chk(&argvars[0]);
11079 if (strregname == NULL) /* type error; errmsg already given */
11080 {
11081 rettv->v_type = VAR_STRING;
11082 rettv->vval.v_string = NULL;
11083 return;
11084 }
11085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 else
11087 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011088 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089
11090 regname = (strregname == NULL ? '"' : *strregname);
11091 if (regname == 0)
11092 regname = '"';
11093
11094 buf[0] = NUL;
11095 buf[1] = NUL;
11096 switch (get_reg_type(regname, &reglen))
11097 {
11098 case MLINE: buf[0] = 'V'; break;
11099 case MCHAR: buf[0] = 'v'; break;
11100#ifdef FEAT_VISUAL
11101 case MBLOCK:
11102 buf[0] = Ctrl_V;
11103 sprintf((char *)buf + 1, "%ld", reglen + 1);
11104 break;
11105#endif
11106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 rettv->v_type = VAR_STRING;
11108 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109}
11110
11111/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011112 * "gettabwinvar()" function
11113 */
11114 static void
11115f_gettabwinvar(argvars, rettv)
11116 typval_T *argvars;
11117 typval_T *rettv;
11118{
11119 getwinvar(argvars, rettv, 1);
11120}
11121
11122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 * "getwinposx()" function
11124 */
11125/*ARGSUSED*/
11126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132#ifdef FEAT_GUI
11133 if (gui.in_use)
11134 {
11135 int x, y;
11136
11137 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 }
11140#endif
11141}
11142
11143/*
11144 * "getwinposy()" function
11145 */
11146/*ARGSUSED*/
11147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011149 typval_T *argvars;
11150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153#ifdef FEAT_GUI
11154 if (gui.in_use)
11155 {
11156 int x, y;
11157
11158 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011159 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 }
11161#endif
11162}
11163
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011164/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011165 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011166 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011167 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011168find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011169 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011170 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011171{
11172#ifdef FEAT_WINDOWS
11173 win_T *wp;
11174#endif
11175 int nr;
11176
11177 nr = get_tv_number_chk(vp, NULL);
11178
11179#ifdef FEAT_WINDOWS
11180 if (nr < 0)
11181 return NULL;
11182 if (nr == 0)
11183 return curwin;
11184
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011185 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11186 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011187 if (--nr <= 0)
11188 break;
11189 return wp;
11190#else
11191 if (nr == 0 || nr == 1)
11192 return curwin;
11193 return NULL;
11194#endif
11195}
11196
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197/*
11198 * "getwinvar()" function
11199 */
11200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011202 typval_T *argvars;
11203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011205 getwinvar(argvars, rettv, 0);
11206}
11207
11208/*
11209 * getwinvar() and gettabwinvar()
11210 */
11211 static void
11212getwinvar(argvars, rettv, off)
11213 typval_T *argvars;
11214 typval_T *rettv;
11215 int off; /* 1 for gettabwinvar() */
11216{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 win_T *win, *oldcurwin;
11218 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011220 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011222#ifdef FEAT_WINDOWS
11223 if (off == 1)
11224 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11225 else
11226 tp = curtab;
11227#endif
11228 win = find_win_by_nr(&argvars[off], tp);
11229 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->v_type = VAR_STRING;
11233 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234
11235 if (win != NULL && varname != NULL)
11236 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011237 /* Set curwin to be our win, temporarily. Also set curbuf, so
11238 * that we can get buffer-local options. */
11239 oldcurwin = curwin;
11240 curwin = win;
11241 curbuf = win->w_buffer;
11242
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 else
11246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 if (*varname == NUL)
11248 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11249 * scope prefix before the NUL byte is required by
11250 * find_var_in_ht(). */
11251 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011253 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011255 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011257
11258 /* restore previous notion of curwin */
11259 curwin = oldcurwin;
11260 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 }
11262
11263 --emsg_off;
11264}
11265
11266/*
11267 * "glob()" function
11268 */
11269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011271 typval_T *argvars;
11272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273{
11274 expand_T xpc;
11275
11276 ExpandInit(&xpc);
11277 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 rettv->v_type = VAR_STRING;
11279 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281}
11282
11283/*
11284 * "globpath()" function
11285 */
11286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T *argvars;
11289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
11291 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011295 if (file == NULL)
11296 rettv->vval.v_string = NULL;
11297 else
11298 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
11302 * "has()" function
11303 */
11304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011306 typval_T *argvars;
11307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308{
11309 int i;
11310 char_u *name;
11311 int n = FALSE;
11312 static char *(has_list[]) =
11313 {
11314#ifdef AMIGA
11315 "amiga",
11316# ifdef FEAT_ARP
11317 "arp",
11318# endif
11319#endif
11320#ifdef __BEOS__
11321 "beos",
11322#endif
11323#ifdef MSDOS
11324# ifdef DJGPP
11325 "dos32",
11326# else
11327 "dos16",
11328# endif
11329#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011330#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 "mac",
11332#endif
11333#if defined(MACOS_X_UNIX)
11334 "macunix",
11335#endif
11336#ifdef OS2
11337 "os2",
11338#endif
11339#ifdef __QNX__
11340 "qnx",
11341#endif
11342#ifdef RISCOS
11343 "riscos",
11344#endif
11345#ifdef UNIX
11346 "unix",
11347#endif
11348#ifdef VMS
11349 "vms",
11350#endif
11351#ifdef WIN16
11352 "win16",
11353#endif
11354#ifdef WIN32
11355 "win32",
11356#endif
11357#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11358 "win32unix",
11359#endif
11360#ifdef WIN64
11361 "win64",
11362#endif
11363#ifdef EBCDIC
11364 "ebcdic",
11365#endif
11366#ifndef CASE_INSENSITIVE_FILENAME
11367 "fname_case",
11368#endif
11369#ifdef FEAT_ARABIC
11370 "arabic",
11371#endif
11372#ifdef FEAT_AUTOCMD
11373 "autocmd",
11374#endif
11375#ifdef FEAT_BEVAL
11376 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011377# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11378 "balloon_multiline",
11379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380#endif
11381#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11382 "builtin_terms",
11383# ifdef ALL_BUILTIN_TCAPS
11384 "all_builtin_terms",
11385# endif
11386#endif
11387#ifdef FEAT_BYTEOFF
11388 "byte_offset",
11389#endif
11390#ifdef FEAT_CINDENT
11391 "cindent",
11392#endif
11393#ifdef FEAT_CLIENTSERVER
11394 "clientserver",
11395#endif
11396#ifdef FEAT_CLIPBOARD
11397 "clipboard",
11398#endif
11399#ifdef FEAT_CMDL_COMPL
11400 "cmdline_compl",
11401#endif
11402#ifdef FEAT_CMDHIST
11403 "cmdline_hist",
11404#endif
11405#ifdef FEAT_COMMENTS
11406 "comments",
11407#endif
11408#ifdef FEAT_CRYPT
11409 "cryptv",
11410#endif
11411#ifdef FEAT_CSCOPE
11412 "cscope",
11413#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011414#ifdef CURSOR_SHAPE
11415 "cursorshape",
11416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417#ifdef DEBUG
11418 "debug",
11419#endif
11420#ifdef FEAT_CON_DIALOG
11421 "dialog_con",
11422#endif
11423#ifdef FEAT_GUI_DIALOG
11424 "dialog_gui",
11425#endif
11426#ifdef FEAT_DIFF
11427 "diff",
11428#endif
11429#ifdef FEAT_DIGRAPHS
11430 "digraphs",
11431#endif
11432#ifdef FEAT_DND
11433 "dnd",
11434#endif
11435#ifdef FEAT_EMACS_TAGS
11436 "emacs_tags",
11437#endif
11438 "eval", /* always present, of course! */
11439#ifdef FEAT_EX_EXTRA
11440 "ex_extra",
11441#endif
11442#ifdef FEAT_SEARCH_EXTRA
11443 "extra_search",
11444#endif
11445#ifdef FEAT_FKMAP
11446 "farsi",
11447#endif
11448#ifdef FEAT_SEARCHPATH
11449 "file_in_path",
11450#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011451#if defined(UNIX) && !defined(USE_SYSTEM)
11452 "filterpipe",
11453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454#ifdef FEAT_FIND_ID
11455 "find_in_path",
11456#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011457#ifdef FEAT_FLOAT
11458 "float",
11459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460#ifdef FEAT_FOLDING
11461 "folding",
11462#endif
11463#ifdef FEAT_FOOTER
11464 "footer",
11465#endif
11466#if !defined(USE_SYSTEM) && defined(UNIX)
11467 "fork",
11468#endif
11469#ifdef FEAT_GETTEXT
11470 "gettext",
11471#endif
11472#ifdef FEAT_GUI
11473 "gui",
11474#endif
11475#ifdef FEAT_GUI_ATHENA
11476# ifdef FEAT_GUI_NEXTAW
11477 "gui_neXtaw",
11478# else
11479 "gui_athena",
11480# endif
11481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482#ifdef FEAT_GUI_GTK
11483 "gui_gtk",
11484# ifdef HAVE_GTK2
11485 "gui_gtk2",
11486# endif
11487#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011488#ifdef FEAT_GUI_GNOME
11489 "gui_gnome",
11490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491#ifdef FEAT_GUI_MAC
11492 "gui_mac",
11493#endif
11494#ifdef FEAT_GUI_MOTIF
11495 "gui_motif",
11496#endif
11497#ifdef FEAT_GUI_PHOTON
11498 "gui_photon",
11499#endif
11500#ifdef FEAT_GUI_W16
11501 "gui_win16",
11502#endif
11503#ifdef FEAT_GUI_W32
11504 "gui_win32",
11505#endif
11506#ifdef FEAT_HANGULIN
11507 "hangul_input",
11508#endif
11509#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11510 "iconv",
11511#endif
11512#ifdef FEAT_INS_EXPAND
11513 "insert_expand",
11514#endif
11515#ifdef FEAT_JUMPLIST
11516 "jumplist",
11517#endif
11518#ifdef FEAT_KEYMAP
11519 "keymap",
11520#endif
11521#ifdef FEAT_LANGMAP
11522 "langmap",
11523#endif
11524#ifdef FEAT_LIBCALL
11525 "libcall",
11526#endif
11527#ifdef FEAT_LINEBREAK
11528 "linebreak",
11529#endif
11530#ifdef FEAT_LISP
11531 "lispindent",
11532#endif
11533#ifdef FEAT_LISTCMDS
11534 "listcmds",
11535#endif
11536#ifdef FEAT_LOCALMAP
11537 "localmap",
11538#endif
11539#ifdef FEAT_MENU
11540 "menu",
11541#endif
11542#ifdef FEAT_SESSION
11543 "mksession",
11544#endif
11545#ifdef FEAT_MODIFY_FNAME
11546 "modify_fname",
11547#endif
11548#ifdef FEAT_MOUSE
11549 "mouse",
11550#endif
11551#ifdef FEAT_MOUSESHAPE
11552 "mouseshape",
11553#endif
11554#if defined(UNIX) || defined(VMS)
11555# ifdef FEAT_MOUSE_DEC
11556 "mouse_dec",
11557# endif
11558# ifdef FEAT_MOUSE_GPM
11559 "mouse_gpm",
11560# endif
11561# ifdef FEAT_MOUSE_JSB
11562 "mouse_jsbterm",
11563# endif
11564# ifdef FEAT_MOUSE_NET
11565 "mouse_netterm",
11566# endif
11567# ifdef FEAT_MOUSE_PTERM
11568 "mouse_pterm",
11569# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011570# ifdef FEAT_SYSMOUSE
11571 "mouse_sysmouse",
11572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573# ifdef FEAT_MOUSE_XTERM
11574 "mouse_xterm",
11575# endif
11576#endif
11577#ifdef FEAT_MBYTE
11578 "multi_byte",
11579#endif
11580#ifdef FEAT_MBYTE_IME
11581 "multi_byte_ime",
11582#endif
11583#ifdef FEAT_MULTI_LANG
11584 "multi_lang",
11585#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011586#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011587#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011588 "mzscheme",
11589#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591#ifdef FEAT_OLE
11592 "ole",
11593#endif
11594#ifdef FEAT_OSFILETYPE
11595 "osfiletype",
11596#endif
11597#ifdef FEAT_PATH_EXTRA
11598 "path_extra",
11599#endif
11600#ifdef FEAT_PERL
11601#ifndef DYNAMIC_PERL
11602 "perl",
11603#endif
11604#endif
11605#ifdef FEAT_PYTHON
11606#ifndef DYNAMIC_PYTHON
11607 "python",
11608#endif
11609#endif
11610#ifdef FEAT_POSTSCRIPT
11611 "postscript",
11612#endif
11613#ifdef FEAT_PRINTER
11614 "printer",
11615#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011616#ifdef FEAT_PROFILE
11617 "profile",
11618#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011619#ifdef FEAT_RELTIME
11620 "reltime",
11621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622#ifdef FEAT_QUICKFIX
11623 "quickfix",
11624#endif
11625#ifdef FEAT_RIGHTLEFT
11626 "rightleft",
11627#endif
11628#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11629 "ruby",
11630#endif
11631#ifdef FEAT_SCROLLBIND
11632 "scrollbind",
11633#endif
11634#ifdef FEAT_CMDL_INFO
11635 "showcmd",
11636 "cmdline_info",
11637#endif
11638#ifdef FEAT_SIGNS
11639 "signs",
11640#endif
11641#ifdef FEAT_SMARTINDENT
11642 "smartindent",
11643#endif
11644#ifdef FEAT_SNIFF
11645 "sniff",
11646#endif
11647#ifdef FEAT_STL_OPT
11648 "statusline",
11649#endif
11650#ifdef FEAT_SUN_WORKSHOP
11651 "sun_workshop",
11652#endif
11653#ifdef FEAT_NETBEANS_INTG
11654 "netbeans_intg",
11655#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011656#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011657 "spell",
11658#endif
11659#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 "syntax",
11661#endif
11662#if defined(USE_SYSTEM) || !defined(UNIX)
11663 "system",
11664#endif
11665#ifdef FEAT_TAG_BINS
11666 "tag_binary",
11667#endif
11668#ifdef FEAT_TAG_OLDSTATIC
11669 "tag_old_static",
11670#endif
11671#ifdef FEAT_TAG_ANYWHITE
11672 "tag_any_white",
11673#endif
11674#ifdef FEAT_TCL
11675# ifndef DYNAMIC_TCL
11676 "tcl",
11677# endif
11678#endif
11679#ifdef TERMINFO
11680 "terminfo",
11681#endif
11682#ifdef FEAT_TERMRESPONSE
11683 "termresponse",
11684#endif
11685#ifdef FEAT_TEXTOBJ
11686 "textobjects",
11687#endif
11688#ifdef HAVE_TGETENT
11689 "tgetent",
11690#endif
11691#ifdef FEAT_TITLE
11692 "title",
11693#endif
11694#ifdef FEAT_TOOLBAR
11695 "toolbar",
11696#endif
11697#ifdef FEAT_USR_CMDS
11698 "user-commands", /* was accidentally included in 5.4 */
11699 "user_commands",
11700#endif
11701#ifdef FEAT_VIMINFO
11702 "viminfo",
11703#endif
11704#ifdef FEAT_VERTSPLIT
11705 "vertsplit",
11706#endif
11707#ifdef FEAT_VIRTUALEDIT
11708 "virtualedit",
11709#endif
11710#ifdef FEAT_VISUAL
11711 "visual",
11712#endif
11713#ifdef FEAT_VISUALEXTRA
11714 "visualextra",
11715#endif
11716#ifdef FEAT_VREPLACE
11717 "vreplace",
11718#endif
11719#ifdef FEAT_WILDIGN
11720 "wildignore",
11721#endif
11722#ifdef FEAT_WILDMENU
11723 "wildmenu",
11724#endif
11725#ifdef FEAT_WINDOWS
11726 "windows",
11727#endif
11728#ifdef FEAT_WAK
11729 "winaltkeys",
11730#endif
11731#ifdef FEAT_WRITEBACKUP
11732 "writebackup",
11733#endif
11734#ifdef FEAT_XIM
11735 "xim",
11736#endif
11737#ifdef FEAT_XFONTSET
11738 "xfontset",
11739#endif
11740#ifdef USE_XSMP
11741 "xsmp",
11742#endif
11743#ifdef USE_XSMP_INTERACT
11744 "xsmp_interact",
11745#endif
11746#ifdef FEAT_XCLIPBOARD
11747 "xterm_clipboard",
11748#endif
11749#ifdef FEAT_XTERM_SAVE
11750 "xterm_save",
11751#endif
11752#if defined(UNIX) && defined(FEAT_X11)
11753 "X11",
11754#endif
11755 NULL
11756 };
11757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 for (i = 0; has_list[i] != NULL; ++i)
11760 if (STRICMP(name, has_list[i]) == 0)
11761 {
11762 n = TRUE;
11763 break;
11764 }
11765
11766 if (n == FALSE)
11767 {
11768 if (STRNICMP(name, "patch", 5) == 0)
11769 n = has_patch(atoi((char *)name + 5));
11770 else if (STRICMP(name, "vim_starting") == 0)
11771 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011772#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11773 else if (STRICMP(name, "balloon_multiline") == 0)
11774 n = multiline_balloon_available();
11775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776#ifdef DYNAMIC_TCL
11777 else if (STRICMP(name, "tcl") == 0)
11778 n = tcl_enabled(FALSE);
11779#endif
11780#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11781 else if (STRICMP(name, "iconv") == 0)
11782 n = iconv_enabled(FALSE);
11783#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011784#ifdef DYNAMIC_MZSCHEME
11785 else if (STRICMP(name, "mzscheme") == 0)
11786 n = mzscheme_enabled(FALSE);
11787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788#ifdef DYNAMIC_RUBY
11789 else if (STRICMP(name, "ruby") == 0)
11790 n = ruby_enabled(FALSE);
11791#endif
11792#ifdef DYNAMIC_PYTHON
11793 else if (STRICMP(name, "python") == 0)
11794 n = python_enabled(FALSE);
11795#endif
11796#ifdef DYNAMIC_PERL
11797 else if (STRICMP(name, "perl") == 0)
11798 n = perl_enabled(FALSE);
11799#endif
11800#ifdef FEAT_GUI
11801 else if (STRICMP(name, "gui_running") == 0)
11802 n = (gui.in_use || gui.starting);
11803# ifdef FEAT_GUI_W32
11804 else if (STRICMP(name, "gui_win32s") == 0)
11805 n = gui_is_win32s();
11806# endif
11807# ifdef FEAT_BROWSE
11808 else if (STRICMP(name, "browse") == 0)
11809 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11810# endif
11811#endif
11812#ifdef FEAT_SYN_HL
11813 else if (STRICMP(name, "syntax_items") == 0)
11814 n = syntax_present(curbuf);
11815#endif
11816#if defined(WIN3264)
11817 else if (STRICMP(name, "win95") == 0)
11818 n = mch_windows95();
11819#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011820#ifdef FEAT_NETBEANS_INTG
11821 else if (STRICMP(name, "netbeans_enabled") == 0)
11822 n = usingNetbeans;
11823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 }
11825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827}
11828
11829/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011830 * "has_key()" function
11831 */
11832 static void
11833f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011834 typval_T *argvars;
11835 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011836{
11837 rettv->vval.v_number = 0;
11838 if (argvars[0].v_type != VAR_DICT)
11839 {
11840 EMSG(_(e_dictreq));
11841 return;
11842 }
11843 if (argvars[0].vval.v_dict == NULL)
11844 return;
11845
11846 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011847 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848}
11849
11850/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011851 * "haslocaldir()" function
11852 */
11853/*ARGSUSED*/
11854 static void
11855f_haslocaldir(argvars, rettv)
11856 typval_T *argvars;
11857 typval_T *rettv;
11858{
11859 rettv->vval.v_number = (curwin->w_localdir != NULL);
11860}
11861
11862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 * "hasmapto()" function
11864 */
11865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T *argvars;
11868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869{
11870 char_u *name;
11871 char_u *mode;
11872 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011873 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011876 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 mode = (char_u *)"nvo";
11878 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011881 if (argvars[2].v_type != VAR_UNKNOWN)
11882 abbr = get_tv_number(&argvars[2]);
11883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884
Bram Moolenaar2c932302006-03-18 21:42:09 +000011885 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011886 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889}
11890
11891/*
11892 * "histadd()" function
11893 */
11894/*ARGSUSED*/
11895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 typval_T *argvars;
11898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899{
11900#ifdef FEAT_CMDHIST
11901 int histype;
11902 char_u *str;
11903 char_u buf[NUMBUFLEN];
11904#endif
11905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011906 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 if (check_restricted() || check_secure())
11908 return;
11909#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11911 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 if (histype >= 0)
11913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 if (*str != NUL)
11916 {
11917 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 return;
11920 }
11921 }
11922#endif
11923}
11924
11925/*
11926 * "histdel()" function
11927 */
11928/*ARGSUSED*/
11929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 typval_T *argvars;
11932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933{
11934#ifdef FEAT_CMDHIST
11935 int n;
11936 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011939 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11940 if (str == NULL)
11941 n = 0;
11942 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011945 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 else
11950 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 get_tv_string_buf(&argvars[1], buf));
11953 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956#endif
11957}
11958
11959/*
11960 * "histget()" function
11961 */
11962/*ARGSUSED*/
11963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011965 typval_T *argvars;
11966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967{
11968#ifdef FEAT_CMDHIST
11969 int type;
11970 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11974 if (str == NULL)
11975 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011977 {
11978 type = get_histtype(str);
11979 if (argvars[1].v_type == VAR_UNKNOWN)
11980 idx = get_history_idx(type);
11981 else
11982 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11983 /* -1 on type error */
11984 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990}
11991
11992/*
11993 * "histnr()" function
11994 */
11995/*ARGSUSED*/
11996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011998 typval_T *argvars;
11999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
12001 int i;
12002
12003#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012004 char_u *history = get_tv_string_chk(&argvars[0]);
12005
12006 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 if (i >= HIST_CMD && i < HIST_COUNT)
12008 i = get_history_idx(i);
12009 else
12010#endif
12011 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013}
12014
12015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 * "highlightID(name)" function
12017 */
12018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024}
12025
12026/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012027 * "highlight_exists()" function
12028 */
12029 static void
12030f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012031 typval_T *argvars;
12032 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012033{
12034 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12035}
12036
12037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 * "hostname()" function
12039 */
12040/*ARGSUSED*/
12041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012043 typval_T *argvars;
12044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045{
12046 char_u hostname[256];
12047
12048 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 rettv->v_type = VAR_STRING;
12050 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051}
12052
12053/*
12054 * iconv() function
12055 */
12056/*ARGSUSED*/
12057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012059 typval_T *argvars;
12060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061{
12062#ifdef FEAT_MBYTE
12063 char_u buf1[NUMBUFLEN];
12064 char_u buf2[NUMBUFLEN];
12065 char_u *from, *to, *str;
12066 vimconv_T vimconv;
12067#endif
12068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069 rettv->v_type = VAR_STRING;
12070 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071
12072#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 str = get_tv_string(&argvars[0]);
12074 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12075 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 vimconv.vc_type = CONV_NONE;
12077 convert_setup(&vimconv, from, to);
12078
12079 /* If the encodings are equal, no conversion needed. */
12080 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084
12085 convert_setup(&vimconv, NULL, NULL);
12086 vim_free(from);
12087 vim_free(to);
12088#endif
12089}
12090
12091/*
12092 * "indent()" function
12093 */
12094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
12099 linenr_T lnum;
12100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106}
12107
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012108/*
12109 * "index()" function
12110 */
12111 static void
12112f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012113 typval_T *argvars;
12114 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012115{
Bram Moolenaar33570922005-01-25 22:26:29 +000012116 list_T *l;
12117 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012118 long idx = 0;
12119 int ic = FALSE;
12120
12121 rettv->vval.v_number = -1;
12122 if (argvars[0].v_type != VAR_LIST)
12123 {
12124 EMSG(_(e_listreq));
12125 return;
12126 }
12127 l = argvars[0].vval.v_list;
12128 if (l != NULL)
12129 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012130 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012131 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012133 int error = FALSE;
12134
Bram Moolenaar758711c2005-02-02 23:11:38 +000012135 /* Start at specified item. Use the cached index that list_find()
12136 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012138 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012139 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012140 ic = get_tv_number_chk(&argvars[3], &error);
12141 if (error)
12142 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012143 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012144
Bram Moolenaar758711c2005-02-02 23:11:38 +000012145 for ( ; item != NULL; item = item->li_next, ++idx)
12146 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012147 {
12148 rettv->vval.v_number = idx;
12149 break;
12150 }
12151 }
12152}
12153
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154static int inputsecret_flag = 0;
12155
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012156static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12157
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012159 * This function is used by f_input() and f_inputdialog() functions. The third
12160 * argument to f_input() specifies the type of completion to use at the
12161 * prompt. The third argument to f_inputdialog() specifies the value to return
12162 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 */
12164 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012165get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012166 typval_T *argvars;
12167 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012168 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 char_u *p = NULL;
12172 int c;
12173 char_u buf[NUMBUFLEN];
12174 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012176 int xp_type = EXPAND_NOTHING;
12177 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012180 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181
12182#ifdef NO_CONSOLE_INPUT
12183 /* While starting up, there is no place to enter text. */
12184 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186#endif
12187
12188 cmd_silent = FALSE; /* Want to see the prompt. */
12189 if (prompt != NULL)
12190 {
12191 /* Only the part of the message after the last NL is considered as
12192 * prompt for the command line */
12193 p = vim_strrchr(prompt, '\n');
12194 if (p == NULL)
12195 p = prompt;
12196 else
12197 {
12198 ++p;
12199 c = *p;
12200 *p = NUL;
12201 msg_start();
12202 msg_clr_eos();
12203 msg_puts_attr(prompt, echo_attr);
12204 msg_didout = FALSE;
12205 msg_starthere();
12206 *p = c;
12207 }
12208 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012210 if (argvars[1].v_type != VAR_UNKNOWN)
12211 {
12212 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12213 if (defstr != NULL)
12214 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012216 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012217 {
12218 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012219 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012220 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012221
Bram Moolenaar4463f292005-09-25 22:20:24 +000012222 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012223
Bram Moolenaar4463f292005-09-25 22:20:24 +000012224 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12225 if (xp_name == NULL)
12226 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012227
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012228 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012229
Bram Moolenaar4463f292005-09-25 22:20:24 +000012230 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12231 &xp_arg) == FAIL)
12232 return;
12233 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012234 }
12235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012236 if (defstr != NULL)
12237 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012238 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12239 xp_type, xp_arg);
12240
12241 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012243 /* since the user typed this, no need to wait for return */
12244 need_wait_return = FALSE;
12245 msg_didout = FALSE;
12246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 cmd_silent = cmd_silent_save;
12248}
12249
12250/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012251 * "input()" function
12252 * Also handles inputsecret() when inputsecret is set.
12253 */
12254 static void
12255f_input(argvars, rettv)
12256 typval_T *argvars;
12257 typval_T *rettv;
12258{
12259 get_user_input(argvars, rettv, FALSE);
12260}
12261
12262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 * "inputdialog()" function
12264 */
12265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012267 typval_T *argvars;
12268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269{
12270#if defined(FEAT_GUI_TEXTDIALOG)
12271 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12272 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12273 {
12274 char_u *message;
12275 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012278 message = get_tv_string_chk(&argvars[0]);
12279 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012280 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012281 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 else
12283 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012284 if (message != NULL && defstr != NULL
12285 && do_dialog(VIM_QUESTION, NULL, message,
12286 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012287 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 else
12289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 if (message != NULL && defstr != NULL
12291 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012292 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293 rettv->vval.v_string = vim_strsave(
12294 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 }
12300 else
12301#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012302 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303}
12304
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012305/*
12306 * "inputlist()" function
12307 */
12308 static void
12309f_inputlist(argvars, rettv)
12310 typval_T *argvars;
12311 typval_T *rettv;
12312{
12313 listitem_T *li;
12314 int selected;
12315 int mouse_used;
12316
12317 rettv->vval.v_number = 0;
12318#ifdef NO_CONSOLE_INPUT
12319 /* While starting up, there is no place to enter text. */
12320 if (no_console_input())
12321 return;
12322#endif
12323 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12324 {
12325 EMSG2(_(e_listarg), "inputlist()");
12326 return;
12327 }
12328
12329 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012330 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012331 lines_left = Rows; /* avoid more prompt */
12332 msg_scroll = TRUE;
12333 msg_clr_eos();
12334
12335 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12336 {
12337 msg_puts(get_tv_string(&li->li_tv));
12338 msg_putchar('\n');
12339 }
12340
12341 /* Ask for choice. */
12342 selected = prompt_for_number(&mouse_used);
12343 if (mouse_used)
12344 selected -= lines_left;
12345
12346 rettv->vval.v_number = selected;
12347}
12348
12349
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12351
12352/*
12353 * "inputrestore()" function
12354 */
12355/*ARGSUSED*/
12356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012358 typval_T *argvars;
12359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360{
12361 if (ga_userinput.ga_len > 0)
12362 {
12363 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12365 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 }
12368 else if (p_verbose > 1)
12369 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012370 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 }
12373}
12374
12375/*
12376 * "inputsave()" function
12377 */
12378/*ARGSUSED*/
12379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *argvars;
12382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012384 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 if (ga_grow(&ga_userinput, 1) == OK)
12386 {
12387 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12388 + ga_userinput.ga_len);
12389 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012390 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 }
12392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394}
12395
12396/*
12397 * "inputsecret()" function
12398 */
12399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012400f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012401 typval_T *argvars;
12402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403{
12404 ++cmdline_star;
12405 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 --cmdline_star;
12408 --inputsecret_flag;
12409}
12410
12411/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012412 * "insert()" function
12413 */
12414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012416 typval_T *argvars;
12417 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012418{
12419 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012420 listitem_T *item;
12421 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012422 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012423
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012424 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012425 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012426 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012427 else if ((l = argvars[0].vval.v_list) != NULL
12428 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012429 {
12430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 before = get_tv_number_chk(&argvars[2], &error);
12432 if (error)
12433 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012434
Bram Moolenaar758711c2005-02-02 23:11:38 +000012435 if (before == l->lv_len)
12436 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012437 else
12438 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012439 item = list_find(l, before);
12440 if (item == NULL)
12441 {
12442 EMSGN(_(e_listidx), before);
12443 l = NULL;
12444 }
12445 }
12446 if (l != NULL)
12447 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012448 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012449 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012450 }
12451 }
12452}
12453
12454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 * "isdirectory()" function
12456 */
12457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012459 typval_T *argvars;
12460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012462 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463}
12464
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012465/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012466 * "islocked()" function
12467 */
12468 static void
12469f_islocked(argvars, rettv)
12470 typval_T *argvars;
12471 typval_T *rettv;
12472{
12473 lval_T lv;
12474 char_u *end;
12475 dictitem_T *di;
12476
12477 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012478 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12479 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012480 if (end != NULL && lv.ll_name != NULL)
12481 {
12482 if (*end != NUL)
12483 EMSG(_(e_trailing));
12484 else
12485 {
12486 if (lv.ll_tv == NULL)
12487 {
12488 if (check_changedtick(lv.ll_name))
12489 rettv->vval.v_number = 1; /* always locked */
12490 else
12491 {
12492 di = find_var(lv.ll_name, NULL);
12493 if (di != NULL)
12494 {
12495 /* Consider a variable locked when:
12496 * 1. the variable itself is locked
12497 * 2. the value of the variable is locked.
12498 * 3. the List or Dict value is locked.
12499 */
12500 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12501 || tv_islocked(&di->di_tv));
12502 }
12503 }
12504 }
12505 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012506 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012507 else if (lv.ll_newkey != NULL)
12508 EMSG2(_(e_dictkey), lv.ll_newkey);
12509 else if (lv.ll_list != NULL)
12510 /* List item. */
12511 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12512 else
12513 /* Dictionary item. */
12514 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12515 }
12516 }
12517
12518 clear_lval(&lv);
12519}
12520
Bram Moolenaar33570922005-01-25 22:26:29 +000012521static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012522
12523/*
12524 * Turn a dict into a list:
12525 * "what" == 0: list of keys
12526 * "what" == 1: list of values
12527 * "what" == 2: list of items
12528 */
12529 static void
12530dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 typval_T *argvars;
12532 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012533 int what;
12534{
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 list_T *l2;
12536 dictitem_T *di;
12537 hashitem_T *hi;
12538 listitem_T *li;
12539 listitem_T *li2;
12540 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012541 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012542
12543 rettv->vval.v_number = 0;
12544 if (argvars[0].v_type != VAR_DICT)
12545 {
12546 EMSG(_(e_dictreq));
12547 return;
12548 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012549 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012550 return;
12551
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012552 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012553 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012554
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012555 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012557 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012558 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012560 --todo;
12561 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012562
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012563 li = listitem_alloc();
12564 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012565 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012566 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012567
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012568 if (what == 0)
12569 {
12570 /* keys() */
12571 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012572 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012573 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12574 }
12575 else if (what == 1)
12576 {
12577 /* values() */
12578 copy_tv(&di->di_tv, &li->li_tv);
12579 }
12580 else
12581 {
12582 /* items() */
12583 l2 = list_alloc();
12584 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012585 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012586 li->li_tv.vval.v_list = l2;
12587 if (l2 == NULL)
12588 break;
12589 ++l2->lv_refcount;
12590
12591 li2 = listitem_alloc();
12592 if (li2 == NULL)
12593 break;
12594 list_append(l2, li2);
12595 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012596 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012597 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12598
12599 li2 = listitem_alloc();
12600 if (li2 == NULL)
12601 break;
12602 list_append(l2, li2);
12603 copy_tv(&di->di_tv, &li2->li_tv);
12604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012605 }
12606 }
12607}
12608
12609/*
12610 * "items(dict)" function
12611 */
12612 static void
12613f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012614 typval_T *argvars;
12615 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012616{
12617 dict_list(argvars, rettv, 2);
12618}
12619
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012621 * "join()" function
12622 */
12623 static void
12624f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 typval_T *argvars;
12626 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012627{
12628 garray_T ga;
12629 char_u *sep;
12630
12631 rettv->vval.v_number = 0;
12632 if (argvars[0].v_type != VAR_LIST)
12633 {
12634 EMSG(_(e_listreq));
12635 return;
12636 }
12637 if (argvars[0].vval.v_list == NULL)
12638 return;
12639 if (argvars[1].v_type == VAR_UNKNOWN)
12640 sep = (char_u *)" ";
12641 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012643
12644 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645
12646 if (sep != NULL)
12647 {
12648 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012649 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012650 ga_append(&ga, NUL);
12651 rettv->vval.v_string = (char_u *)ga.ga_data;
12652 }
12653 else
12654 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012655}
12656
12657/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012658 * "keys()" function
12659 */
12660 static void
12661f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012662 typval_T *argvars;
12663 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012664{
12665 dict_list(argvars, rettv, 0);
12666}
12667
12668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 * "last_buffer_nr()" function.
12670 */
12671/*ARGSUSED*/
12672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012674 typval_T *argvars;
12675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676{
12677 int n = 0;
12678 buf_T *buf;
12679
12680 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12681 if (n < buf->b_fnum)
12682 n = buf->b_fnum;
12683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685}
12686
12687/*
12688 * "len()" function
12689 */
12690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012692 typval_T *argvars;
12693 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012694{
12695 switch (argvars[0].v_type)
12696 {
12697 case VAR_STRING:
12698 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 rettv->vval.v_number = (varnumber_T)STRLEN(
12700 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 break;
12702 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012704 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012705 case VAR_DICT:
12706 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12707 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012708 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012709 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710 break;
12711 }
12712}
12713
Bram Moolenaar33570922005-01-25 22:26:29 +000012714static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012715
12716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012718 typval_T *argvars;
12719 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012720 int type;
12721{
12722#ifdef FEAT_LIBCALL
12723 char_u *string_in;
12724 char_u **string_result;
12725 int nr_result;
12726#endif
12727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012729 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012731 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012733
12734 if (check_restricted() || check_secure())
12735 return;
12736
12737#ifdef FEAT_LIBCALL
12738 /* The first two args must be strings, otherwise its meaningless */
12739 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12740 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012741 string_in = NULL;
12742 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 string_in = argvars[2].vval.v_string;
12744 if (type == VAR_NUMBER)
12745 string_result = NULL;
12746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012748 if (mch_libcall(argvars[0].vval.v_string,
12749 argvars[1].vval.v_string,
12750 string_in,
12751 argvars[2].vval.v_number,
12752 string_result,
12753 &nr_result) == OK
12754 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012756 }
12757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758}
12759
12760/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012761 * "libcall()" function
12762 */
12763 static void
12764f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012765 typval_T *argvars;
12766 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012767{
12768 libcall_common(argvars, rettv, VAR_STRING);
12769}
12770
12771/*
12772 * "libcallnr()" function
12773 */
12774 static void
12775f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012776 typval_T *argvars;
12777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012778{
12779 libcall_common(argvars, rettv, VAR_NUMBER);
12780}
12781
12782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 * "line(string)" function
12784 */
12785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012787 typval_T *argvars;
12788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
12790 linenr_T lnum = 0;
12791 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012792 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012794 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 if (fp != NULL)
12796 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798}
12799
12800/*
12801 * "line2byte(lnum)" function
12802 */
12803/*ARGSUSED*/
12804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *argvars;
12807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808{
12809#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811#else
12812 linenr_T lnum;
12813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12819 if (rettv->vval.v_number >= 0)
12820 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821#endif
12822}
12823
12824/*
12825 * "lispindent(lnum)" function
12826 */
12827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *argvars;
12830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
12832#ifdef FEAT_LISP
12833 pos_T pos;
12834 linenr_T lnum;
12835
12836 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12839 {
12840 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 curwin->w_cursor = pos;
12843 }
12844 else
12845#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847}
12848
12849/*
12850 * "localtime()" function
12851 */
12852/*ARGSUSED*/
12853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012855 typval_T *argvars;
12856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859}
12860
Bram Moolenaar33570922005-01-25 22:26:29 +000012861static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
12863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 typval_T *argvars;
12866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 int exact;
12868{
12869 char_u *keys;
12870 char_u *which;
12871 char_u buf[NUMBUFLEN];
12872 char_u *keys_buf = NULL;
12873 char_u *rhs;
12874 int mode;
12875 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012876 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877
12878 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->v_type = VAR_STRING;
12880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 if (*keys == NUL)
12884 return;
12885
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012886 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012888 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012889 if (argvars[2].v_type != VAR_UNKNOWN)
12890 abbr = get_tv_number(&argvars[2]);
12891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 else
12893 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012894 if (which == NULL)
12895 return;
12896
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 mode = get_map_mode(&which, 0);
12898
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012899 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012900 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 vim_free(keys_buf);
12902 if (rhs != NULL)
12903 {
12904 ga_init(&ga);
12905 ga.ga_itemsize = 1;
12906 ga.ga_growsize = 40;
12907
12908 while (*rhs != NUL)
12909 ga_concat(&ga, str2special(&rhs, FALSE));
12910
12911 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 }
12914}
12915
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012916#ifdef FEAT_FLOAT
12917/*
12918 * "log10()" function
12919 */
12920 static void
12921f_log10(argvars, rettv)
12922 typval_T *argvars;
12923 typval_T *rettv;
12924{
12925 float_T f;
12926
12927 rettv->v_type = VAR_FLOAT;
12928 if (get_float_arg(argvars, &f) == OK)
12929 rettv->vval.v_float = log10(f);
12930 else
12931 rettv->vval.v_float = 0.0;
12932}
12933#endif
12934
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012936 * "map()" function
12937 */
12938 static void
12939f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012940 typval_T *argvars;
12941 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012942{
12943 filter_map(argvars, rettv, TRUE);
12944}
12945
12946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012947 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 */
12949 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012950f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012951 typval_T *argvars;
12952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012954 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955}
12956
12957/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012958 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 */
12960 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012961f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012962 typval_T *argvars;
12963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012965 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966}
12967
Bram Moolenaar33570922005-01-25 22:26:29 +000012968static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969
12970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012972 typval_T *argvars;
12973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 int type;
12975{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012976 char_u *str = NULL;
12977 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 char_u *pat;
12979 regmatch_T regmatch;
12980 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012981 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 char_u *save_cpo;
12983 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012984 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012985 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012986 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012987 list_T *l = NULL;
12988 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012989 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012990 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991
12992 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12993 save_cpo = p_cpo;
12994 p_cpo = (char_u *)"";
12995
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012996 rettv->vval.v_number = -1;
12997 if (type == 3)
12998 {
12999 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013000 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013001 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013002 }
13003 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013005 rettv->v_type = VAR_STRING;
13006 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013009 if (argvars[0].v_type == VAR_LIST)
13010 {
13011 if ((l = argvars[0].vval.v_list) == NULL)
13012 goto theend;
13013 li = l->lv_first;
13014 }
13015 else
13016 expr = str = get_tv_string(&argvars[0]);
13017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13019 if (pat == NULL)
13020 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013021
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013022 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024 int error = FALSE;
13025
13026 start = get_tv_number_chk(&argvars[2], &error);
13027 if (error)
13028 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013029 if (l != NULL)
13030 {
13031 li = list_find(l, start);
13032 if (li == NULL)
13033 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013034 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013035 }
13036 else
13037 {
13038 if (start < 0)
13039 start = 0;
13040 if (start > (long)STRLEN(str))
13041 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013042 /* When "count" argument is there ignore matches before "start",
13043 * otherwise skip part of the string. Differs when pattern is "^"
13044 * or "\<". */
13045 if (argvars[3].v_type != VAR_UNKNOWN)
13046 startcol = start;
13047 else
13048 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013049 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013050
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013051 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013052 nth = get_tv_number_chk(&argvars[3], &error);
13053 if (error)
13054 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 }
13056
13057 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13058 if (regmatch.regprog != NULL)
13059 {
13060 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013061
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013062 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013063 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013064 if (l != NULL)
13065 {
13066 if (li == NULL)
13067 {
13068 match = FALSE;
13069 break;
13070 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013071 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013072 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013073 if (str == NULL)
13074 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013075 }
13076
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013077 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013078
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013079 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013080 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013081 if (l == NULL && !match)
13082 break;
13083
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013084 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013085 if (l != NULL)
13086 {
13087 li = li->li_next;
13088 ++idx;
13089 }
13090 else
13091 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013092#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013093 startcol = (colnr_T)(regmatch.startp[0]
13094 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013095#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013096 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013097#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013098 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013099 }
13100
13101 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013103 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013105 int i;
13106
13107 /* return list with matched string and submatches */
13108 for (i = 0; i < NSUBEXP; ++i)
13109 {
13110 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013111 {
13112 if (list_append_string(rettv->vval.v_list,
13113 (char_u *)"", 0) == FAIL)
13114 break;
13115 }
13116 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013117 regmatch.startp[i],
13118 (int)(regmatch.endp[i] - regmatch.startp[i]))
13119 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013120 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013121 }
13122 }
13123 else if (type == 2)
13124 {
13125 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013126 if (l != NULL)
13127 copy_tv(&li->li_tv, rettv);
13128 else
13129 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013131 }
13132 else if (l != NULL)
13133 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 else
13135 {
13136 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 (varnumber_T)(regmatch.startp[0] - str);
13139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013142 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 }
13144 }
13145 vim_free(regmatch.regprog);
13146 }
13147
13148theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013149 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 p_cpo = save_cpo;
13151}
13152
13153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013154 * "match()" function
13155 */
13156 static void
13157f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013158 typval_T *argvars;
13159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013160{
13161 find_some_match(argvars, rettv, 1);
13162}
13163
13164/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013165 * "matchadd()" function
13166 */
13167 static void
13168f_matchadd(argvars, rettv)
13169 typval_T *argvars;
13170 typval_T *rettv;
13171{
13172#ifdef FEAT_SEARCH_EXTRA
13173 char_u buf[NUMBUFLEN];
13174 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13175 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13176 int prio = 10; /* default priority */
13177 int id = -1;
13178 int error = FALSE;
13179
13180 rettv->vval.v_number = -1;
13181
13182 if (grp == NULL || pat == NULL)
13183 return;
13184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013185 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013186 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013187 if (argvars[3].v_type != VAR_UNKNOWN)
13188 id = get_tv_number_chk(&argvars[3], &error);
13189 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013190 if (error == TRUE)
13191 return;
13192 if (id >= 1 && id <= 3)
13193 {
13194 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13195 return;
13196 }
13197
13198 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13199#endif
13200}
13201
13202/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013203 * "matcharg()" function
13204 */
13205 static void
13206f_matcharg(argvars, rettv)
13207 typval_T *argvars;
13208 typval_T *rettv;
13209{
13210 if (rettv_list_alloc(rettv) == OK)
13211 {
13212#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013213 int id = get_tv_number(&argvars[0]);
13214 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013215
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013216 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013217 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013218 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13219 {
13220 list_append_string(rettv->vval.v_list,
13221 syn_id2name(m->hlg_id), -1);
13222 list_append_string(rettv->vval.v_list, m->pattern, -1);
13223 }
13224 else
13225 {
13226 list_append_string(rettv->vval.v_list, NUL, -1);
13227 list_append_string(rettv->vval.v_list, NUL, -1);
13228 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013229 }
13230#endif
13231 }
13232}
13233
13234/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013235 * "matchdelete()" function
13236 */
13237 static void
13238f_matchdelete(argvars, rettv)
13239 typval_T *argvars;
13240 typval_T *rettv;
13241{
13242#ifdef FEAT_SEARCH_EXTRA
13243 rettv->vval.v_number = match_delete(curwin,
13244 (int)get_tv_number(&argvars[0]), TRUE);
13245#endif
13246}
13247
13248/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013249 * "matchend()" function
13250 */
13251 static void
13252f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013253 typval_T *argvars;
13254 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255{
13256 find_some_match(argvars, rettv, 0);
13257}
13258
13259/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013260 * "matchlist()" function
13261 */
13262 static void
13263f_matchlist(argvars, rettv)
13264 typval_T *argvars;
13265 typval_T *rettv;
13266{
13267 find_some_match(argvars, rettv, 3);
13268}
13269
13270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013271 * "matchstr()" function
13272 */
13273 static void
13274f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013275 typval_T *argvars;
13276 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013277{
13278 find_some_match(argvars, rettv, 2);
13279}
13280
Bram Moolenaar33570922005-01-25 22:26:29 +000013281static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013282
13283 static void
13284max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 typval_T *argvars;
13286 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013287 int domax;
13288{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013289 long n = 0;
13290 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013292
13293 if (argvars[0].v_type == VAR_LIST)
13294 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013295 list_T *l;
13296 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013297
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013298 l = argvars[0].vval.v_list;
13299 if (l != NULL)
13300 {
13301 li = l->lv_first;
13302 if (li != NULL)
13303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013305 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013306 {
13307 li = li->li_next;
13308 if (li == NULL)
13309 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013310 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013311 if (domax ? i > n : i < n)
13312 n = i;
13313 }
13314 }
13315 }
13316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013317 else if (argvars[0].v_type == VAR_DICT)
13318 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013319 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013320 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013322 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013323
13324 d = argvars[0].vval.v_dict;
13325 if (d != NULL)
13326 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013327 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013328 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013330 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013332 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013333 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013334 if (first)
13335 {
13336 n = i;
13337 first = FALSE;
13338 }
13339 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013340 n = i;
13341 }
13342 }
13343 }
13344 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013345 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013346 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013347 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013348}
13349
13350/*
13351 * "max()" function
13352 */
13353 static void
13354f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013355 typval_T *argvars;
13356 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013357{
13358 max_min(argvars, rettv, TRUE);
13359}
13360
13361/*
13362 * "min()" function
13363 */
13364 static void
13365f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 typval_T *argvars;
13367 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013368{
13369 max_min(argvars, rettv, FALSE);
13370}
13371
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013372static int mkdir_recurse __ARGS((char_u *dir, int prot));
13373
13374/*
13375 * Create the directory in which "dir" is located, and higher levels when
13376 * needed.
13377 */
13378 static int
13379mkdir_recurse(dir, prot)
13380 char_u *dir;
13381 int prot;
13382{
13383 char_u *p;
13384 char_u *updir;
13385 int r = FAIL;
13386
13387 /* Get end of directory name in "dir".
13388 * We're done when it's "/" or "c:/". */
13389 p = gettail_sep(dir);
13390 if (p <= get_past_head(dir))
13391 return OK;
13392
13393 /* If the directory exists we're done. Otherwise: create it.*/
13394 updir = vim_strnsave(dir, (int)(p - dir));
13395 if (updir == NULL)
13396 return FAIL;
13397 if (mch_isdir(updir))
13398 r = OK;
13399 else if (mkdir_recurse(updir, prot) == OK)
13400 r = vim_mkdir_emsg(updir, prot);
13401 vim_free(updir);
13402 return r;
13403}
13404
13405#ifdef vim_mkdir
13406/*
13407 * "mkdir()" function
13408 */
13409 static void
13410f_mkdir(argvars, rettv)
13411 typval_T *argvars;
13412 typval_T *rettv;
13413{
13414 char_u *dir;
13415 char_u buf[NUMBUFLEN];
13416 int prot = 0755;
13417
13418 rettv->vval.v_number = FAIL;
13419 if (check_restricted() || check_secure())
13420 return;
13421
13422 dir = get_tv_string_buf(&argvars[0], buf);
13423 if (argvars[1].v_type != VAR_UNKNOWN)
13424 {
13425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013426 prot = get_tv_number_chk(&argvars[2], NULL);
13427 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013428 mkdir_recurse(dir, prot);
13429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013431}
13432#endif
13433
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 * "mode()" function
13436 */
13437/*ARGSUSED*/
13438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013440 typval_T *argvars;
13441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013443 char_u buf[3];
13444
13445 buf[1] = NUL;
13446 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447
13448#ifdef FEAT_VISUAL
13449 if (VIsual_active)
13450 {
13451 if (VIsual_select)
13452 buf[0] = VIsual_mode + 's' - 'v';
13453 else
13454 buf[0] = VIsual_mode;
13455 }
13456 else
13457#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013458 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13459 || State == CONFIRM)
13460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013462 if (State == ASKMORE)
13463 buf[1] = 'm';
13464 else if (State == CONFIRM)
13465 buf[1] = '?';
13466 }
13467 else if (State == EXTERNCMD)
13468 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 else if (State & INSERT)
13470 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013471#ifdef FEAT_VREPLACE
13472 if (State & VREPLACE_FLAG)
13473 {
13474 buf[0] = 'R';
13475 buf[1] = 'v';
13476 }
13477 else
13478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 if (State & REPLACE_FLAG)
13480 buf[0] = 'R';
13481 else
13482 buf[0] = 'i';
13483 }
13484 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013487 if (exmode_active)
13488 buf[1] = 'v';
13489 }
13490 else if (exmode_active)
13491 {
13492 buf[0] = 'c';
13493 buf[1] = 'e';
13494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013498 if (finish_op)
13499 buf[1] = 'o';
13500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013502 /* Clear out the minor mode when the argument is not a non-zero number or
13503 * non-empty string. */
13504 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013505 buf[1] = NUL;
13506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507 rettv->vval.v_string = vim_strsave(buf);
13508 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509}
13510
13511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013512 * "nextnonblank()" function
13513 */
13514 static void
13515f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013516 typval_T *argvars;
13517 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518{
13519 linenr_T lnum;
13520
13521 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013523 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013524 {
13525 lnum = 0;
13526 break;
13527 }
13528 if (*skipwhite(ml_get(lnum)) != NUL)
13529 break;
13530 }
13531 rettv->vval.v_number = lnum;
13532}
13533
13534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 * "nr2char()" function
13536 */
13537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013539 typval_T *argvars;
13540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541{
13542 char_u buf[NUMBUFLEN];
13543
13544#ifdef FEAT_MBYTE
13545 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 else
13548#endif
13549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 buf[1] = NUL;
13552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->v_type = VAR_STRING;
13554 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013555}
13556
13557/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013558 * "pathshorten()" function
13559 */
13560 static void
13561f_pathshorten(argvars, rettv)
13562 typval_T *argvars;
13563 typval_T *rettv;
13564{
13565 char_u *p;
13566
13567 rettv->v_type = VAR_STRING;
13568 p = get_tv_string_chk(&argvars[0]);
13569 if (p == NULL)
13570 rettv->vval.v_string = NULL;
13571 else
13572 {
13573 p = vim_strsave(p);
13574 rettv->vval.v_string = p;
13575 if (p != NULL)
13576 shorten_dir(p);
13577 }
13578}
13579
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013580#ifdef FEAT_FLOAT
13581/*
13582 * "pow()" function
13583 */
13584 static void
13585f_pow(argvars, rettv)
13586 typval_T *argvars;
13587 typval_T *rettv;
13588{
13589 float_T fx, fy;
13590
13591 rettv->v_type = VAR_FLOAT;
13592 if (get_float_arg(argvars, &fx) == OK
13593 && get_float_arg(&argvars[1], &fy) == OK)
13594 rettv->vval.v_float = pow(fx, fy);
13595 else
13596 rettv->vval.v_float = 0.0;
13597}
13598#endif
13599
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601 * "prevnonblank()" function
13602 */
13603 static void
13604f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013605 typval_T *argvars;
13606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013607{
13608 linenr_T lnum;
13609
13610 lnum = get_tv_lnum(argvars);
13611 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13612 lnum = 0;
13613 else
13614 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13615 --lnum;
13616 rettv->vval.v_number = lnum;
13617}
13618
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013619#ifdef HAVE_STDARG_H
13620/* This dummy va_list is here because:
13621 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13622 * - locally in the function results in a "used before set" warning
13623 * - using va_start() to initialize it gives "function with fixed args" error */
13624static va_list ap;
13625#endif
13626
Bram Moolenaar8c711452005-01-14 21:53:12 +000013627/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013628 * "printf()" function
13629 */
13630 static void
13631f_printf(argvars, rettv)
13632 typval_T *argvars;
13633 typval_T *rettv;
13634{
13635 rettv->v_type = VAR_STRING;
13636 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013637#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013638 {
13639 char_u buf[NUMBUFLEN];
13640 int len;
13641 char_u *s;
13642 int saved_did_emsg = did_emsg;
13643 char *fmt;
13644
13645 /* Get the required length, allocate the buffer and do it for real. */
13646 did_emsg = FALSE;
13647 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013648 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013649 if (!did_emsg)
13650 {
13651 s = alloc(len + 1);
13652 if (s != NULL)
13653 {
13654 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013655 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013656 }
13657 }
13658 did_emsg |= saved_did_emsg;
13659 }
13660#endif
13661}
13662
13663/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013664 * "pumvisible()" function
13665 */
13666/*ARGSUSED*/
13667 static void
13668f_pumvisible(argvars, rettv)
13669 typval_T *argvars;
13670 typval_T *rettv;
13671{
13672 rettv->vval.v_number = 0;
13673#ifdef FEAT_INS_EXPAND
13674 if (pum_visible())
13675 rettv->vval.v_number = 1;
13676#endif
13677}
13678
13679/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013680 * "range()" function
13681 */
13682 static void
13683f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 typval_T *argvars;
13685 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013686{
13687 long start;
13688 long end;
13689 long stride = 1;
13690 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013694 if (argvars[1].v_type == VAR_UNKNOWN)
13695 {
13696 end = start - 1;
13697 start = 0;
13698 }
13699 else
13700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013704 }
13705
13706 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 if (error)
13708 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013709 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013710 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013711 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013712 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013713 else
13714 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013715 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013716 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013717 if (list_append_number(rettv->vval.v_list,
13718 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013719 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013720 }
13721}
13722
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013723/*
13724 * "readfile()" function
13725 */
13726 static void
13727f_readfile(argvars, rettv)
13728 typval_T *argvars;
13729 typval_T *rettv;
13730{
13731 int binary = FALSE;
13732 char_u *fname;
13733 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013734 listitem_T *li;
13735#define FREAD_SIZE 200 /* optimized for text lines */
13736 char_u buf[FREAD_SIZE];
13737 int readlen; /* size of last fread() */
13738 int buflen; /* nr of valid chars in buf[] */
13739 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13740 int tolist; /* first byte in buf[] still to be put in list */
13741 int chop; /* how many CR to chop off */
13742 char_u *prev = NULL; /* previously read bytes, if any */
13743 int prevlen = 0; /* length of "prev" if not NULL */
13744 char_u *s;
13745 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013746 long maxline = MAXLNUM;
13747 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013748
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013749 if (argvars[1].v_type != VAR_UNKNOWN)
13750 {
13751 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13752 binary = TRUE;
13753 if (argvars[2].v_type != VAR_UNKNOWN)
13754 maxline = get_tv_number(&argvars[2]);
13755 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013756
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013757 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013758 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013759
13760 /* Always open the file in binary mode, library functions have a mind of
13761 * their own about CR-LF conversion. */
13762 fname = get_tv_string(&argvars[0]);
13763 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13764 {
13765 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13766 return;
13767 }
13768
13769 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013770 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013771 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013772 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013773 buflen = filtd + readlen;
13774 tolist = 0;
13775 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13776 {
13777 if (buf[filtd] == '\n' || readlen <= 0)
13778 {
13779 /* Only when in binary mode add an empty list item when the
13780 * last line ends in a '\n'. */
13781 if (!binary && readlen == 0 && filtd == 0)
13782 break;
13783
13784 /* Found end-of-line or end-of-file: add a text line to the
13785 * list. */
13786 chop = 0;
13787 if (!binary)
13788 while (filtd - chop - 1 >= tolist
13789 && buf[filtd - chop - 1] == '\r')
13790 ++chop;
13791 len = filtd - tolist - chop;
13792 if (prev == NULL)
13793 s = vim_strnsave(buf + tolist, len);
13794 else
13795 {
13796 s = alloc((unsigned)(prevlen + len + 1));
13797 if (s != NULL)
13798 {
13799 mch_memmove(s, prev, prevlen);
13800 vim_free(prev);
13801 prev = NULL;
13802 mch_memmove(s + prevlen, buf + tolist, len);
13803 s[prevlen + len] = NUL;
13804 }
13805 }
13806 tolist = filtd + 1;
13807
13808 li = listitem_alloc();
13809 if (li == NULL)
13810 {
13811 vim_free(s);
13812 break;
13813 }
13814 li->li_tv.v_type = VAR_STRING;
13815 li->li_tv.v_lock = 0;
13816 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013817 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013818
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013819 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013820 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013821 if (readlen <= 0)
13822 break;
13823 }
13824 else if (buf[filtd] == NUL)
13825 buf[filtd] = '\n';
13826 }
13827 if (readlen <= 0)
13828 break;
13829
13830 if (tolist == 0)
13831 {
13832 /* "buf" is full, need to move text to an allocated buffer */
13833 if (prev == NULL)
13834 {
13835 prev = vim_strnsave(buf, buflen);
13836 prevlen = buflen;
13837 }
13838 else
13839 {
13840 s = alloc((unsigned)(prevlen + buflen));
13841 if (s != NULL)
13842 {
13843 mch_memmove(s, prev, prevlen);
13844 mch_memmove(s + prevlen, buf, buflen);
13845 vim_free(prev);
13846 prev = s;
13847 prevlen += buflen;
13848 }
13849 }
13850 filtd = 0;
13851 }
13852 else
13853 {
13854 mch_memmove(buf, buf + tolist, buflen - tolist);
13855 filtd -= tolist;
13856 }
13857 }
13858
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013859 /*
13860 * For a negative line count use only the lines at the end of the file,
13861 * free the rest.
13862 */
13863 if (maxline < 0)
13864 while (cnt > -maxline)
13865 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013866 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013867 --cnt;
13868 }
13869
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013870 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013871 fclose(fd);
13872}
13873
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013874#if defined(FEAT_RELTIME)
13875static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13876
13877/*
13878 * Convert a List to proftime_T.
13879 * Return FAIL when there is something wrong.
13880 */
13881 static int
13882list2proftime(arg, tm)
13883 typval_T *arg;
13884 proftime_T *tm;
13885{
13886 long n1, n2;
13887 int error = FALSE;
13888
13889 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13890 || arg->vval.v_list->lv_len != 2)
13891 return FAIL;
13892 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13893 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13894# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013895 tm->HighPart = n1;
13896 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013897# else
13898 tm->tv_sec = n1;
13899 tm->tv_usec = n2;
13900# endif
13901 return error ? FAIL : OK;
13902}
13903#endif /* FEAT_RELTIME */
13904
13905/*
13906 * "reltime()" function
13907 */
13908 static void
13909f_reltime(argvars, rettv)
13910 typval_T *argvars;
13911 typval_T *rettv;
13912{
13913#ifdef FEAT_RELTIME
13914 proftime_T res;
13915 proftime_T start;
13916
13917 if (argvars[0].v_type == VAR_UNKNOWN)
13918 {
13919 /* No arguments: get current time. */
13920 profile_start(&res);
13921 }
13922 else if (argvars[1].v_type == VAR_UNKNOWN)
13923 {
13924 if (list2proftime(&argvars[0], &res) == FAIL)
13925 return;
13926 profile_end(&res);
13927 }
13928 else
13929 {
13930 /* Two arguments: compute the difference. */
13931 if (list2proftime(&argvars[0], &start) == FAIL
13932 || list2proftime(&argvars[1], &res) == FAIL)
13933 return;
13934 profile_sub(&res, &start);
13935 }
13936
13937 if (rettv_list_alloc(rettv) == OK)
13938 {
13939 long n1, n2;
13940
13941# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013942 n1 = res.HighPart;
13943 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013944# else
13945 n1 = res.tv_sec;
13946 n2 = res.tv_usec;
13947# endif
13948 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13949 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13950 }
13951#endif
13952}
13953
13954/*
13955 * "reltimestr()" function
13956 */
13957 static void
13958f_reltimestr(argvars, rettv)
13959 typval_T *argvars;
13960 typval_T *rettv;
13961{
13962#ifdef FEAT_RELTIME
13963 proftime_T tm;
13964#endif
13965
13966 rettv->v_type = VAR_STRING;
13967 rettv->vval.v_string = NULL;
13968#ifdef FEAT_RELTIME
13969 if (list2proftime(&argvars[0], &tm) == OK)
13970 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13971#endif
13972}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013973
Bram Moolenaar0d660222005-01-07 21:51:51 +000013974#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13975static void make_connection __ARGS((void));
13976static int check_connection __ARGS((void));
13977
13978 static void
13979make_connection()
13980{
13981 if (X_DISPLAY == NULL
13982# ifdef FEAT_GUI
13983 && !gui.in_use
13984# endif
13985 )
13986 {
13987 x_force_connect = TRUE;
13988 setup_term_clip();
13989 x_force_connect = FALSE;
13990 }
13991}
13992
13993 static int
13994check_connection()
13995{
13996 make_connection();
13997 if (X_DISPLAY == NULL)
13998 {
13999 EMSG(_("E240: No connection to Vim server"));
14000 return FAIL;
14001 }
14002 return OK;
14003}
14004#endif
14005
14006#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014007static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014008
14009 static void
14010remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014011 typval_T *argvars;
14012 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013 int expr;
14014{
14015 char_u *server_name;
14016 char_u *keys;
14017 char_u *r = NULL;
14018 char_u buf[NUMBUFLEN];
14019# ifdef WIN32
14020 HWND w;
14021# else
14022 Window w;
14023# endif
14024
14025 if (check_restricted() || check_secure())
14026 return;
14027
14028# ifdef FEAT_X11
14029 if (check_connection() == FAIL)
14030 return;
14031# endif
14032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 server_name = get_tv_string_chk(&argvars[0]);
14034 if (server_name == NULL)
14035 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014036 keys = get_tv_string_buf(&argvars[1], buf);
14037# ifdef WIN32
14038 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14039# else
14040 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14041 < 0)
14042# endif
14043 {
14044 if (r != NULL)
14045 EMSG(r); /* sending worked but evaluation failed */
14046 else
14047 EMSG2(_("E241: Unable to send to %s"), server_name);
14048 return;
14049 }
14050
14051 rettv->vval.v_string = r;
14052
14053 if (argvars[2].v_type != VAR_UNKNOWN)
14054 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014056 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014059 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014060 v.di_tv.v_type = VAR_STRING;
14061 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062 idvar = get_tv_string_chk(&argvars[2]);
14063 if (idvar != NULL)
14064 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014065 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 }
14067}
14068#endif
14069
14070/*
14071 * "remote_expr()" function
14072 */
14073/*ARGSUSED*/
14074 static void
14075f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 typval_T *argvars;
14077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078{
14079 rettv->v_type = VAR_STRING;
14080 rettv->vval.v_string = NULL;
14081#ifdef FEAT_CLIENTSERVER
14082 remote_common(argvars, rettv, TRUE);
14083#endif
14084}
14085
14086/*
14087 * "remote_foreground()" function
14088 */
14089/*ARGSUSED*/
14090 static void
14091f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014092 typval_T *argvars;
14093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094{
14095 rettv->vval.v_number = 0;
14096#ifdef FEAT_CLIENTSERVER
14097# ifdef WIN32
14098 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099 {
14100 char_u *server_name = get_tv_string_chk(&argvars[0]);
14101
14102 if (server_name != NULL)
14103 serverForeground(server_name);
14104 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014105# else
14106 /* Send a foreground() expression to the server. */
14107 argvars[1].v_type = VAR_STRING;
14108 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14109 argvars[2].v_type = VAR_UNKNOWN;
14110 remote_common(argvars, rettv, TRUE);
14111 vim_free(argvars[1].vval.v_string);
14112# endif
14113#endif
14114}
14115
14116/*ARGSUSED*/
14117 static void
14118f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014119 typval_T *argvars;
14120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121{
14122#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014123 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014124 char_u *s = NULL;
14125# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014126 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014127# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129
14130 if (check_restricted() || check_secure())
14131 {
14132 rettv->vval.v_number = -1;
14133 return;
14134 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 serverid = get_tv_string_chk(&argvars[0]);
14136 if (serverid == NULL)
14137 {
14138 rettv->vval.v_number = -1;
14139 return; /* type error; errmsg already given */
14140 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014141# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014142 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014143 if (n == 0)
14144 rettv->vval.v_number = -1;
14145 else
14146 {
14147 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14148 rettv->vval.v_number = (s != NULL);
14149 }
14150# else
14151 rettv->vval.v_number = 0;
14152 if (check_connection() == FAIL)
14153 return;
14154
14155 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014157# endif
14158
14159 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014161 char_u *retvar;
14162
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 v.di_tv.v_type = VAR_STRING;
14164 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 retvar = get_tv_string_chk(&argvars[1]);
14166 if (retvar != NULL)
14167 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014168 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014169 }
14170#else
14171 rettv->vval.v_number = -1;
14172#endif
14173}
14174
14175/*ARGSUSED*/
14176 static void
14177f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014178 typval_T *argvars;
14179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014180{
14181 char_u *r = NULL;
14182
14183#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 char_u *serverid = get_tv_string_chk(&argvars[0]);
14185
14186 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014187 {
14188# ifdef WIN32
14189 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014190 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014191
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014192 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193 if (n != 0)
14194 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14195 if (r == NULL)
14196# else
14197 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014199# endif
14200 EMSG(_("E277: Unable to read a server reply"));
14201 }
14202#endif
14203 rettv->v_type = VAR_STRING;
14204 rettv->vval.v_string = r;
14205}
14206
14207/*
14208 * "remote_send()" function
14209 */
14210/*ARGSUSED*/
14211 static void
14212f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 typval_T *argvars;
14214 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014215{
14216 rettv->v_type = VAR_STRING;
14217 rettv->vval.v_string = NULL;
14218#ifdef FEAT_CLIENTSERVER
14219 remote_common(argvars, rettv, FALSE);
14220#endif
14221}
14222
14223/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014224 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014225 */
14226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014227f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014228 typval_T *argvars;
14229 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014230{
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 list_T *l;
14232 listitem_T *item, *item2;
14233 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014234 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014235 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014237 dict_T *d;
14238 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014239
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014240 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014241 if (argvars[0].v_type == VAR_DICT)
14242 {
14243 if (argvars[2].v_type != VAR_UNKNOWN)
14244 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014245 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014246 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 key = get_tv_string_chk(&argvars[1]);
14249 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 di = dict_find(d, key, -1);
14252 if (di == NULL)
14253 EMSG2(_(e_dictkey), key);
14254 else
14255 {
14256 *rettv = di->di_tv;
14257 init_tv(&di->di_tv);
14258 dictitem_remove(d, di);
14259 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014260 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014261 }
14262 }
14263 else if (argvars[0].v_type != VAR_LIST)
14264 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014265 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014266 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 int error = FALSE;
14269
14270 idx = get_tv_number_chk(&argvars[1], &error);
14271 if (error)
14272 ; /* type error: do nothing, errmsg already given */
14273 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014274 EMSGN(_(e_listidx), idx);
14275 else
14276 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014277 if (argvars[2].v_type == VAR_UNKNOWN)
14278 {
14279 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014280 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014281 *rettv = item->li_tv;
14282 vim_free(item);
14283 }
14284 else
14285 {
14286 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 end = get_tv_number_chk(&argvars[2], &error);
14288 if (error)
14289 ; /* type error: do nothing */
14290 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014291 EMSGN(_(e_listidx), end);
14292 else
14293 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014294 int cnt = 0;
14295
14296 for (li = item; li != NULL; li = li->li_next)
14297 {
14298 ++cnt;
14299 if (li == item2)
14300 break;
14301 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014302 if (li == NULL) /* didn't find "item2" after "item" */
14303 EMSG(_(e_invrange));
14304 else
14305 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014306 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014307 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014308 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014309 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014310 l->lv_first = item;
14311 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014312 item->li_prev = NULL;
14313 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014314 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014315 }
14316 }
14317 }
14318 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014319 }
14320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321}
14322
14323/*
14324 * "rename({from}, {to})" function
14325 */
14326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *argvars;
14329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330{
14331 char_u buf[NUMBUFLEN];
14332
14333 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14337 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338}
14339
14340/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014341 * "repeat()" function
14342 */
14343/*ARGSUSED*/
14344 static void
14345f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014346 typval_T *argvars;
14347 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014348{
14349 char_u *p;
14350 int n;
14351 int slen;
14352 int len;
14353 char_u *r;
14354 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014355
14356 n = get_tv_number(&argvars[1]);
14357 if (argvars[0].v_type == VAR_LIST)
14358 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014359 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014360 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014361 if (list_extend(rettv->vval.v_list,
14362 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014363 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014364 }
14365 else
14366 {
14367 p = get_tv_string(&argvars[0]);
14368 rettv->v_type = VAR_STRING;
14369 rettv->vval.v_string = NULL;
14370
14371 slen = (int)STRLEN(p);
14372 len = slen * n;
14373 if (len <= 0)
14374 return;
14375
14376 r = alloc(len + 1);
14377 if (r != NULL)
14378 {
14379 for (i = 0; i < n; i++)
14380 mch_memmove(r + i * slen, p, (size_t)slen);
14381 r[len] = NUL;
14382 }
14383
14384 rettv->vval.v_string = r;
14385 }
14386}
14387
14388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 * "resolve()" function
14390 */
14391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395{
14396 char_u *p;
14397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399#ifdef FEAT_SHORTCUT
14400 {
14401 char_u *v = NULL;
14402
14403 v = mch_resolve_shortcut(p);
14404 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 }
14409#else
14410# ifdef HAVE_READLINK
14411 {
14412 char_u buf[MAXPATHL + 1];
14413 char_u *cpy;
14414 int len;
14415 char_u *remain = NULL;
14416 char_u *q;
14417 int is_relative_to_current = FALSE;
14418 int has_trailing_pathsep = FALSE;
14419 int limit = 100;
14420
14421 p = vim_strsave(p);
14422
14423 if (p[0] == '.' && (vim_ispathsep(p[1])
14424 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14425 is_relative_to_current = TRUE;
14426
14427 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014428 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 has_trailing_pathsep = TRUE;
14430
14431 q = getnextcomp(p);
14432 if (*q != NUL)
14433 {
14434 /* Separate the first path component in "p", and keep the
14435 * remainder (beginning with the path separator). */
14436 remain = vim_strsave(q - 1);
14437 q[-1] = NUL;
14438 }
14439
14440 for (;;)
14441 {
14442 for (;;)
14443 {
14444 len = readlink((char *)p, (char *)buf, MAXPATHL);
14445 if (len <= 0)
14446 break;
14447 buf[len] = NUL;
14448
14449 if (limit-- == 0)
14450 {
14451 vim_free(p);
14452 vim_free(remain);
14453 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 goto fail;
14456 }
14457
14458 /* Ensure that the result will have a trailing path separator
14459 * if the argument has one. */
14460 if (remain == NULL && has_trailing_pathsep)
14461 add_pathsep(buf);
14462
14463 /* Separate the first path component in the link value and
14464 * concatenate the remainders. */
14465 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14466 if (*q != NUL)
14467 {
14468 if (remain == NULL)
14469 remain = vim_strsave(q - 1);
14470 else
14471 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014472 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 if (cpy != NULL)
14474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 vim_free(remain);
14476 remain = cpy;
14477 }
14478 }
14479 q[-1] = NUL;
14480 }
14481
14482 q = gettail(p);
14483 if (q > p && *q == NUL)
14484 {
14485 /* Ignore trailing path separator. */
14486 q[-1] = NUL;
14487 q = gettail(p);
14488 }
14489 if (q > p && !mch_isFullName(buf))
14490 {
14491 /* symlink is relative to directory of argument */
14492 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14493 if (cpy != NULL)
14494 {
14495 STRCPY(cpy, p);
14496 STRCPY(gettail(cpy), buf);
14497 vim_free(p);
14498 p = cpy;
14499 }
14500 }
14501 else
14502 {
14503 vim_free(p);
14504 p = vim_strsave(buf);
14505 }
14506 }
14507
14508 if (remain == NULL)
14509 break;
14510
14511 /* Append the first path component of "remain" to "p". */
14512 q = getnextcomp(remain + 1);
14513 len = q - remain - (*q != NUL);
14514 cpy = vim_strnsave(p, STRLEN(p) + len);
14515 if (cpy != NULL)
14516 {
14517 STRNCAT(cpy, remain, len);
14518 vim_free(p);
14519 p = cpy;
14520 }
14521 /* Shorten "remain". */
14522 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014523 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 else
14525 {
14526 vim_free(remain);
14527 remain = NULL;
14528 }
14529 }
14530
14531 /* If the result is a relative path name, make it explicitly relative to
14532 * the current directory if and only if the argument had this form. */
14533 if (!vim_ispathsep(*p))
14534 {
14535 if (is_relative_to_current
14536 && *p != NUL
14537 && !(p[0] == '.'
14538 && (p[1] == NUL
14539 || vim_ispathsep(p[1])
14540 || (p[1] == '.'
14541 && (p[2] == NUL
14542 || vim_ispathsep(p[2]))))))
14543 {
14544 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014545 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 if (cpy != NULL)
14547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 vim_free(p);
14549 p = cpy;
14550 }
14551 }
14552 else if (!is_relative_to_current)
14553 {
14554 /* Strip leading "./". */
14555 q = p;
14556 while (q[0] == '.' && vim_ispathsep(q[1]))
14557 q += 2;
14558 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014559 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 }
14561 }
14562
14563 /* Ensure that the result will have no trailing path separator
14564 * if the argument had none. But keep "/" or "//". */
14565 if (!has_trailing_pathsep)
14566 {
14567 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014568 if (after_pathsep(p, q))
14569 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 }
14571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 }
14574# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014575 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576# endif
14577#endif
14578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014579 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580
14581#ifdef HAVE_READLINK
14582fail:
14583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585}
14586
14587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 */
14590 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 typval_T *argvars;
14593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594{
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 list_T *l;
14596 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597
Bram Moolenaar0d660222005-01-07 21:51:51 +000014598 rettv->vval.v_number = 0;
14599 if (argvars[0].v_type != VAR_LIST)
14600 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014601 else if ((l = argvars[0].vval.v_list) != NULL
14602 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603 {
14604 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014605 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014606 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607 while (li != NULL)
14608 {
14609 ni = li->li_prev;
14610 list_append(l, li);
14611 li = ni;
14612 }
14613 rettv->vval.v_list = l;
14614 rettv->v_type = VAR_LIST;
14615 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014616 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618}
14619
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014620#define SP_NOMOVE 0x01 /* don't move cursor */
14621#define SP_REPEAT 0x02 /* repeat to find outer pair */
14622#define SP_RETCOUNT 0x04 /* return matchcount */
14623#define SP_SETPCMARK 0x08 /* set previous context mark */
14624#define SP_START 0x10 /* accept match at start position */
14625#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14626#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014627
Bram Moolenaar33570922005-01-25 22:26:29 +000014628static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629
14630/*
14631 * Get flags for a search function.
14632 * Possibly sets "p_ws".
14633 * Returns BACKWARD, FORWARD or zero (for an error).
14634 */
14635 static int
14636get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014637 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 int *flagsp;
14639{
14640 int dir = FORWARD;
14641 char_u *flags;
14642 char_u nbuf[NUMBUFLEN];
14643 int mask;
14644
14645 if (varp->v_type != VAR_UNKNOWN)
14646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014647 flags = get_tv_string_buf_chk(varp, nbuf);
14648 if (flags == NULL)
14649 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014650 while (*flags != NUL)
14651 {
14652 switch (*flags)
14653 {
14654 case 'b': dir = BACKWARD; break;
14655 case 'w': p_ws = TRUE; break;
14656 case 'W': p_ws = FALSE; break;
14657 default: mask = 0;
14658 if (flagsp != NULL)
14659 switch (*flags)
14660 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014661 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014662 case 'e': mask = SP_END; break;
14663 case 'm': mask = SP_RETCOUNT; break;
14664 case 'n': mask = SP_NOMOVE; break;
14665 case 'p': mask = SP_SUBPAT; break;
14666 case 'r': mask = SP_REPEAT; break;
14667 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668 }
14669 if (mask == 0)
14670 {
14671 EMSG2(_(e_invarg2), flags);
14672 dir = 0;
14673 }
14674 else
14675 *flagsp |= mask;
14676 }
14677 if (dir == 0)
14678 break;
14679 ++flags;
14680 }
14681 }
14682 return dir;
14683}
14684
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014686 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014688 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014689search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014691 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014692 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014694 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 char_u *pat;
14696 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014697 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 int save_p_ws = p_ws;
14699 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014700 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014701 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014702 proftime_T tm;
14703#ifdef FEAT_RELTIME
14704 long time_limit = 0;
14705#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014706 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014707 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014709 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014710 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014711 if (dir == 0)
14712 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014713 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014714 if (flags & SP_START)
14715 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014716 if (flags & SP_END)
14717 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014718
Bram Moolenaar76929292008-01-06 19:07:36 +000014719 /* Optional arguments: line number to stop searching and timeout. */
14720 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014721 {
14722 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14723 if (lnum_stop < 0)
14724 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014725#ifdef FEAT_RELTIME
14726 if (argvars[3].v_type != VAR_UNKNOWN)
14727 {
14728 time_limit = get_tv_number_chk(&argvars[3], NULL);
14729 if (time_limit < 0)
14730 goto theend;
14731 }
14732#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014733 }
14734
Bram Moolenaar76929292008-01-06 19:07:36 +000014735#ifdef FEAT_RELTIME
14736 /* Set the time limit, if there is one. */
14737 profile_setlimit(time_limit, &tm);
14738#endif
14739
Bram Moolenaar231334e2005-07-25 20:46:57 +000014740 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014741 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014742 * Check to make sure only those flags are set.
14743 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14744 * flags cannot be set. Check for that condition also.
14745 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014746 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014747 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014750 goto theend;
14751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014753 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014754 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014755 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014756 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014758 if (flags & SP_SUBPAT)
14759 retval = subpatnum;
14760 else
14761 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014762 if (flags & SP_SETPCMARK)
14763 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014765 if (match_pos != NULL)
14766 {
14767 /* Store the match cursor position */
14768 match_pos->lnum = pos.lnum;
14769 match_pos->col = pos.col + 1;
14770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 /* "/$" will put the cursor after the end of the line, may need to
14772 * correct that here */
14773 check_cursor();
14774 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014775
14776 /* If 'n' flag is used: restore cursor position. */
14777 if (flags & SP_NOMOVE)
14778 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014779 else
14780 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014781theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014783
14784 return retval;
14785}
14786
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014787#ifdef FEAT_FLOAT
14788/*
14789 * "round({float})" function
14790 */
14791 static void
14792f_round(argvars, rettv)
14793 typval_T *argvars;
14794 typval_T *rettv;
14795{
14796 float_T f;
14797
14798 rettv->v_type = VAR_FLOAT;
14799 if (get_float_arg(argvars, &f) == OK)
14800 /* round() is not in C90, use ceil() or floor() instead. */
14801 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14802 else
14803 rettv->vval.v_float = 0.0;
14804}
14805#endif
14806
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014807/*
14808 * "search()" function
14809 */
14810 static void
14811f_search(argvars, rettv)
14812 typval_T *argvars;
14813 typval_T *rettv;
14814{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014815 int flags = 0;
14816
14817 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818}
14819
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014821 * "searchdecl()" function
14822 */
14823 static void
14824f_searchdecl(argvars, rettv)
14825 typval_T *argvars;
14826 typval_T *rettv;
14827{
14828 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014829 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014830 int error = FALSE;
14831 char_u *name;
14832
14833 rettv->vval.v_number = 1; /* default: FAIL */
14834
14835 name = get_tv_string_chk(&argvars[0]);
14836 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014837 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014838 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014839 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14840 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14841 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014842 if (!error && name != NULL)
14843 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014844 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014845}
14846
14847/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014848 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014850 static int
14851searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014852 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014853 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854{
14855 char_u *spat, *mpat, *epat;
14856 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 int dir;
14859 int flags = 0;
14860 char_u nbuf1[NUMBUFLEN];
14861 char_u nbuf2[NUMBUFLEN];
14862 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014863 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014864 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014865 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 spat = get_tv_string_chk(&argvars[0]);
14869 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14870 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14871 if (spat == NULL || mpat == NULL || epat == NULL)
14872 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 /* Handle the optional fourth argument: flags */
14875 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014876 if (dir == 0)
14877 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014878
14879 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014880 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14881 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014882 if ((flags & (SP_END | SP_SUBPAT)) != 0
14883 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014884 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014885 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014886 goto theend;
14887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014889 /* Using 'r' implies 'W', otherwise it doesn't work. */
14890 if (flags & SP_REPEAT)
14891 p_ws = FALSE;
14892
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014893 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014894 if (argvars[3].v_type == VAR_UNKNOWN
14895 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 skip = (char_u *)"";
14897 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014899 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014900 if (argvars[5].v_type != VAR_UNKNOWN)
14901 {
14902 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14903 if (lnum_stop < 0)
14904 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014905#ifdef FEAT_RELTIME
14906 if (argvars[6].v_type != VAR_UNKNOWN)
14907 {
14908 time_limit = get_tv_number_chk(&argvars[6], NULL);
14909 if (time_limit < 0)
14910 goto theend;
14911 }
14912#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014913 }
14914 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014915 if (skip == NULL)
14916 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014918 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014919 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014920
14921theend:
14922 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014923
14924 return retval;
14925}
14926
14927/*
14928 * "searchpair()" function
14929 */
14930 static void
14931f_searchpair(argvars, rettv)
14932 typval_T *argvars;
14933 typval_T *rettv;
14934{
14935 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14936}
14937
14938/*
14939 * "searchpairpos()" function
14940 */
14941 static void
14942f_searchpairpos(argvars, rettv)
14943 typval_T *argvars;
14944 typval_T *rettv;
14945{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014946 pos_T match_pos;
14947 int lnum = 0;
14948 int col = 0;
14949
14950 rettv->vval.v_number = 0;
14951
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014952 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014953 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014954
14955 if (searchpair_cmn(argvars, &match_pos) > 0)
14956 {
14957 lnum = match_pos.lnum;
14958 col = match_pos.col;
14959 }
14960
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014961 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14962 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014963}
14964
14965/*
14966 * Search for a start/middle/end thing.
14967 * Used by searchpair(), see its documentation for the details.
14968 * Returns 0 or -1 for no match,
14969 */
14970 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014971do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14972 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014973 char_u *spat; /* start pattern */
14974 char_u *mpat; /* middle pattern */
14975 char_u *epat; /* end pattern */
14976 int dir; /* BACKWARD or FORWARD */
14977 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014978 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014979 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014980 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014981 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014982{
14983 char_u *save_cpo;
14984 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14985 long retval = 0;
14986 pos_T pos;
14987 pos_T firstpos;
14988 pos_T foundpos;
14989 pos_T save_cursor;
14990 pos_T save_pos;
14991 int n;
14992 int r;
14993 int nest = 1;
14994 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014995 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014996 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014997
14998 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14999 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015000 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015001
Bram Moolenaar76929292008-01-06 19:07:36 +000015002#ifdef FEAT_RELTIME
15003 /* Set the time limit, if there is one. */
15004 profile_setlimit(time_limit, &tm);
15005#endif
15006
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015007 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15008 * start/middle/end (pat3, for the top pair). */
15009 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15010 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15011 if (pat2 == NULL || pat3 == NULL)
15012 goto theend;
15013 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15014 if (*mpat == NUL)
15015 STRCPY(pat3, pat2);
15016 else
15017 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15018 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015019 if (flags & SP_START)
15020 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015021
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 save_cursor = curwin->w_cursor;
15023 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015024 clearpos(&firstpos);
15025 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026 pat = pat3;
15027 for (;;)
15028 {
15029 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015030 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15032 /* didn't find it or found the first match again: FAIL */
15033 break;
15034
15035 if (firstpos.lnum == 0)
15036 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015037 if (equalpos(pos, foundpos))
15038 {
15039 /* Found the same position again. Can happen with a pattern that
15040 * has "\zs" at the end and searching backwards. Advance one
15041 * character and try again. */
15042 if (dir == BACKWARD)
15043 decl(&pos);
15044 else
15045 incl(&pos);
15046 }
15047 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015049 /* clear the start flag to avoid getting stuck here */
15050 options &= ~SEARCH_START;
15051
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 /* If the skip pattern matches, ignore this match. */
15053 if (*skip != NUL)
15054 {
15055 save_pos = curwin->w_cursor;
15056 curwin->w_cursor = pos;
15057 r = eval_to_bool(skip, &err, NULL, FALSE);
15058 curwin->w_cursor = save_pos;
15059 if (err)
15060 {
15061 /* Evaluating {skip} caused an error, break here. */
15062 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015063 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 break;
15065 }
15066 if (r)
15067 continue;
15068 }
15069
15070 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15071 {
15072 /* Found end when searching backwards or start when searching
15073 * forward: nested pair. */
15074 ++nest;
15075 pat = pat2; /* nested, don't search for middle */
15076 }
15077 else
15078 {
15079 /* Found end when searching forward or start when searching
15080 * backward: end of (nested) pair; or found middle in outer pair. */
15081 if (--nest == 1)
15082 pat = pat3; /* outer level, search for middle */
15083 }
15084
15085 if (nest == 0)
15086 {
15087 /* Found the match: return matchcount or line number. */
15088 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015089 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015091 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015092 if (flags & SP_SETPCMARK)
15093 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094 curwin->w_cursor = pos;
15095 if (!(flags & SP_REPEAT))
15096 break;
15097 nest = 1; /* search for next unmatched */
15098 }
15099 }
15100
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015101 if (match_pos != NULL)
15102 {
15103 /* Store the match cursor position */
15104 match_pos->lnum = curwin->w_cursor.lnum;
15105 match_pos->col = curwin->w_cursor.col + 1;
15106 }
15107
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015109 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 curwin->w_cursor = save_cursor;
15111
15112theend:
15113 vim_free(pat2);
15114 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015115 if (p_cpo == empty_option)
15116 p_cpo = save_cpo;
15117 else
15118 /* Darn, evaluating the {skip} expression changed the value. */
15119 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015120
15121 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122}
15123
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015124/*
15125 * "searchpos()" function
15126 */
15127 static void
15128f_searchpos(argvars, rettv)
15129 typval_T *argvars;
15130 typval_T *rettv;
15131{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015132 pos_T match_pos;
15133 int lnum = 0;
15134 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015135 int n;
15136 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015137
15138 rettv->vval.v_number = 0;
15139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015140 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015141 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015142
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015143 n = search_cmn(argvars, &match_pos, &flags);
15144 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015145 {
15146 lnum = match_pos.lnum;
15147 col = match_pos.col;
15148 }
15149
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015150 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15151 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015152 if (flags & SP_SUBPAT)
15153 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015154}
15155
15156
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157/*ARGSUSED*/
15158 static void
15159f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015160 typval_T *argvars;
15161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163#ifdef FEAT_CLIENTSERVER
15164 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015165 char_u *server = get_tv_string_chk(&argvars[0]);
15166 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015169 if (server == NULL || reply == NULL)
15170 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 if (check_restricted() || check_secure())
15172 return;
15173# ifdef FEAT_X11
15174 if (check_connection() == FAIL)
15175 return;
15176# endif
15177
15178 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180 EMSG(_("E258: Unable to send to client"));
15181 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015183 rettv->vval.v_number = 0;
15184#else
15185 rettv->vval.v_number = -1;
15186#endif
15187}
15188
15189/*ARGSUSED*/
15190 static void
15191f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *argvars;
15193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194{
15195 char_u *r = NULL;
15196
15197#ifdef FEAT_CLIENTSERVER
15198# ifdef WIN32
15199 r = serverGetVimNames();
15200# else
15201 make_connection();
15202 if (X_DISPLAY != NULL)
15203 r = serverGetVimNames(X_DISPLAY);
15204# endif
15205#endif
15206 rettv->v_type = VAR_STRING;
15207 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208}
15209
15210/*
15211 * "setbufvar()" function
15212 */
15213/*ARGSUSED*/
15214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015215f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015216 typval_T *argvars;
15217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218{
15219 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015222 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 char_u nbuf[NUMBUFLEN];
15224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 rettv->vval.v_number = 0;
15226
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227 if (check_restricted() || check_secure())
15228 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15230 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 varp = &argvars[2];
15233
15234 if (buf != NULL && varname != NULL && varp != NULL)
15235 {
15236 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238
15239 if (*varname == '&')
15240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015241 long numval;
15242 char_u *strval;
15243 int error = FALSE;
15244
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015246 numval = get_tv_number_chk(varp, &error);
15247 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 if (!error && strval != NULL)
15249 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250 }
15251 else
15252 {
15253 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15254 if (bufvarname != NULL)
15255 {
15256 STRCPY(bufvarname, "b:");
15257 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015258 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 vim_free(bufvarname);
15260 }
15261 }
15262
15263 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266}
15267
15268/*
15269 * "setcmdpos()" function
15270 */
15271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015272f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 typval_T *argvars;
15274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015276 int pos = (int)get_tv_number(&argvars[0]) - 1;
15277
15278 if (pos >= 0)
15279 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280}
15281
15282/*
15283 * "setline()" function
15284 */
15285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015287 typval_T *argvars;
15288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289{
15290 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015291 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015292 list_T *l = NULL;
15293 listitem_T *li = NULL;
15294 long added = 0;
15295 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015297 lnum = get_tv_lnum(&argvars[0]);
15298 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015300 l = argvars[1].vval.v_list;
15301 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015303 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015305
15306 rettv->vval.v_number = 0; /* OK */
15307 for (;;)
15308 {
15309 if (l != NULL)
15310 {
15311 /* list argument, get next string */
15312 if (li == NULL)
15313 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015315 li = li->li_next;
15316 }
15317
15318 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015320 break;
15321 if (lnum <= curbuf->b_ml.ml_line_count)
15322 {
15323 /* existing line, replace it */
15324 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15325 {
15326 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015327 if (lnum == curwin->w_cursor.lnum)
15328 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015329 rettv->vval.v_number = 0; /* OK */
15330 }
15331 }
15332 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15333 {
15334 /* lnum is one past the last line, append the line */
15335 ++added;
15336 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15337 rettv->vval.v_number = 0; /* OK */
15338 }
15339
15340 if (l == NULL) /* only one string argument */
15341 break;
15342 ++lnum;
15343 }
15344
15345 if (added > 0)
15346 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347}
15348
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015349static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15350
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015352 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015353 */
15354/*ARGSUSED*/
15355 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015356set_qf_ll_list(wp, list_arg, action_arg, rettv)
15357 win_T *wp;
15358 typval_T *list_arg;
15359 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015360 typval_T *rettv;
15361{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015362#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015363 char_u *act;
15364 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015365#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015366
Bram Moolenaar2641f772005-03-25 21:58:17 +000015367 rettv->vval.v_number = -1;
15368
15369#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015370 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015371 EMSG(_(e_listreq));
15372 else
15373 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015374 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015375
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015376 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015377 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015378 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015379 if (act == NULL)
15380 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015381 if (*act == 'a' || *act == 'r')
15382 action = *act;
15383 }
15384
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015385 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015386 rettv->vval.v_number = 0;
15387 }
15388#endif
15389}
15390
15391/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015392 * "setloclist()" function
15393 */
15394/*ARGSUSED*/
15395 static void
15396f_setloclist(argvars, rettv)
15397 typval_T *argvars;
15398 typval_T *rettv;
15399{
15400 win_T *win;
15401
15402 rettv->vval.v_number = -1;
15403
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015404 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015405 if (win != NULL)
15406 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15407}
15408
15409/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015410 * "setmatches()" function
15411 */
15412 static void
15413f_setmatches(argvars, rettv)
15414 typval_T *argvars;
15415 typval_T *rettv;
15416{
15417#ifdef FEAT_SEARCH_EXTRA
15418 list_T *l;
15419 listitem_T *li;
15420 dict_T *d;
15421
15422 rettv->vval.v_number = -1;
15423 if (argvars[0].v_type != VAR_LIST)
15424 {
15425 EMSG(_(e_listreq));
15426 return;
15427 }
15428 if ((l = argvars[0].vval.v_list) != NULL)
15429 {
15430
15431 /* To some extent make sure that we are dealing with a list from
15432 * "getmatches()". */
15433 li = l->lv_first;
15434 while (li != NULL)
15435 {
15436 if (li->li_tv.v_type != VAR_DICT
15437 || (d = li->li_tv.vval.v_dict) == NULL)
15438 {
15439 EMSG(_(e_invarg));
15440 return;
15441 }
15442 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15443 && dict_find(d, (char_u *)"pattern", -1) != NULL
15444 && dict_find(d, (char_u *)"priority", -1) != NULL
15445 && dict_find(d, (char_u *)"id", -1) != NULL))
15446 {
15447 EMSG(_(e_invarg));
15448 return;
15449 }
15450 li = li->li_next;
15451 }
15452
15453 clear_matches(curwin);
15454 li = l->lv_first;
15455 while (li != NULL)
15456 {
15457 d = li->li_tv.vval.v_dict;
15458 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15459 get_dict_string(d, (char_u *)"pattern", FALSE),
15460 (int)get_dict_number(d, (char_u *)"priority"),
15461 (int)get_dict_number(d, (char_u *)"id"));
15462 li = li->li_next;
15463 }
15464 rettv->vval.v_number = 0;
15465 }
15466#endif
15467}
15468
15469/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015470 * "setpos()" function
15471 */
15472/*ARGSUSED*/
15473 static void
15474f_setpos(argvars, rettv)
15475 typval_T *argvars;
15476 typval_T *rettv;
15477{
15478 pos_T pos;
15479 int fnum;
15480 char_u *name;
15481
Bram Moolenaar08250432008-02-13 11:42:46 +000015482 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015483 name = get_tv_string_chk(argvars);
15484 if (name != NULL)
15485 {
15486 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15487 {
15488 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015489 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015490 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015491 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015492 if (fnum == curbuf->b_fnum)
15493 {
15494 curwin->w_cursor = pos;
15495 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015496 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015497 }
15498 else
15499 EMSG(_(e_invarg));
15500 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015501 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15502 {
15503 /* set mark */
15504 if (setmark_pos(name[1], &pos, fnum) == OK)
15505 rettv->vval.v_number = 0;
15506 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015507 else
15508 EMSG(_(e_invarg));
15509 }
15510 }
15511}
15512
15513/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015514 * "setqflist()" function
15515 */
15516/*ARGSUSED*/
15517 static void
15518f_setqflist(argvars, rettv)
15519 typval_T *argvars;
15520 typval_T *rettv;
15521{
15522 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15523}
15524
15525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 * "setreg()" function
15527 */
15528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 typval_T *argvars;
15531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532{
15533 int regname;
15534 char_u *strregname;
15535 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015536 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 int append;
15538 char_u yank_type;
15539 long block_len;
15540
15541 block_len = -1;
15542 yank_type = MAUTO;
15543 append = FALSE;
15544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015545 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015546 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015548 if (strregname == NULL)
15549 return; /* type error; errmsg already given */
15550 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 if (regname == 0 || regname == '@')
15552 regname = '"';
15553 else if (regname == '=')
15554 return;
15555
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015558 stropt = get_tv_string_chk(&argvars[2]);
15559 if (stropt == NULL)
15560 return; /* type error */
15561 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 switch (*stropt)
15563 {
15564 case 'a': case 'A': /* append */
15565 append = TRUE;
15566 break;
15567 case 'v': case 'c': /* character-wise selection */
15568 yank_type = MCHAR;
15569 break;
15570 case 'V': case 'l': /* line-wise selection */
15571 yank_type = MLINE;
15572 break;
15573#ifdef FEAT_VISUAL
15574 case 'b': case Ctrl_V: /* block-wise selection */
15575 yank_type = MBLOCK;
15576 if (VIM_ISDIGIT(stropt[1]))
15577 {
15578 ++stropt;
15579 block_len = getdigits(&stropt) - 1;
15580 --stropt;
15581 }
15582 break;
15583#endif
15584 }
15585 }
15586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015587 strval = get_tv_string_chk(&argvars[1]);
15588 if (strval != NULL)
15589 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592}
15593
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015594/*
15595 * "settabwinvar()" function
15596 */
15597 static void
15598f_settabwinvar(argvars, rettv)
15599 typval_T *argvars;
15600 typval_T *rettv;
15601{
15602 setwinvar(argvars, rettv, 1);
15603}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604
15605/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015606 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 typval_T *argvars;
15611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015613 setwinvar(argvars, rettv, 0);
15614}
15615
15616/*
15617 * "setwinvar()" and "settabwinvar()" functions
15618 */
15619 static void
15620setwinvar(argvars, rettv, off)
15621 typval_T *argvars;
15622 typval_T *rettv;
15623 int off;
15624{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 win_T *win;
15626#ifdef FEAT_WINDOWS
15627 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015628 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629#endif
15630 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015631 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015633 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 rettv->vval.v_number = 0;
15636
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 if (check_restricted() || check_secure())
15638 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015639
15640#ifdef FEAT_WINDOWS
15641 if (off == 1)
15642 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15643 else
15644 tp = curtab;
15645#endif
15646 win = find_win_by_nr(&argvars[off], tp);
15647 varname = get_tv_string_chk(&argvars[off + 1]);
15648 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649
15650 if (win != NULL && varname != NULL && varp != NULL)
15651 {
15652#ifdef FEAT_WINDOWS
15653 /* set curwin to be our win, temporarily */
15654 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015655 save_curtab = curtab;
15656 goto_tabpage_tp(tp);
15657 if (!win_valid(win))
15658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 curwin = win;
15660 curbuf = curwin->w_buffer;
15661#endif
15662
15663 if (*varname == '&')
15664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 long numval;
15666 char_u *strval;
15667 int error = FALSE;
15668
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015670 numval = get_tv_number_chk(varp, &error);
15671 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015672 if (!error && strval != NULL)
15673 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 }
15675 else
15676 {
15677 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15678 if (winvarname != NULL)
15679 {
15680 STRCPY(winvarname, "w:");
15681 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015682 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 vim_free(winvarname);
15684 }
15685 }
15686
15687#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015688 /* Restore current tabpage and window, if still valid (autocomands can
15689 * make them invalid). */
15690 if (valid_tabpage(save_curtab))
15691 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 if (win_valid(save_curwin))
15693 {
15694 curwin = save_curwin;
15695 curbuf = curwin->w_buffer;
15696 }
15697#endif
15698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699}
15700
15701/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015702 * "shellescape({string})" function
15703 */
15704 static void
15705f_shellescape(argvars, rettv)
15706 typval_T *argvars;
15707 typval_T *rettv;
15708{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015709 rettv->vval.v_string = vim_strsave_shellescape(
15710 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015711 rettv->v_type = VAR_STRING;
15712}
15713
15714/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015715 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 */
15717 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015718f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015719 typval_T *argvars;
15720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724 p = get_tv_string(&argvars[0]);
15725 rettv->vval.v_string = vim_strsave(p);
15726 simplify_filename(rettv->vval.v_string); /* simplify in place */
15727 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728}
15729
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015730#ifdef FEAT_FLOAT
15731/*
15732 * "sin()" function
15733 */
15734 static void
15735f_sin(argvars, rettv)
15736 typval_T *argvars;
15737 typval_T *rettv;
15738{
15739 float_T f;
15740
15741 rettv->v_type = VAR_FLOAT;
15742 if (get_float_arg(argvars, &f) == OK)
15743 rettv->vval.v_float = sin(f);
15744 else
15745 rettv->vval.v_float = 0.0;
15746}
15747#endif
15748
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749static int
15750#ifdef __BORLANDC__
15751 _RTLENTRYF
15752#endif
15753 item_compare __ARGS((const void *s1, const void *s2));
15754static int
15755#ifdef __BORLANDC__
15756 _RTLENTRYF
15757#endif
15758 item_compare2 __ARGS((const void *s1, const void *s2));
15759
15760static int item_compare_ic;
15761static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015762static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763#define ITEM_COMPARE_FAIL 999
15764
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015766 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768 static int
15769#ifdef __BORLANDC__
15770_RTLENTRYF
15771#endif
15772item_compare(s1, s2)
15773 const void *s1;
15774 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776 char_u *p1, *p2;
15777 char_u *tofree1, *tofree2;
15778 int res;
15779 char_u numbuf1[NUMBUFLEN];
15780 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015782 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15783 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015784 if (p1 == NULL)
15785 p1 = (char_u *)"";
15786 if (p2 == NULL)
15787 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788 if (item_compare_ic)
15789 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791 res = STRCMP(p1, p2);
15792 vim_free(tofree1);
15793 vim_free(tofree2);
15794 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795}
15796
15797 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798#ifdef __BORLANDC__
15799_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015801item_compare2(s1, s2)
15802 const void *s1;
15803 const void *s2;
15804{
15805 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015806 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015807 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015808 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015810 /* shortcut after failure in previous call; compare all items equal */
15811 if (item_compare_func_err)
15812 return 0;
15813
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015814 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15815 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015816 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15817 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818
15819 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015820 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015821 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 clear_tv(&argv[0]);
15823 clear_tv(&argv[1]);
15824
15825 if (res == FAIL)
15826 res = ITEM_COMPARE_FAIL;
15827 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015828 /* return value has wrong type */
15829 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15830 if (item_compare_func_err)
15831 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832 clear_tv(&rettv);
15833 return res;
15834}
15835
15836/*
15837 * "sort({list})" function
15838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015840f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015841 typval_T *argvars;
15842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843{
Bram Moolenaar33570922005-01-25 22:26:29 +000015844 list_T *l;
15845 listitem_T *li;
15846 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847 long len;
15848 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850 rettv->vval.v_number = 0;
15851 if (argvars[0].v_type != VAR_LIST)
15852 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853 else
15854 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015856 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015857 return;
15858 rettv->vval.v_list = l;
15859 rettv->v_type = VAR_LIST;
15860 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861
Bram Moolenaar0d660222005-01-07 21:51:51 +000015862 len = list_len(l);
15863 if (len <= 1)
15864 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866 item_compare_ic = FALSE;
15867 item_compare_func = NULL;
15868 if (argvars[1].v_type != VAR_UNKNOWN)
15869 {
15870 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015871 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015872 else
15873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015874 int error = FALSE;
15875
15876 i = get_tv_number_chk(&argvars[1], &error);
15877 if (error)
15878 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015879 if (i == 1)
15880 item_compare_ic = TRUE;
15881 else
15882 item_compare_func = get_tv_string(&argvars[1]);
15883 }
15884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885
Bram Moolenaar0d660222005-01-07 21:51:51 +000015886 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015887 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888 if (ptrs == NULL)
15889 return;
15890 i = 0;
15891 for (li = l->lv_first; li != NULL; li = li->li_next)
15892 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015894 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895 /* test the compare function */
15896 if (item_compare_func != NULL
15897 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15898 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015899 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015901 {
15902 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015903 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015904 item_compare_func == NULL ? item_compare : item_compare2);
15905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015906 if (!item_compare_func_err)
15907 {
15908 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015909 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 l->lv_len = 0;
15911 for (i = 0; i < len; ++i)
15912 list_append(l, ptrs[i]);
15913 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015914 }
15915
15916 vim_free(ptrs);
15917 }
15918}
15919
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015920/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015921 * "soundfold({word})" function
15922 */
15923 static void
15924f_soundfold(argvars, rettv)
15925 typval_T *argvars;
15926 typval_T *rettv;
15927{
15928 char_u *s;
15929
15930 rettv->v_type = VAR_STRING;
15931 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015932#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015933 rettv->vval.v_string = eval_soundfold(s);
15934#else
15935 rettv->vval.v_string = vim_strsave(s);
15936#endif
15937}
15938
15939/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015940 * "spellbadword()" function
15941 */
15942/* ARGSUSED */
15943 static void
15944f_spellbadword(argvars, rettv)
15945 typval_T *argvars;
15946 typval_T *rettv;
15947{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015948 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015949 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015950 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015951
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015952 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015953 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015954
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015955#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015956 if (argvars[0].v_type == VAR_UNKNOWN)
15957 {
15958 /* Find the start and length of the badly spelled word. */
15959 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15960 if (len != 0)
15961 word = ml_get_cursor();
15962 }
15963 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15964 {
15965 char_u *str = get_tv_string_chk(&argvars[0]);
15966 int capcol = -1;
15967
15968 if (str != NULL)
15969 {
15970 /* Check the argument for spelling. */
15971 while (*str != NUL)
15972 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015973 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015974 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015975 {
15976 word = str;
15977 break;
15978 }
15979 str += len;
15980 }
15981 }
15982 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015983#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015984
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015985 list_append_string(rettv->vval.v_list, word, len);
15986 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015987 attr == HLF_SPB ? "bad" :
15988 attr == HLF_SPR ? "rare" :
15989 attr == HLF_SPL ? "local" :
15990 attr == HLF_SPC ? "caps" :
15991 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015992}
15993
15994/*
15995 * "spellsuggest()" function
15996 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015997/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015998 static void
15999f_spellsuggest(argvars, rettv)
16000 typval_T *argvars;
16001 typval_T *rettv;
16002{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016003#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016004 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016005 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016006 int maxcount;
16007 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016008 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016009 listitem_T *li;
16010 int need_capital = FALSE;
16011#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016012
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016013 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016014 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016015
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016016#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016017 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16018 {
16019 str = get_tv_string(&argvars[0]);
16020 if (argvars[1].v_type != VAR_UNKNOWN)
16021 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016022 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016023 if (maxcount <= 0)
16024 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016025 if (argvars[2].v_type != VAR_UNKNOWN)
16026 {
16027 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16028 if (typeerr)
16029 return;
16030 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016031 }
16032 else
16033 maxcount = 25;
16034
Bram Moolenaar4770d092006-01-12 23:22:24 +000016035 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016036
16037 for (i = 0; i < ga.ga_len; ++i)
16038 {
16039 str = ((char_u **)ga.ga_data)[i];
16040
16041 li = listitem_alloc();
16042 if (li == NULL)
16043 vim_free(str);
16044 else
16045 {
16046 li->li_tv.v_type = VAR_STRING;
16047 li->li_tv.v_lock = 0;
16048 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016049 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016050 }
16051 }
16052 ga_clear(&ga);
16053 }
16054#endif
16055}
16056
Bram Moolenaar0d660222005-01-07 21:51:51 +000016057 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016058f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016059 typval_T *argvars;
16060 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016061{
16062 char_u *str;
16063 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016064 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016065 regmatch_T regmatch;
16066 char_u patbuf[NUMBUFLEN];
16067 char_u *save_cpo;
16068 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016069 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016070 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016071 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016072
16073 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16074 save_cpo = p_cpo;
16075 p_cpo = (char_u *)"";
16076
16077 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016078 if (argvars[1].v_type != VAR_UNKNOWN)
16079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16081 if (pat == NULL)
16082 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016084 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016085 }
16086 if (pat == NULL || *pat == NUL)
16087 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016089 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 if (typeerr)
16092 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093
Bram Moolenaar0d660222005-01-07 21:51:51 +000016094 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16095 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016097 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016098 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016099 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016100 if (*str == NUL)
16101 match = FALSE; /* empty item at the end */
16102 else
16103 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104 if (match)
16105 end = regmatch.startp[0];
16106 else
16107 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016108 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16109 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016111 if (list_append_string(rettv->vval.v_list, str,
16112 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016113 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114 }
16115 if (!match)
16116 break;
16117 /* Advance to just after the match. */
16118 if (regmatch.endp[0] > str)
16119 col = 0;
16120 else
16121 {
16122 /* Don't get stuck at the same match. */
16123#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016124 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125#else
16126 col = 1;
16127#endif
16128 }
16129 str = regmatch.endp[0];
16130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131
Bram Moolenaar0d660222005-01-07 21:51:51 +000016132 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134
Bram Moolenaar0d660222005-01-07 21:51:51 +000016135 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136}
16137
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016138#ifdef FEAT_FLOAT
16139/*
16140 * "sqrt()" function
16141 */
16142 static void
16143f_sqrt(argvars, rettv)
16144 typval_T *argvars;
16145 typval_T *rettv;
16146{
16147 float_T f;
16148
16149 rettv->v_type = VAR_FLOAT;
16150 if (get_float_arg(argvars, &f) == OK)
16151 rettv->vval.v_float = sqrt(f);
16152 else
16153 rettv->vval.v_float = 0.0;
16154}
16155
16156/*
16157 * "str2float()" function
16158 */
16159 static void
16160f_str2float(argvars, rettv)
16161 typval_T *argvars;
16162 typval_T *rettv;
16163{
16164 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16165
16166 if (*p == '+')
16167 p = skipwhite(p + 1);
16168 (void)string2float(p, &rettv->vval.v_float);
16169 rettv->v_type = VAR_FLOAT;
16170}
16171#endif
16172
Bram Moolenaar2c932302006-03-18 21:42:09 +000016173/*
16174 * "str2nr()" function
16175 */
16176 static void
16177f_str2nr(argvars, rettv)
16178 typval_T *argvars;
16179 typval_T *rettv;
16180{
16181 int base = 10;
16182 char_u *p;
16183 long n;
16184
16185 if (argvars[1].v_type != VAR_UNKNOWN)
16186 {
16187 base = get_tv_number(&argvars[1]);
16188 if (base != 8 && base != 10 && base != 16)
16189 {
16190 EMSG(_(e_invarg));
16191 return;
16192 }
16193 }
16194
16195 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016196 if (*p == '+')
16197 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016198 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16199 rettv->vval.v_number = n;
16200}
16201
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202#ifdef HAVE_STRFTIME
16203/*
16204 * "strftime({format}[, {time}])" function
16205 */
16206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016208 typval_T *argvars;
16209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210{
16211 char_u result_buf[256];
16212 struct tm *curtime;
16213 time_t seconds;
16214 char_u *p;
16215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016218 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016219 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 seconds = time(NULL);
16221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 curtime = localtime(&seconds);
16224 /* MSVC returns NULL for an invalid value of seconds. */
16225 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 else
16228 {
16229# ifdef FEAT_MBYTE
16230 vimconv_T conv;
16231 char_u *enc;
16232
16233 conv.vc_type = CONV_NONE;
16234 enc = enc_locale();
16235 convert_setup(&conv, p_enc, enc);
16236 if (conv.vc_type != CONV_NONE)
16237 p = string_convert(&conv, p, NULL);
16238# endif
16239 if (p != NULL)
16240 (void)strftime((char *)result_buf, sizeof(result_buf),
16241 (char *)p, curtime);
16242 else
16243 result_buf[0] = NUL;
16244
16245# ifdef FEAT_MBYTE
16246 if (conv.vc_type != CONV_NONE)
16247 vim_free(p);
16248 convert_setup(&conv, enc, p_enc);
16249 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 else
16252# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016253 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254
16255# ifdef FEAT_MBYTE
16256 /* Release conversion descriptors */
16257 convert_setup(&conv, NULL, NULL);
16258 vim_free(enc);
16259# endif
16260 }
16261}
16262#endif
16263
16264/*
16265 * "stridx()" function
16266 */
16267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 typval_T *argvars;
16270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271{
16272 char_u buf[NUMBUFLEN];
16273 char_u *needle;
16274 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016279 needle = get_tv_string_chk(&argvars[1]);
16280 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016282 if (needle == NULL || haystack == NULL)
16283 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 if (argvars[2].v_type != VAR_UNKNOWN)
16286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016287 int error = FALSE;
16288
16289 start_idx = get_tv_number_chk(&argvars[2], &error);
16290 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016292 if (start_idx >= 0)
16293 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 }
16295
16296 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16297 if (pos != NULL)
16298 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299}
16300
16301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016302 * "string()" function
16303 */
16304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016306 typval_T *argvars;
16307 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016308{
16309 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016310 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016312 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016313 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016314 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016315 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317}
16318
16319/*
16320 * "strlen()" function
16321 */
16322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016323f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016324 typval_T *argvars;
16325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016327 rettv->vval.v_number = (varnumber_T)(STRLEN(
16328 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329}
16330
16331/*
16332 * "strpart()" function
16333 */
16334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016335f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 typval_T *argvars;
16337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338{
16339 char_u *p;
16340 int n;
16341 int len;
16342 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016343 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016345 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346 slen = (int)STRLEN(p);
16347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016348 n = get_tv_number_chk(&argvars[1], &error);
16349 if (error)
16350 len = 0;
16351 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 else
16354 len = slen - n; /* default len: all bytes that are available. */
16355
16356 /*
16357 * Only return the overlap between the specified part and the actual
16358 * string.
16359 */
16360 if (n < 0)
16361 {
16362 len += n;
16363 n = 0;
16364 }
16365 else if (n > slen)
16366 n = slen;
16367 if (len < 0)
16368 len = 0;
16369 else if (n + len > slen)
16370 len = slen - n;
16371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016372 rettv->v_type = VAR_STRING;
16373 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374}
16375
16376/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377 * "strridx()" function
16378 */
16379 static void
16380f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016381 typval_T *argvars;
16382 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383{
16384 char_u buf[NUMBUFLEN];
16385 char_u *needle;
16386 char_u *haystack;
16387 char_u *rest;
16388 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016389 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 needle = get_tv_string_chk(&argvars[1]);
16392 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016393
16394 rettv->vval.v_number = -1;
16395 if (needle == NULL || haystack == NULL)
16396 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016397
16398 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016399 if (argvars[2].v_type != VAR_UNKNOWN)
16400 {
16401 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016403 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016405 }
16406 else
16407 end_idx = haystack_len;
16408
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016410 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016412 lastmatch = haystack + end_idx;
16413 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016415 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416 for (rest = haystack; *rest != '\0'; ++rest)
16417 {
16418 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016419 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016420 break;
16421 lastmatch = rest;
16422 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424
16425 if (lastmatch == NULL)
16426 rettv->vval.v_number = -1;
16427 else
16428 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16429}
16430
16431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 * "strtrans()" function
16433 */
16434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016435f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 typval_T *argvars;
16437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 rettv->v_type = VAR_STRING;
16440 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441}
16442
16443/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016444 * "submatch()" function
16445 */
16446 static void
16447f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016448 typval_T *argvars;
16449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450{
16451 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016452 rettv->vval.v_string =
16453 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454}
16455
16456/*
16457 * "substitute()" function
16458 */
16459 static void
16460f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *argvars;
16462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016463{
16464 char_u patbuf[NUMBUFLEN];
16465 char_u subbuf[NUMBUFLEN];
16466 char_u flagsbuf[NUMBUFLEN];
16467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016468 char_u *str = get_tv_string_chk(&argvars[0]);
16469 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16470 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16471 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16472
Bram Moolenaar0d660222005-01-07 21:51:51 +000016473 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016474 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16475 rettv->vval.v_string = NULL;
16476 else
16477 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016478}
16479
16480/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016481 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 */
16483/*ARGSUSED*/
16484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016485f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 typval_T *argvars;
16487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488{
16489 int id = 0;
16490#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016491 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 long col;
16493 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016494 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016496 lnum = get_tv_lnum(argvars); /* -1 on type error */
16497 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16498 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016500 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016501 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016502 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503#endif
16504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016505 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506}
16507
16508/*
16509 * "synIDattr(id, what [, mode])" function
16510 */
16511/*ARGSUSED*/
16512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016513f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016514 typval_T *argvars;
16515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516{
16517 char_u *p = NULL;
16518#ifdef FEAT_SYN_HL
16519 int id;
16520 char_u *what;
16521 char_u *mode;
16522 char_u modebuf[NUMBUFLEN];
16523 int modec;
16524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016525 id = get_tv_number(&argvars[0]);
16526 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016529 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 modec = TOLOWER_ASC(mode[0]);
16531 if (modec != 't' && modec != 'c'
16532#ifdef FEAT_GUI
16533 && modec != 'g'
16534#endif
16535 )
16536 modec = 0; /* replace invalid with current */
16537 }
16538 else
16539 {
16540#ifdef FEAT_GUI
16541 if (gui.in_use)
16542 modec = 'g';
16543 else
16544#endif
16545 if (t_colors > 1)
16546 modec = 'c';
16547 else
16548 modec = 't';
16549 }
16550
16551
16552 switch (TOLOWER_ASC(what[0]))
16553 {
16554 case 'b':
16555 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16556 p = highlight_color(id, what, modec);
16557 else /* bold */
16558 p = highlight_has_attr(id, HL_BOLD, modec);
16559 break;
16560
16561 case 'f': /* fg[#] */
16562 p = highlight_color(id, what, modec);
16563 break;
16564
16565 case 'i':
16566 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16567 p = highlight_has_attr(id, HL_INVERSE, modec);
16568 else /* italic */
16569 p = highlight_has_attr(id, HL_ITALIC, modec);
16570 break;
16571
16572 case 'n': /* name */
16573 p = get_highlight_name(NULL, id - 1);
16574 break;
16575
16576 case 'r': /* reverse */
16577 p = highlight_has_attr(id, HL_INVERSE, modec);
16578 break;
16579
16580 case 's': /* standout */
16581 p = highlight_has_attr(id, HL_STANDOUT, modec);
16582 break;
16583
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016584 case 'u':
16585 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16586 /* underline */
16587 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16588 else
16589 /* undercurl */
16590 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 break;
16592 }
16593
16594 if (p != NULL)
16595 p = vim_strsave(p);
16596#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016597 rettv->v_type = VAR_STRING;
16598 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599}
16600
16601/*
16602 * "synIDtrans(id)" function
16603 */
16604/*ARGSUSED*/
16605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016606f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016607 typval_T *argvars;
16608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609{
16610 int id;
16611
16612#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614
16615 if (id > 0)
16616 id = syn_get_final_id(id);
16617 else
16618#endif
16619 id = 0;
16620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622}
16623
16624/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016625 * "synstack(lnum, col)" function
16626 */
16627/*ARGSUSED*/
16628 static void
16629f_synstack(argvars, rettv)
16630 typval_T *argvars;
16631 typval_T *rettv;
16632{
16633#ifdef FEAT_SYN_HL
16634 long lnum;
16635 long col;
16636 int i;
16637 int id;
16638#endif
16639
16640 rettv->v_type = VAR_LIST;
16641 rettv->vval.v_list = NULL;
16642
16643#ifdef FEAT_SYN_HL
16644 lnum = get_tv_lnum(argvars); /* -1 on type error */
16645 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16646
16647 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
16648 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
16649 && rettv_list_alloc(rettv) != FAIL)
16650 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016651 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016652 for (i = 0; ; ++i)
16653 {
16654 id = syn_get_stack_item(i);
16655 if (id < 0)
16656 break;
16657 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16658 break;
16659 }
16660 }
16661#endif
16662}
16663
16664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 * "system()" function
16666 */
16667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016668f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016669 typval_T *argvars;
16670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016672 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016674 char_u *infile = NULL;
16675 char_u buf[NUMBUFLEN];
16676 int err = FALSE;
16677 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016679 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016680 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016681
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016682 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016683 {
16684 /*
16685 * Write the string to a temp file, to be used for input of the shell
16686 * command.
16687 */
16688 if ((infile = vim_tempname('i')) == NULL)
16689 {
16690 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016691 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016692 }
16693
16694 fd = mch_fopen((char *)infile, WRITEBIN);
16695 if (fd == NULL)
16696 {
16697 EMSG2(_(e_notopen), infile);
16698 goto done;
16699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 p = get_tv_string_buf_chk(&argvars[1], buf);
16701 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016702 {
16703 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016704 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016705 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016706 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16707 err = TRUE;
16708 if (fclose(fd) != 0)
16709 err = TRUE;
16710 if (err)
16711 {
16712 EMSG(_("E677: Error writing temp file"));
16713 goto done;
16714 }
16715 }
16716
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016717 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16718 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016719
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720#ifdef USE_CR
16721 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016722 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 {
16724 char_u *s;
16725
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016726 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 {
16728 if (*s == CAR)
16729 *s = NL;
16730 }
16731 }
16732#else
16733# ifdef USE_CRNL
16734 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016735 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 {
16737 char_u *s, *d;
16738
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016739 d = res;
16740 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 {
16742 if (s[0] == CAR && s[1] == NL)
16743 ++s;
16744 *d++ = *s;
16745 }
16746 *d = NUL;
16747 }
16748# endif
16749#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016750
16751done:
16752 if (infile != NULL)
16753 {
16754 mch_remove(infile);
16755 vim_free(infile);
16756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016757 rettv->v_type = VAR_STRING;
16758 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759}
16760
16761/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016762 * "tabpagebuflist()" function
16763 */
16764/* ARGSUSED */
16765 static void
16766f_tabpagebuflist(argvars, rettv)
16767 typval_T *argvars;
16768 typval_T *rettv;
16769{
16770#ifndef FEAT_WINDOWS
16771 rettv->vval.v_number = 0;
16772#else
16773 tabpage_T *tp;
16774 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016775
16776 if (argvars[0].v_type == VAR_UNKNOWN)
16777 wp = firstwin;
16778 else
16779 {
16780 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16781 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016782 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016783 }
16784 if (wp == NULL)
16785 rettv->vval.v_number = 0;
16786 else
16787 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016788 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016789 rettv->vval.v_number = 0;
16790 else
16791 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016792 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016793 if (list_append_number(rettv->vval.v_list,
16794 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016795 break;
16796 }
16797 }
16798#endif
16799}
16800
16801
16802/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016803 * "tabpagenr()" function
16804 */
16805/* ARGSUSED */
16806 static void
16807f_tabpagenr(argvars, rettv)
16808 typval_T *argvars;
16809 typval_T *rettv;
16810{
16811 int nr = 1;
16812#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016813 char_u *arg;
16814
16815 if (argvars[0].v_type != VAR_UNKNOWN)
16816 {
16817 arg = get_tv_string_chk(&argvars[0]);
16818 nr = 0;
16819 if (arg != NULL)
16820 {
16821 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016822 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016823 else
16824 EMSG2(_(e_invexpr2), arg);
16825 }
16826 }
16827 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016828 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016829#endif
16830 rettv->vval.v_number = nr;
16831}
16832
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016833
16834#ifdef FEAT_WINDOWS
16835static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16836
16837/*
16838 * Common code for tabpagewinnr() and winnr().
16839 */
16840 static int
16841get_winnr(tp, argvar)
16842 tabpage_T *tp;
16843 typval_T *argvar;
16844{
16845 win_T *twin;
16846 int nr = 1;
16847 win_T *wp;
16848 char_u *arg;
16849
16850 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16851 if (argvar->v_type != VAR_UNKNOWN)
16852 {
16853 arg = get_tv_string_chk(argvar);
16854 if (arg == NULL)
16855 nr = 0; /* type error; errmsg already given */
16856 else if (STRCMP(arg, "$") == 0)
16857 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16858 else if (STRCMP(arg, "#") == 0)
16859 {
16860 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16861 if (twin == NULL)
16862 nr = 0;
16863 }
16864 else
16865 {
16866 EMSG2(_(e_invexpr2), arg);
16867 nr = 0;
16868 }
16869 }
16870
16871 if (nr > 0)
16872 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16873 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016874 {
16875 if (wp == NULL)
16876 {
16877 /* didn't find it in this tabpage */
16878 nr = 0;
16879 break;
16880 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016881 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016882 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016883 return nr;
16884}
16885#endif
16886
16887/*
16888 * "tabpagewinnr()" function
16889 */
16890/* ARGSUSED */
16891 static void
16892f_tabpagewinnr(argvars, rettv)
16893 typval_T *argvars;
16894 typval_T *rettv;
16895{
16896 int nr = 1;
16897#ifdef FEAT_WINDOWS
16898 tabpage_T *tp;
16899
16900 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16901 if (tp == NULL)
16902 nr = 0;
16903 else
16904 nr = get_winnr(tp, &argvars[1]);
16905#endif
16906 rettv->vval.v_number = nr;
16907}
16908
16909
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016910/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016911 * "tagfiles()" function
16912 */
16913/*ARGSUSED*/
16914 static void
16915f_tagfiles(argvars, rettv)
16916 typval_T *argvars;
16917 typval_T *rettv;
16918{
16919 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016920 tagname_T tn;
16921 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016922
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016923 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016924 {
16925 rettv->vval.v_number = 0;
16926 return;
16927 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016928
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016929 for (first = TRUE; ; first = FALSE)
16930 if (get_tagfname(&tn, first, fname) == FAIL
16931 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016932 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016933 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016934}
16935
16936/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016937 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016938 */
16939 static void
16940f_taglist(argvars, rettv)
16941 typval_T *argvars;
16942 typval_T *rettv;
16943{
16944 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016945
16946 tag_pattern = get_tv_string(&argvars[0]);
16947
16948 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016949 if (*tag_pattern == NUL)
16950 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016951
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016952 if (rettv_list_alloc(rettv) == OK)
16953 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016954}
16955
16956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 * "tempname()" function
16958 */
16959/*ARGSUSED*/
16960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016961f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 typval_T *argvars;
16963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964{
16965 static int x = 'A';
16966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016967 rettv->v_type = VAR_STRING;
16968 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969
16970 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16971 * names. Skip 'I' and 'O', they are used for shell redirection. */
16972 do
16973 {
16974 if (x == 'Z')
16975 x = '0';
16976 else if (x == '9')
16977 x = 'A';
16978 else
16979 {
16980#ifdef EBCDIC
16981 if (x == 'I')
16982 x = 'J';
16983 else if (x == 'R')
16984 x = 'S';
16985 else
16986#endif
16987 ++x;
16988 }
16989 } while (x == 'I' || x == 'O');
16990}
16991
16992/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016993 * "test(list)" function: Just checking the walls...
16994 */
16995/*ARGSUSED*/
16996 static void
16997f_test(argvars, rettv)
16998 typval_T *argvars;
16999 typval_T *rettv;
17000{
17001 /* Used for unit testing. Change the code below to your liking. */
17002#if 0
17003 listitem_T *li;
17004 list_T *l;
17005 char_u *bad, *good;
17006
17007 if (argvars[0].v_type != VAR_LIST)
17008 return;
17009 l = argvars[0].vval.v_list;
17010 if (l == NULL)
17011 return;
17012 li = l->lv_first;
17013 if (li == NULL)
17014 return;
17015 bad = get_tv_string(&li->li_tv);
17016 li = li->li_next;
17017 if (li == NULL)
17018 return;
17019 good = get_tv_string(&li->li_tv);
17020 rettv->vval.v_number = test_edit_score(bad, good);
17021#endif
17022}
17023
17024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 * "tolower(string)" function
17026 */
17027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017028f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017029 typval_T *argvars;
17030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031{
17032 char_u *p;
17033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 p = vim_strsave(get_tv_string(&argvars[0]));
17035 rettv->v_type = VAR_STRING;
17036 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037
17038 if (p != NULL)
17039 while (*p != NUL)
17040 {
17041#ifdef FEAT_MBYTE
17042 int l;
17043
17044 if (enc_utf8)
17045 {
17046 int c, lc;
17047
17048 c = utf_ptr2char(p);
17049 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017050 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 /* TODO: reallocate string when byte count changes. */
17052 if (utf_char2len(lc) == l)
17053 utf_char2bytes(lc, p);
17054 p += l;
17055 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017056 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 p += l; /* skip multi-byte character */
17058 else
17059#endif
17060 {
17061 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17062 ++p;
17063 }
17064 }
17065}
17066
17067/*
17068 * "toupper(string)" function
17069 */
17070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017071f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017072 typval_T *argvars;
17073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017076 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077}
17078
17079/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017080 * "tr(string, fromstr, tostr)" function
17081 */
17082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017083f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017084 typval_T *argvars;
17085 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017086{
17087 char_u *instr;
17088 char_u *fromstr;
17089 char_u *tostr;
17090 char_u *p;
17091#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017092 int inlen;
17093 int fromlen;
17094 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017095 int idx;
17096 char_u *cpstr;
17097 int cplen;
17098 int first = TRUE;
17099#endif
17100 char_u buf[NUMBUFLEN];
17101 char_u buf2[NUMBUFLEN];
17102 garray_T ga;
17103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017104 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017105 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17106 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017107
17108 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017109 rettv->v_type = VAR_STRING;
17110 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017111 if (fromstr == NULL || tostr == NULL)
17112 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017113 ga_init2(&ga, (int)sizeof(char), 80);
17114
17115#ifdef FEAT_MBYTE
17116 if (!has_mbyte)
17117#endif
17118 /* not multi-byte: fromstr and tostr must be the same length */
17119 if (STRLEN(fromstr) != STRLEN(tostr))
17120 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017121#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017122error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017123#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017124 EMSG2(_(e_invarg2), fromstr);
17125 ga_clear(&ga);
17126 return;
17127 }
17128
17129 /* fromstr and tostr have to contain the same number of chars */
17130 while (*instr != NUL)
17131 {
17132#ifdef FEAT_MBYTE
17133 if (has_mbyte)
17134 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017135 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017136 cpstr = instr;
17137 cplen = inlen;
17138 idx = 0;
17139 for (p = fromstr; *p != NUL; p += fromlen)
17140 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017141 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017142 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17143 {
17144 for (p = tostr; *p != NUL; p += tolen)
17145 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017146 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017147 if (idx-- == 0)
17148 {
17149 cplen = tolen;
17150 cpstr = p;
17151 break;
17152 }
17153 }
17154 if (*p == NUL) /* tostr is shorter than fromstr */
17155 goto error;
17156 break;
17157 }
17158 ++idx;
17159 }
17160
17161 if (first && cpstr == instr)
17162 {
17163 /* Check that fromstr and tostr have the same number of
17164 * (multi-byte) characters. Done only once when a character
17165 * of instr doesn't appear in fromstr. */
17166 first = FALSE;
17167 for (p = tostr; *p != NUL; p += tolen)
17168 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017169 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017170 --idx;
17171 }
17172 if (idx != 0)
17173 goto error;
17174 }
17175
17176 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017177 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017178 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017179
17180 instr += inlen;
17181 }
17182 else
17183#endif
17184 {
17185 /* When not using multi-byte chars we can do it faster. */
17186 p = vim_strchr(fromstr, *instr);
17187 if (p != NULL)
17188 ga_append(&ga, tostr[p - fromstr]);
17189 else
17190 ga_append(&ga, *instr);
17191 ++instr;
17192 }
17193 }
17194
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017195 /* add a terminating NUL */
17196 ga_grow(&ga, 1);
17197 ga_append(&ga, NUL);
17198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017200}
17201
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017202#ifdef FEAT_FLOAT
17203/*
17204 * "trunc({float})" function
17205 */
17206 static void
17207f_trunc(argvars, rettv)
17208 typval_T *argvars;
17209 typval_T *rettv;
17210{
17211 float_T f;
17212
17213 rettv->v_type = VAR_FLOAT;
17214 if (get_float_arg(argvars, &f) == OK)
17215 /* trunc() is not in C90, use floor() or ceil() instead. */
17216 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17217 else
17218 rettv->vval.v_float = 0.0;
17219}
17220#endif
17221
Bram Moolenaar8299df92004-07-10 09:47:34 +000017222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 * "type(expr)" function
17224 */
17225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 typval_T *argvars;
17228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017230 int n;
17231
17232 switch (argvars[0].v_type)
17233 {
17234 case VAR_NUMBER: n = 0; break;
17235 case VAR_STRING: n = 1; break;
17236 case VAR_FUNC: n = 2; break;
17237 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017238 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017239#ifdef FEAT_FLOAT
17240 case VAR_FLOAT: n = 5; break;
17241#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017242 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17243 }
17244 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245}
17246
17247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017248 * "values(dict)" function
17249 */
17250 static void
17251f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017252 typval_T *argvars;
17253 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017254{
17255 dict_list(argvars, rettv, 1);
17256}
17257
17258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 * "virtcol(string)" function
17260 */
17261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017262f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 typval_T *argvars;
17264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265{
17266 colnr_T vcol = 0;
17267 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017268 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017270 fp = var2fpos(&argvars[0], FALSE, &fnum);
17271 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17272 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 {
17274 getvvcol(curwin, fp, NULL, NULL, &vcol);
17275 ++vcol;
17276 }
17277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279}
17280
17281/*
17282 * "visualmode()" function
17283 */
17284/*ARGSUSED*/
17285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017287 typval_T *argvars;
17288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289{
17290#ifdef FEAT_VISUAL
17291 char_u str[2];
17292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 str[0] = curbuf->b_visual_mode_eval;
17295 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297
17298 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017299 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 curbuf->b_visual_mode_eval = NUL;
17301#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303#endif
17304}
17305
17306/*
17307 * "winbufnr(nr)" function
17308 */
17309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017311 typval_T *argvars;
17312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313{
17314 win_T *wp;
17315
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017316 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017320 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321}
17322
17323/*
17324 * "wincol()" function
17325 */
17326/*ARGSUSED*/
17327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017328f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 typval_T *argvars;
17330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331{
17332 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
17336/*
17337 * "winheight(nr)" function
17338 */
17339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 typval_T *argvars;
17342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343{
17344 win_T *wp;
17345
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017346 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017350 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351}
17352
17353/*
17354 * "winline()" function
17355 */
17356/*ARGSUSED*/
17357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017358f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 typval_T *argvars;
17360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361{
17362 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364}
17365
17366/*
17367 * "winnr()" function
17368 */
17369/* ARGSUSED */
17370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 typval_T *argvars;
17373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
17375 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017376
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017378 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381}
17382
17383/*
17384 * "winrestcmd()" function
17385 */
17386/* ARGSUSED */
17387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017389 typval_T *argvars;
17390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391{
17392#ifdef FEAT_WINDOWS
17393 win_T *wp;
17394 int winnr = 1;
17395 garray_T ga;
17396 char_u buf[50];
17397
17398 ga_init2(&ga, (int)sizeof(char), 70);
17399 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17400 {
17401 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17402 ga_concat(&ga, buf);
17403# ifdef FEAT_VERTSPLIT
17404 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17405 ga_concat(&ga, buf);
17406# endif
17407 ++winnr;
17408 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017409 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416}
17417
17418/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017419 * "winrestview()" function
17420 */
17421/* ARGSUSED */
17422 static void
17423f_winrestview(argvars, rettv)
17424 typval_T *argvars;
17425 typval_T *rettv;
17426{
17427 dict_T *dict;
17428
17429 if (argvars[0].v_type != VAR_DICT
17430 || (dict = argvars[0].vval.v_dict) == NULL)
17431 EMSG(_(e_invarg));
17432 else
17433 {
17434 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17435 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17436#ifdef FEAT_VIRTUALEDIT
17437 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17438#endif
17439 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017440 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017441
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017442 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017443#ifdef FEAT_DIFF
17444 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17445#endif
17446 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17447 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17448
17449 check_cursor();
17450 changed_cline_bef_curs();
17451 invalidate_botline();
17452 redraw_later(VALID);
17453
17454 if (curwin->w_topline == 0)
17455 curwin->w_topline = 1;
17456 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17457 curwin->w_topline = curbuf->b_ml.ml_line_count;
17458#ifdef FEAT_DIFF
17459 check_topfill(curwin, TRUE);
17460#endif
17461 }
17462}
17463
17464/*
17465 * "winsaveview()" function
17466 */
17467/* ARGSUSED */
17468 static void
17469f_winsaveview(argvars, rettv)
17470 typval_T *argvars;
17471 typval_T *rettv;
17472{
17473 dict_T *dict;
17474
17475 dict = dict_alloc();
17476 if (dict == NULL)
17477 return;
17478 rettv->v_type = VAR_DICT;
17479 rettv->vval.v_dict = dict;
17480 ++dict->dv_refcount;
17481
17482 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17483 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17484#ifdef FEAT_VIRTUALEDIT
17485 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17486#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017487 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017488 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17489
17490 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17491#ifdef FEAT_DIFF
17492 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17493#endif
17494 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17495 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17496}
17497
17498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 * "winwidth(nr)" function
17500 */
17501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017502f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017503 typval_T *argvars;
17504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505{
17506 win_T *wp;
17507
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017508 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017510 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 else
17512#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017513 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516#endif
17517}
17518
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017520 * "writefile()" function
17521 */
17522 static void
17523f_writefile(argvars, rettv)
17524 typval_T *argvars;
17525 typval_T *rettv;
17526{
17527 int binary = FALSE;
17528 char_u *fname;
17529 FILE *fd;
17530 listitem_T *li;
17531 char_u *s;
17532 int ret = 0;
17533 int c;
17534
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017535 if (check_restricted() || check_secure())
17536 return;
17537
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017538 if (argvars[0].v_type != VAR_LIST)
17539 {
17540 EMSG2(_(e_listarg), "writefile()");
17541 return;
17542 }
17543 if (argvars[0].vval.v_list == NULL)
17544 return;
17545
17546 if (argvars[2].v_type != VAR_UNKNOWN
17547 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17548 binary = TRUE;
17549
17550 /* Always open the file in binary mode, library functions have a mind of
17551 * their own about CR-LF conversion. */
17552 fname = get_tv_string(&argvars[1]);
17553 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17554 {
17555 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17556 ret = -1;
17557 }
17558 else
17559 {
17560 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17561 li = li->li_next)
17562 {
17563 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17564 {
17565 if (*s == '\n')
17566 c = putc(NUL, fd);
17567 else
17568 c = putc(*s, fd);
17569 if (c == EOF)
17570 {
17571 ret = -1;
17572 break;
17573 }
17574 }
17575 if (!binary || li->li_next != NULL)
17576 if (putc('\n', fd) == EOF)
17577 {
17578 ret = -1;
17579 break;
17580 }
17581 if (ret < 0)
17582 {
17583 EMSG(_(e_write));
17584 break;
17585 }
17586 }
17587 fclose(fd);
17588 }
17589
17590 rettv->vval.v_number = ret;
17591}
17592
17593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017595 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 */
17597 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017598var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017599 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017600 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017601 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017603 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017605 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606
Bram Moolenaara5525202006-03-02 22:52:09 +000017607 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017608 if (varp->v_type == VAR_LIST)
17609 {
17610 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017611 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017612 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017613 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017614
17615 l = varp->vval.v_list;
17616 if (l == NULL)
17617 return NULL;
17618
17619 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017620 pos.lnum = list_find_nr(l, 0L, &error);
17621 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017622 return NULL; /* invalid line number */
17623
17624 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017625 pos.col = list_find_nr(l, 1L, &error);
17626 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017627 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017628 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017629
17630 /* We accept "$" for the column number: last column. */
17631 li = list_find(l, 1L);
17632 if (li != NULL && li->li_tv.v_type == VAR_STRING
17633 && li->li_tv.vval.v_string != NULL
17634 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17635 pos.col = len + 1;
17636
Bram Moolenaara5525202006-03-02 22:52:09 +000017637 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017638 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017639 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017640 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017641
Bram Moolenaara5525202006-03-02 22:52:09 +000017642#ifdef FEAT_VIRTUALEDIT
17643 /* Get the virtual offset. Defaults to zero. */
17644 pos.coladd = list_find_nr(l, 2L, &error);
17645 if (error)
17646 pos.coladd = 0;
17647#endif
17648
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017649 return &pos;
17650 }
17651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017652 name = get_tv_string_chk(varp);
17653 if (name == NULL)
17654 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017655 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017657#ifdef FEAT_VISUAL
17658 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17659 {
17660 if (VIsual_active)
17661 return &VIsual;
17662 return &curwin->w_cursor;
17663 }
17664#endif
17665 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017667 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17669 return NULL;
17670 return pp;
17671 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017672
17673#ifdef FEAT_VIRTUALEDIT
17674 pos.coladd = 0;
17675#endif
17676
Bram Moolenaar477933c2007-07-17 14:32:23 +000017677 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017678 {
17679 pos.col = 0;
17680 if (name[1] == '0') /* "w0": first visible line */
17681 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017682 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017683 pos.lnum = curwin->w_topline;
17684 return &pos;
17685 }
17686 else if (name[1] == '$') /* "w$": last visible line */
17687 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017688 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017689 pos.lnum = curwin->w_botline - 1;
17690 return &pos;
17691 }
17692 }
17693 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017695 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 {
17697 pos.lnum = curbuf->b_ml.ml_line_count;
17698 pos.col = 0;
17699 }
17700 else
17701 {
17702 pos.lnum = curwin->w_cursor.lnum;
17703 pos.col = (colnr_T)STRLEN(ml_get_curline());
17704 }
17705 return &pos;
17706 }
17707 return NULL;
17708}
17709
17710/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017711 * Convert list in "arg" into a position and optional file number.
17712 * When "fnump" is NULL there is no file number, only 3 items.
17713 * Note that the column is passed on as-is, the caller may want to decrement
17714 * it to use 1 for the first column.
17715 * Return FAIL when conversion is not possible, doesn't check the position for
17716 * validity.
17717 */
17718 static int
17719list2fpos(arg, posp, fnump)
17720 typval_T *arg;
17721 pos_T *posp;
17722 int *fnump;
17723{
17724 list_T *l = arg->vval.v_list;
17725 long i = 0;
17726 long n;
17727
Bram Moolenaarbde35262006-07-23 20:12:24 +000017728 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17729 * when "fnump" isn't NULL and "coladd" is optional. */
17730 if (arg->v_type != VAR_LIST
17731 || l == NULL
17732 || l->lv_len < (fnump == NULL ? 2 : 3)
17733 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017734 return FAIL;
17735
17736 if (fnump != NULL)
17737 {
17738 n = list_find_nr(l, i++, NULL); /* fnum */
17739 if (n < 0)
17740 return FAIL;
17741 if (n == 0)
17742 n = curbuf->b_fnum; /* current buffer */
17743 *fnump = n;
17744 }
17745
17746 n = list_find_nr(l, i++, NULL); /* lnum */
17747 if (n < 0)
17748 return FAIL;
17749 posp->lnum = n;
17750
17751 n = list_find_nr(l, i++, NULL); /* col */
17752 if (n < 0)
17753 return FAIL;
17754 posp->col = n;
17755
17756#ifdef FEAT_VIRTUALEDIT
17757 n = list_find_nr(l, i, NULL);
17758 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017759 posp->coladd = 0;
17760 else
17761 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017762#endif
17763
17764 return OK;
17765}
17766
17767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 * Get the length of an environment variable name.
17769 * Advance "arg" to the first character after the name.
17770 * Return 0 for error.
17771 */
17772 static int
17773get_env_len(arg)
17774 char_u **arg;
17775{
17776 char_u *p;
17777 int len;
17778
17779 for (p = *arg; vim_isIDc(*p); ++p)
17780 ;
17781 if (p == *arg) /* no name found */
17782 return 0;
17783
17784 len = (int)(p - *arg);
17785 *arg = p;
17786 return len;
17787}
17788
17789/*
17790 * Get the length of the name of a function or internal variable.
17791 * "arg" is advanced to the first non-white character after the name.
17792 * Return 0 if something is wrong.
17793 */
17794 static int
17795get_id_len(arg)
17796 char_u **arg;
17797{
17798 char_u *p;
17799 int len;
17800
17801 /* Find the end of the name. */
17802 for (p = *arg; eval_isnamec(*p); ++p)
17803 ;
17804 if (p == *arg) /* no name found */
17805 return 0;
17806
17807 len = (int)(p - *arg);
17808 *arg = skipwhite(p);
17809
17810 return len;
17811}
17812
17813/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017814 * Get the length of the name of a variable or function.
17815 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017817 * Return -1 if curly braces expansion failed.
17818 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 * If the name contains 'magic' {}'s, expand them and return the
17820 * expanded name in an allocated string via 'alias' - caller must free.
17821 */
17822 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017823get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 char_u **arg;
17825 char_u **alias;
17826 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017827 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828{
17829 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 char_u *p;
17831 char_u *expr_start;
17832 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833
17834 *alias = NULL; /* default to no alias */
17835
17836 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17837 && (*arg)[2] == (int)KE_SNR)
17838 {
17839 /* hard coded <SNR>, already translated */
17840 *arg += 3;
17841 return get_id_len(arg) + 3;
17842 }
17843 len = eval_fname_script(*arg);
17844 if (len > 0)
17845 {
17846 /* literal "<SID>", "s:" or "<SNR>" */
17847 *arg += len;
17848 }
17849
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017851 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017853 p = find_name_end(*arg, &expr_start, &expr_end,
17854 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 if (expr_start != NULL)
17856 {
17857 char_u *temp_string;
17858
17859 if (!evaluate)
17860 {
17861 len += (int)(p - *arg);
17862 *arg = skipwhite(p);
17863 return len;
17864 }
17865
17866 /*
17867 * Include any <SID> etc in the expanded string:
17868 * Thus the -len here.
17869 */
17870 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17871 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017872 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 *alias = temp_string;
17874 *arg = skipwhite(p);
17875 return (int)STRLEN(temp_string);
17876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877
17878 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017879 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 EMSG2(_(e_invexpr2), *arg);
17881
17882 return len;
17883}
17884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017885/*
17886 * Find the end of a variable or function name, taking care of magic braces.
17887 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17888 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017889 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017890 * Return a pointer to just after the name. Equal to "arg" if there is no
17891 * valid name.
17892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017894find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 char_u *arg;
17896 char_u **expr_start;
17897 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017898 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017900 int mb_nest = 0;
17901 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 char_u *p;
17903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017904 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017906 *expr_start = NULL;
17907 *expr_end = NULL;
17908 }
17909
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017910 /* Quick check for valid starting character. */
17911 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17912 return arg;
17913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017914 for (p = arg; *p != NUL
17915 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017916 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017917 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017919 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017920 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017921 if (*p == '\'')
17922 {
17923 /* skip over 'string' to avoid counting [ and ] inside it. */
17924 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17925 ;
17926 if (*p == NUL)
17927 break;
17928 }
17929 else if (*p == '"')
17930 {
17931 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17932 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17933 if (*p == '\\' && p[1] != NUL)
17934 ++p;
17935 if (*p == NUL)
17936 break;
17937 }
17938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017939 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017941 if (*p == '[')
17942 ++br_nest;
17943 else if (*p == ']')
17944 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017947 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017949 if (*p == '{')
17950 {
17951 mb_nest++;
17952 if (expr_start != NULL && *expr_start == NULL)
17953 *expr_start = p;
17954 }
17955 else if (*p == '}')
17956 {
17957 mb_nest--;
17958 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17959 *expr_end = p;
17960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 }
17963
17964 return p;
17965}
17966
17967/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017968 * Expands out the 'magic' {}'s in a variable/function name.
17969 * Note that this can call itself recursively, to deal with
17970 * constructs like foo{bar}{baz}{bam}
17971 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17972 * "in_start" ^
17973 * "expr_start" ^
17974 * "expr_end" ^
17975 * "in_end" ^
17976 *
17977 * Returns a new allocated string, which the caller must free.
17978 * Returns NULL for failure.
17979 */
17980 static char_u *
17981make_expanded_name(in_start, expr_start, expr_end, in_end)
17982 char_u *in_start;
17983 char_u *expr_start;
17984 char_u *expr_end;
17985 char_u *in_end;
17986{
17987 char_u c1;
17988 char_u *retval = NULL;
17989 char_u *temp_result;
17990 char_u *nextcmd = NULL;
17991
17992 if (expr_end == NULL || in_end == NULL)
17993 return NULL;
17994 *expr_start = NUL;
17995 *expr_end = NUL;
17996 c1 = *in_end;
17997 *in_end = NUL;
17998
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017999 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018000 if (temp_result != NULL && nextcmd == NULL)
18001 {
18002 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18003 + (in_end - expr_end) + 1));
18004 if (retval != NULL)
18005 {
18006 STRCPY(retval, in_start);
18007 STRCAT(retval, temp_result);
18008 STRCAT(retval, expr_end + 1);
18009 }
18010 }
18011 vim_free(temp_result);
18012
18013 *in_end = c1; /* put char back for error messages */
18014 *expr_start = '{';
18015 *expr_end = '}';
18016
18017 if (retval != NULL)
18018 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018019 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018020 if (expr_start != NULL)
18021 {
18022 /* Further expansion! */
18023 temp_result = make_expanded_name(retval, expr_start,
18024 expr_end, temp_result);
18025 vim_free(retval);
18026 retval = temp_result;
18027 }
18028 }
18029
18030 return retval;
18031}
18032
18033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018035 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 */
18037 static int
18038eval_isnamec(c)
18039 int c;
18040{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018041 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18042}
18043
18044/*
18045 * Return TRUE if character "c" can be used as the first character in a
18046 * variable or function name (excluding '{' and '}').
18047 */
18048 static int
18049eval_isnamec1(c)
18050 int c;
18051{
18052 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053}
18054
18055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 * Set number v: variable to "val".
18057 */
18058 void
18059set_vim_var_nr(idx, val)
18060 int idx;
18061 long val;
18062{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018063 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064}
18065
18066/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018067 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 */
18069 long
18070get_vim_var_nr(idx)
18071 int idx;
18072{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018073 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074}
18075
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018076/*
18077 * Get string v: variable value. Uses a static buffer, can only be used once.
18078 */
18079 char_u *
18080get_vim_var_str(idx)
18081 int idx;
18082{
18083 return get_tv_string(&vimvars[idx].vv_tv);
18084}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018085
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086/*
18087 * Set v:count, v:count1 and v:prevcount.
18088 */
18089 void
18090set_vcount(count, count1)
18091 long count;
18092 long count1;
18093{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018094 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
18095 vimvars[VV_COUNT].vv_nr = count;
18096 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097}
18098
18099/*
18100 * Set string v: variable to a copy of "val".
18101 */
18102 void
18103set_vim_var_string(idx, val, len)
18104 int idx;
18105 char_u *val;
18106 int len; /* length of "val" to use or -1 (whole string) */
18107{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018108 /* Need to do this (at least) once, since we can't initialize a union.
18109 * Will always be invoked when "v:progname" is set. */
18110 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18111
Bram Moolenaare9a41262005-01-15 22:18:47 +000018112 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018114 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018116 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018118 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119}
18120
18121/*
18122 * Set v:register if needed.
18123 */
18124 void
18125set_reg_var(c)
18126 int c;
18127{
18128 char_u regname;
18129
18130 if (c == 0 || c == ' ')
18131 regname = '"';
18132 else
18133 regname = c;
18134 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018135 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 set_vim_var_string(VV_REG, &regname, 1);
18137}
18138
18139/*
18140 * Get or set v:exception. If "oldval" == NULL, return the current value.
18141 * Otherwise, restore the value to "oldval" and return NULL.
18142 * Must always be called in pairs to save and restore v:exception! Does not
18143 * take care of memory allocations.
18144 */
18145 char_u *
18146v_exception(oldval)
18147 char_u *oldval;
18148{
18149 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018150 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151
Bram Moolenaare9a41262005-01-15 22:18:47 +000018152 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 return NULL;
18154}
18155
18156/*
18157 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18158 * Otherwise, restore the value to "oldval" and return NULL.
18159 * Must always be called in pairs to save and restore v:throwpoint! Does not
18160 * take care of memory allocations.
18161 */
18162 char_u *
18163v_throwpoint(oldval)
18164 char_u *oldval;
18165{
18166 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018167 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168
Bram Moolenaare9a41262005-01-15 22:18:47 +000018169 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 return NULL;
18171}
18172
18173#if defined(FEAT_AUTOCMD) || defined(PROTO)
18174/*
18175 * Set v:cmdarg.
18176 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18177 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18178 * Must always be called in pairs!
18179 */
18180 char_u *
18181set_cmdarg(eap, oldarg)
18182 exarg_T *eap;
18183 char_u *oldarg;
18184{
18185 char_u *oldval;
18186 char_u *newval;
18187 unsigned len;
18188
Bram Moolenaare9a41262005-01-15 22:18:47 +000018189 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018190 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018192 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018193 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018194 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 }
18196
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018197 if (eap->force_bin == FORCE_BIN)
18198 len = 6;
18199 else if (eap->force_bin == FORCE_NOBIN)
18200 len = 8;
18201 else
18202 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018203
18204 if (eap->read_edit)
18205 len += 7;
18206
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018207 if (eap->force_ff != 0)
18208 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18209# ifdef FEAT_MBYTE
18210 if (eap->force_enc != 0)
18211 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018212 if (eap->bad_char != 0)
18213 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018214# endif
18215
18216 newval = alloc(len + 1);
18217 if (newval == NULL)
18218 return NULL;
18219
18220 if (eap->force_bin == FORCE_BIN)
18221 sprintf((char *)newval, " ++bin");
18222 else if (eap->force_bin == FORCE_NOBIN)
18223 sprintf((char *)newval, " ++nobin");
18224 else
18225 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018226
18227 if (eap->read_edit)
18228 STRCAT(newval, " ++edit");
18229
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018230 if (eap->force_ff != 0)
18231 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18232 eap->cmd + eap->force_ff);
18233# ifdef FEAT_MBYTE
18234 if (eap->force_enc != 0)
18235 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18236 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018237 if (eap->bad_char != 0)
18238 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18239 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018240# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018241 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018242 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243}
18244#endif
18245
18246/*
18247 * Get the value of internal variable "name".
18248 * Return OK or FAIL.
18249 */
18250 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018251get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 char_u *name;
18253 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018254 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018255 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256{
18257 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018258 typval_T *tv = NULL;
18259 typval_T atv;
18260 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262
18263 /* truncate the name, so that we can use strcmp() */
18264 cc = name[len];
18265 name[len] = NUL;
18266
18267 /*
18268 * Check for "b:changedtick".
18269 */
18270 if (STRCMP(name, "b:changedtick") == 0)
18271 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018272 atv.v_type = VAR_NUMBER;
18273 atv.vval.v_number = curbuf->b_changedtick;
18274 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 }
18276
18277 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 * Check for user-defined variables.
18279 */
18280 else
18281 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018282 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018284 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 }
18286
Bram Moolenaare9a41262005-01-15 22:18:47 +000018287 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018289 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018290 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 ret = FAIL;
18292 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018293 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018294 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295
18296 name[len] = cc;
18297
18298 return ret;
18299}
18300
18301/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018302 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18303 * Also handle function call with Funcref variable: func(expr)
18304 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18305 */
18306 static int
18307handle_subscript(arg, rettv, evaluate, verbose)
18308 char_u **arg;
18309 typval_T *rettv;
18310 int evaluate; /* do more than finding the end */
18311 int verbose; /* give error messages */
18312{
18313 int ret = OK;
18314 dict_T *selfdict = NULL;
18315 char_u *s;
18316 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018317 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018318
18319 while (ret == OK
18320 && (**arg == '['
18321 || (**arg == '.' && rettv->v_type == VAR_DICT)
18322 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18323 && !vim_iswhite(*(*arg - 1)))
18324 {
18325 if (**arg == '(')
18326 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018327 /* need to copy the funcref so that we can clear rettv */
18328 functv = *rettv;
18329 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018330
18331 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018332 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018333 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018334 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18335 &len, evaluate, selfdict);
18336
18337 /* Clear the funcref afterwards, so that deleting it while
18338 * evaluating the arguments is possible (see test55). */
18339 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018340
18341 /* Stop the expression evaluation when immediately aborting on
18342 * error, or when an interrupt occurred or an exception was thrown
18343 * but not caught. */
18344 if (aborting())
18345 {
18346 if (ret == OK)
18347 clear_tv(rettv);
18348 ret = FAIL;
18349 }
18350 dict_unref(selfdict);
18351 selfdict = NULL;
18352 }
18353 else /* **arg == '[' || **arg == '.' */
18354 {
18355 dict_unref(selfdict);
18356 if (rettv->v_type == VAR_DICT)
18357 {
18358 selfdict = rettv->vval.v_dict;
18359 if (selfdict != NULL)
18360 ++selfdict->dv_refcount;
18361 }
18362 else
18363 selfdict = NULL;
18364 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18365 {
18366 clear_tv(rettv);
18367 ret = FAIL;
18368 }
18369 }
18370 }
18371 dict_unref(selfdict);
18372 return ret;
18373}
18374
18375/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018376 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018377 * value).
18378 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018379 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018380alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018381{
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018383}
18384
18385/*
18386 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 * The string "s" must have been allocated, it is consumed.
18388 * Return NULL for out of memory, the variable otherwise.
18389 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018390 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018391alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 char_u *s;
18393{
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018396 rettv = alloc_tv();
18397 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399 rettv->v_type = VAR_STRING;
18400 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 }
18402 else
18403 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018404 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405}
18406
18407/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018408 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018410 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018411free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018412 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413{
18414 if (varp != NULL)
18415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018416 switch (varp->v_type)
18417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018418 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018419 func_unref(varp->vval.v_string);
18420 /*FALLTHROUGH*/
18421 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018422 vim_free(varp->vval.v_string);
18423 break;
18424 case VAR_LIST:
18425 list_unref(varp->vval.v_list);
18426 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018427 case VAR_DICT:
18428 dict_unref(varp->vval.v_dict);
18429 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018430 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018431#ifdef FEAT_FLOAT
18432 case VAR_FLOAT:
18433#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018434 case VAR_UNKNOWN:
18435 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018436 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018437 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018438 break;
18439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 vim_free(varp);
18441 }
18442}
18443
18444/*
18445 * Free the memory for a variable value and set the value to NULL or 0.
18446 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018447 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018448clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018449 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450{
18451 if (varp != NULL)
18452 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018453 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018455 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018456 func_unref(varp->vval.v_string);
18457 /*FALLTHROUGH*/
18458 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018459 vim_free(varp->vval.v_string);
18460 varp->vval.v_string = NULL;
18461 break;
18462 case VAR_LIST:
18463 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018464 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018465 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018466 case VAR_DICT:
18467 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018468 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018469 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018470 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018471 varp->vval.v_number = 0;
18472 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018473#ifdef FEAT_FLOAT
18474 case VAR_FLOAT:
18475 varp->vval.v_float = 0.0;
18476 break;
18477#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018478 case VAR_UNKNOWN:
18479 break;
18480 default:
18481 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018483 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 }
18485}
18486
18487/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018488 * Set the value of a variable to NULL without freeing items.
18489 */
18490 static void
18491init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018493{
18494 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018495 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018496}
18497
18498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 * Get the number value of a variable.
18500 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018501 * For incompatible types, return 0.
18502 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18503 * caller of incompatible types: it sets *denote to TRUE if "denote"
18504 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 */
18506 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018507get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018508 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018510 int error = FALSE;
18511
18512 return get_tv_number_chk(varp, &error); /* return 0L on error */
18513}
18514
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018515 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018516get_tv_number_chk(varp, denote)
18517 typval_T *varp;
18518 int *denote;
18519{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018520 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018522 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018524 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018525 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018526#ifdef FEAT_FLOAT
18527 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018528 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018529 break;
18530#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018531 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018532 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018533 break;
18534 case VAR_STRING:
18535 if (varp->vval.v_string != NULL)
18536 vim_str2nr(varp->vval.v_string, NULL, NULL,
18537 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018538 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018539 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018540 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018541 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018542 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018543 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018544 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018545 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018546 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018549 if (denote == NULL) /* useful for values that must be unsigned */
18550 n = -1;
18551 else
18552 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018553 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554}
18555
18556/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018557 * Get the lnum from the first argument.
18558 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018559 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 */
18561 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018562get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018563 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564{
Bram Moolenaar33570922005-01-25 22:26:29 +000018565 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 linenr_T lnum;
18567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018568 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 if (lnum == 0) /* no valid number, try using line() */
18570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571 rettv.v_type = VAR_NUMBER;
18572 f_line(argvars, &rettv);
18573 lnum = rettv.vval.v_number;
18574 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 }
18576 return lnum;
18577}
18578
18579/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018580 * Get the lnum from the first argument.
18581 * Also accepts "$", then "buf" is used.
18582 * Returns 0 on error.
18583 */
18584 static linenr_T
18585get_tv_lnum_buf(argvars, buf)
18586 typval_T *argvars;
18587 buf_T *buf;
18588{
18589 if (argvars[0].v_type == VAR_STRING
18590 && argvars[0].vval.v_string != NULL
18591 && argvars[0].vval.v_string[0] == '$'
18592 && buf != NULL)
18593 return buf->b_ml.ml_line_count;
18594 return get_tv_number_chk(&argvars[0], NULL);
18595}
18596
18597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 * Get the string value of a variable.
18599 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018600 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18601 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 * If the String variable has never been set, return an empty string.
18603 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18605 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 */
18607 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018609 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610{
18611 static char_u mybuf[NUMBUFLEN];
18612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614}
18615
18616 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018618 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018619 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018621 char_u *res = get_tv_string_buf_chk(varp, buf);
18622
18623 return res != NULL ? res : (char_u *)"";
18624}
18625
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018626 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018627get_tv_string_chk(varp)
18628 typval_T *varp;
18629{
18630 static char_u mybuf[NUMBUFLEN];
18631
18632 return get_tv_string_buf_chk(varp, mybuf);
18633}
18634
18635 static char_u *
18636get_tv_string_buf_chk(varp, buf)
18637 typval_T *varp;
18638 char_u *buf;
18639{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018640 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018642 case VAR_NUMBER:
18643 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18644 return buf;
18645 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018646 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018647 break;
18648 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018649 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018650 break;
18651 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018652 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018653 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018654#ifdef FEAT_FLOAT
18655 case VAR_FLOAT:
18656 EMSG(_("E806: using Float as a String"));
18657 break;
18658#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018659 case VAR_STRING:
18660 if (varp->vval.v_string != NULL)
18661 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018662 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018663 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018665 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018667 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668}
18669
18670/*
18671 * Find variable "name" in the list of variables.
18672 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018673 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018674 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018675 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018677 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018678find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018680 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018683 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684
Bram Moolenaara7043832005-01-21 11:56:39 +000018685 ht = find_var_ht(name, &varname);
18686 if (htp != NULL)
18687 *htp = ht;
18688 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018690 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691}
18692
18693/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018695 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018697 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018698find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018699 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018700 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018701 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018702{
Bram Moolenaar33570922005-01-25 22:26:29 +000018703 hashitem_T *hi;
18704
18705 if (*varname == NUL)
18706 {
18707 /* Must be something like "s:", otherwise "ht" would be NULL. */
18708 switch (varname[-2])
18709 {
18710 case 's': return &SCRIPT_SV(current_SID).sv_var;
18711 case 'g': return &globvars_var;
18712 case 'v': return &vimvars_var;
18713 case 'b': return &curbuf->b_bufvar;
18714 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018715#ifdef FEAT_WINDOWS
18716 case 't': return &curtab->tp_winvar;
18717#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018718 case 'l': return current_funccal == NULL
18719 ? NULL : &current_funccal->l_vars_var;
18720 case 'a': return current_funccal == NULL
18721 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018722 }
18723 return NULL;
18724 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018725
18726 hi = hash_find(ht, varname);
18727 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018728 {
18729 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018730 * worked find the variable again. Don't auto-load a script if it was
18731 * loaded already, otherwise it would be loaded every time when
18732 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018733 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018734 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018735 hi = hash_find(ht, varname);
18736 if (HASHITEM_EMPTY(hi))
18737 return NULL;
18738 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018739 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018740}
18741
18742/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018743 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018744 * Set "varname" to the start of name without ':'.
18745 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018746 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018747find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 char_u *name;
18749 char_u **varname;
18750{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018751 hashitem_T *hi;
18752
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 if (name[1] != ':')
18754 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018755 /* The name must not start with a colon or #. */
18756 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 return NULL;
18758 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018759
18760 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018761 hi = hash_find(&compat_hashtab, name);
18762 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018763 return &compat_hashtab;
18764
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 return &globvarht; /* global variable */
18767 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 }
18769 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018770 if (*name == 'g') /* global variable */
18771 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018772 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18773 */
18774 if (vim_strchr(name + 2, ':') != NULL
18775 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018776 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018778 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018780 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018781#ifdef FEAT_WINDOWS
18782 if (*name == 't') /* tab page variable */
18783 return &curtab->tp_vars.dv_hashtab;
18784#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018785 if (*name == 'v') /* v: variable */
18786 return &vimvarht;
18787 if (*name == 'a' && current_funccal != NULL) /* function argument */
18788 return &current_funccal->l_avars.dv_hashtab;
18789 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18790 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 if (*name == 's' /* script variable */
18792 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18793 return &SCRIPT_VARS(current_SID);
18794 return NULL;
18795}
18796
18797/*
18798 * Get the string value of a (global/local) variable.
18799 * Returns NULL when it doesn't exist.
18800 */
18801 char_u *
18802get_var_value(name)
18803 char_u *name;
18804{
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806
Bram Moolenaara7043832005-01-21 11:56:39 +000018807 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 if (v == NULL)
18809 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811}
18812
18813/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018814 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 * sourcing this script and when executing functions defined in the script.
18816 */
18817 void
18818new_script_vars(id)
18819 scid_T id;
18820{
Bram Moolenaara7043832005-01-21 11:56:39 +000018821 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018822 hashtab_T *ht;
18823 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018824
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18826 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018827 /* Re-allocating ga_data means that an ht_array pointing to
18828 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018829 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018830 for (i = 1; i <= ga_scripts.ga_len; ++i)
18831 {
18832 ht = &SCRIPT_VARS(i);
18833 if (ht->ht_mask == HT_INIT_SIZE - 1)
18834 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018835 sv = &SCRIPT_SV(i);
18836 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018837 }
18838
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 while (ga_scripts.ga_len < id)
18840 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18842 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844 }
18845 }
18846}
18847
18848/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018849 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18850 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 */
18852 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018853init_var_dict(dict, dict_var)
18854 dict_T *dict;
18855 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856{
Bram Moolenaar33570922005-01-25 22:26:29 +000018857 hash_init(&dict->dv_hashtab);
18858 dict->dv_refcount = 99999;
18859 dict_var->di_tv.vval.v_dict = dict;
18860 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018861 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018862 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18863 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864}
18865
18866/*
18867 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018868 * Frees all allocated variables and the value they contain.
18869 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 */
18871 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018872vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018873 hashtab_T *ht;
18874{
18875 vars_clear_ext(ht, TRUE);
18876}
18877
18878/*
18879 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18880 */
18881 static void
18882vars_clear_ext(ht, free_val)
18883 hashtab_T *ht;
18884 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885{
Bram Moolenaara7043832005-01-21 11:56:39 +000018886 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 hashitem_T *hi;
18888 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889
Bram Moolenaar33570922005-01-25 22:26:29 +000018890 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018891 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018892 for (hi = ht->ht_array; todo > 0; ++hi)
18893 {
18894 if (!HASHITEM_EMPTY(hi))
18895 {
18896 --todo;
18897
Bram Moolenaar33570922005-01-25 22:26:29 +000018898 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018899 * ht_array might change then. hash_clear() takes care of it
18900 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018901 v = HI2DI(hi);
18902 if (free_val)
18903 clear_tv(&v->di_tv);
18904 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18905 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018906 }
18907 }
18908 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018909 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910}
18911
Bram Moolenaara7043832005-01-21 11:56:39 +000018912/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018913 * Delete a variable from hashtab "ht" at item "hi".
18914 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018917delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018918 hashtab_T *ht;
18919 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920{
Bram Moolenaar33570922005-01-25 22:26:29 +000018921 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018922
18923 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018924 clear_tv(&di->di_tv);
18925 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926}
18927
18928/*
18929 * List the value of one internal variable.
18930 */
18931 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018932list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018933 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018935 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018937 char_u *tofree;
18938 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018939 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018940
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018941 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018943 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018944 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945}
18946
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018948list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 char_u *prefix;
18950 char_u *name;
18951 int type;
18952 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018953 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954{
Bram Moolenaar31859182007-08-14 20:41:13 +000018955 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18956 msg_start();
18957 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 if (name != NULL) /* "a:" vars don't have a name stored */
18959 msg_puts(name);
18960 msg_putchar(' ');
18961 msg_advance(22);
18962 if (type == VAR_NUMBER)
18963 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018964 else if (type == VAR_FUNC)
18965 msg_putchar('*');
18966 else if (type == VAR_LIST)
18967 {
18968 msg_putchar('[');
18969 if (*string == '[')
18970 ++string;
18971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018972 else if (type == VAR_DICT)
18973 {
18974 msg_putchar('{');
18975 if (*string == '{')
18976 ++string;
18977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 else
18979 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018980
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018982
18983 if (type == VAR_FUNC)
18984 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018985 if (*first)
18986 {
18987 msg_clr_eos();
18988 *first = FALSE;
18989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990}
18991
18992/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018993 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 * If the variable already exists, the value is updated.
18995 * Otherwise the variable is created.
18996 */
18997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018998set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019000 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019001 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002{
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019005 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019006 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019008 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019009 {
19010 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19011 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19012 ? name[2] : name[0]))
19013 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019014 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019015 return;
19016 }
19017 if (function_exists(name))
19018 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019019 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019020 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019021 return;
19022 }
19023 }
19024
Bram Moolenaara7043832005-01-21 11:56:39 +000019025 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019026 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019027 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019028 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019029 return;
19030 }
19031
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019032 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019033 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019035 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019036 if (var_check_ro(v->di_flags, name)
19037 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019038 return;
19039 if (v->di_tv.v_type != tv->v_type
19040 && !((v->di_tv.v_type == VAR_STRING
19041 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019042 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019043 || tv->v_type == VAR_NUMBER))
19044#ifdef FEAT_FLOAT
19045 && !((v->di_tv.v_type == VAR_NUMBER
19046 || v->di_tv.v_type == VAR_FLOAT)
19047 && (tv->v_type == VAR_NUMBER
19048 || tv->v_type == VAR_FLOAT))
19049#endif
19050 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019052 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019053 return;
19054 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019055
19056 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019057 * Handle setting internal v: variables separately: we don't change
19058 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 */
19060 if (ht == &vimvarht)
19061 {
19062 if (v->di_tv.v_type == VAR_STRING)
19063 {
19064 vim_free(v->di_tv.vval.v_string);
19065 if (copy || tv->v_type != VAR_STRING)
19066 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19067 else
19068 {
19069 /* Take over the string to avoid an extra alloc/free. */
19070 v->di_tv.vval.v_string = tv->vval.v_string;
19071 tv->vval.v_string = NULL;
19072 }
19073 }
19074 else if (v->di_tv.v_type != VAR_NUMBER)
19075 EMSG2(_(e_intern2), "set_var()");
19076 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019077 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019078 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019079 if (STRCMP(varname, "searchforward") == 0)
19080 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19081 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 return;
19083 }
19084
19085 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 }
19087 else /* add a new variable */
19088 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019089 /* Can't add "v:" variable. */
19090 if (ht == &vimvarht)
19091 {
19092 EMSG2(_(e_illvar), name);
19093 return;
19094 }
19095
Bram Moolenaar92124a32005-06-17 22:03:40 +000019096 /* Make sure the variable name is valid. */
19097 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019098 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19099 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019100 {
19101 EMSG2(_(e_illvar), varname);
19102 return;
19103 }
19104
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019105 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19106 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019107 if (v == NULL)
19108 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019109 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019112 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 return;
19114 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019115 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019117
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019118 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019119 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019120 else
19121 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019122 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019123 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126}
19127
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019128/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019129 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019130 * Also give an error message.
19131 */
19132 static int
19133var_check_ro(flags, name)
19134 int flags;
19135 char_u *name;
19136{
19137 if (flags & DI_FLAGS_RO)
19138 {
19139 EMSG2(_(e_readonlyvar), name);
19140 return TRUE;
19141 }
19142 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19143 {
19144 EMSG2(_(e_readonlysbx), name);
19145 return TRUE;
19146 }
19147 return FALSE;
19148}
19149
19150/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019151 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19152 * Also give an error message.
19153 */
19154 static int
19155var_check_fixed(flags, name)
19156 int flags;
19157 char_u *name;
19158{
19159 if (flags & DI_FLAGS_FIX)
19160 {
19161 EMSG2(_("E795: Cannot delete variable %s"), name);
19162 return TRUE;
19163 }
19164 return FALSE;
19165}
19166
19167/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019168 * Return TRUE if typeval "tv" is set to be locked (immutable).
19169 * Also give an error message, using "name".
19170 */
19171 static int
19172tv_check_lock(lock, name)
19173 int lock;
19174 char_u *name;
19175{
19176 if (lock & VAR_LOCKED)
19177 {
19178 EMSG2(_("E741: Value is locked: %s"),
19179 name == NULL ? (char_u *)_("Unknown") : name);
19180 return TRUE;
19181 }
19182 if (lock & VAR_FIXED)
19183 {
19184 EMSG2(_("E742: Cannot change value of %s"),
19185 name == NULL ? (char_u *)_("Unknown") : name);
19186 return TRUE;
19187 }
19188 return FALSE;
19189}
19190
19191/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019192 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019193 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019194 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019197copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019198 typval_T *from;
19199 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019201 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019202 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019203 switch (from->v_type)
19204 {
19205 case VAR_NUMBER:
19206 to->vval.v_number = from->vval.v_number;
19207 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019208#ifdef FEAT_FLOAT
19209 case VAR_FLOAT:
19210 to->vval.v_float = from->vval.v_float;
19211 break;
19212#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019213 case VAR_STRING:
19214 case VAR_FUNC:
19215 if (from->vval.v_string == NULL)
19216 to->vval.v_string = NULL;
19217 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019218 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019219 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019220 if (from->v_type == VAR_FUNC)
19221 func_ref(to->vval.v_string);
19222 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 break;
19224 case VAR_LIST:
19225 if (from->vval.v_list == NULL)
19226 to->vval.v_list = NULL;
19227 else
19228 {
19229 to->vval.v_list = from->vval.v_list;
19230 ++to->vval.v_list->lv_refcount;
19231 }
19232 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019233 case VAR_DICT:
19234 if (from->vval.v_dict == NULL)
19235 to->vval.v_dict = NULL;
19236 else
19237 {
19238 to->vval.v_dict = from->vval.v_dict;
19239 ++to->vval.v_dict->dv_refcount;
19240 }
19241 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019242 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019243 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019244 break;
19245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
19248/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019249 * Make a copy of an item.
19250 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019251 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19252 * reference to an already copied list/dict can be used.
19253 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019254 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019255 static int
19256item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019257 typval_T *from;
19258 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019259 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019260 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019261{
19262 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019263 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019264
Bram Moolenaar33570922005-01-25 22:26:29 +000019265 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019266 {
19267 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019268 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019269 }
19270 ++recurse;
19271
19272 switch (from->v_type)
19273 {
19274 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019275#ifdef FEAT_FLOAT
19276 case VAR_FLOAT:
19277#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019278 case VAR_STRING:
19279 case VAR_FUNC:
19280 copy_tv(from, to);
19281 break;
19282 case VAR_LIST:
19283 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019284 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019285 if (from->vval.v_list == NULL)
19286 to->vval.v_list = NULL;
19287 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19288 {
19289 /* use the copy made earlier */
19290 to->vval.v_list = from->vval.v_list->lv_copylist;
19291 ++to->vval.v_list->lv_refcount;
19292 }
19293 else
19294 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19295 if (to->vval.v_list == NULL)
19296 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019297 break;
19298 case VAR_DICT:
19299 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019300 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019301 if (from->vval.v_dict == NULL)
19302 to->vval.v_dict = NULL;
19303 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19304 {
19305 /* use the copy made earlier */
19306 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19307 ++to->vval.v_dict->dv_refcount;
19308 }
19309 else
19310 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19311 if (to->vval.v_dict == NULL)
19312 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019313 break;
19314 default:
19315 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019316 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019317 }
19318 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019319 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019320}
19321
19322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 * ":echo expr1 ..." print each argument separated with a space, add a
19324 * newline at the end.
19325 * ":echon expr1 ..." print each argument plain.
19326 */
19327 void
19328ex_echo(eap)
19329 exarg_T *eap;
19330{
19331 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019332 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019333 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334 char_u *p;
19335 int needclr = TRUE;
19336 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019337 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338
19339 if (eap->skip)
19340 ++emsg_skip;
19341 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19342 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019343 /* If eval1() causes an error message the text from the command may
19344 * still need to be cleared. E.g., "echo 22,44". */
19345 need_clr_eos = needclr;
19346
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019348 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 {
19350 /*
19351 * Report the invalid expression unless the expression evaluation
19352 * has been cancelled due to an aborting error, an interrupt, or an
19353 * exception.
19354 */
19355 if (!aborting())
19356 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019357 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 break;
19359 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019360 need_clr_eos = FALSE;
19361
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 if (!eap->skip)
19363 {
19364 if (atstart)
19365 {
19366 atstart = FALSE;
19367 /* Call msg_start() after eval1(), evaluating the expression
19368 * may cause a message to appear. */
19369 if (eap->cmdidx == CMD_echo)
19370 msg_start();
19371 }
19372 else if (eap->cmdidx == CMD_echo)
19373 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019374 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019375 if (p != NULL)
19376 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019378 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019380 if (*p != TAB && needclr)
19381 {
19382 /* remove any text still there from the command */
19383 msg_clr_eos();
19384 needclr = FALSE;
19385 }
19386 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 }
19388 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019389 {
19390#ifdef FEAT_MBYTE
19391 if (has_mbyte)
19392 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019393 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019394
19395 (void)msg_outtrans_len_attr(p, i, echo_attr);
19396 p += i - 1;
19397 }
19398 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019400 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019403 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019405 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 arg = skipwhite(arg);
19407 }
19408 eap->nextcmd = check_nextcmd(arg);
19409
19410 if (eap->skip)
19411 --emsg_skip;
19412 else
19413 {
19414 /* remove text that may still be there from the command */
19415 if (needclr)
19416 msg_clr_eos();
19417 if (eap->cmdidx == CMD_echo)
19418 msg_end();
19419 }
19420}
19421
19422/*
19423 * ":echohl {name}".
19424 */
19425 void
19426ex_echohl(eap)
19427 exarg_T *eap;
19428{
19429 int id;
19430
19431 id = syn_name2id(eap->arg);
19432 if (id == 0)
19433 echo_attr = 0;
19434 else
19435 echo_attr = syn_id2attr(id);
19436}
19437
19438/*
19439 * ":execute expr1 ..." execute the result of an expression.
19440 * ":echomsg expr1 ..." Print a message
19441 * ":echoerr expr1 ..." Print an error
19442 * Each gets spaces around each argument and a newline at the end for
19443 * echo commands
19444 */
19445 void
19446ex_execute(eap)
19447 exarg_T *eap;
19448{
19449 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 int ret = OK;
19452 char_u *p;
19453 garray_T ga;
19454 int len;
19455 int save_did_emsg;
19456
19457 ga_init2(&ga, 1, 80);
19458
19459 if (eap->skip)
19460 ++emsg_skip;
19461 while (*arg != NUL && *arg != '|' && *arg != '\n')
19462 {
19463 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019464 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 {
19466 /*
19467 * Report the invalid expression unless the expression evaluation
19468 * has been cancelled due to an aborting error, an interrupt, or an
19469 * exception.
19470 */
19471 if (!aborting())
19472 EMSG2(_(e_invexpr2), p);
19473 ret = FAIL;
19474 break;
19475 }
19476
19477 if (!eap->skip)
19478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019479 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 len = (int)STRLEN(p);
19481 if (ga_grow(&ga, len + 2) == FAIL)
19482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019483 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 ret = FAIL;
19485 break;
19486 }
19487 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 ga.ga_len += len;
19491 }
19492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 arg = skipwhite(arg);
19495 }
19496
19497 if (ret != FAIL && ga.ga_data != NULL)
19498 {
19499 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019502 out_flush();
19503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 else if (eap->cmdidx == CMD_echoerr)
19505 {
19506 /* We don't want to abort following commands, restore did_emsg. */
19507 save_did_emsg = did_emsg;
19508 EMSG((char_u *)ga.ga_data);
19509 if (!force_abort)
19510 did_emsg = save_did_emsg;
19511 }
19512 else if (eap->cmdidx == CMD_execute)
19513 do_cmdline((char_u *)ga.ga_data,
19514 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19515 }
19516
19517 ga_clear(&ga);
19518
19519 if (eap->skip)
19520 --emsg_skip;
19521
19522 eap->nextcmd = check_nextcmd(arg);
19523}
19524
19525/*
19526 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19527 * "arg" points to the "&" or '+' when called, to "option" when returning.
19528 * Returns NULL when no option name found. Otherwise pointer to the char
19529 * after the option name.
19530 */
19531 static char_u *
19532find_option_end(arg, opt_flags)
19533 char_u **arg;
19534 int *opt_flags;
19535{
19536 char_u *p = *arg;
19537
19538 ++p;
19539 if (*p == 'g' && p[1] == ':')
19540 {
19541 *opt_flags = OPT_GLOBAL;
19542 p += 2;
19543 }
19544 else if (*p == 'l' && p[1] == ':')
19545 {
19546 *opt_flags = OPT_LOCAL;
19547 p += 2;
19548 }
19549 else
19550 *opt_flags = 0;
19551
19552 if (!ASCII_ISALPHA(*p))
19553 return NULL;
19554 *arg = p;
19555
19556 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19557 p += 4; /* termcap option */
19558 else
19559 while (ASCII_ISALPHA(*p))
19560 ++p;
19561 return p;
19562}
19563
19564/*
19565 * ":function"
19566 */
19567 void
19568ex_function(eap)
19569 exarg_T *eap;
19570{
19571 char_u *theline;
19572 int j;
19573 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 char_u *name = NULL;
19576 char_u *p;
19577 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019578 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 garray_T newargs;
19580 garray_T newlines;
19581 int varargs = FALSE;
19582 int mustend = FALSE;
19583 int flags = 0;
19584 ufunc_T *fp;
19585 int indent;
19586 int nesting;
19587 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019588 dictitem_T *v;
19589 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019590 static int func_nr = 0; /* number for nameless function */
19591 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019592 hashtab_T *ht;
19593 int todo;
19594 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019595 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596
19597 /*
19598 * ":function" without argument: list functions.
19599 */
19600 if (ends_excmd(*eap->arg))
19601 {
19602 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019603 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019604 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019605 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019606 {
19607 if (!HASHITEM_EMPTY(hi))
19608 {
19609 --todo;
19610 fp = HI2UF(hi);
19611 if (!isdigit(*fp->uf_name))
19612 list_func_head(fp, FALSE);
19613 }
19614 }
19615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 eap->nextcmd = check_nextcmd(eap->arg);
19617 return;
19618 }
19619
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019620 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019621 * ":function /pat": list functions matching pattern.
19622 */
19623 if (*eap->arg == '/')
19624 {
19625 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19626 if (!eap->skip)
19627 {
19628 regmatch_T regmatch;
19629
19630 c = *p;
19631 *p = NUL;
19632 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19633 *p = c;
19634 if (regmatch.regprog != NULL)
19635 {
19636 regmatch.rm_ic = p_ic;
19637
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019638 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019639 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19640 {
19641 if (!HASHITEM_EMPTY(hi))
19642 {
19643 --todo;
19644 fp = HI2UF(hi);
19645 if (!isdigit(*fp->uf_name)
19646 && vim_regexec(&regmatch, fp->uf_name, 0))
19647 list_func_head(fp, FALSE);
19648 }
19649 }
19650 }
19651 }
19652 if (*p == '/')
19653 ++p;
19654 eap->nextcmd = check_nextcmd(p);
19655 return;
19656 }
19657
19658 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019659 * Get the function name. There are these situations:
19660 * func normal function name
19661 * "name" == func, "fudi.fd_dict" == NULL
19662 * dict.func new dictionary entry
19663 * "name" == NULL, "fudi.fd_dict" set,
19664 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19665 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019666 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019667 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19668 * dict.func existing dict entry that's not a Funcref
19669 * "name" == NULL, "fudi.fd_dict" set,
19670 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019673 name = trans_function_name(&p, eap->skip, 0, &fudi);
19674 paren = (vim_strchr(p, '(') != NULL);
19675 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 {
19677 /*
19678 * Return on an invalid expression in braces, unless the expression
19679 * evaluation has been cancelled due to an aborting error, an
19680 * interrupt, or an exception.
19681 */
19682 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019684 if (!eap->skip && fudi.fd_newkey != NULL)
19685 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019686 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 else
19690 eap->skip = TRUE;
19691 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019692
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 /* An error in a function call during evaluation of an expression in magic
19694 * braces should not cause the function not to be defined. */
19695 saved_did_emsg = did_emsg;
19696 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697
19698 /*
19699 * ":function func" with only function name: list function.
19700 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019701 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 {
19703 if (!ends_excmd(*skipwhite(p)))
19704 {
19705 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019706 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 }
19708 eap->nextcmd = check_nextcmd(p);
19709 if (eap->nextcmd != NULL)
19710 *p = NUL;
19711 if (!eap->skip && !got_int)
19712 {
19713 fp = find_func(name);
19714 if (fp != NULL)
19715 {
19716 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019717 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019719 if (FUNCLINE(fp, j) == NULL)
19720 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 msg_putchar('\n');
19722 msg_outnum((long)(j + 1));
19723 if (j < 9)
19724 msg_putchar(' ');
19725 if (j < 99)
19726 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019727 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 out_flush(); /* show a line at a time */
19729 ui_breakcheck();
19730 }
19731 if (!got_int)
19732 {
19733 msg_putchar('\n');
19734 msg_puts((char_u *)" endfunction");
19735 }
19736 }
19737 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019738 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019740 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 }
19742
19743 /*
19744 * ":function name(arg1, arg2)" Define function.
19745 */
19746 p = skipwhite(p);
19747 if (*p != '(')
19748 {
19749 if (!eap->skip)
19750 {
19751 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019752 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 }
19754 /* attempt to continue by skipping some text */
19755 if (vim_strchr(p, '(') != NULL)
19756 p = vim_strchr(p, '(');
19757 }
19758 p = skipwhite(p + 1);
19759
19760 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19761 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19762
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019763 if (!eap->skip)
19764 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019765 /* Check the name of the function. Unless it's a dictionary function
19766 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019767 if (name != NULL)
19768 arg = name;
19769 else
19770 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019771 if (arg != NULL && (fudi.fd_di == NULL
19772 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019773 {
19774 if (*arg == K_SPECIAL)
19775 j = 3;
19776 else
19777 j = 0;
19778 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19779 : eval_isnamec(arg[j])))
19780 ++j;
19781 if (arg[j] != NUL)
19782 emsg_funcname(_(e_invarg2), arg);
19783 }
19784 }
19785
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 /*
19787 * Isolate the arguments: "arg1, arg2, ...)"
19788 */
19789 while (*p != ')')
19790 {
19791 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19792 {
19793 varargs = TRUE;
19794 p += 3;
19795 mustend = TRUE;
19796 }
19797 else
19798 {
19799 arg = p;
19800 while (ASCII_ISALNUM(*p) || *p == '_')
19801 ++p;
19802 if (arg == p || isdigit(*arg)
19803 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19804 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19805 {
19806 if (!eap->skip)
19807 EMSG2(_("E125: Illegal argument: %s"), arg);
19808 break;
19809 }
19810 if (ga_grow(&newargs, 1) == FAIL)
19811 goto erret;
19812 c = *p;
19813 *p = NUL;
19814 arg = vim_strsave(arg);
19815 if (arg == NULL)
19816 goto erret;
19817 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19818 *p = c;
19819 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 if (*p == ',')
19821 ++p;
19822 else
19823 mustend = TRUE;
19824 }
19825 p = skipwhite(p);
19826 if (mustend && *p != ')')
19827 {
19828 if (!eap->skip)
19829 EMSG2(_(e_invarg2), eap->arg);
19830 break;
19831 }
19832 }
19833 ++p; /* skip the ')' */
19834
Bram Moolenaare9a41262005-01-15 22:18:47 +000019835 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 for (;;)
19837 {
19838 p = skipwhite(p);
19839 if (STRNCMP(p, "range", 5) == 0)
19840 {
19841 flags |= FC_RANGE;
19842 p += 5;
19843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019844 else if (STRNCMP(p, "dict", 4) == 0)
19845 {
19846 flags |= FC_DICT;
19847 p += 4;
19848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 else if (STRNCMP(p, "abort", 5) == 0)
19850 {
19851 flags |= FC_ABORT;
19852 p += 5;
19853 }
19854 else
19855 break;
19856 }
19857
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019858 /* When there is a line break use what follows for the function body.
19859 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19860 if (*p == '\n')
19861 line_arg = p + 1;
19862 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 EMSG(_(e_trailing));
19864
19865 /*
19866 * Read the body of the function, until ":endfunction" is found.
19867 */
19868 if (KeyTyped)
19869 {
19870 /* Check if the function already exists, don't let the user type the
19871 * whole function before telling him it doesn't work! For a script we
19872 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019873 if (!eap->skip && !eap->forceit)
19874 {
19875 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19876 EMSG(_(e_funcdict));
19877 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019878 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019881 if (!eap->skip && did_emsg)
19882 goto erret;
19883
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 msg_putchar('\n'); /* don't overwrite the function name */
19885 cmdline_row = msg_row;
19886 }
19887
19888 indent = 2;
19889 nesting = 0;
19890 for (;;)
19891 {
19892 msg_scroll = TRUE;
19893 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019894 sourcing_lnum_off = sourcing_lnum;
19895
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019896 if (line_arg != NULL)
19897 {
19898 /* Use eap->arg, split up in parts by line breaks. */
19899 theline = line_arg;
19900 p = vim_strchr(theline, '\n');
19901 if (p == NULL)
19902 line_arg += STRLEN(line_arg);
19903 else
19904 {
19905 *p = NUL;
19906 line_arg = p + 1;
19907 }
19908 }
19909 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 theline = getcmdline(':', 0L, indent);
19911 else
19912 theline = eap->getline(':', eap->cookie, indent);
19913 if (KeyTyped)
19914 lines_left = Rows - 1;
19915 if (theline == NULL)
19916 {
19917 EMSG(_("E126: Missing :endfunction"));
19918 goto erret;
19919 }
19920
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019921 /* Detect line continuation: sourcing_lnum increased more than one. */
19922 if (sourcing_lnum > sourcing_lnum_off + 1)
19923 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19924 else
19925 sourcing_lnum_off = 0;
19926
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927 if (skip_until != NULL)
19928 {
19929 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19930 * don't check for ":endfunc". */
19931 if (STRCMP(theline, skip_until) == 0)
19932 {
19933 vim_free(skip_until);
19934 skip_until = NULL;
19935 }
19936 }
19937 else
19938 {
19939 /* skip ':' and blanks*/
19940 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19941 ;
19942
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019943 /* Check for "endfunction". */
19944 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019946 if (line_arg == NULL)
19947 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 break;
19949 }
19950
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019951 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 * at "end". */
19953 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19954 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019955 else if (STRNCMP(p, "if", 2) == 0
19956 || STRNCMP(p, "wh", 2) == 0
19957 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 || STRNCMP(p, "try", 3) == 0)
19959 indent += 2;
19960
19961 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019962 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019964 if (*p == '!')
19965 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 p += eval_fname_script(p);
19967 if (ASCII_ISALPHA(*p))
19968 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019969 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 if (*skipwhite(p) == '(')
19971 {
19972 ++nesting;
19973 indent += 2;
19974 }
19975 }
19976 }
19977
19978 /* Check for ":append" or ":insert". */
19979 p = skip_range(p, NULL);
19980 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19981 || (p[0] == 'i'
19982 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19983 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19984 skip_until = vim_strsave((char_u *)".");
19985
19986 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19987 arg = skipwhite(skiptowhite(p));
19988 if (arg[0] == '<' && arg[1] =='<'
19989 && ((p[0] == 'p' && p[1] == 'y'
19990 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19991 || (p[0] == 'p' && p[1] == 'e'
19992 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19993 || (p[0] == 't' && p[1] == 'c'
19994 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19995 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19996 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019997 || (p[0] == 'm' && p[1] == 'z'
19998 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 ))
20000 {
20001 /* ":python <<" continues until a dot, like ":append" */
20002 p = skipwhite(arg + 2);
20003 if (*p == NUL)
20004 skip_until = vim_strsave((char_u *)".");
20005 else
20006 skip_until = vim_strsave(p);
20007 }
20008 }
20009
20010 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020011 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020012 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020013 if (line_arg == NULL)
20014 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020016 }
20017
20018 /* Copy the line to newly allocated memory. get_one_sourceline()
20019 * allocates 250 bytes per line, this saves 80% on average. The cost
20020 * is an extra alloc/free. */
20021 p = vim_strsave(theline);
20022 if (p != NULL)
20023 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020024 if (line_arg == NULL)
20025 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020026 theline = p;
20027 }
20028
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020029 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20030
20031 /* Add NULL lines for continuation lines, so that the line count is
20032 * equal to the index in the growarray. */
20033 while (sourcing_lnum_off-- > 0)
20034 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020035
20036 /* Check for end of eap->arg. */
20037 if (line_arg != NULL && *line_arg == NUL)
20038 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 }
20040
20041 /* Don't define the function when skipping commands or when an error was
20042 * detected. */
20043 if (eap->skip || did_emsg)
20044 goto erret;
20045
20046 /*
20047 * If there are no errors, add the function
20048 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020049 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020050 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020051 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020053 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020054 emsg_funcname("E707: Function name conflicts with variable: %s",
20055 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020056 goto erret;
20057 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020058
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020059 fp = find_func(name);
20060 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020062 if (!eap->forceit)
20063 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020064 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020065 goto erret;
20066 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020067 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020068 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020069 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020070 name);
20071 goto erret;
20072 }
20073 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020074 ga_clear_strings(&(fp->uf_args));
20075 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020076 vim_free(name);
20077 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 }
20080 else
20081 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020082 char numbuf[20];
20083
20084 fp = NULL;
20085 if (fudi.fd_newkey == NULL && !eap->forceit)
20086 {
20087 EMSG(_(e_funcdict));
20088 goto erret;
20089 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020090 if (fudi.fd_di == NULL)
20091 {
20092 /* Can't add a function to a locked dictionary */
20093 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20094 goto erret;
20095 }
20096 /* Can't change an existing function if it is locked */
20097 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20098 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020099
20100 /* Give the function a sequential number. Can only be used with a
20101 * Funcref! */
20102 vim_free(name);
20103 sprintf(numbuf, "%d", ++func_nr);
20104 name = vim_strsave((char_u *)numbuf);
20105 if (name == NULL)
20106 goto erret;
20107 }
20108
20109 if (fp == NULL)
20110 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020111 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020112 {
20113 int slen, plen;
20114 char_u *scriptname;
20115
20116 /* Check that the autoload name matches the script name. */
20117 j = FAIL;
20118 if (sourcing_name != NULL)
20119 {
20120 scriptname = autoload_name(name);
20121 if (scriptname != NULL)
20122 {
20123 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020124 plen = (int)STRLEN(p);
20125 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020126 if (slen > plen && fnamecmp(p,
20127 sourcing_name + slen - plen) == 0)
20128 j = OK;
20129 vim_free(scriptname);
20130 }
20131 }
20132 if (j == FAIL)
20133 {
20134 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20135 goto erret;
20136 }
20137 }
20138
20139 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 if (fp == NULL)
20141 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020142
20143 if (fudi.fd_dict != NULL)
20144 {
20145 if (fudi.fd_di == NULL)
20146 {
20147 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020148 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020149 if (fudi.fd_di == NULL)
20150 {
20151 vim_free(fp);
20152 goto erret;
20153 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020154 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20155 {
20156 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020157 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020158 goto erret;
20159 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020160 }
20161 else
20162 /* overwrite existing dict entry */
20163 clear_tv(&fudi.fd_di->di_tv);
20164 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020165 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020166 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020167 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020168
20169 /* behave like "dict" was used */
20170 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020171 }
20172
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020174 STRCPY(fp->uf_name, name);
20175 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020177 fp->uf_args = newargs;
20178 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020179#ifdef FEAT_PROFILE
20180 fp->uf_tml_count = NULL;
20181 fp->uf_tml_total = NULL;
20182 fp->uf_tml_self = NULL;
20183 fp->uf_profiling = FALSE;
20184 if (prof_def_func())
20185 func_do_profile(fp);
20186#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020187 fp->uf_varargs = varargs;
20188 fp->uf_flags = flags;
20189 fp->uf_calls = 0;
20190 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020191 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192
20193erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 ga_clear_strings(&newargs);
20195 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020196ret_free:
20197 vim_free(skip_until);
20198 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201}
20202
20203/*
20204 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020205 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020207 * flags:
20208 * TFN_INT: internal function name OK
20209 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 * Advances "pp" to just after the function name (if no error).
20211 */
20212 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020213trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 char_u **pp;
20215 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020216 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020217 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020219 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 char_u *start;
20221 char_u *end;
20222 int lead;
20223 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020225 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020226
20227 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020230
20231 /* Check for hard coded <SNR>: already translated function ID (from a user
20232 * command). */
20233 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20234 && (*pp)[2] == (int)KE_SNR)
20235 {
20236 *pp += 3;
20237 len = get_id_len(pp) + 3;
20238 return vim_strnsave(start, len);
20239 }
20240
20241 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20242 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020244 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020246
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020247 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20248 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020249 if (end == start)
20250 {
20251 if (!skip)
20252 EMSG(_("E129: Function name required"));
20253 goto theend;
20254 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020255 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020256 {
20257 /*
20258 * Report an invalid expression in braces, unless the expression
20259 * evaluation has been cancelled due to an aborting error, an
20260 * interrupt, or an exception.
20261 */
20262 if (!aborting())
20263 {
20264 if (end != NULL)
20265 EMSG2(_(e_invarg2), start);
20266 }
20267 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020268 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020269 goto theend;
20270 }
20271
20272 if (lv.ll_tv != NULL)
20273 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020274 if (fdp != NULL)
20275 {
20276 fdp->fd_dict = lv.ll_dict;
20277 fdp->fd_newkey = lv.ll_newkey;
20278 lv.ll_newkey = NULL;
20279 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020280 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020281 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20282 {
20283 name = vim_strsave(lv.ll_tv->vval.v_string);
20284 *pp = end;
20285 }
20286 else
20287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020288 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20289 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020290 EMSG(_(e_funcref));
20291 else
20292 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020293 name = NULL;
20294 }
20295 goto theend;
20296 }
20297
20298 if (lv.ll_name == NULL)
20299 {
20300 /* Error found, but continue after the function name. */
20301 *pp = end;
20302 goto theend;
20303 }
20304
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020305 /* Check if the name is a Funcref. If so, use the value. */
20306 if (lv.ll_exp_name != NULL)
20307 {
20308 len = (int)STRLEN(lv.ll_exp_name);
20309 name = deref_func_name(lv.ll_exp_name, &len);
20310 if (name == lv.ll_exp_name)
20311 name = NULL;
20312 }
20313 else
20314 {
20315 len = (int)(end - *pp);
20316 name = deref_func_name(*pp, &len);
20317 if (name == *pp)
20318 name = NULL;
20319 }
20320 if (name != NULL)
20321 {
20322 name = vim_strsave(name);
20323 *pp = end;
20324 goto theend;
20325 }
20326
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020327 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020328 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020329 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020330 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20331 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20332 {
20333 /* When there was "s:" already or the name expanded to get a
20334 * leading "s:" then remove it. */
20335 lv.ll_name += 2;
20336 len -= 2;
20337 lead = 2;
20338 }
20339 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020340 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020341 {
20342 if (lead == 2) /* skip over "s:" */
20343 lv.ll_name += 2;
20344 len = (int)(end - lv.ll_name);
20345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020346
20347 /*
20348 * Copy the function name to allocated memory.
20349 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20350 * Accept <SNR>123_name() outside a script.
20351 */
20352 if (skip)
20353 lead = 0; /* do nothing */
20354 else if (lead > 0)
20355 {
20356 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020357 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20358 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020359 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020360 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020361 if (current_SID <= 0)
20362 {
20363 EMSG(_(e_usingsid));
20364 goto theend;
20365 }
20366 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20367 lead += (int)STRLEN(sid_buf);
20368 }
20369 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020370 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020371 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020372 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020373 goto theend;
20374 }
20375 name = alloc((unsigned)(len + lead + 1));
20376 if (name != NULL)
20377 {
20378 if (lead > 0)
20379 {
20380 name[0] = K_SPECIAL;
20381 name[1] = KS_EXTRA;
20382 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020383 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020384 STRCPY(name + 3, sid_buf);
20385 }
20386 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20387 name[len + lead] = NUL;
20388 }
20389 *pp = end;
20390
20391theend:
20392 clear_lval(&lv);
20393 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394}
20395
20396/*
20397 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20398 * Return 2 if "p" starts with "s:".
20399 * Return 0 otherwise.
20400 */
20401 static int
20402eval_fname_script(p)
20403 char_u *p;
20404{
20405 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20406 || STRNICMP(p + 1, "SNR>", 4) == 0))
20407 return 5;
20408 if (p[0] == 's' && p[1] == ':')
20409 return 2;
20410 return 0;
20411}
20412
20413/*
20414 * Return TRUE if "p" starts with "<SID>" or "s:".
20415 * Only works if eval_fname_script() returned non-zero for "p"!
20416 */
20417 static int
20418eval_fname_sid(p)
20419 char_u *p;
20420{
20421 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20422}
20423
20424/*
20425 * List the head of the function: "name(arg1, arg2)".
20426 */
20427 static void
20428list_func_head(fp, indent)
20429 ufunc_T *fp;
20430 int indent;
20431{
20432 int j;
20433
20434 msg_start();
20435 if (indent)
20436 MSG_PUTS(" ");
20437 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020438 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 {
20440 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020441 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 }
20443 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020444 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020446 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 {
20448 if (j)
20449 MSG_PUTS(", ");
20450 msg_puts(FUNCARG(fp, j));
20451 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020452 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
20454 if (j)
20455 MSG_PUTS(", ");
20456 MSG_PUTS("...");
20457 }
20458 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020459 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020460 if (p_verbose > 0)
20461 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462}
20463
20464/*
20465 * Find a function by name, return pointer to it in ufuncs.
20466 * Return NULL for unknown function.
20467 */
20468 static ufunc_T *
20469find_func(name)
20470 char_u *name;
20471{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020472 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020474 hi = hash_find(&func_hashtab, name);
20475 if (!HASHITEM_EMPTY(hi))
20476 return HI2UF(hi);
20477 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478}
20479
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020480#if defined(EXITFREE) || defined(PROTO)
20481 void
20482free_all_functions()
20483{
20484 hashitem_T *hi;
20485
20486 /* Need to start all over every time, because func_free() may change the
20487 * hash table. */
20488 while (func_hashtab.ht_used > 0)
20489 for (hi = func_hashtab.ht_array; ; ++hi)
20490 if (!HASHITEM_EMPTY(hi))
20491 {
20492 func_free(HI2UF(hi));
20493 break;
20494 }
20495}
20496#endif
20497
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020498/*
20499 * Return TRUE if a function "name" exists.
20500 */
20501 static int
20502function_exists(name)
20503 char_u *name;
20504{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020505 char_u *nm = name;
20506 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020507 int n = FALSE;
20508
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020509 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020510 nm = skipwhite(nm);
20511
20512 /* Only accept "funcname", "funcname ", "funcname (..." and
20513 * "funcname(...", not "funcname!...". */
20514 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020515 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020516 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020517 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020518 else
20519 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020520 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020521 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020522 return n;
20523}
20524
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020525/*
20526 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020527 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020528 */
20529 static int
20530builtin_function(name)
20531 char_u *name;
20532{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020533 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20534 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020535}
20536
Bram Moolenaar05159a02005-02-26 23:04:13 +000020537#if defined(FEAT_PROFILE) || defined(PROTO)
20538/*
20539 * Start profiling function "fp".
20540 */
20541 static void
20542func_do_profile(fp)
20543 ufunc_T *fp;
20544{
20545 fp->uf_tm_count = 0;
20546 profile_zero(&fp->uf_tm_self);
20547 profile_zero(&fp->uf_tm_total);
20548 if (fp->uf_tml_count == NULL)
20549 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20550 (sizeof(int) * fp->uf_lines.ga_len));
20551 if (fp->uf_tml_total == NULL)
20552 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20553 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20554 if (fp->uf_tml_self == NULL)
20555 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20556 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20557 fp->uf_tml_idx = -1;
20558 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20559 || fp->uf_tml_self == NULL)
20560 return; /* out of memory */
20561
20562 fp->uf_profiling = TRUE;
20563}
20564
20565/*
20566 * Dump the profiling results for all functions in file "fd".
20567 */
20568 void
20569func_dump_profile(fd)
20570 FILE *fd;
20571{
20572 hashitem_T *hi;
20573 int todo;
20574 ufunc_T *fp;
20575 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020576 ufunc_T **sorttab;
20577 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020579 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000020580 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20581
Bram Moolenaar05159a02005-02-26 23:04:13 +000020582 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20583 {
20584 if (!HASHITEM_EMPTY(hi))
20585 {
20586 --todo;
20587 fp = HI2UF(hi);
20588 if (fp->uf_profiling)
20589 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020590 if (sorttab != NULL)
20591 sorttab[st_len++] = fp;
20592
Bram Moolenaar05159a02005-02-26 23:04:13 +000020593 if (fp->uf_name[0] == K_SPECIAL)
20594 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20595 else
20596 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20597 if (fp->uf_tm_count == 1)
20598 fprintf(fd, "Called 1 time\n");
20599 else
20600 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20601 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20602 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20603 fprintf(fd, "\n");
20604 fprintf(fd, "count total (s) self (s)\n");
20605
20606 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20607 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020608 if (FUNCLINE(fp, i) == NULL)
20609 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020610 prof_func_line(fd, fp->uf_tml_count[i],
20611 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020612 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20613 }
20614 fprintf(fd, "\n");
20615 }
20616 }
20617 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020618
20619 if (sorttab != NULL && st_len > 0)
20620 {
20621 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20622 prof_total_cmp);
20623 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20624 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20625 prof_self_cmp);
20626 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20627 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020628}
Bram Moolenaar73830342005-02-28 22:48:19 +000020629
20630 static void
20631prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20632 FILE *fd;
20633 ufunc_T **sorttab;
20634 int st_len;
20635 char *title;
20636 int prefer_self; /* when equal print only self time */
20637{
20638 int i;
20639 ufunc_T *fp;
20640
20641 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20642 fprintf(fd, "count total (s) self (s) function\n");
20643 for (i = 0; i < 20 && i < st_len; ++i)
20644 {
20645 fp = sorttab[i];
20646 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20647 prefer_self);
20648 if (fp->uf_name[0] == K_SPECIAL)
20649 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20650 else
20651 fprintf(fd, " %s()\n", fp->uf_name);
20652 }
20653 fprintf(fd, "\n");
20654}
20655
20656/*
20657 * Print the count and times for one function or function line.
20658 */
20659 static void
20660prof_func_line(fd, count, total, self, prefer_self)
20661 FILE *fd;
20662 int count;
20663 proftime_T *total;
20664 proftime_T *self;
20665 int prefer_self; /* when equal print only self time */
20666{
20667 if (count > 0)
20668 {
20669 fprintf(fd, "%5d ", count);
20670 if (prefer_self && profile_equal(total, self))
20671 fprintf(fd, " ");
20672 else
20673 fprintf(fd, "%s ", profile_msg(total));
20674 if (!prefer_self && profile_equal(total, self))
20675 fprintf(fd, " ");
20676 else
20677 fprintf(fd, "%s ", profile_msg(self));
20678 }
20679 else
20680 fprintf(fd, " ");
20681}
20682
20683/*
20684 * Compare function for total time sorting.
20685 */
20686 static int
20687#ifdef __BORLANDC__
20688_RTLENTRYF
20689#endif
20690prof_total_cmp(s1, s2)
20691 const void *s1;
20692 const void *s2;
20693{
20694 ufunc_T *p1, *p2;
20695
20696 p1 = *(ufunc_T **)s1;
20697 p2 = *(ufunc_T **)s2;
20698 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20699}
20700
20701/*
20702 * Compare function for self time sorting.
20703 */
20704 static int
20705#ifdef __BORLANDC__
20706_RTLENTRYF
20707#endif
20708prof_self_cmp(s1, s2)
20709 const void *s1;
20710 const void *s2;
20711{
20712 ufunc_T *p1, *p2;
20713
20714 p1 = *(ufunc_T **)s1;
20715 p2 = *(ufunc_T **)s2;
20716 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20717}
20718
Bram Moolenaar05159a02005-02-26 23:04:13 +000020719#endif
20720
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020721/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020722 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020723 * Return TRUE if a package was loaded.
20724 */
20725 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020726script_autoload(name, reload)
20727 char_u *name;
20728 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020729{
20730 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020731 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020732 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020733 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020734
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020735 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020736 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020737 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020738 return FALSE;
20739
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020740 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020741
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020742 /* Find the name in the list of previously loaded package names. Skip
20743 * "autoload/", it's always the same. */
20744 for (i = 0; i < ga_loaded.ga_len; ++i)
20745 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20746 break;
20747 if (!reload && i < ga_loaded.ga_len)
20748 ret = FALSE; /* was loaded already */
20749 else
20750 {
20751 /* Remember the name if it wasn't loaded already. */
20752 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20753 {
20754 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20755 tofree = NULL;
20756 }
20757
20758 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020759 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020760 ret = TRUE;
20761 }
20762
20763 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020764 return ret;
20765}
20766
20767/*
20768 * Return the autoload script name for a function or variable name.
20769 * Returns NULL when out of memory.
20770 */
20771 static char_u *
20772autoload_name(name)
20773 char_u *name;
20774{
20775 char_u *p;
20776 char_u *scriptname;
20777
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020778 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020779 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20780 if (scriptname == NULL)
20781 return FALSE;
20782 STRCPY(scriptname, "autoload/");
20783 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020784 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020785 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020786 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020787 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020788 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020789}
20790
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20792
20793/*
20794 * Function given to ExpandGeneric() to obtain the list of user defined
20795 * function names.
20796 */
20797 char_u *
20798get_user_func_name(xp, idx)
20799 expand_T *xp;
20800 int idx;
20801{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020802 static long_u done;
20803 static hashitem_T *hi;
20804 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805
20806 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020808 done = 0;
20809 hi = func_hashtab.ht_array;
20810 }
20811 if (done < func_hashtab.ht_used)
20812 {
20813 if (done++ > 0)
20814 ++hi;
20815 while (HASHITEM_EMPTY(hi))
20816 ++hi;
20817 fp = HI2UF(hi);
20818
20819 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20820 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821
20822 cat_func_name(IObuff, fp);
20823 if (xp->xp_context != EXPAND_USER_FUNC)
20824 {
20825 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020826 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 STRCAT(IObuff, ")");
20828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 return IObuff;
20830 }
20831 return NULL;
20832}
20833
20834#endif /* FEAT_CMDL_COMPL */
20835
20836/*
20837 * Copy the function name of "fp" to buffer "buf".
20838 * "buf" must be able to hold the function name plus three bytes.
20839 * Takes care of script-local function names.
20840 */
20841 static void
20842cat_func_name(buf, fp)
20843 char_u *buf;
20844 ufunc_T *fp;
20845{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020846 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 {
20848 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020849 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 }
20851 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020852 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853}
20854
20855/*
20856 * ":delfunction {name}"
20857 */
20858 void
20859ex_delfunction(eap)
20860 exarg_T *eap;
20861{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020862 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 char_u *p;
20864 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020865 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866
20867 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020868 name = trans_function_name(&p, eap->skip, 0, &fudi);
20869 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020871 {
20872 if (fudi.fd_dict != NULL && !eap->skip)
20873 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 if (!ends_excmd(*skipwhite(p)))
20877 {
20878 vim_free(name);
20879 EMSG(_(e_trailing));
20880 return;
20881 }
20882 eap->nextcmd = check_nextcmd(p);
20883 if (eap->nextcmd != NULL)
20884 *p = NUL;
20885
20886 if (!eap->skip)
20887 fp = find_func(name);
20888 vim_free(name);
20889
20890 if (!eap->skip)
20891 {
20892 if (fp == NULL)
20893 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020894 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 return;
20896 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020897 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 {
20899 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20900 return;
20901 }
20902
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020903 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020905 /* Delete the dict item that refers to the function, it will
20906 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020907 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020909 else
20910 func_free(fp);
20911 }
20912}
20913
20914/*
20915 * Free a function and remove it from the list of functions.
20916 */
20917 static void
20918func_free(fp)
20919 ufunc_T *fp;
20920{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020921 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020922
20923 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020924 ga_clear_strings(&(fp->uf_args));
20925 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020926#ifdef FEAT_PROFILE
20927 vim_free(fp->uf_tml_count);
20928 vim_free(fp->uf_tml_total);
20929 vim_free(fp->uf_tml_self);
20930#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020931
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020932 /* remove the function from the function hashtable */
20933 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20934 if (HASHITEM_EMPTY(hi))
20935 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020936 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020937 hash_remove(&func_hashtab, hi);
20938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 vim_free(fp);
20940}
20941
20942/*
20943 * Unreference a Function: decrement the reference count and free it when it
20944 * becomes zero. Only for numbered functions.
20945 */
20946 static void
20947func_unref(name)
20948 char_u *name;
20949{
20950 ufunc_T *fp;
20951
20952 if (name != NULL && isdigit(*name))
20953 {
20954 fp = find_func(name);
20955 if (fp == NULL)
20956 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020957 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020958 {
20959 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020960 * when "uf_calls" becomes zero. */
20961 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020962 func_free(fp);
20963 }
20964 }
20965}
20966
20967/*
20968 * Count a reference to a Function.
20969 */
20970 static void
20971func_ref(name)
20972 char_u *name;
20973{
20974 ufunc_T *fp;
20975
20976 if (name != NULL && isdigit(*name))
20977 {
20978 fp = find_func(name);
20979 if (fp == NULL)
20980 EMSG2(_(e_intern2), "func_ref()");
20981 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020982 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 }
20984}
20985
20986/*
20987 * Call a user function.
20988 */
20989 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020990call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 ufunc_T *fp; /* pointer to function */
20992 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020993 typval_T *argvars; /* arguments */
20994 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 linenr_T firstline; /* first line of range */
20996 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020997 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998{
Bram Moolenaar33570922005-01-25 22:26:29 +000020999 char_u *save_sourcing_name;
21000 linenr_T save_sourcing_lnum;
21001 scid_T save_current_SID;
21002 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021003 int save_did_emsg;
21004 static int depth = 0;
21005 dictitem_T *v;
21006 int fixvar_idx = 0; /* index in fixvar[] */
21007 int i;
21008 int ai;
21009 char_u numbuf[NUMBUFLEN];
21010 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021011#ifdef FEAT_PROFILE
21012 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021013 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015
21016 /* If depth of calling is getting too high, don't execute the function */
21017 if (depth >= p_mfd)
21018 {
21019 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021020 rettv->v_type = VAR_NUMBER;
21021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 return;
21023 }
21024 ++depth;
21025
21026 line_breakcheck(); /* check for CTRL-C hit */
21027
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021028 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000021029 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021031 fc.rettv = rettv;
21032 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 fc.linenr = 0;
21034 fc.returned = FALSE;
21035 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021037 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 fc.dbg_tick = debug_tick;
21039
Bram Moolenaar33570922005-01-25 22:26:29 +000021040 /*
21041 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21042 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21043 * each argument variable and saves a lot of time.
21044 */
21045 /*
21046 * Init l: variables.
21047 */
21048 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021049 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021050 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021051 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21052 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021054 name = v->di_key;
21055 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021056 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
21057 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
21058 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021059 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021060 v->di_tv.vval.v_dict = selfdict;
21061 ++selfdict->dv_refcount;
21062 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021063
Bram Moolenaar33570922005-01-25 22:26:29 +000021064 /*
21065 * Init a: variables.
21066 * Set a:0 to "argcount".
21067 * Set a:000 to a list with room for the "..." arguments.
21068 */
21069 init_var_dict(&fc.l_avars, &fc.l_avars_var);
21070 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021071 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000021072 v = &fc.fixvar[fixvar_idx++].var;
21073 STRCPY(v->di_key, "000");
21074 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21075 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21076 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021077 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021078 v->di_tv.vval.v_list = &fc.l_varlist;
21079 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
21080 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000021081 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021082
21083 /*
21084 * Set a:firstline to "firstline" and a:lastline to "lastline".
21085 * Set a:name to named arguments.
21086 * Set a:N to the "..." arguments.
21087 */
21088 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
21089 (varnumber_T)firstline);
21090 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
21091 (varnumber_T)lastline);
21092 for (i = 0; i < argcount; ++i)
21093 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021094 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021095 if (ai < 0)
21096 /* named argument a:name */
21097 name = FUNCARG(fp, i);
21098 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021099 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021100 /* "..." argument a:1, a:2, etc. */
21101 sprintf((char *)numbuf, "%d", ai + 1);
21102 name = numbuf;
21103 }
21104 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21105 {
21106 v = &fc.fixvar[fixvar_idx++].var;
21107 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21108 }
21109 else
21110 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021111 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21112 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021113 if (v == NULL)
21114 break;
21115 v->di_flags = DI_FLAGS_RO;
21116 }
21117 STRCPY(v->di_key, name);
21118 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21119
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021120 /* Note: the values are copied directly to avoid alloc/free.
21121 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021123 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021124
21125 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21126 {
21127 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
21128 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021129 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021130 }
21131 }
21132
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 /* Don't redraw while executing the function. */
21134 ++RedrawingDisabled;
21135 save_sourcing_name = sourcing_name;
21136 save_sourcing_lnum = sourcing_lnum;
21137 sourcing_lnum = 1;
21138 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021139 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 if (sourcing_name != NULL)
21141 {
21142 if (save_sourcing_name != NULL
21143 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21144 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21145 else
21146 STRCPY(sourcing_name, "function ");
21147 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21148
21149 if (p_verbose >= 12)
21150 {
21151 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021152 verbose_enter_scroll();
21153
Bram Moolenaar555b2802005-05-19 21:08:39 +000021154 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155 if (p_verbose >= 14)
21156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021158 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021159 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021160 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161
21162 msg_puts((char_u *)"(");
21163 for (i = 0; i < argcount; ++i)
21164 {
21165 if (i > 0)
21166 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021167 if (argvars[i].v_type == VAR_NUMBER)
21168 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 else
21170 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021171 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21172 if (s != NULL)
21173 {
21174 trunc_string(s, buf, MSG_BUF_CLEN);
21175 msg_puts(buf);
21176 vim_free(tofree);
21177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 }
21179 }
21180 msg_puts((char_u *)")");
21181 }
21182 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021183
21184 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 --no_wait_return;
21186 }
21187 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021188#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021189 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021190 {
21191 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21192 func_do_profile(fp);
21193 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021194 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021195 {
21196 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021197 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021198 profile_zero(&fp->uf_tm_children);
21199 }
21200 script_prof_save(&wait_start);
21201 }
21202#endif
21203
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021205 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 save_did_emsg = did_emsg;
21207 did_emsg = FALSE;
21208
21209 /* call do_cmdline() to execute the lines */
21210 do_cmdline(NULL, get_func_line, (void *)&fc,
21211 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21212
21213 --RedrawingDisabled;
21214
21215 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021216 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021218 clear_tv(rettv);
21219 rettv->v_type = VAR_NUMBER;
21220 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 }
21222
Bram Moolenaar05159a02005-02-26 23:04:13 +000021223#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021224 if (do_profiling == PROF_YES && (fp->uf_profiling
21225 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021226 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021227 profile_end(&call_start);
21228 profile_sub_wait(&wait_start, &call_start);
21229 profile_add(&fp->uf_tm_total, &call_start);
21230 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021231 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021232 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021233 profile_add(&fc.caller->func->uf_tm_children, &call_start);
21234 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021235 }
21236 }
21237#endif
21238
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 /* when being verbose, mention the return value */
21240 if (p_verbose >= 12)
21241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021243 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021246 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021247 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021248 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
21249 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021252 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021253 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021254 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021255 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021256
Bram Moolenaar555b2802005-05-19 21:08:39 +000021257 /* The value may be very long. Skip the middle part, so that we
21258 * have some idea how it starts and ends. smsg() would always
21259 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021260 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
21261 if (s != NULL)
21262 {
21263 trunc_string(s, buf, MSG_BUF_CLEN);
21264 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21265 vim_free(tofree);
21266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 }
21268 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021269
21270 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 --no_wait_return;
21272 }
21273
21274 vim_free(sourcing_name);
21275 sourcing_name = save_sourcing_name;
21276 sourcing_lnum = save_sourcing_lnum;
21277 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021278#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021279 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021280 script_prof_restore(&wait_start);
21281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282
21283 if (p_verbose >= 12 && sourcing_name != NULL)
21284 {
21285 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021286 verbose_enter_scroll();
21287
Bram Moolenaar555b2802005-05-19 21:08:39 +000021288 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021290
21291 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 --no_wait_return;
21293 }
21294
21295 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021296 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021298 /* The a: variables typevals were not allocated, only free the allocated
Bram Moolenaar33570922005-01-25 22:26:29 +000021299 * variables. */
21300 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
21301
21302 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 --depth;
21304}
21305
21306/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021307 * Add a number variable "name" to dict "dp" with value "nr".
21308 */
21309 static void
21310add_nr_var(dp, v, name, nr)
21311 dict_T *dp;
21312 dictitem_T *v;
21313 char *name;
21314 varnumber_T nr;
21315{
21316 STRCPY(v->di_key, name);
21317 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21318 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21319 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021320 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 v->di_tv.vval.v_number = nr;
21322}
21323
21324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 * ":return [expr]"
21326 */
21327 void
21328ex_return(eap)
21329 exarg_T *eap;
21330{
21331 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021332 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 int returning = FALSE;
21334
21335 if (current_funccal == NULL)
21336 {
21337 EMSG(_("E133: :return not inside a function"));
21338 return;
21339 }
21340
21341 if (eap->skip)
21342 ++emsg_skip;
21343
21344 eap->nextcmd = NULL;
21345 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021346 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 {
21348 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021349 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 }
21353 /* It's safer to return also on error. */
21354 else if (!eap->skip)
21355 {
21356 /*
21357 * Return unless the expression evaluation has been cancelled due to an
21358 * aborting error, an interrupt, or an exception.
21359 */
21360 if (!aborting())
21361 returning = do_return(eap, FALSE, TRUE, NULL);
21362 }
21363
21364 /* When skipping or the return gets pending, advance to the next command
21365 * in this line (!returning). Otherwise, ignore the rest of the line.
21366 * Following lines will be ignored by get_func_line(). */
21367 if (returning)
21368 eap->nextcmd = NULL;
21369 else if (eap->nextcmd == NULL) /* no argument */
21370 eap->nextcmd = check_nextcmd(arg);
21371
21372 if (eap->skip)
21373 --emsg_skip;
21374}
21375
21376/*
21377 * Return from a function. Possibly makes the return pending. Also called
21378 * for a pending return at the ":endtry" or after returning from an extra
21379 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021381 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 * FALSE when the return gets pending.
21383 */
21384 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021385do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 exarg_T *eap;
21387 int reanimate;
21388 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021389 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390{
21391 int idx;
21392 struct condstack *cstack = eap->cstack;
21393
21394 if (reanimate)
21395 /* Undo the return. */
21396 current_funccal->returned = FALSE;
21397
21398 /*
21399 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21400 * not in its finally clause (which then is to be executed next) is found.
21401 * In this case, make the ":return" pending for execution at the ":endtry".
21402 * Otherwise, return normally.
21403 */
21404 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21405 if (idx >= 0)
21406 {
21407 cstack->cs_pending[idx] = CSTP_RETURN;
21408
21409 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021410 /* A pending return again gets pending. "rettv" points to an
21411 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021413 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 else
21415 {
21416 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021417 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021419 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021421 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 {
21423 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021424 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021425 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 else
21427 EMSG(_(e_outofmem));
21428 }
21429 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021430 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431
21432 if (reanimate)
21433 {
21434 /* The pending return value could be overwritten by a ":return"
21435 * without argument in a finally clause; reset the default
21436 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021437 current_funccal->rettv->v_type = VAR_NUMBER;
21438 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 }
21440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021441 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 }
21443 else
21444 {
21445 current_funccal->returned = TRUE;
21446
21447 /* If the return is carried out now, store the return value. For
21448 * a return immediately after reanimation, the value is already
21449 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021453 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021455 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 }
21457 }
21458
21459 return idx < 0;
21460}
21461
21462/*
21463 * Free the variable with a pending return value.
21464 */
21465 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021466discard_pending_return(rettv)
21467 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468{
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470}
21471
21472/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021473 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 * is an allocated string. Used by report_pending() for verbose messages.
21475 */
21476 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021477get_return_cmd(rettv)
21478 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021480 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021481 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021482 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021484 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021485 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021486 if (s == NULL)
21487 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021488
21489 STRCPY(IObuff, ":return ");
21490 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21491 if (STRLEN(s) + 8 >= IOSIZE)
21492 STRCPY(IObuff + IOSIZE - 4, "...");
21493 vim_free(tofree);
21494 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495}
21496
21497/*
21498 * Get next function line.
21499 * Called by do_cmdline() to get the next line.
21500 * Returns allocated string, or NULL for end of function.
21501 */
21502/* ARGSUSED */
21503 char_u *
21504get_func_line(c, cookie, indent)
21505 int c; /* not used */
21506 void *cookie;
21507 int indent; /* not used */
21508{
Bram Moolenaar33570922005-01-25 22:26:29 +000021509 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021510 ufunc_T *fp = fcp->func;
21511 char_u *retval;
21512 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513
21514 /* If breakpoints have been added/deleted need to check for it. */
21515 if (fcp->dbg_tick != debug_tick)
21516 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021517 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 sourcing_lnum);
21519 fcp->dbg_tick = debug_tick;
21520 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021521#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021522 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021523 func_line_end(cookie);
21524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525
Bram Moolenaar05159a02005-02-26 23:04:13 +000021526 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021527 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21528 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 retval = NULL;
21530 else
21531 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021532 /* Skip NULL lines (continuation lines). */
21533 while (fcp->linenr < gap->ga_len
21534 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21535 ++fcp->linenr;
21536 if (fcp->linenr >= gap->ga_len)
21537 retval = NULL;
21538 else
21539 {
21540 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21541 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021542#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021543 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021544 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021545#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 }
21548
21549 /* Did we encounter a breakpoint? */
21550 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21551 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021552 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021554 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 sourcing_lnum);
21556 fcp->dbg_tick = debug_tick;
21557 }
21558
21559 return retval;
21560}
21561
Bram Moolenaar05159a02005-02-26 23:04:13 +000021562#if defined(FEAT_PROFILE) || defined(PROTO)
21563/*
21564 * Called when starting to read a function line.
21565 * "sourcing_lnum" must be correct!
21566 * When skipping lines it may not actually be executed, but we won't find out
21567 * until later and we need to store the time now.
21568 */
21569 void
21570func_line_start(cookie)
21571 void *cookie;
21572{
21573 funccall_T *fcp = (funccall_T *)cookie;
21574 ufunc_T *fp = fcp->func;
21575
21576 if (fp->uf_profiling && sourcing_lnum >= 1
21577 && sourcing_lnum <= fp->uf_lines.ga_len)
21578 {
21579 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021580 /* Skip continuation lines. */
21581 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21582 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021583 fp->uf_tml_execed = FALSE;
21584 profile_start(&fp->uf_tml_start);
21585 profile_zero(&fp->uf_tml_children);
21586 profile_get_wait(&fp->uf_tml_wait);
21587 }
21588}
21589
21590/*
21591 * Called when actually executing a function line.
21592 */
21593 void
21594func_line_exec(cookie)
21595 void *cookie;
21596{
21597 funccall_T *fcp = (funccall_T *)cookie;
21598 ufunc_T *fp = fcp->func;
21599
21600 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21601 fp->uf_tml_execed = TRUE;
21602}
21603
21604/*
21605 * Called when done with a function line.
21606 */
21607 void
21608func_line_end(cookie)
21609 void *cookie;
21610{
21611 funccall_T *fcp = (funccall_T *)cookie;
21612 ufunc_T *fp = fcp->func;
21613
21614 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21615 {
21616 if (fp->uf_tml_execed)
21617 {
21618 ++fp->uf_tml_count[fp->uf_tml_idx];
21619 profile_end(&fp->uf_tml_start);
21620 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021621 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021622 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21623 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021624 }
21625 fp->uf_tml_idx = -1;
21626 }
21627}
21628#endif
21629
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630/*
21631 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021632 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 */
21634 int
21635func_has_ended(cookie)
21636 void *cookie;
21637{
Bram Moolenaar33570922005-01-25 22:26:29 +000021638 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639
21640 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21641 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021642 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 || fcp->returned);
21644}
21645
21646/*
21647 * return TRUE if cookie indicates a function which "abort"s on errors.
21648 */
21649 int
21650func_has_abort(cookie)
21651 void *cookie;
21652{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021653 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654}
21655
21656#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21657typedef enum
21658{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021659 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21660 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21661 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662} var_flavour_T;
21663
21664static var_flavour_T var_flavour __ARGS((char_u *varname));
21665
21666 static var_flavour_T
21667var_flavour(varname)
21668 char_u *varname;
21669{
21670 char_u *p = varname;
21671
21672 if (ASCII_ISUPPER(*p))
21673 {
21674 while (*(++p))
21675 if (ASCII_ISLOWER(*p))
21676 return VAR_FLAVOUR_SESSION;
21677 return VAR_FLAVOUR_VIMINFO;
21678 }
21679 else
21680 return VAR_FLAVOUR_DEFAULT;
21681}
21682#endif
21683
21684#if defined(FEAT_VIMINFO) || defined(PROTO)
21685/*
21686 * Restore global vars that start with a capital from the viminfo file
21687 */
21688 int
21689read_viminfo_varlist(virp, writing)
21690 vir_T *virp;
21691 int writing;
21692{
21693 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021694 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021695 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696
21697 if (!writing && (find_viminfo_parameter('!') != NULL))
21698 {
21699 tab = vim_strchr(virp->vir_line + 1, '\t');
21700 if (tab != NULL)
21701 {
21702 *tab++ = '\0'; /* isolate the variable name */
21703 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021704 type = VAR_STRING;
21705#ifdef FEAT_FLOAT
21706 else if (*tab == 'F')
21707 type = VAR_FLOAT;
21708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709
21710 tab = vim_strchr(tab, '\t');
21711 if (tab != NULL)
21712 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021713 tv.v_type = type;
21714 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021715 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021717#ifdef FEAT_FLOAT
21718 else if (type == VAR_FLOAT)
21719 (void)string2float(tab + 1, &tv.vval.v_float);
21720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021722 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021723 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021724 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021725 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 }
21727 }
21728 }
21729
21730 return viminfo_readline(virp);
21731}
21732
21733/*
21734 * Write global vars that start with a capital to the viminfo file
21735 */
21736 void
21737write_viminfo_varlist(fp)
21738 FILE *fp;
21739{
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 hashitem_T *hi;
21741 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021742 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021743 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021744 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021745 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021746 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747
21748 if (find_viminfo_parameter('!') == NULL)
21749 return;
21750
21751 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021752
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021753 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021756 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021758 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021759 this_var = HI2DI(hi);
21760 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021761 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021763 {
21764 case VAR_STRING: s = "STR"; break;
21765 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021766#ifdef FEAT_FLOAT
21767 case VAR_FLOAT: s = "FLO"; break;
21768#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021769 default: continue;
21770 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021772 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021773 if (p != NULL)
21774 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021775 vim_free(tofree);
21776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 }
21778 }
21779}
21780#endif
21781
21782#if defined(FEAT_SESSION) || defined(PROTO)
21783 int
21784store_session_globals(fd)
21785 FILE *fd;
21786{
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 hashitem_T *hi;
21788 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021789 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 char_u *p, *t;
21791
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021792 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021793 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021795 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021797 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021798 this_var = HI2DI(hi);
21799 if ((this_var->di_tv.v_type == VAR_NUMBER
21800 || this_var->di_tv.v_type == VAR_STRING)
21801 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021802 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021803 /* Escape special characters with a backslash. Turn a LF and
21804 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021806 (char_u *)"\\\"\n\r");
21807 if (p == NULL) /* out of memory */
21808 break;
21809 for (t = p; *t != NUL; ++t)
21810 if (*t == '\n')
21811 *t = 'n';
21812 else if (*t == '\r')
21813 *t = 'r';
21814 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021815 this_var->di_key,
21816 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21817 : ' ',
21818 p,
21819 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21820 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021821 || put_eol(fd) == FAIL)
21822 {
21823 vim_free(p);
21824 return FAIL;
21825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 vim_free(p);
21827 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021828#ifdef FEAT_FLOAT
21829 else if (this_var->di_tv.v_type == VAR_FLOAT
21830 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21831 {
21832 float_T f = this_var->di_tv.vval.v_float;
21833 int sign = ' ';
21834
21835 if (f < 0)
21836 {
21837 f = -f;
21838 sign = '-';
21839 }
21840 if ((fprintf(fd, "let %s = %c&%f",
21841 this_var->di_key, sign, f) < 0)
21842 || put_eol(fd) == FAIL)
21843 return FAIL;
21844 }
21845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 }
21847 }
21848 return OK;
21849}
21850#endif
21851
Bram Moolenaar661b1822005-07-28 22:36:45 +000021852/*
21853 * Display script name where an item was last set.
21854 * Should only be invoked when 'verbose' is non-zero.
21855 */
21856 void
21857last_set_msg(scriptID)
21858 scid_T scriptID;
21859{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021860 char_u *p;
21861
Bram Moolenaar661b1822005-07-28 22:36:45 +000021862 if (scriptID != 0)
21863 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021864 p = home_replace_save(NULL, get_scriptname(scriptID));
21865 if (p != NULL)
21866 {
21867 verbose_enter();
21868 MSG_PUTS(_("\n\tLast set from "));
21869 MSG_PUTS(p);
21870 vim_free(p);
21871 verbose_leave();
21872 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021873 }
21874}
21875
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876#endif /* FEAT_EVAL */
21877
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021879#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880
21881#ifdef WIN3264
21882/*
21883 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21884 */
21885static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21886static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21887static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21888
21889/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021890 * Get the short path (8.3) for the filename in "fnamep".
21891 * Only works for a valid file name.
21892 * When the path gets longer "fnamep" is changed and the allocated buffer
21893 * is put in "bufp".
21894 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21895 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 */
21897 static int
21898get_short_pathname(fnamep, bufp, fnamelen)
21899 char_u **fnamep;
21900 char_u **bufp;
21901 int *fnamelen;
21902{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021903 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904 char_u *newbuf;
21905
21906 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 l = GetShortPathName(*fnamep, *fnamep, len);
21908 if (l > len - 1)
21909 {
21910 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021911 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 newbuf = vim_strnsave(*fnamep, l);
21913 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915
21916 vim_free(*bufp);
21917 *fnamep = *bufp = newbuf;
21918
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021919 /* Really should always succeed, as the buffer is big enough. */
21920 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 }
21922
21923 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021924 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925}
21926
21927/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021928 * Get the short path (8.3) for the filename in "fname". The converted
21929 * path is returned in "bufp".
21930 *
21931 * Some of the directories specified in "fname" may not exist. This function
21932 * will shorten the existing directories at the beginning of the path and then
21933 * append the remaining non-existing path.
21934 *
21935 * fname - Pointer to the filename to shorten. On return, contains the
21936 * pointer to the shortened pathname
21937 * bufp - Pointer to an allocated buffer for the filename.
21938 * fnamelen - Length of the filename pointed to by fname
21939 *
21940 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 */
21942 static int
21943shortpath_for_invalid_fname(fname, bufp, fnamelen)
21944 char_u **fname;
21945 char_u **bufp;
21946 int *fnamelen;
21947{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021948 char_u *short_fname, *save_fname, *pbuf_unused;
21949 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021951 int old_len, len;
21952 int new_len, sfx_len;
21953 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954
21955 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021956 old_len = *fnamelen;
21957 save_fname = vim_strnsave(*fname, old_len);
21958 pbuf_unused = NULL;
21959 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021961 endp = save_fname + old_len - 1; /* Find the end of the copy */
21962 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021964 /*
21965 * Try shortening the supplied path till it succeeds by removing one
21966 * directory at a time from the tail of the path.
21967 */
21968 len = 0;
21969 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021971 /* go back one path-separator */
21972 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
21973 --endp;
21974 if (endp <= save_fname)
21975 break; /* processed the complete path */
21976
21977 /*
21978 * Replace the path separator with a NUL and try to shorten the
21979 * resulting path.
21980 */
21981 ch = *endp;
21982 *endp = 0;
21983 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000021984 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021985 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
21986 {
21987 retval = FAIL;
21988 goto theend;
21989 }
21990 *endp = ch; /* preserve the string */
21991
21992 if (len > 0)
21993 break; /* successfully shortened the path */
21994
21995 /* failed to shorten the path. Skip the path separator */
21996 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 }
21998
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021999 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022001 /*
22002 * Succeeded in shortening the path. Now concatenate the shortened
22003 * path with the remaining path at the tail.
22004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022006 /* Compute the length of the new path. */
22007 sfx_len = (int)(save_endp - endp) + 1;
22008 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022010 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022012 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022014 /* There is not enough space in the currently allocated string,
22015 * copy it to a buffer big enough. */
22016 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022018 {
22019 retval = FAIL;
22020 goto theend;
22021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 }
22023 else
22024 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022025 /* Transfer short_fname to the main buffer (it's big enough),
22026 * unless get_short_pathname() did its work in-place. */
22027 *fname = *bufp = save_fname;
22028 if (short_fname != save_fname)
22029 vim_strncpy(save_fname, short_fname, len);
22030 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022032
22033 /* concat the not-shortened part of the path */
22034 vim_strncpy(*fname + len, endp, sfx_len);
22035 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022037
22038theend:
22039 vim_free(pbuf_unused);
22040 vim_free(save_fname);
22041
22042 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043}
22044
22045/*
22046 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022047 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 */
22049 static int
22050shortpath_for_partial(fnamep, bufp, fnamelen)
22051 char_u **fnamep;
22052 char_u **bufp;
22053 int *fnamelen;
22054{
22055 int sepcount, len, tflen;
22056 char_u *p;
22057 char_u *pbuf, *tfname;
22058 int hasTilde;
22059
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022060 /* Count up the path separators from the RHS.. so we know which part
22061 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022063 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 if (vim_ispathsep(*p))
22065 ++sepcount;
22066
22067 /* Need full path first (use expand_env() to remove a "~/") */
22068 hasTilde = (**fnamep == '~');
22069 if (hasTilde)
22070 pbuf = tfname = expand_env_save(*fnamep);
22071 else
22072 pbuf = tfname = FullName_save(*fnamep, FALSE);
22073
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022074 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022076 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22077 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078
22079 if (len == 0)
22080 {
22081 /* Don't have a valid filename, so shorten the rest of the
22082 * path if we can. This CAN give us invalid 8.3 filenames, but
22083 * there's not a lot of point in guessing what it might be.
22084 */
22085 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022086 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22087 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 }
22089
22090 /* Count the paths backward to find the beginning of the desired string. */
22091 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022092 {
22093#ifdef FEAT_MBYTE
22094 if (has_mbyte)
22095 p -= mb_head_off(tfname, p);
22096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 if (vim_ispathsep(*p))
22098 {
22099 if (sepcount == 0 || (hasTilde && sepcount == 1))
22100 break;
22101 else
22102 sepcount --;
22103 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 if (hasTilde)
22106 {
22107 --p;
22108 if (p >= tfname)
22109 *p = '~';
22110 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022111 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 }
22113 else
22114 ++p;
22115
22116 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22117 vim_free(*bufp);
22118 *fnamelen = (int)STRLEN(p);
22119 *bufp = pbuf;
22120 *fnamep = p;
22121
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022122 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123}
22124#endif /* WIN3264 */
22125
22126/*
22127 * Adjust a filename, according to a string of modifiers.
22128 * *fnamep must be NUL terminated when called. When returning, the length is
22129 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022130 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 * When there is an error, *fnamep is set to NULL.
22132 */
22133 int
22134modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22135 char_u *src; /* string with modifiers */
22136 int *usedlen; /* characters after src that are used */
22137 char_u **fnamep; /* file name so far */
22138 char_u **bufp; /* buffer for allocated file name or NULL */
22139 int *fnamelen; /* length of fnamep */
22140{
22141 int valid = 0;
22142 char_u *tail;
22143 char_u *s, *p, *pbuf;
22144 char_u dirname[MAXPATHL];
22145 int c;
22146 int has_fullname = 0;
22147#ifdef WIN3264
22148 int has_shortname = 0;
22149#endif
22150
22151repeat:
22152 /* ":p" - full path/file_name */
22153 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22154 {
22155 has_fullname = 1;
22156
22157 valid |= VALID_PATH;
22158 *usedlen += 2;
22159
22160 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22161 if ((*fnamep)[0] == '~'
22162#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22163 && ((*fnamep)[1] == '/'
22164# ifdef BACKSLASH_IN_FILENAME
22165 || (*fnamep)[1] == '\\'
22166# endif
22167 || (*fnamep)[1] == NUL)
22168
22169#endif
22170 )
22171 {
22172 *fnamep = expand_env_save(*fnamep);
22173 vim_free(*bufp); /* free any allocated file name */
22174 *bufp = *fnamep;
22175 if (*fnamep == NULL)
22176 return -1;
22177 }
22178
22179 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022180 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 {
22182 if (vim_ispathsep(*p)
22183 && p[1] == '.'
22184 && (p[2] == NUL
22185 || vim_ispathsep(p[2])
22186 || (p[2] == '.'
22187 && (p[3] == NUL || vim_ispathsep(p[3])))))
22188 break;
22189 }
22190
22191 /* FullName_save() is slow, don't use it when not needed. */
22192 if (*p != NUL || !vim_isAbsName(*fnamep))
22193 {
22194 *fnamep = FullName_save(*fnamep, *p != NUL);
22195 vim_free(*bufp); /* free any allocated file name */
22196 *bufp = *fnamep;
22197 if (*fnamep == NULL)
22198 return -1;
22199 }
22200
22201 /* Append a path separator to a directory. */
22202 if (mch_isdir(*fnamep))
22203 {
22204 /* Make room for one or two extra characters. */
22205 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22206 vim_free(*bufp); /* free any allocated file name */
22207 *bufp = *fnamep;
22208 if (*fnamep == NULL)
22209 return -1;
22210 add_pathsep(*fnamep);
22211 }
22212 }
22213
22214 /* ":." - path relative to the current directory */
22215 /* ":~" - path relative to the home directory */
22216 /* ":8" - shortname path - postponed till after */
22217 while (src[*usedlen] == ':'
22218 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22219 {
22220 *usedlen += 2;
22221 if (c == '8')
22222 {
22223#ifdef WIN3264
22224 has_shortname = 1; /* Postpone this. */
22225#endif
22226 continue;
22227 }
22228 pbuf = NULL;
22229 /* Need full path first (use expand_env() to remove a "~/") */
22230 if (!has_fullname)
22231 {
22232 if (c == '.' && **fnamep == '~')
22233 p = pbuf = expand_env_save(*fnamep);
22234 else
22235 p = pbuf = FullName_save(*fnamep, FALSE);
22236 }
22237 else
22238 p = *fnamep;
22239
22240 has_fullname = 0;
22241
22242 if (p != NULL)
22243 {
22244 if (c == '.')
22245 {
22246 mch_dirname(dirname, MAXPATHL);
22247 s = shorten_fname(p, dirname);
22248 if (s != NULL)
22249 {
22250 *fnamep = s;
22251 if (pbuf != NULL)
22252 {
22253 vim_free(*bufp); /* free any allocated file name */
22254 *bufp = pbuf;
22255 pbuf = NULL;
22256 }
22257 }
22258 }
22259 else
22260 {
22261 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22262 /* Only replace it when it starts with '~' */
22263 if (*dirname == '~')
22264 {
22265 s = vim_strsave(dirname);
22266 if (s != NULL)
22267 {
22268 *fnamep = s;
22269 vim_free(*bufp);
22270 *bufp = s;
22271 }
22272 }
22273 }
22274 vim_free(pbuf);
22275 }
22276 }
22277
22278 tail = gettail(*fnamep);
22279 *fnamelen = (int)STRLEN(*fnamep);
22280
22281 /* ":h" - head, remove "/file_name", can be repeated */
22282 /* Don't remove the first "/" or "c:\" */
22283 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22284 {
22285 valid |= VALID_HEAD;
22286 *usedlen += 2;
22287 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022288 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022289 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 *fnamelen = (int)(tail - *fnamep);
22291#ifdef VMS
22292 if (*fnamelen > 0)
22293 *fnamelen += 1; /* the path separator is part of the path */
22294#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022295 if (*fnamelen == 0)
22296 {
22297 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22298 p = vim_strsave((char_u *)".");
22299 if (p == NULL)
22300 return -1;
22301 vim_free(*bufp);
22302 *bufp = *fnamep = tail = p;
22303 *fnamelen = 1;
22304 }
22305 else
22306 {
22307 while (tail > s && !after_pathsep(s, tail))
22308 mb_ptr_back(*fnamep, tail);
22309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 }
22311
22312 /* ":8" - shortname */
22313 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22314 {
22315 *usedlen += 2;
22316#ifdef WIN3264
22317 has_shortname = 1;
22318#endif
22319 }
22320
22321#ifdef WIN3264
22322 /* Check shortname after we have done 'heads' and before we do 'tails'
22323 */
22324 if (has_shortname)
22325 {
22326 pbuf = NULL;
22327 /* Copy the string if it is shortened by :h */
22328 if (*fnamelen < (int)STRLEN(*fnamep))
22329 {
22330 p = vim_strnsave(*fnamep, *fnamelen);
22331 if (p == 0)
22332 return -1;
22333 vim_free(*bufp);
22334 *bufp = *fnamep = p;
22335 }
22336
22337 /* Split into two implementations - makes it easier. First is where
22338 * there isn't a full name already, second is where there is.
22339 */
22340 if (!has_fullname && !vim_isAbsName(*fnamep))
22341 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022342 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 return -1;
22344 }
22345 else
22346 {
22347 int l;
22348
22349 /* Simple case, already have the full-name
22350 * Nearly always shorter, so try first time. */
22351 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022352 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 return -1;
22354
22355 if (l == 0)
22356 {
22357 /* Couldn't find the filename.. search the paths.
22358 */
22359 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022360 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 return -1;
22362 }
22363 *fnamelen = l;
22364 }
22365 }
22366#endif /* WIN3264 */
22367
22368 /* ":t" - tail, just the basename */
22369 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22370 {
22371 *usedlen += 2;
22372 *fnamelen -= (int)(tail - *fnamep);
22373 *fnamep = tail;
22374 }
22375
22376 /* ":e" - extension, can be repeated */
22377 /* ":r" - root, without extension, can be repeated */
22378 while (src[*usedlen] == ':'
22379 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22380 {
22381 /* find a '.' in the tail:
22382 * - for second :e: before the current fname
22383 * - otherwise: The last '.'
22384 */
22385 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22386 s = *fnamep - 2;
22387 else
22388 s = *fnamep + *fnamelen - 1;
22389 for ( ; s > tail; --s)
22390 if (s[0] == '.')
22391 break;
22392 if (src[*usedlen + 1] == 'e') /* :e */
22393 {
22394 if (s > tail)
22395 {
22396 *fnamelen += (int)(*fnamep - (s + 1));
22397 *fnamep = s + 1;
22398#ifdef VMS
22399 /* cut version from the extension */
22400 s = *fnamep + *fnamelen - 1;
22401 for ( ; s > *fnamep; --s)
22402 if (s[0] == ';')
22403 break;
22404 if (s > *fnamep)
22405 *fnamelen = s - *fnamep;
22406#endif
22407 }
22408 else if (*fnamep <= tail)
22409 *fnamelen = 0;
22410 }
22411 else /* :r */
22412 {
22413 if (s > tail) /* remove one extension */
22414 *fnamelen = (int)(s - *fnamep);
22415 }
22416 *usedlen += 2;
22417 }
22418
22419 /* ":s?pat?foo?" - substitute */
22420 /* ":gs?pat?foo?" - global substitute */
22421 if (src[*usedlen] == ':'
22422 && (src[*usedlen + 1] == 's'
22423 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22424 {
22425 char_u *str;
22426 char_u *pat;
22427 char_u *sub;
22428 int sep;
22429 char_u *flags;
22430 int didit = FALSE;
22431
22432 flags = (char_u *)"";
22433 s = src + *usedlen + 2;
22434 if (src[*usedlen + 1] == 'g')
22435 {
22436 flags = (char_u *)"g";
22437 ++s;
22438 }
22439
22440 sep = *s++;
22441 if (sep)
22442 {
22443 /* find end of pattern */
22444 p = vim_strchr(s, sep);
22445 if (p != NULL)
22446 {
22447 pat = vim_strnsave(s, (int)(p - s));
22448 if (pat != NULL)
22449 {
22450 s = p + 1;
22451 /* find end of substitution */
22452 p = vim_strchr(s, sep);
22453 if (p != NULL)
22454 {
22455 sub = vim_strnsave(s, (int)(p - s));
22456 str = vim_strnsave(*fnamep, *fnamelen);
22457 if (sub != NULL && str != NULL)
22458 {
22459 *usedlen = (int)(p + 1 - src);
22460 s = do_string_sub(str, pat, sub, flags);
22461 if (s != NULL)
22462 {
22463 *fnamep = s;
22464 *fnamelen = (int)STRLEN(s);
22465 vim_free(*bufp);
22466 *bufp = s;
22467 didit = TRUE;
22468 }
22469 }
22470 vim_free(sub);
22471 vim_free(str);
22472 }
22473 vim_free(pat);
22474 }
22475 }
22476 /* after using ":s", repeat all the modifiers */
22477 if (didit)
22478 goto repeat;
22479 }
22480 }
22481
22482 return valid;
22483}
22484
22485/*
22486 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22487 * "flags" can be "g" to do a global substitute.
22488 * Returns an allocated string, NULL for error.
22489 */
22490 char_u *
22491do_string_sub(str, pat, sub, flags)
22492 char_u *str;
22493 char_u *pat;
22494 char_u *sub;
22495 char_u *flags;
22496{
22497 int sublen;
22498 regmatch_T regmatch;
22499 int i;
22500 int do_all;
22501 char_u *tail;
22502 garray_T ga;
22503 char_u *ret;
22504 char_u *save_cpo;
22505
22506 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22507 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022508 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509
22510 ga_init2(&ga, 1, 200);
22511
22512 do_all = (flags[0] == 'g');
22513
22514 regmatch.rm_ic = p_ic;
22515 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22516 if (regmatch.regprog != NULL)
22517 {
22518 tail = str;
22519 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22520 {
22521 /*
22522 * Get some space for a temporary buffer to do the substitution
22523 * into. It will contain:
22524 * - The text up to where the match is.
22525 * - The substituted text.
22526 * - The text after the match.
22527 */
22528 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22529 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22530 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22531 {
22532 ga_clear(&ga);
22533 break;
22534 }
22535
22536 /* copy the text up to where the match is */
22537 i = (int)(regmatch.startp[0] - tail);
22538 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22539 /* add the substituted text */
22540 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22541 + ga.ga_len + i, TRUE, TRUE, FALSE);
22542 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 /* avoid getting stuck on a match with an empty string */
22544 if (tail == regmatch.endp[0])
22545 {
22546 if (*tail == NUL)
22547 break;
22548 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22549 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550 }
22551 else
22552 {
22553 tail = regmatch.endp[0];
22554 if (*tail == NUL)
22555 break;
22556 }
22557 if (!do_all)
22558 break;
22559 }
22560
22561 if (ga.ga_data != NULL)
22562 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22563
22564 vim_free(regmatch.regprog);
22565 }
22566
22567 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22568 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022569 if (p_cpo == empty_option)
22570 p_cpo = save_cpo;
22571 else
22572 /* Darn, evaluating {sub} expression changed the value. */
22573 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574
22575 return ret;
22576}
22577
22578#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */