blob: 5802a0894049012e0dc0db23bd470e363e0edc5e [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);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001278 if (tv.vval.v_list != NULL)
1279 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001280 ga_append(&ga, NUL);
1281 retval = (char_u *)ga.ga_data;
1282 }
1283 else
1284 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001285 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287
1288 return retval;
1289}
1290
1291/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001292 * Call eval_to_string() without using current local variables and using
1293 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 */
1295 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001296eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 char_u *arg;
1298 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001299 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 char_u *retval;
1302 void *save_funccalp;
1303
1304 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001305 if (use_sandbox)
1306 ++sandbox;
1307 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001308 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001309 if (use_sandbox)
1310 --sandbox;
1311 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 restore_funccal(save_funccalp);
1313 return retval;
1314}
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316/*
1317 * Top level evaluation function, returning a number.
1318 * Evaluates "expr" silently.
1319 * Returns -1 for an error.
1320 */
1321 int
1322eval_to_number(expr)
1323 char_u *expr;
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001327 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
1329 ++emsg_off;
1330
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001331 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 retval = -1;
1333 else
1334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001335 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338 --emsg_off;
1339
1340 return retval;
1341}
1342
Bram Moolenaara40058a2005-07-11 22:42:07 +00001343/*
1344 * Prepare v: variable "idx" to be used.
1345 * Save the current typeval in "save_tv".
1346 * When not used yet add the variable to the v: hashtable.
1347 */
1348 static void
1349prepare_vimvar(idx, save_tv)
1350 int idx;
1351 typval_T *save_tv;
1352{
1353 *save_tv = vimvars[idx].vv_tv;
1354 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1355 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1356}
1357
1358/*
1359 * Restore v: variable "idx" to typeval "save_tv".
1360 * When no longer defined, remove the variable from the v: hashtable.
1361 */
1362 static void
1363restore_vimvar(idx, save_tv)
1364 int idx;
1365 typval_T *save_tv;
1366{
1367 hashitem_T *hi;
1368
Bram Moolenaara40058a2005-07-11 22:42:07 +00001369 vimvars[idx].vv_tv = *save_tv;
1370 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1371 {
1372 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1373 if (HASHITEM_EMPTY(hi))
1374 EMSG2(_(e_intern2), "restore_vimvar()");
1375 else
1376 hash_remove(&vimvarht, hi);
1377 }
1378}
1379
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001380#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001381/*
1382 * Evaluate an expression to a list with suggestions.
1383 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001384 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001385 */
1386 list_T *
1387eval_spell_expr(badword, expr)
1388 char_u *badword;
1389 char_u *expr;
1390{
1391 typval_T save_val;
1392 typval_T rettv;
1393 list_T *list = NULL;
1394 char_u *p = skipwhite(expr);
1395
1396 /* Set "v:val" to the bad word. */
1397 prepare_vimvar(VV_VAL, &save_val);
1398 vimvars[VV_VAL].vv_type = VAR_STRING;
1399 vimvars[VV_VAL].vv_str = badword;
1400 if (p_verbose == 0)
1401 ++emsg_off;
1402
1403 if (eval1(&p, &rettv, TRUE) == OK)
1404 {
1405 if (rettv.v_type != VAR_LIST)
1406 clear_tv(&rettv);
1407 else
1408 list = rettv.vval.v_list;
1409 }
1410
1411 if (p_verbose == 0)
1412 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001413 restore_vimvar(VV_VAL, &save_val);
1414
1415 return list;
1416}
1417
1418/*
1419 * "list" is supposed to contain two items: a word and a number. Return the
1420 * word in "pp" and the number as the return value.
1421 * Return -1 if anything isn't right.
1422 * Used to get the good word and score from the eval_spell_expr() result.
1423 */
1424 int
1425get_spellword(list, pp)
1426 list_T *list;
1427 char_u **pp;
1428{
1429 listitem_T *li;
1430
1431 li = list->lv_first;
1432 if (li == NULL)
1433 return -1;
1434 *pp = get_tv_string(&li->li_tv);
1435
1436 li = li->li_next;
1437 if (li == NULL)
1438 return -1;
1439 return get_tv_number(&li->li_tv);
1440}
1441#endif
1442
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001443/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001444 * Top level evaluation function.
1445 * Returns an allocated typval_T with the result.
1446 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001447 */
1448 typval_T *
1449eval_expr(arg, nextcmd)
1450 char_u *arg;
1451 char_u **nextcmd;
1452{
1453 typval_T *tv;
1454
1455 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001456 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001457 {
1458 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001459 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001460 }
1461
1462 return tv;
1463}
1464
1465
Bram Moolenaar4f688582007-07-24 12:34:30 +00001466#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1467 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001470 * Uses argv[argc] for the function arguments. Only Number and String
1471 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001474 static int
1475call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 char_u *func;
1477 int argc;
1478 char_u **argv;
1479 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
Bram Moolenaar33570922005-01-25 22:26:29 +00001482 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 long n;
1484 int len;
1485 int i;
1486 int doesrange;
1487 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001488 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001490 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 for (i = 0; i < argc; i++)
1495 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001496 /* Pass a NULL or empty argument as an empty string */
1497 if (argv[i] == NULL || *argv[i] == NUL)
1498 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001499 argvars[i].v_type = VAR_STRING;
1500 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001501 continue;
1502 }
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 /* Recognize a number argument, the others must be strings. */
1505 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1506 if (len != 0 && len == (int)STRLEN(argv[i]))
1507 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001508 argvars[i].v_type = VAR_NUMBER;
1509 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 else
1512 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001513 argvars[i].v_type = VAR_STRING;
1514 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516 }
1517
1518 if (safe)
1519 {
1520 save_funccalp = save_funccal();
1521 ++sandbox;
1522 }
1523
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001524 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1525 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001527 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (safe)
1529 {
1530 --sandbox;
1531 restore_funccal(save_funccalp);
1532 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001533 vim_free(argvars);
1534
1535 if (ret == FAIL)
1536 clear_tv(rettv);
1537
1538 return ret;
1539}
1540
Bram Moolenaar4f688582007-07-24 12:34:30 +00001541# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001542/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001543 * Call vimL function "func" and return the result as a string.
1544 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001545 * Uses argv[argc] for the function arguments.
1546 */
1547 void *
1548call_func_retstr(func, argc, argv, safe)
1549 char_u *func;
1550 int argc;
1551 char_u **argv;
1552 int safe; /* use the sandbox */
1553{
1554 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001555 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556
1557 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1558 return NULL;
1559
1560 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 return retval;
1563}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001564# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565
Bram Moolenaar4f688582007-07-24 12:34:30 +00001566# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001568 * Call vimL function "func" and return the result as a number.
1569 * Returns -1 when calling the function fails.
1570 * Uses argv[argc] for the function arguments.
1571 */
1572 long
1573call_func_retnr(func, argc, argv, safe)
1574 char_u *func;
1575 int argc;
1576 char_u **argv;
1577 int safe; /* use the sandbox */
1578{
1579 typval_T rettv;
1580 long retval;
1581
1582 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1583 return -1;
1584
1585 retval = get_tv_number_chk(&rettv, NULL);
1586 clear_tv(&rettv);
1587 return retval;
1588}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001589# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001590
1591/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001592 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001594 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 */
1596 void *
1597call_func_retlist(func, argc, argv, safe)
1598 char_u *func;
1599 int argc;
1600 char_u **argv;
1601 int safe; /* use the sandbox */
1602{
1603 typval_T rettv;
1604
1605 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1606 return NULL;
1607
1608 if (rettv.v_type != VAR_LIST)
1609 {
1610 clear_tv(&rettv);
1611 return NULL;
1612 }
1613
1614 return rettv.vval.v_list;
1615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616#endif
1617
Bram Moolenaar4f688582007-07-24 12:34:30 +00001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
1620 * Save the current function call pointer, and set it to NULL.
1621 * Used when executing autocommands and for ":source".
1622 */
1623 void *
1624save_funccal()
1625{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001626 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 current_funccal = NULL;
1629 return (void *)fc;
1630}
1631
1632 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001633restore_funccal(vfc)
1634 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001636 funccall_T *fc = (funccall_T *)vfc;
1637
1638 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639}
1640
Bram Moolenaar05159a02005-02-26 23:04:13 +00001641#if defined(FEAT_PROFILE) || defined(PROTO)
1642/*
1643 * Prepare profiling for entering a child or something else that is not
1644 * counted for the script/function itself.
1645 * Should always be called in pair with prof_child_exit().
1646 */
1647 void
1648prof_child_enter(tm)
1649 proftime_T *tm; /* place to store waittime */
1650{
1651 funccall_T *fc = current_funccal;
1652
1653 if (fc != NULL && fc->func->uf_profiling)
1654 profile_start(&fc->prof_child);
1655 script_prof_save(tm);
1656}
1657
1658/*
1659 * Take care of time spent in a child.
1660 * Should always be called after prof_child_enter().
1661 */
1662 void
1663prof_child_exit(tm)
1664 proftime_T *tm; /* where waittime was stored */
1665{
1666 funccall_T *fc = current_funccal;
1667
1668 if (fc != NULL && fc->func->uf_profiling)
1669 {
1670 profile_end(&fc->prof_child);
1671 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1672 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1673 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1674 }
1675 script_prof_restore(tm);
1676}
1677#endif
1678
1679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680#ifdef FEAT_FOLDING
1681/*
1682 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1683 * it in "*cp". Doesn't give error messages.
1684 */
1685 int
1686eval_foldexpr(arg, cp)
1687 char_u *arg;
1688 int *cp;
1689{
Bram Moolenaar33570922005-01-25 22:26:29 +00001690 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 int retval;
1692 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001693 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1694 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
1696 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001697 if (use_sandbox)
1698 ++sandbox;
1699 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 retval = 0;
1703 else
1704 {
1705 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001706 if (tv.v_type == VAR_NUMBER)
1707 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001708 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 retval = 0;
1710 else
1711 {
1712 /* If the result is a string, check if there is a non-digit before
1713 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001714 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (!VIM_ISDIGIT(*s) && *s != '-')
1716 *cp = *s++;
1717 retval = atol((char *)s);
1718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
1721 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001722 if (use_sandbox)
1723 --sandbox;
1724 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726 return retval;
1727}
1728#endif
1729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 * ":let" list all variable values
1732 * ":let var1 var2" list variable values
1733 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001734 * ":let var += expr" assignment command.
1735 * ":let var -= expr" assignment command.
1736 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001737 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 */
1739 void
1740ex_let(eap)
1741 exarg_T *eap;
1742{
1743 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001745 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 int var_count = 0;
1748 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001750 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001751 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaardb552d602006-03-23 22:59:57 +00001753 argend = skip_var_list(arg, &var_count, &semicolon);
1754 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001756 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1757 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001758 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001761 /*
1762 * ":let" without "=": list variables
1763 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 if (*arg == '[')
1765 EMSG(_(e_invarg));
1766 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001768 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001769 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001772 list_glob_vars(&first);
1773 list_buf_vars(&first);
1774 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001775#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001776 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001777#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001778 list_script_vars(&first);
1779 list_func_vars(&first);
1780 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 eap->nextcmd = check_nextcmd(arg);
1783 }
1784 else
1785 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001786 op[0] = '=';
1787 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001788 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 {
1790 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1791 op[0] = expr[-1]; /* +=, -= or .= */
1792 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (eap->skip)
1796 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (eap->skip)
1799 {
1800 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 --emsg_skip;
1803 }
1804 else if (i != FAIL)
1805 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001807 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810 }
1811}
1812
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813/*
1814 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1815 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001816 * When "nextchars" is not NULL it points to a string with characters that
1817 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1818 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 * Returns OK or FAIL;
1820 */
1821 static int
1822ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1823 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001824 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 int copy; /* copy values from "tv", don't move */
1826 int semicolon; /* from skip_var_list() */
1827 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829{
1830 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 listitem_T *item;
1834 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835
1836 if (*arg != '[')
1837 {
1838 /*
1839 * ":let var = expr" or ":for var in list"
1840 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 return FAIL;
1843 return OK;
1844 }
1845
1846 /*
1847 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1848 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001849 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 {
1851 EMSG(_(e_listreq));
1852 return FAIL;
1853 }
1854
1855 i = list_len(l);
1856 if (semicolon == 0 && var_count < i)
1857 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001858 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 return FAIL;
1860 }
1861 if (var_count - semicolon > i)
1862 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001863 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 return FAIL;
1865 }
1866
1867 item = l->lv_first;
1868 while (*arg != ']')
1869 {
1870 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 item = item->li_next;
1873 if (arg == NULL)
1874 return FAIL;
1875
1876 arg = skipwhite(arg);
1877 if (*arg == ';')
1878 {
1879 /* Put the rest of the list (may be empty) in the var after ';'.
1880 * Create a new list for this. */
1881 l = list_alloc();
1882 if (l == NULL)
1883 return FAIL;
1884 while (item != NULL)
1885 {
1886 list_append_tv(l, &item->li_tv);
1887 item = item->li_next;
1888 }
1889
1890 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001891 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 ltv.vval.v_list = l;
1893 l->lv_refcount = 1;
1894
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1896 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 clear_tv(&ltv);
1898 if (arg == NULL)
1899 return FAIL;
1900 break;
1901 }
1902 else if (*arg != ',' && *arg != ']')
1903 {
1904 EMSG2(_(e_intern2), "ex_let_vars()");
1905 return FAIL;
1906 }
1907 }
1908
1909 return OK;
1910}
1911
1912/*
1913 * Skip over assignable variable "var" or list of variables "[var, var]".
1914 * Used for ":let varvar = expr" and ":for varvar in expr".
1915 * For "[var, var]" increment "*var_count" for each variable.
1916 * for "[var, var; var]" set "semicolon".
1917 * Return NULL for an error.
1918 */
1919 static char_u *
1920skip_var_list(arg, var_count, semicolon)
1921 char_u *arg;
1922 int *var_count;
1923 int *semicolon;
1924{
1925 char_u *p, *s;
1926
1927 if (*arg == '[')
1928 {
1929 /* "[var, var]": find the matching ']'. */
1930 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001931 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 {
1933 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1934 s = skip_var_one(p);
1935 if (s == p)
1936 {
1937 EMSG2(_(e_invarg2), p);
1938 return NULL;
1939 }
1940 ++*var_count;
1941
1942 p = skipwhite(s);
1943 if (*p == ']')
1944 break;
1945 else if (*p == ';')
1946 {
1947 if (*semicolon == 1)
1948 {
1949 EMSG(_("Double ; in list of variables"));
1950 return NULL;
1951 }
1952 *semicolon = 1;
1953 }
1954 else if (*p != ',')
1955 {
1956 EMSG2(_(e_invarg2), p);
1957 return NULL;
1958 }
1959 }
1960 return p + 1;
1961 }
1962 else
1963 return skip_var_one(arg);
1964}
1965
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001967 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001968 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001969 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 static char_u *
1971skip_var_one(arg)
1972 char_u *arg;
1973{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001974 if (*arg == '@' && arg[1] != NUL)
1975 return arg + 2;
1976 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1977 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978}
1979
Bram Moolenaara7043832005-01-21 11:56:39 +00001980/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001981 * List variables for hashtab "ht" with prefix "prefix".
1982 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001983 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001985list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001986 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001987 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001989 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001990{
Bram Moolenaar33570922005-01-25 22:26:29 +00001991 hashitem_T *hi;
1992 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 int todo;
1994
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001995 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001996 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1997 {
1998 if (!HASHITEM_EMPTY(hi))
1999 {
2000 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002001 di = HI2DI(hi);
2002 if (empty || di->di_tv.v_type != VAR_STRING
2003 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002004 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002005 }
2006 }
2007}
2008
2009/*
2010 * List global variables.
2011 */
2012 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002013list_glob_vars(first)
2014 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002015{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002016 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002017}
2018
2019/*
2020 * List buffer variables.
2021 */
2022 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002023list_buf_vars(first)
2024 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002025{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002026 char_u numbuf[NUMBUFLEN];
2027
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002028 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2029 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002030
2031 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002032 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2033 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002034}
2035
2036/*
2037 * List window variables.
2038 */
2039 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002040list_win_vars(first)
2041 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002042{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002043 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2044 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002045}
2046
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002047#ifdef FEAT_WINDOWS
2048/*
2049 * List tab page variables.
2050 */
2051 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002052list_tab_vars(first)
2053 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002055 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2056 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002057}
2058#endif
2059
Bram Moolenaara7043832005-01-21 11:56:39 +00002060/*
2061 * List Vim variables.
2062 */
2063 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002064list_vim_vars(first)
2065 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002067 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068}
2069
2070/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002071 * List script-local variables, if there is a script.
2072 */
2073 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002074list_script_vars(first)
2075 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002076{
2077 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2079 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002080}
2081
2082/*
2083 * List function variables, if there is a function.
2084 */
2085 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002086list_func_vars(first)
2087 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002088{
2089 if (current_funccal != NULL)
2090 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002092}
2093
2094/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 * List variables in "arg".
2096 */
2097 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 exarg_T *eap;
2100 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102{
2103 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002104 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 char_u *name_start;
2107 char_u *arg_subsc;
2108 char_u *tofree;
2109 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110
2111 while (!ends_excmd(*arg) && !got_int)
2112 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002115 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2117 {
2118 emsg_severe = TRUE;
2119 EMSG(_(e_trailing));
2120 break;
2121 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002125 /* get_name_len() takes care of expanding curly braces */
2126 name_start = name = arg;
2127 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2128 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 /* This is mainly to keep test 49 working: when expanding
2131 * curly braces fails overrule the exception error message. */
2132 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002134 emsg_severe = TRUE;
2135 EMSG2(_(e_invarg2), arg);
2136 break;
2137 }
2138 error = TRUE;
2139 }
2140 else
2141 {
2142 if (tofree != NULL)
2143 name = tofree;
2144 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 else
2147 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002148 /* handle d.key, l[idx], f(expr) */
2149 arg_subsc = arg;
2150 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002155 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002157 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 case 'g': list_glob_vars(first); break;
2159 case 'b': list_buf_vars(first); break;
2160 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002161#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 case 'v': list_vim_vars(first); break;
2165 case 's': list_script_vars(first); break;
2166 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002167 default:
2168 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002170 }
2171 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172 {
2173 char_u numbuf[NUMBUFLEN];
2174 char_u *tf;
2175 int c;
2176 char_u *s;
2177
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002178 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179 c = *arg;
2180 *arg = NUL;
2181 list_one_var_a((char_u *)"",
2182 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 tv.v_type,
2184 s == NULL ? (char_u *)"" : s,
2185 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002186 *arg = c;
2187 vim_free(tf);
2188 }
2189 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 }
2192 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193
2194 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196
2197 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
2199
2200 return arg;
2201}
2202
2203/*
2204 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2205 * Returns a pointer to the char just after the var name.
2206 * Returns NULL if there is an error.
2207 */
2208 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002209ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002211 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002213 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002214 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215{
2216 int c1;
2217 char_u *name;
2218 char_u *p;
2219 char_u *arg_end = NULL;
2220 int len;
2221 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002222 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223
2224 /*
2225 * ":let $VAR = expr": Set environment variable.
2226 */
2227 if (*arg == '$')
2228 {
2229 /* Find the end of the name. */
2230 ++arg;
2231 name = arg;
2232 len = get_env_len(&arg);
2233 if (len == 0)
2234 EMSG2(_(e_invarg2), name - 1);
2235 else
2236 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002237 if (op != NULL && (*op == '+' || *op == '-'))
2238 EMSG2(_(e_letwrong), op);
2239 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002240 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 EMSG(_(e_letunexp));
2242 else
2243 {
2244 c1 = name[len];
2245 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002246 p = get_tv_string_chk(tv);
2247 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 {
2249 int mustfree = FALSE;
2250 char_u *s = vim_getenv(name, &mustfree);
2251
2252 if (s != NULL)
2253 {
2254 p = tofree = concat_str(s, p);
2255 if (mustfree)
2256 vim_free(s);
2257 }
2258 }
2259 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002260 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002261 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 if (STRICMP(name, "HOME") == 0)
2263 init_homedir();
2264 else if (didset_vim && STRICMP(name, "VIM") == 0)
2265 didset_vim = FALSE;
2266 else if (didset_vimruntime
2267 && STRICMP(name, "VIMRUNTIME") == 0)
2268 didset_vimruntime = FALSE;
2269 arg_end = arg;
2270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 }
2274 }
2275 }
2276
2277 /*
2278 * ":let &option = expr": Set option value.
2279 * ":let &l:option = expr": Set local option value.
2280 * ":let &g:option = expr": Set global option value.
2281 */
2282 else if (*arg == '&')
2283 {
2284 /* Find the end of the name. */
2285 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002286 if (p == NULL || (endchars != NULL
2287 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 EMSG(_(e_letunexp));
2289 else
2290 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291 long n;
2292 int opt_type;
2293 long numval;
2294 char_u *stringval = NULL;
2295 char_u *s;
2296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 c1 = *p;
2298 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299
2300 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002301 s = get_tv_string_chk(tv); /* != NULL if number or string */
2302 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 {
2304 opt_type = get_option_value(arg, &numval,
2305 &stringval, opt_flags);
2306 if ((opt_type == 1 && *op == '.')
2307 || (opt_type == 0 && *op != '.'))
2308 EMSG2(_(e_letwrong), op);
2309 else
2310 {
2311 if (opt_type == 1) /* number */
2312 {
2313 if (*op == '+')
2314 n = numval + n;
2315 else
2316 n = numval - n;
2317 }
2318 else if (opt_type == 0 && stringval != NULL) /* string */
2319 {
2320 s = concat_str(stringval, s);
2321 vim_free(stringval);
2322 stringval = s;
2323 }
2324 }
2325 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 if (s != NULL)
2327 {
2328 set_option_value(arg, n, s, opt_flags);
2329 arg_end = p;
2330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 }
2334 }
2335
2336 /*
2337 * ":let @r = expr": Set register contents.
2338 */
2339 else if (*arg == '@')
2340 {
2341 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 if (op != NULL && (*op == '+' || *op == '-'))
2343 EMSG2(_(e_letwrong), op);
2344 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 EMSG(_(e_letunexp));
2347 else
2348 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002349 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 char_u *s;
2351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002355 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 if (s != NULL)
2357 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002358 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_free(s);
2360 }
2361 }
2362 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002363 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 arg_end = arg + 1;
2366 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002367 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002368 }
2369 }
2370
2371 /*
2372 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002375 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002377 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002379 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2383 EMSG(_(e_letunexp));
2384 else
2385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 arg_end = p;
2388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 }
2392
2393 else
2394 EMSG2(_(e_invarg2), arg);
2395
2396 return arg_end;
2397}
2398
2399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2401 */
2402 static int
2403check_changedtick(arg)
2404 char_u *arg;
2405{
2406 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2407 {
2408 EMSG2(_(e_readonlyvar), arg);
2409 return TRUE;
2410 }
2411 return FALSE;
2412}
2413
2414/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 * Get an lval: variable, Dict item or List item that can be assigned a value
2416 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2417 * "name.key", "name.key[expr]" etc.
2418 * Indexing only works if "name" is an existing List or Dictionary.
2419 * "name" points to the start of the name.
2420 * If "rettv" is not NULL it points to the value to be assigned.
2421 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2422 * wrong; must end in space or cmd separator.
2423 *
2424 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002425 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 */
2428 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002429get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002431 typval_T *rettv;
2432 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 int unlet;
2434 int skip;
2435 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002436 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 char_u *expr_start, *expr_end;
2440 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002441 dictitem_T *v;
2442 typval_T var1;
2443 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002445 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002448 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002451 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452
2453 if (skip)
2454 {
2455 /* When skipping just find the end of the name. */
2456 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002457 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 }
2459
2460 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002461 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (expr_start != NULL)
2463 {
2464 /* Don't expand the name when we already know there is an error. */
2465 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2466 && *p != '[' && *p != '.')
2467 {
2468 EMSG(_(e_trailing));
2469 return NULL;
2470 }
2471
2472 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2473 if (lp->ll_exp_name == NULL)
2474 {
2475 /* Report an invalid expression in braces, unless the
2476 * expression evaluation has been cancelled due to an
2477 * aborting error, an interrupt, or an exception. */
2478 if (!aborting() && !quiet)
2479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002480 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 EMSG2(_(e_invarg2), name);
2482 return NULL;
2483 }
2484 }
2485 lp->ll_name = lp->ll_exp_name;
2486 }
2487 else
2488 lp->ll_name = name;
2489
2490 /* Without [idx] or .key we are done. */
2491 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2492 return p;
2493
2494 cc = *p;
2495 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002496 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (v == NULL && !quiet)
2498 EMSG2(_(e_undefvar), lp->ll_name);
2499 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 if (v == NULL)
2501 return NULL;
2502
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 /*
2504 * Loop until no more [idx] or .key is following.
2505 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2510 && !(lp->ll_tv->v_type == VAR_DICT
2511 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (!quiet)
2514 EMSG(_("E689: Can only index a List or Dictionary"));
2515 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (!quiet)
2520 EMSG(_("E708: [:] must come last"));
2521 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002523
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 len = -1;
2525 if (*p == '.')
2526 {
2527 key = p + 1;
2528 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2529 ;
2530 if (len == 0)
2531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (!quiet)
2533 EMSG(_(e_emptykey));
2534 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 }
2536 p = key + len;
2537 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002538 else
2539 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002541 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 if (*p == ':')
2543 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002544 else
2545 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 empty1 = FALSE;
2547 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002549 if (get_tv_string_chk(&var1) == NULL)
2550 {
2551 /* not a number or string */
2552 clear_tv(&var1);
2553 return NULL;
2554 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 }
2556
2557 /* Optionally get the second index [ :expr]. */
2558 if (*p == ':')
2559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002563 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002564 if (!empty1)
2565 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002567 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (rettv != NULL && (rettv->v_type != VAR_LIST
2569 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (!quiet)
2572 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 if (!empty1)
2574 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 }
2577 p = skipwhite(p + 1);
2578 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 else
2581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2584 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 if (!empty1)
2586 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002589 if (get_tv_string_chk(&var2) == NULL)
2590 {
2591 /* not a number or string */
2592 if (!empty1)
2593 clear_tv(&var1);
2594 clear_tv(&var2);
2595 return NULL;
2596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002599 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002602
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002607 if (!empty1)
2608 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 }
2613
2614 /* Skip to past ']'. */
2615 ++p;
2616 }
2617
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 {
2620 if (len == -1)
2621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002623 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 if (*key == NUL)
2625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (!quiet)
2627 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 }
2631 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002632 lp->ll_list = NULL;
2633 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002634 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002637 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 if (len == -1)
2643 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 }
2646 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 if (len == -1)
2651 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 p = NULL;
2654 break;
2655 }
2656 if (len == -1)
2657 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 }
2660 else
2661 {
2662 /*
2663 * Get the number and item for the only or first index of the List.
2664 */
2665 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 else
2668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 clear_tv(&var1);
2671 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_list = lp->ll_tv->vval.v_list;
2674 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2675 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002677 if (lp->ll_n1 < 0)
2678 {
2679 lp->ll_n1 = 0;
2680 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2681 }
2682 }
2683 if (lp->ll_li == NULL)
2684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689
2690 /*
2691 * May need to find the item or absolute index for the second
2692 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 * When no index given: "lp->ll_empty2" is TRUE.
2694 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002698 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2709 if (lp->ll_n1 < 0)
2710 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2711 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
2714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 }
2718
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return p;
2720}
2721
2722/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002723 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 */
2725 static void
2726clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002727 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728{
2729 vim_free(lp->ll_exp_name);
2730 vim_free(lp->ll_newkey);
2731}
2732
2733/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002734 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 */
2738 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002739set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002740 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002742 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002744 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745{
2746 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002747 listitem_T *ri;
2748 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749
2750 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 cc = *endp;
2755 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 if (op != NULL && *op != '=')
2757 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002758 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002760 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002761 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002762 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002763 {
2764 if (tv_op(&tv, rettv, op) == OK)
2765 set_var(lp->ll_name, &tv, FALSE);
2766 clear_tv(&tv);
2767 }
2768 }
2769 else
2770 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002774 else if (tv_check_lock(lp->ll_newkey == NULL
2775 ? lp->ll_tv->v_lock
2776 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2777 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 else if (lp->ll_range)
2779 {
2780 /*
2781 * Assign the List values to the list items.
2782 */
2783 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002784 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 if (op != NULL && *op != '=')
2786 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2787 else
2788 {
2789 clear_tv(&lp->ll_li->li_tv);
2790 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2791 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 ri = ri->li_next;
2793 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2794 break;
2795 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002796 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002798 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 ri = NULL;
2801 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002802 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002803 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 lp->ll_li = lp->ll_li->li_next;
2805 ++lp->ll_n1;
2806 }
2807 if (ri != NULL)
2808 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 else if (lp->ll_empty2
2810 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 : lp->ll_n1 != lp->ll_n2)
2812 EMSG(_("E711: List value has not enough items"));
2813 }
2814 else
2815 {
2816 /*
2817 * Assign to a List or Dictionary item.
2818 */
2819 if (lp->ll_newkey != NULL)
2820 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 if (op != NULL && *op != '=')
2822 {
2823 EMSG2(_(e_letwrong), op);
2824 return;
2825 }
2826
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002828 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (di == NULL)
2830 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002831 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2832 {
2833 vim_free(di);
2834 return;
2835 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002837 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002838 else if (op != NULL && *op != '=')
2839 {
2840 tv_op(lp->ll_tv, rettv, op);
2841 return;
2842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 /*
2847 * Assign the value to the variable or list item.
2848 */
2849 if (copy)
2850 copy_tv(rettv, lp->ll_tv);
2851 else
2852 {
2853 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002854 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002856 }
2857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858}
2859
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002861 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2862 * Returns OK or FAIL.
2863 */
2864 static int
2865tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 typval_T *tv1;
2867 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002868 char_u *op;
2869{
2870 long n;
2871 char_u numbuf[NUMBUFLEN];
2872 char_u *s;
2873
2874 /* Can't do anything with a Funcref or a Dict on the right. */
2875 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2876 {
2877 switch (tv1->v_type)
2878 {
2879 case VAR_DICT:
2880 case VAR_FUNC:
2881 break;
2882
2883 case VAR_LIST:
2884 if (*op != '+' || tv2->v_type != VAR_LIST)
2885 break;
2886 /* List += List */
2887 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2888 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2889 return OK;
2890
2891 case VAR_NUMBER:
2892 case VAR_STRING:
2893 if (tv2->v_type == VAR_LIST)
2894 break;
2895 if (*op == '+' || *op == '-')
2896 {
2897 /* nr += nr or nr -= nr*/
2898 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002899#ifdef FEAT_FLOAT
2900 if (tv2->v_type == VAR_FLOAT)
2901 {
2902 float_T f = n;
2903
2904 if (*op == '+')
2905 f += tv2->vval.v_float;
2906 else
2907 f -= tv2->vval.v_float;
2908 clear_tv(tv1);
2909 tv1->v_type = VAR_FLOAT;
2910 tv1->vval.v_float = f;
2911 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002913#endif
2914 {
2915 if (*op == '+')
2916 n += get_tv_number(tv2);
2917 else
2918 n -= get_tv_number(tv2);
2919 clear_tv(tv1);
2920 tv1->v_type = VAR_NUMBER;
2921 tv1->vval.v_number = n;
2922 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923 }
2924 else
2925 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002926 if (tv2->v_type == VAR_FLOAT)
2927 break;
2928
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 /* str .= str */
2930 s = get_tv_string(tv1);
2931 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2932 clear_tv(tv1);
2933 tv1->v_type = VAR_STRING;
2934 tv1->vval.v_string = s;
2935 }
2936 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002937
2938#ifdef FEAT_FLOAT
2939 case VAR_FLOAT:
2940 {
2941 float_T f;
2942
2943 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2944 && tv2->v_type != VAR_NUMBER
2945 && tv2->v_type != VAR_STRING))
2946 break;
2947 if (tv2->v_type == VAR_FLOAT)
2948 f = tv2->vval.v_float;
2949 else
2950 f = get_tv_number(tv2);
2951 if (*op == '+')
2952 tv1->vval.v_float += f;
2953 else
2954 tv1->vval.v_float -= f;
2955 }
2956 return OK;
2957#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 }
2959 }
2960
2961 EMSG2(_(e_letwrong), op);
2962 return FAIL;
2963}
2964
2965/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002966 * Add a watcher to a list.
2967 */
2968 static void
2969list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002970 list_T *l;
2971 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972{
2973 lw->lw_next = l->lv_watch;
2974 l->lv_watch = lw;
2975}
2976
2977/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002978 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 * No warning when it isn't found...
2980 */
2981 static void
2982list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002983 list_T *l;
2984 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985{
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987
2988 lwp = &l->lv_watch;
2989 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2990 {
2991 if (lw == lwrem)
2992 {
2993 *lwp = lw->lw_next;
2994 break;
2995 }
2996 lwp = &lw->lw_next;
2997 }
2998}
2999
3000/*
3001 * Just before removing an item from a list: advance watchers to the next
3002 * item.
3003 */
3004 static void
3005list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 list_T *l;
3007 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003008{
Bram Moolenaar33570922005-01-25 22:26:29 +00003009 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010
3011 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3012 if (lw->lw_item == item)
3013 lw->lw_item = item->li_next;
3014}
3015
3016/*
3017 * Evaluate the expression used in a ":for var in expr" command.
3018 * "arg" points to "var".
3019 * Set "*errp" to TRUE for an error, FALSE otherwise;
3020 * Return a pointer that holds the info. Null when there is an error.
3021 */
3022 void *
3023eval_for_line(arg, errp, nextcmdp, skip)
3024 char_u *arg;
3025 int *errp;
3026 char_u **nextcmdp;
3027 int skip;
3028{
Bram Moolenaar33570922005-01-25 22:26:29 +00003029 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003031 typval_T tv;
3032 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003033
3034 *errp = TRUE; /* default: there is an error */
3035
Bram Moolenaar33570922005-01-25 22:26:29 +00003036 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003037 if (fi == NULL)
3038 return NULL;
3039
3040 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3041 if (expr == NULL)
3042 return fi;
3043
3044 expr = skipwhite(expr);
3045 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003047 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048 return fi;
3049 }
3050
3051 if (skip)
3052 ++emsg_skip;
3053 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3054 {
3055 *errp = FALSE;
3056 if (!skip)
3057 {
3058 l = tv.vval.v_list;
3059 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003060 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003062 clear_tv(&tv);
3063 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064 else
3065 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003066 /* No need to increment the refcount, it's already set for the
3067 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068 fi->fi_list = l;
3069 list_add_watch(l, &fi->fi_lw);
3070 fi->fi_lw.lw_item = l->lv_first;
3071 }
3072 }
3073 }
3074 if (skip)
3075 --emsg_skip;
3076
3077 return fi;
3078}
3079
3080/*
3081 * Use the first item in a ":for" list. Advance to the next.
3082 * Assign the values to the variable (list). "arg" points to the first one.
3083 * Return TRUE when a valid item was found, FALSE when at end of list or
3084 * something wrong.
3085 */
3086 int
3087next_for_item(fi_void, arg)
3088 void *fi_void;
3089 char_u *arg;
3090{
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094
3095 item = fi->fi_lw.lw_item;
3096 if (item == NULL)
3097 result = FALSE;
3098 else
3099 {
3100 fi->fi_lw.lw_item = item->li_next;
3101 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3102 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3103 }
3104 return result;
3105}
3106
3107/*
3108 * Free the structure used to store info used by ":for".
3109 */
3110 void
3111free_for_info(fi_void)
3112 void *fi_void;
3113{
Bram Moolenaar33570922005-01-25 22:26:29 +00003114 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003116 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003117 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003119 list_unref(fi->fi_list);
3120 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121 vim_free(fi);
3122}
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3125
3126 void
3127set_context_for_expression(xp, arg, cmdidx)
3128 expand_T *xp;
3129 char_u *arg;
3130 cmdidx_T cmdidx;
3131{
3132 int got_eq = FALSE;
3133 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136 if (cmdidx == CMD_let)
3137 {
3138 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003139 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 {
3141 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003142 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143 {
3144 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146 if (vim_iswhite(*p))
3147 break;
3148 }
3149 return;
3150 }
3151 }
3152 else
3153 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3154 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 while ((xp->xp_pattern = vim_strpbrk(arg,
3156 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3157 {
3158 c = *xp->xp_pattern;
3159 if (c == '&')
3160 {
3161 c = xp->xp_pattern[1];
3162 if (c == '&')
3163 {
3164 ++xp->xp_pattern;
3165 xp->xp_context = cmdidx != CMD_let || got_eq
3166 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3167 }
3168 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003171 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3172 xp->xp_pattern += 2;
3173
3174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176 else if (c == '$')
3177 {
3178 /* environment variable */
3179 xp->xp_context = EXPAND_ENV_VARS;
3180 }
3181 else if (c == '=')
3182 {
3183 got_eq = TRUE;
3184 xp->xp_context = EXPAND_EXPRESSION;
3185 }
3186 else if (c == '<'
3187 && xp->xp_context == EXPAND_FUNCTIONS
3188 && vim_strchr(xp->xp_pattern, '(') == NULL)
3189 {
3190 /* Function name can start with "<SNR>" */
3191 break;
3192 }
3193 else if (cmdidx != CMD_let || got_eq)
3194 {
3195 if (c == '"') /* string */
3196 {
3197 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3198 if (c == '\\' && xp->xp_pattern[1] != NUL)
3199 ++xp->xp_pattern;
3200 xp->xp_context = EXPAND_NOTHING;
3201 }
3202 else if (c == '\'') /* literal string */
3203 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003204 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3206 /* skip */ ;
3207 xp->xp_context = EXPAND_NOTHING;
3208 }
3209 else if (c == '|')
3210 {
3211 if (xp->xp_pattern[1] == '|')
3212 {
3213 ++xp->xp_pattern;
3214 xp->xp_context = EXPAND_EXPRESSION;
3215 }
3216 else
3217 xp->xp_context = EXPAND_COMMANDS;
3218 }
3219 else
3220 xp->xp_context = EXPAND_EXPRESSION;
3221 }
3222 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 /* Doesn't look like something valid, expand as an expression
3224 * anyway. */
3225 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 arg = xp->xp_pattern;
3227 if (*arg != NUL)
3228 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3229 /* skip */ ;
3230 }
3231 xp->xp_pattern = arg;
3232}
3233
3234#endif /* FEAT_CMDL_COMPL */
3235
3236/*
3237 * ":1,25call func(arg1, arg2)" function call.
3238 */
3239 void
3240ex_call(eap)
3241 exarg_T *eap;
3242{
3243 char_u *arg = eap->arg;
3244 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003246 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 linenr_T lnum;
3250 int doesrange;
3251 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003254 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003255 if (fudi.fd_newkey != NULL)
3256 {
3257 /* Still need to give an error message for missing key. */
3258 EMSG2(_(e_dictkey), fudi.fd_newkey);
3259 vim_free(fudi.fd_newkey);
3260 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003261 if (tofree == NULL)
3262 return;
3263
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003264 /* Increase refcount on dictionary, it could get deleted when evaluating
3265 * the arguments. */
3266 if (fudi.fd_dict != NULL)
3267 ++fudi.fd_dict->dv_refcount;
3268
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003269 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003270 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003271 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar532c7802005-01-27 14:44:31 +00003273 /* Skip white space to allow ":call func ()". Not good, but required for
3274 * backward compatibility. */
3275 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278 if (*startarg != '(')
3279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003280 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 goto end;
3282 }
3283
3284 /*
3285 * When skipping, evaluate the function once, to find the end of the
3286 * arguments.
3287 * When the function takes a range, this is discovered after the first
3288 * call, and the loop is broken.
3289 */
3290 if (eap->skip)
3291 {
3292 ++emsg_skip;
3293 lnum = eap->line2; /* do it once, also with an invalid range */
3294 }
3295 else
3296 lnum = eap->line1;
3297 for ( ; lnum <= eap->line2; ++lnum)
3298 {
3299 if (!eap->skip && eap->addr_count > 0)
3300 {
3301 curwin->w_cursor.lnum = lnum;
3302 curwin->w_cursor.col = 0;
3303 }
3304 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003305 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003306 eap->line1, eap->line2, &doesrange,
3307 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 failed = TRUE;
3310 break;
3311 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003312
3313 /* Handle a function returning a Funcref, Dictionary or List. */
3314 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3315 {
3316 failed = TRUE;
3317 break;
3318 }
3319
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003320 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (doesrange || eap->skip)
3322 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003323
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003325 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003326 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003327 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (aborting())
3329 break;
3330 }
3331 if (eap->skip)
3332 --emsg_skip;
3333
3334 if (!failed)
3335 {
3336 /* Check for trailing illegal characters and a following command. */
3337 if (!ends_excmd(*arg))
3338 {
3339 emsg_severe = TRUE;
3340 EMSG(_(e_trailing));
3341 }
3342 else
3343 eap->nextcmd = check_nextcmd(arg);
3344 }
3345
3346end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003347 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003348 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349}
3350
3351/*
3352 * ":unlet[!] var1 ... " command.
3353 */
3354 void
3355ex_unlet(eap)
3356 exarg_T *eap;
3357{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003358 ex_unletlock(eap, eap->arg, 0);
3359}
3360
3361/*
3362 * ":lockvar" and ":unlockvar" commands
3363 */
3364 void
3365ex_lockvar(eap)
3366 exarg_T *eap;
3367{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369 int deep = 2;
3370
3371 if (eap->forceit)
3372 deep = -1;
3373 else if (vim_isdigit(*arg))
3374 {
3375 deep = getdigits(&arg);
3376 arg = skipwhite(arg);
3377 }
3378
3379 ex_unletlock(eap, arg, deep);
3380}
3381
3382/*
3383 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3384 */
3385 static void
3386ex_unletlock(eap, argstart, deep)
3387 exarg_T *eap;
3388 char_u *argstart;
3389 int deep;
3390{
3391 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003394 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 do
3397 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003398 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003399 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3400 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003401 if (lv.ll_name == NULL)
3402 error = TRUE; /* error but continue parsing */
3403 if (name_end == NULL || (!vim_iswhite(*name_end)
3404 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003406 if (name_end != NULL)
3407 {
3408 emsg_severe = TRUE;
3409 EMSG(_(e_trailing));
3410 }
3411 if (!(eap->skip || error))
3412 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 break;
3414 }
3415
3416 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003417 {
3418 if (eap->cmdidx == CMD_unlet)
3419 {
3420 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3421 error = TRUE;
3422 }
3423 else
3424 {
3425 if (do_lock_var(&lv, name_end, deep,
3426 eap->cmdidx == CMD_lockvar) == FAIL)
3427 error = TRUE;
3428 }
3429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003431 if (!eap->skip)
3432 clear_lval(&lv);
3433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 arg = skipwhite(name_end);
3435 } while (!ends_excmd(*arg));
3436
3437 eap->nextcmd = check_nextcmd(arg);
3438}
3439
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003441do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003442 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003443 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003444 int forceit;
3445{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003446 int ret = OK;
3447 int cc;
3448
3449 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003450 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003451 cc = *name_end;
3452 *name_end = NUL;
3453
3454 /* Normal name or expanded name. */
3455 if (check_changedtick(lp->ll_name))
3456 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003457 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003458 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003460 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003461 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3462 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003463 else if (lp->ll_range)
3464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003466
3467 /* Delete a range of List items. */
3468 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3469 {
3470 li = lp->ll_li->li_next;
3471 listitem_remove(lp->ll_list, lp->ll_li);
3472 lp->ll_li = li;
3473 ++lp->ll_n1;
3474 }
3475 }
3476 else
3477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 /* unlet a List item. */
3480 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003481 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003482 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003484 }
3485
3486 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003487}
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489/*
3490 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003491 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
3493 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003494do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003496 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
Bram Moolenaar33570922005-01-25 22:26:29 +00003498 hashtab_T *ht;
3499 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003500 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003501 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar33570922005-01-25 22:26:29 +00003503 ht = find_var_ht(name, &varname);
3504 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003506 hi = hash_find(ht, varname);
3507 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003508 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003509 di = HI2DI(hi);
3510 if (var_check_fixed(di->di_flags, name)
3511 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003512 return FAIL;
3513 delete_var(ht, hi);
3514 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003517 if (forceit)
3518 return OK;
3519 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 return FAIL;
3521}
3522
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003523/*
3524 * Lock or unlock variable indicated by "lp".
3525 * "deep" is the levels to go (-1 for unlimited);
3526 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3527 */
3528 static int
3529do_lock_var(lp, name_end, deep, lock)
3530 lval_T *lp;
3531 char_u *name_end;
3532 int deep;
3533 int lock;
3534{
3535 int ret = OK;
3536 int cc;
3537 dictitem_T *di;
3538
3539 if (deep == 0) /* nothing to do */
3540 return OK;
3541
3542 if (lp->ll_tv == NULL)
3543 {
3544 cc = *name_end;
3545 *name_end = NUL;
3546
3547 /* Normal name or expanded name. */
3548 if (check_changedtick(lp->ll_name))
3549 ret = FAIL;
3550 else
3551 {
3552 di = find_var(lp->ll_name, NULL);
3553 if (di == NULL)
3554 ret = FAIL;
3555 else
3556 {
3557 if (lock)
3558 di->di_flags |= DI_FLAGS_LOCK;
3559 else
3560 di->di_flags &= ~DI_FLAGS_LOCK;
3561 item_lock(&di->di_tv, deep, lock);
3562 }
3563 }
3564 *name_end = cc;
3565 }
3566 else if (lp->ll_range)
3567 {
3568 listitem_T *li = lp->ll_li;
3569
3570 /* (un)lock a range of List items. */
3571 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3572 {
3573 item_lock(&li->li_tv, deep, lock);
3574 li = li->li_next;
3575 ++lp->ll_n1;
3576 }
3577 }
3578 else if (lp->ll_list != NULL)
3579 /* (un)lock a List item. */
3580 item_lock(&lp->ll_li->li_tv, deep, lock);
3581 else
3582 /* un(lock) a Dictionary item. */
3583 item_lock(&lp->ll_di->di_tv, deep, lock);
3584
3585 return ret;
3586}
3587
3588/*
3589 * Lock or unlock an item. "deep" is nr of levels to go.
3590 */
3591 static void
3592item_lock(tv, deep, lock)
3593 typval_T *tv;
3594 int deep;
3595 int lock;
3596{
3597 static int recurse = 0;
3598 list_T *l;
3599 listitem_T *li;
3600 dict_T *d;
3601 hashitem_T *hi;
3602 int todo;
3603
3604 if (recurse >= DICT_MAXNEST)
3605 {
3606 EMSG(_("E743: variable nested too deep for (un)lock"));
3607 return;
3608 }
3609 if (deep == 0)
3610 return;
3611 ++recurse;
3612
3613 /* lock/unlock the item itself */
3614 if (lock)
3615 tv->v_lock |= VAR_LOCKED;
3616 else
3617 tv->v_lock &= ~VAR_LOCKED;
3618
3619 switch (tv->v_type)
3620 {
3621 case VAR_LIST:
3622 if ((l = tv->vval.v_list) != NULL)
3623 {
3624 if (lock)
3625 l->lv_lock |= VAR_LOCKED;
3626 else
3627 l->lv_lock &= ~VAR_LOCKED;
3628 if (deep < 0 || deep > 1)
3629 /* recursive: lock/unlock the items the List contains */
3630 for (li = l->lv_first; li != NULL; li = li->li_next)
3631 item_lock(&li->li_tv, deep - 1, lock);
3632 }
3633 break;
3634 case VAR_DICT:
3635 if ((d = tv->vval.v_dict) != NULL)
3636 {
3637 if (lock)
3638 d->dv_lock |= VAR_LOCKED;
3639 else
3640 d->dv_lock &= ~VAR_LOCKED;
3641 if (deep < 0 || deep > 1)
3642 {
3643 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003644 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3646 {
3647 if (!HASHITEM_EMPTY(hi))
3648 {
3649 --todo;
3650 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3651 }
3652 }
3653 }
3654 }
3655 }
3656 --recurse;
3657}
3658
Bram Moolenaara40058a2005-07-11 22:42:07 +00003659/*
3660 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3661 * it refers to a List or Dictionary that is locked.
3662 */
3663 static int
3664tv_islocked(tv)
3665 typval_T *tv;
3666{
3667 return (tv->v_lock & VAR_LOCKED)
3668 || (tv->v_type == VAR_LIST
3669 && tv->vval.v_list != NULL
3670 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3671 || (tv->v_type == VAR_DICT
3672 && tv->vval.v_dict != NULL
3673 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3674}
3675
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3677/*
3678 * Delete all "menutrans_" variables.
3679 */
3680 void
3681del_menutrans_vars()
3682{
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003687 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003689 {
3690 if (!HASHITEM_EMPTY(hi))
3691 {
3692 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003693 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3694 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003695 }
3696 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003697 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698}
3699#endif
3700
3701#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3702
3703/*
3704 * Local string buffer for the next two functions to store a variable name
3705 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3706 * get_user_var_name().
3707 */
3708
3709static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3710
3711static char_u *varnamebuf = NULL;
3712static int varnamebuflen = 0;
3713
3714/*
3715 * Function to concatenate a prefix and a variable name.
3716 */
3717 static char_u *
3718cat_prefix_varname(prefix, name)
3719 int prefix;
3720 char_u *name;
3721{
3722 int len;
3723
3724 len = (int)STRLEN(name) + 3;
3725 if (len > varnamebuflen)
3726 {
3727 vim_free(varnamebuf);
3728 len += 10; /* some additional space */
3729 varnamebuf = alloc(len);
3730 if (varnamebuf == NULL)
3731 {
3732 varnamebuflen = 0;
3733 return NULL;
3734 }
3735 varnamebuflen = len;
3736 }
3737 *varnamebuf = prefix;
3738 varnamebuf[1] = ':';
3739 STRCPY(varnamebuf + 2, name);
3740 return varnamebuf;
3741}
3742
3743/*
3744 * Function given to ExpandGeneric() to obtain the list of user defined
3745 * (global/buffer/window/built-in) variable names.
3746 */
3747/*ARGSUSED*/
3748 char_u *
3749get_user_var_name(xp, idx)
3750 expand_T *xp;
3751 int idx;
3752{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003753 static long_u gdone;
3754 static long_u bdone;
3755 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003756#ifdef FEAT_WINDOWS
3757 static long_u tdone;
3758#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003759 static int vidx;
3760 static hashitem_T *hi;
3761 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003764 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003765 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003766#ifdef FEAT_WINDOWS
3767 tdone = 0;
3768#endif
3769 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003770
3771 /* Global variables */
3772 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003774 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003776 else
3777 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003778 while (HASHITEM_EMPTY(hi))
3779 ++hi;
3780 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3781 return cat_prefix_varname('g', hi->hi_key);
3782 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003784
3785 /* b: variables */
3786 ht = &curbuf->b_vars.dv_hashtab;
3787 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003789 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003791 else
3792 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003793 while (HASHITEM_EMPTY(hi))
3794 ++hi;
3795 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003797 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003799 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 return (char_u *)"b:changedtick";
3801 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003802
3803 /* w: variables */
3804 ht = &curwin->w_vars.dv_hashtab;
3805 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003807 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003808 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003809 else
3810 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003811 while (HASHITEM_EMPTY(hi))
3812 ++hi;
3813 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003815
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003816#ifdef FEAT_WINDOWS
3817 /* t: variables */
3818 ht = &curtab->tp_vars.dv_hashtab;
3819 if (tdone < ht->ht_used)
3820 {
3821 if (tdone++ == 0)
3822 hi = ht->ht_array;
3823 else
3824 ++hi;
3825 while (HASHITEM_EMPTY(hi))
3826 ++hi;
3827 return cat_prefix_varname('t', hi->hi_key);
3828 }
3829#endif
3830
Bram Moolenaar33570922005-01-25 22:26:29 +00003831 /* v: variables */
3832 if (vidx < VV_LEN)
3833 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 vim_free(varnamebuf);
3836 varnamebuf = NULL;
3837 varnamebuflen = 0;
3838 return NULL;
3839}
3840
3841#endif /* FEAT_CMDL_COMPL */
3842
3843/*
3844 * types for expressions.
3845 */
3846typedef enum
3847{
3848 TYPE_UNKNOWN = 0
3849 , TYPE_EQUAL /* == */
3850 , TYPE_NEQUAL /* != */
3851 , TYPE_GREATER /* > */
3852 , TYPE_GEQUAL /* >= */
3853 , TYPE_SMALLER /* < */
3854 , TYPE_SEQUAL /* <= */
3855 , TYPE_MATCH /* =~ */
3856 , TYPE_NOMATCH /* !~ */
3857} exptype_T;
3858
3859/*
3860 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3863 */
3864
3865/*
3866 * Handle zero level expression.
3867 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003869 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * Return OK or FAIL.
3871 */
3872 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003873eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 char_u **nextcmd;
3877 int evaluate;
3878{
3879 int ret;
3880 char_u *p;
3881
3882 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003883 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (ret == FAIL || !ends_excmd(*p))
3885 {
3886 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 /*
3889 * Report the invalid expression unless the expression evaluation has
3890 * been cancelled due to an aborting error, an interrupt, or an
3891 * exception.
3892 */
3893 if (!aborting())
3894 EMSG2(_(e_invexpr2), arg);
3895 ret = FAIL;
3896 }
3897 if (nextcmd != NULL)
3898 *nextcmd = check_nextcmd(p);
3899
3900 return ret;
3901}
3902
3903/*
3904 * Handle top level expression:
3905 * expr1 ? expr0 : expr0
3906 *
3907 * "arg" must point to the first non-white of the expression.
3908 * "arg" is advanced to the next non-white after the recognized expression.
3909 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003910 * Note: "rettv.v_lock" is not set.
3911 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 * Return OK or FAIL.
3913 */
3914 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 int evaluate;
3919{
3920 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003921 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 /*
3924 * Get the first variable.
3925 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 return FAIL;
3928
3929 if ((*arg)[0] == '?')
3930 {
3931 result = FALSE;
3932 if (evaluate)
3933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003934 int error = FALSE;
3935
3936 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003938 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003939 if (error)
3940 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942
3943 /*
3944 * Get the second variable.
3945 */
3946 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 return FAIL;
3949
3950 /*
3951 * Check for the ":".
3952 */
3953 if ((*arg)[0] != ':')
3954 {
3955 EMSG(_("E109: Missing ':' after '?'"));
3956 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003957 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 return FAIL;
3959 }
3960
3961 /*
3962 * Get the third variable.
3963 */
3964 *arg = skipwhite(*arg + 1);
3965 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3966 {
3967 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 return FAIL;
3970 }
3971 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974
3975 return OK;
3976}
3977
3978/*
3979 * Handle first level expression:
3980 * expr2 || expr2 || expr2 logical OR
3981 *
3982 * "arg" must point to the first non-white of the expression.
3983 * "arg" is advanced to the next non-white after the recognized expression.
3984 *
3985 * Return OK or FAIL.
3986 */
3987 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int evaluate;
3992{
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 long result;
3995 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
3998 /*
3999 * Get the first variable.
4000 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 return FAIL;
4003
4004 /*
4005 * Repeat until there is no following "||".
4006 */
4007 first = TRUE;
4008 result = FALSE;
4009 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4010 {
4011 if (evaluate && first)
4012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004016 if (error)
4017 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 first = FALSE;
4019 }
4020
4021 /*
4022 * Get the second variable.
4023 */
4024 *arg = skipwhite(*arg + 2);
4025 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4026 return FAIL;
4027
4028 /*
4029 * Compute the result.
4030 */
4031 if (evaluate && !result)
4032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004033 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004035 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004036 if (error)
4037 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 if (evaluate)
4040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 rettv->v_type = VAR_NUMBER;
4042 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044 }
4045
4046 return OK;
4047}
4048
4049/*
4050 * Handle second level expression:
4051 * expr3 && expr3 && expr3 logical AND
4052 *
4053 * "arg" must point to the first non-white of the expression.
4054 * "arg" is advanced to the next non-white after the recognized expression.
4055 *
4056 * Return OK or FAIL.
4057 */
4058 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 int evaluate;
4063{
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 long result;
4066 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 /*
4070 * Get the first variable.
4071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return FAIL;
4074
4075 /*
4076 * Repeat until there is no following "&&".
4077 */
4078 first = TRUE;
4079 result = TRUE;
4080 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4081 {
4082 if (evaluate && first)
4083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004084 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004087 if (error)
4088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 first = FALSE;
4090 }
4091
4092 /*
4093 * Get the second variable.
4094 */
4095 *arg = skipwhite(*arg + 2);
4096 if (eval4(arg, &var2, evaluate && result) == FAIL)
4097 return FAIL;
4098
4099 /*
4100 * Compute the result.
4101 */
4102 if (evaluate && result)
4103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004107 if (error)
4108 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 if (evaluate)
4111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 rettv->v_type = VAR_NUMBER;
4113 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 }
4116
4117 return OK;
4118}
4119
4120/*
4121 * Handle third level expression:
4122 * var1 == var2
4123 * var1 =~ var2
4124 * var1 != var2
4125 * var1 !~ var2
4126 * var1 > var2
4127 * var1 >= var2
4128 * var1 < var2
4129 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004130 * var1 is var2
4131 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 char_u *p;
4146 int i;
4147 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004148 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int len = 2;
4150 long n1, n2;
4151 char_u *s1, *s2;
4152 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4153 regmatch_T regmatch;
4154 int ic;
4155 char_u *save_cpo;
4156
4157 /*
4158 * Get the first variable.
4159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return FAIL;
4162
4163 p = *arg;
4164 switch (p[0])
4165 {
4166 case '=': if (p[1] == '=')
4167 type = TYPE_EQUAL;
4168 else if (p[1] == '~')
4169 type = TYPE_MATCH;
4170 break;
4171 case '!': if (p[1] == '=')
4172 type = TYPE_NEQUAL;
4173 else if (p[1] == '~')
4174 type = TYPE_NOMATCH;
4175 break;
4176 case '>': if (p[1] != '=')
4177 {
4178 type = TYPE_GREATER;
4179 len = 1;
4180 }
4181 else
4182 type = TYPE_GEQUAL;
4183 break;
4184 case '<': if (p[1] != '=')
4185 {
4186 type = TYPE_SMALLER;
4187 len = 1;
4188 }
4189 else
4190 type = TYPE_SEQUAL;
4191 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004192 case 'i': if (p[1] == 's')
4193 {
4194 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4195 len = 5;
4196 if (!vim_isIDc(p[len]))
4197 {
4198 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4199 type_is = TRUE;
4200 }
4201 }
4202 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204
4205 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004206 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 */
4208 if (type != TYPE_UNKNOWN)
4209 {
4210 /* extra question mark appended: ignore case */
4211 if (p[len] == '?')
4212 {
4213 ic = TRUE;
4214 ++len;
4215 }
4216 /* extra '#' appended: match case */
4217 else if (p[len] == '#')
4218 {
4219 ic = FALSE;
4220 ++len;
4221 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004222 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 else
4224 ic = p_ic;
4225
4226 /*
4227 * Get the second variable.
4228 */
4229 *arg = skipwhite(p + len);
4230 if (eval5(arg, &var2, evaluate) == FAIL)
4231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return FAIL;
4234 }
4235
4236 if (evaluate)
4237 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004238 if (type_is && rettv->v_type != var2.v_type)
4239 {
4240 /* For "is" a different type always means FALSE, for "notis"
4241 * it means TRUE. */
4242 n1 = (type == TYPE_NEQUAL);
4243 }
4244 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4245 {
4246 if (type_is)
4247 {
4248 n1 = (rettv->v_type == var2.v_type
4249 && rettv->vval.v_list == var2.vval.v_list);
4250 if (type == TYPE_NEQUAL)
4251 n1 = !n1;
4252 }
4253 else if (rettv->v_type != var2.v_type
4254 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4255 {
4256 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004257 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004258 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004259 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004260 clear_tv(rettv);
4261 clear_tv(&var2);
4262 return FAIL;
4263 }
4264 else
4265 {
4266 /* Compare two Lists for being equal or unequal. */
4267 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4268 if (type == TYPE_NEQUAL)
4269 n1 = !n1;
4270 }
4271 }
4272
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004273 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4274 {
4275 if (type_is)
4276 {
4277 n1 = (rettv->v_type == var2.v_type
4278 && rettv->vval.v_dict == var2.vval.v_dict);
4279 if (type == TYPE_NEQUAL)
4280 n1 = !n1;
4281 }
4282 else if (rettv->v_type != var2.v_type
4283 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4284 {
4285 if (rettv->v_type != var2.v_type)
4286 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4287 else
4288 EMSG(_("E736: Invalid operation for Dictionary"));
4289 clear_tv(rettv);
4290 clear_tv(&var2);
4291 return FAIL;
4292 }
4293 else
4294 {
4295 /* Compare two Dictionaries for being equal or unequal. */
4296 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4297 if (type == TYPE_NEQUAL)
4298 n1 = !n1;
4299 }
4300 }
4301
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4303 {
4304 if (rettv->v_type != var2.v_type
4305 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4306 {
4307 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004308 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004309 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004310 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004311 clear_tv(rettv);
4312 clear_tv(&var2);
4313 return FAIL;
4314 }
4315 else
4316 {
4317 /* Compare two Funcrefs for being equal or unequal. */
4318 if (rettv->vval.v_string == NULL
4319 || var2.vval.v_string == NULL)
4320 n1 = FALSE;
4321 else
4322 n1 = STRCMP(rettv->vval.v_string,
4323 var2.vval.v_string) == 0;
4324 if (type == TYPE_NEQUAL)
4325 n1 = !n1;
4326 }
4327 }
4328
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004329#ifdef FEAT_FLOAT
4330 /*
4331 * If one of the two variables is a float, compare as a float.
4332 * When using "=~" or "!~", always compare as string.
4333 */
4334 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4335 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4336 {
4337 float_T f1, f2;
4338
4339 if (rettv->v_type == VAR_FLOAT)
4340 f1 = rettv->vval.v_float;
4341 else
4342 f1 = get_tv_number(rettv);
4343 if (var2.v_type == VAR_FLOAT)
4344 f2 = var2.vval.v_float;
4345 else
4346 f2 = get_tv_number(&var2);
4347 n1 = FALSE;
4348 switch (type)
4349 {
4350 case TYPE_EQUAL: n1 = (f1 == f2); break;
4351 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4352 case TYPE_GREATER: n1 = (f1 > f2); break;
4353 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4354 case TYPE_SMALLER: n1 = (f1 < f2); break;
4355 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4356 case TYPE_UNKNOWN:
4357 case TYPE_MATCH:
4358 case TYPE_NOMATCH: break; /* avoid gcc warning */
4359 }
4360 }
4361#endif
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 /*
4364 * If one of the two variables is a number, compare as a number.
4365 * When using "=~" or "!~", always compare as string.
4366 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 n1 = get_tv_number(rettv);
4371 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 switch (type)
4373 {
4374 case TYPE_EQUAL: n1 = (n1 == n2); break;
4375 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4376 case TYPE_GREATER: n1 = (n1 > n2); break;
4377 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4378 case TYPE_SMALLER: n1 = (n1 < n2); break;
4379 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4380 case TYPE_UNKNOWN:
4381 case TYPE_MATCH:
4382 case TYPE_NOMATCH: break; /* avoid gcc warning */
4383 }
4384 }
4385 else
4386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 s1 = get_tv_string_buf(rettv, buf1);
4388 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4390 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4391 else
4392 i = 0;
4393 n1 = FALSE;
4394 switch (type)
4395 {
4396 case TYPE_EQUAL: n1 = (i == 0); break;
4397 case TYPE_NEQUAL: n1 = (i != 0); break;
4398 case TYPE_GREATER: n1 = (i > 0); break;
4399 case TYPE_GEQUAL: n1 = (i >= 0); break;
4400 case TYPE_SMALLER: n1 = (i < 0); break;
4401 case TYPE_SEQUAL: n1 = (i <= 0); break;
4402
4403 case TYPE_MATCH:
4404 case TYPE_NOMATCH:
4405 /* avoid 'l' flag in 'cpoptions' */
4406 save_cpo = p_cpo;
4407 p_cpo = (char_u *)"";
4408 regmatch.regprog = vim_regcomp(s2,
4409 RE_MAGIC + RE_STRING);
4410 regmatch.rm_ic = ic;
4411 if (regmatch.regprog != NULL)
4412 {
4413 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4414 vim_free(regmatch.regprog);
4415 if (type == TYPE_NOMATCH)
4416 n1 = !n1;
4417 }
4418 p_cpo = save_cpo;
4419 break;
4420
4421 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4422 }
4423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 clear_tv(rettv);
4425 clear_tv(&var2);
4426 rettv->v_type = VAR_NUMBER;
4427 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 }
4430
4431 return OK;
4432}
4433
4434/*
4435 * Handle fourth level expression:
4436 * + number addition
4437 * - number subtraction
4438 * . string concatenation
4439 *
4440 * "arg" must point to the first non-white of the expression.
4441 * "arg" is advanced to the next non-white after the recognized expression.
4442 *
4443 * Return OK or FAIL.
4444 */
4445 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 int evaluate;
4450{
Bram Moolenaar33570922005-01-25 22:26:29 +00004451 typval_T var2;
4452 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 int op;
4454 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455#ifdef FEAT_FLOAT
4456 float_T f1 = 0, f2 = 0;
4457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 char_u *s1, *s2;
4459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4460 char_u *p;
4461
4462 /*
4463 * Get the first variable.
4464 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004465 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 return FAIL;
4467
4468 /*
4469 * Repeat computing, until no '+', '-' or '.' is following.
4470 */
4471 for (;;)
4472 {
4473 op = **arg;
4474 if (op != '+' && op != '-' && op != '.')
4475 break;
4476
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004477 if ((op != '+' || rettv->v_type != VAR_LIST)
4478#ifdef FEAT_FLOAT
4479 && (op == '.' || rettv->v_type != VAR_FLOAT)
4480#endif
4481 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 {
4483 /* For "list + ...", an illegal use of the first operand as
4484 * a number cannot be determined before evaluating the 2nd
4485 * operand: if this is also a list, all is ok.
4486 * For "something . ...", "something - ..." or "non-list + ...",
4487 * we know that the first operand needs to be a string or number
4488 * without evaluating the 2nd operand. So check before to avoid
4489 * side effects after an error. */
4490 if (evaluate && get_tv_string_chk(rettv) == NULL)
4491 {
4492 clear_tv(rettv);
4493 return FAIL;
4494 }
4495 }
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 /*
4498 * Get the second variable.
4499 */
4500 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004501 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004503 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 return FAIL;
4505 }
4506
4507 if (evaluate)
4508 {
4509 /*
4510 * Compute the result.
4511 */
4512 if (op == '.')
4513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004514 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4515 s2 = get_tv_string_buf_chk(&var2, buf2);
4516 if (s2 == NULL) /* type error ? */
4517 {
4518 clear_tv(rettv);
4519 clear_tv(&var2);
4520 return FAIL;
4521 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004522 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 clear_tv(rettv);
4524 rettv->v_type = VAR_STRING;
4525 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004527 else if (op == '+' && rettv->v_type == VAR_LIST
4528 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004529 {
4530 /* concatenate Lists */
4531 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4532 &var3) == FAIL)
4533 {
4534 clear_tv(rettv);
4535 clear_tv(&var2);
4536 return FAIL;
4537 }
4538 clear_tv(rettv);
4539 *rettv = var3;
4540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else
4542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004543 int error = FALSE;
4544
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004545#ifdef FEAT_FLOAT
4546 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004547 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004548 f1 = rettv->vval.v_float;
4549 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004550 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004551 else
4552#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004554 n1 = get_tv_number_chk(rettv, &error);
4555 if (error)
4556 {
4557 /* This can only happen for "list + non-list". For
4558 * "non-list + ..." or "something - ...", we returned
4559 * before evaluating the 2nd operand. */
4560 clear_tv(rettv);
4561 return FAIL;
4562 }
4563#ifdef FEAT_FLOAT
4564 if (var2.v_type == VAR_FLOAT)
4565 f1 = n1;
4566#endif
4567 }
4568#ifdef FEAT_FLOAT
4569 if (var2.v_type == VAR_FLOAT)
4570 {
4571 f2 = var2.vval.v_float;
4572 n2 = 0;
4573 }
4574 else
4575#endif
4576 {
4577 n2 = get_tv_number_chk(&var2, &error);
4578 if (error)
4579 {
4580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 return FAIL;
4583 }
4584#ifdef FEAT_FLOAT
4585 if (rettv->v_type == VAR_FLOAT)
4586 f2 = n2;
4587#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004588 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004589 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004590
4591#ifdef FEAT_FLOAT
4592 /* If there is a float on either side the result is a float. */
4593 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4594 {
4595 if (op == '+')
4596 f1 = f1 + f2;
4597 else
4598 f1 = f1 - f2;
4599 rettv->v_type = VAR_FLOAT;
4600 rettv->vval.v_float = f1;
4601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004603#endif
4604 {
4605 if (op == '+')
4606 n1 = n1 + n2;
4607 else
4608 n1 = n1 - n2;
4609 rettv->v_type = VAR_NUMBER;
4610 rettv->vval.v_number = n1;
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 }
4616 return OK;
4617}
4618
4619/*
4620 * Handle fifth level expression:
4621 * * number multiplication
4622 * / number division
4623 * % number modulo
4624 *
4625 * "arg" must point to the first non-white of the expression.
4626 * "arg" is advanced to the next non-white after the recognized expression.
4627 *
4628 * Return OK or FAIL.
4629 */
4630 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004631eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004635 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
Bram Moolenaar33570922005-01-25 22:26:29 +00004637 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 int op;
4639 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640#ifdef FEAT_FLOAT
4641 int use_float = FALSE;
4642 float_T f1 = 0, f2;
4643#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 /*
4647 * Get the first variable.
4648 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004649 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return FAIL;
4651
4652 /*
4653 * Repeat computing, until no '*', '/' or '%' is following.
4654 */
4655 for (;;)
4656 {
4657 op = **arg;
4658 if (op != '*' && op != '/' && op != '%')
4659 break;
4660
4661 if (evaluate)
4662 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004663#ifdef FEAT_FLOAT
4664 if (rettv->v_type == VAR_FLOAT)
4665 {
4666 f1 = rettv->vval.v_float;
4667 use_float = TRUE;
4668 n1 = 0;
4669 }
4670 else
4671#endif
4672 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 if (error)
4675 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 }
4677 else
4678 n1 = 0;
4679
4680 /*
4681 * Get the second variable.
4682 */
4683 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004684 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 return FAIL;
4686
4687 if (evaluate)
4688 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689#ifdef FEAT_FLOAT
4690 if (var2.v_type == VAR_FLOAT)
4691 {
4692 if (!use_float)
4693 {
4694 f1 = n1;
4695 use_float = TRUE;
4696 }
4697 f2 = var2.vval.v_float;
4698 n2 = 0;
4699 }
4700 else
4701#endif
4702 {
4703 n2 = get_tv_number_chk(&var2, &error);
4704 clear_tv(&var2);
4705 if (error)
4706 return FAIL;
4707#ifdef FEAT_FLOAT
4708 if (use_float)
4709 f2 = n2;
4710#endif
4711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713 /*
4714 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717#ifdef FEAT_FLOAT
4718 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720 if (op == '*')
4721 f1 = f1 * f2;
4722 else if (op == '/')
4723 {
4724 /* We rely on the floating point library to handle divide
4725 * by zero to result in "inf" and not a crash. */
4726 f1 = f1 / f2;
4727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004729 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004730 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 return FAIL;
4732 }
4733 rettv->v_type = VAR_FLOAT;
4734 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004739 if (op == '*')
4740 n1 = n1 * n2;
4741 else if (op == '/')
4742 {
4743 if (n2 == 0) /* give an error message? */
4744 {
4745 if (n1 == 0)
4746 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4747 else if (n1 < 0)
4748 n1 = -0x7fffffffL;
4749 else
4750 n1 = 0x7fffffffL;
4751 }
4752 else
4753 n1 = n1 / n2;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756 {
4757 if (n2 == 0) /* give an error message? */
4758 n1 = 0;
4759 else
4760 n1 = n1 % n2;
4761 }
4762 rettv->v_type = VAR_NUMBER;
4763 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 }
4767
4768 return OK;
4769}
4770
4771/*
4772 * Handle sixth level expression:
4773 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004774 * "string" string constant
4775 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 * &option-name option value
4777 * @r register contents
4778 * identifier variable value
4779 * function() function call
4780 * $VAR environment variable
4781 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004782 * [expr, expr] List
4783 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 *
4785 * Also handle:
4786 * ! in front logical NOT
4787 * - in front unary minus
4788 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 * trailing [] subscript in String or List
4790 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 *
4792 * "arg" must point to the first non-white of the expression.
4793 * "arg" is advanced to the next non-white after the recognized expression.
4794 *
4795 * Return OK or FAIL.
4796 */
4797 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 long n;
4805 int len;
4806 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 char_u *start_leader, *end_leader;
4808 int ret = OK;
4809 char_u *alias;
4810
4811 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
4817 /*
4818 * Skip '!' and '-' characters. They are handled later.
4819 */
4820 start_leader = *arg;
4821 while (**arg == '!' || **arg == '-' || **arg == '+')
4822 *arg = skipwhite(*arg + 1);
4823 end_leader = *arg;
4824
4825 switch (**arg)
4826 {
4827 /*
4828 * Number constant.
4829 */
4830 case '0':
4831 case '1':
4832 case '2':
4833 case '3':
4834 case '4':
4835 case '5':
4836 case '6':
4837 case '7':
4838 case '8':
4839 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 {
4841#ifdef FEAT_FLOAT
4842 char_u *p = skipdigits(*arg + 1);
4843 int get_float = FALSE;
4844
4845 /* We accept a float when the format matches
4846 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004847 * strict to avoid backwards compatibility problems.
4848 * Don't look for a float after the "." operator, so that
4849 * ":let vers = 1.2.3" doesn't fail. */
4850 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852 get_float = TRUE;
4853 p = skipdigits(p + 2);
4854 if (*p == 'e' || *p == 'E')
4855 {
4856 ++p;
4857 if (*p == '-' || *p == '+')
4858 ++p;
4859 if (!vim_isdigit(*p))
4860 get_float = FALSE;
4861 else
4862 p = skipdigits(p + 1);
4863 }
4864 if (ASCII_ISALPHA(*p) || *p == '.')
4865 get_float = FALSE;
4866 }
4867 if (get_float)
4868 {
4869 float_T f;
4870
4871 *arg += string2float(*arg, &f);
4872 if (evaluate)
4873 {
4874 rettv->v_type = VAR_FLOAT;
4875 rettv->vval.v_float = f;
4876 }
4877 }
4878 else
4879#endif
4880 {
4881 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4882 *arg += len;
4883 if (evaluate)
4884 {
4885 rettv->v_type = VAR_NUMBER;
4886 rettv->vval.v_number = n;
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
4892 /*
4893 * String constant: "string".
4894 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 break;
4897
4898 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004899 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004901 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004902 break;
4903
4904 /*
4905 * List: [expr, expr]
4906 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 break;
4909
4910 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004911 * Dictionary: {key: val, key: val}
4912 */
4913 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4914 break;
4915
4916 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004917 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004919 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 break;
4921
4922 /*
4923 * Environment variable: $VAR.
4924 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 break;
4927
4928 /*
4929 * Register contents: @r.
4930 */
4931 case '@': ++*arg;
4932 if (evaluate)
4933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004935 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 if (**arg != NUL)
4938 ++*arg;
4939 break;
4940
4941 /*
4942 * nested expression: (expression).
4943 */
4944 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004945 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (**arg == ')')
4947 ++*arg;
4948 else if (ret == OK)
4949 {
4950 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004951 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 ret = FAIL;
4953 }
4954 break;
4955
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 break;
4958 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959
4960 if (ret == NOTDONE)
4961 {
4962 /*
4963 * Must be a variable or function name.
4964 * Can also be a curly-braces kind of name: {expr}.
4965 */
4966 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004967 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 if (alias != NULL)
4969 s = alias;
4970
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004971 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 ret = FAIL;
4973 else
4974 {
4975 if (**arg == '(') /* recursive! */
4976 {
4977 /* If "s" is the name of a variable of type VAR_FUNC
4978 * use its contents. */
4979 s = deref_func_name(s, &len);
4980
4981 /* Invoke the function. */
4982 ret = get_func_tv(s, len, rettv, arg,
4983 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004984 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004985 /* Stop the expression evaluation when immediately
4986 * aborting on error, or when an interrupt occurred or
4987 * an exception was thrown but not caught. */
4988 if (aborting())
4989 {
4990 if (ret == OK)
4991 clear_tv(rettv);
4992 ret = FAIL;
4993 }
4994 }
4995 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004996 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004997 else
4998 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 }
5000
5001 if (alias != NULL)
5002 vim_free(alias);
5003 }
5004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 *arg = skipwhite(*arg);
5006
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005007 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5008 * expr(expr). */
5009 if (ret == OK)
5010 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
5012 /*
5013 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5014 */
5015 if (ret == OK && evaluate && end_leader > start_leader)
5016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005017 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 int val = 0;
5019#ifdef FEAT_FLOAT
5020 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005021
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 if (rettv->v_type == VAR_FLOAT)
5023 f = rettv->vval.v_float;
5024 else
5025#endif
5026 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005027 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005029 clear_tv(rettv);
5030 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005032 else
5033 {
5034 while (end_leader > start_leader)
5035 {
5036 --end_leader;
5037 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005038 {
5039#ifdef FEAT_FLOAT
5040 if (rettv->v_type == VAR_FLOAT)
5041 f = !f;
5042 else
5043#endif
5044 val = !val;
5045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005046 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005047 {
5048#ifdef FEAT_FLOAT
5049 if (rettv->v_type == VAR_FLOAT)
5050 f = -f;
5051 else
5052#endif
5053 val = -val;
5054 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005055 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056#ifdef FEAT_FLOAT
5057 if (rettv->v_type == VAR_FLOAT)
5058 {
5059 clear_tv(rettv);
5060 rettv->vval.v_float = f;
5061 }
5062 else
5063#endif
5064 {
5065 clear_tv(rettv);
5066 rettv->v_type = VAR_NUMBER;
5067 rettv->vval.v_number = val;
5068 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071
5072 return ret;
5073}
5074
5075/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005076 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5077 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5079 */
5080 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005081eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005083 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005085 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086{
5087 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005088 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005089 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 long len = -1;
5091 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 if (rettv->v_type == VAR_FUNC
5096#ifdef FEAT_FLOAT
5097 || rettv->v_type == VAR_FLOAT
5098#endif
5099 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005101 if (verbose)
5102 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 return FAIL;
5104 }
5105
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 /*
5109 * dict.name
5110 */
5111 key = *arg + 1;
5112 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5113 ;
5114 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 }
5118 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 /*
5121 * something[idx]
5122 *
5123 * Get the (first) variable from inside the [].
5124 */
5125 *arg = skipwhite(*arg + 1);
5126 if (**arg == ':')
5127 empty1 = TRUE;
5128 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5129 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005130 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5131 {
5132 /* not a number or string */
5133 clear_tv(&var1);
5134 return FAIL;
5135 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136
5137 /*
5138 * Get the second variable from inside the [:].
5139 */
5140 if (**arg == ':')
5141 {
5142 range = TRUE;
5143 *arg = skipwhite(*arg + 1);
5144 if (**arg == ']')
5145 empty2 = TRUE;
5146 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005148 if (!empty1)
5149 clear_tv(&var1);
5150 return FAIL;
5151 }
5152 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5153 {
5154 /* not a number or string */
5155 if (!empty1)
5156 clear_tv(&var1);
5157 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 return FAIL;
5159 }
5160 }
5161
5162 /* Check for the ']'. */
5163 if (**arg != ']')
5164 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165 if (verbose)
5166 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 clear_tv(&var1);
5168 if (range)
5169 clear_tv(&var2);
5170 return FAIL;
5171 }
5172 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 }
5174
5175 if (evaluate)
5176 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 n1 = 0;
5178 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 n1 = get_tv_number(&var1);
5181 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182 }
5183 if (range)
5184 {
5185 if (empty2)
5186 n2 = -1;
5187 else
5188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 n2 = get_tv_number(&var2);
5190 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 }
5192 }
5193
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 {
5196 case VAR_NUMBER:
5197 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 len = (long)STRLEN(s);
5200 if (range)
5201 {
5202 /* The resulting variable is a substring. If the indexes
5203 * are out of range the result is empty. */
5204 if (n1 < 0)
5205 {
5206 n1 = len + n1;
5207 if (n1 < 0)
5208 n1 = 0;
5209 }
5210 if (n2 < 0)
5211 n2 = len + n2;
5212 else if (n2 >= len)
5213 n2 = len;
5214 if (n1 >= len || n2 < 0 || n1 > n2)
5215 s = NULL;
5216 else
5217 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5218 }
5219 else
5220 {
5221 /* The resulting variable is a string of a single
5222 * character. If the index is too big or negative the
5223 * result is empty. */
5224 if (n1 >= len || n1 < 0)
5225 s = NULL;
5226 else
5227 s = vim_strnsave(s + n1, 1);
5228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005229 clear_tv(rettv);
5230 rettv->v_type = VAR_STRING;
5231 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 break;
5233
5234 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005235 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 if (n1 < 0)
5237 n1 = len + n1;
5238 if (!empty1 && (n1 < 0 || n1 >= len))
5239 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005240 /* For a range we allow invalid values and return an empty
5241 * list. A list index out of range is an error. */
5242 if (!range)
5243 {
5244 if (verbose)
5245 EMSGN(_(e_listidx), n1);
5246 return FAIL;
5247 }
5248 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 }
5250 if (range)
5251 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005252 list_T *l;
5253 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254
5255 if (n2 < 0)
5256 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005257 else if (n2 >= len)
5258 n2 = len - 1;
5259 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005260 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 l = list_alloc();
5262 if (l == NULL)
5263 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005264 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 n1 <= n2; ++n1)
5266 {
5267 if (list_append_tv(l, &item->li_tv) == FAIL)
5268 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005269 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 return FAIL;
5271 }
5272 item = item->li_next;
5273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 clear_tv(rettv);
5275 rettv->v_type = VAR_LIST;
5276 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005277 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 }
5279 else
5280 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005281 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282 clear_tv(rettv);
5283 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 }
5285 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286
5287 case VAR_DICT:
5288 if (range)
5289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005290 if (verbose)
5291 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 if (len == -1)
5293 clear_tv(&var1);
5294 return FAIL;
5295 }
5296 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298
5299 if (len == -1)
5300 {
5301 key = get_tv_string(&var1);
5302 if (*key == NUL)
5303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005304 if (verbose)
5305 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 clear_tv(&var1);
5307 return FAIL;
5308 }
5309 }
5310
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005311 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005313 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005314 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 if (len == -1)
5316 clear_tv(&var1);
5317 if (item == NULL)
5318 return FAIL;
5319
5320 copy_tv(&item->di_tv, &var1);
5321 clear_tv(rettv);
5322 *rettv = var1;
5323 }
5324 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 }
5326 }
5327
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 return OK;
5329}
5330
5331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 * Get an option value.
5333 * "arg" points to the '&' or '+' before the option name.
5334 * "arg" is advanced to character after the option name.
5335 * Return OK or FAIL.
5336 */
5337 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 int evaluate;
5342{
5343 char_u *option_end;
5344 long numval;
5345 char_u *stringval;
5346 int opt_type;
5347 int c;
5348 int working = (**arg == '+'); /* has("+option") */
5349 int ret = OK;
5350 int opt_flags;
5351
5352 /*
5353 * Isolate the option name and find its value.
5354 */
5355 option_end = find_option_end(arg, &opt_flags);
5356 if (option_end == NULL)
5357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 EMSG2(_("E112: Option name missing: %s"), *arg);
5360 return FAIL;
5361 }
5362
5363 if (!evaluate)
5364 {
5365 *arg = option_end;
5366 return OK;
5367 }
5368
5369 c = *option_end;
5370 *option_end = NUL;
5371 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
5374 if (opt_type == -3) /* invalid name */
5375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 EMSG2(_("E113: Unknown option: %s"), *arg);
5378 ret = FAIL;
5379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005380 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 if (opt_type == -2) /* hidden string option */
5383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 rettv->v_type = VAR_STRING;
5385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387 else if (opt_type == -1) /* hidden number option */
5388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 rettv->v_type = VAR_NUMBER;
5390 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
5392 else if (opt_type == 1) /* number option */
5393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 rettv->v_type = VAR_NUMBER;
5395 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397 else /* string option */
5398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 rettv->v_type = VAR_STRING;
5400 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402 }
5403 else if (working && (opt_type == -2 || opt_type == -1))
5404 ret = FAIL;
5405
5406 *option_end = c; /* put back for error messages */
5407 *arg = option_end;
5408
5409 return ret;
5410}
5411
5412/*
5413 * Allocate a variable for a string constant.
5414 * Return OK or FAIL.
5415 */
5416 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 int evaluate;
5421{
5422 char_u *p;
5423 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 int extra = 0;
5425
5426 /*
5427 * Find the end of the string, skipping backslashed characters.
5428 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005429 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 if (*p == '\\' && p[1] != NUL)
5432 {
5433 ++p;
5434 /* A "\<x>" form occupies at least 4 characters, and produces up
5435 * to 6 characters: reserve space for 2 extra */
5436 if (*p == '<')
5437 extra += 2;
5438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
5440
5441 if (*p != '"')
5442 {
5443 EMSG2(_("E114: Missing quote: %s"), *arg);
5444 return FAIL;
5445 }
5446
5447 /* If only parsing, set *arg and return here */
5448 if (!evaluate)
5449 {
5450 *arg = p + 1;
5451 return OK;
5452 }
5453
5454 /*
5455 * Copy the string into allocated memory, handling backslashed
5456 * characters.
5457 */
5458 name = alloc((unsigned)(p - *arg + extra));
5459 if (name == NULL)
5460 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 rettv->v_type = VAR_STRING;
5462 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 if (*p == '\\')
5467 {
5468 switch (*++p)
5469 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470 case 'b': *name++ = BS; ++p; break;
5471 case 'e': *name++ = ESC; ++p; break;
5472 case 'f': *name++ = FF; ++p; break;
5473 case 'n': *name++ = NL; ++p; break;
5474 case 'r': *name++ = CAR; ++p; break;
5475 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 case 'X': /* hex: "\x1", "\x12" */
5478 case 'x':
5479 case 'u': /* Unicode: "\u0023" */
5480 case 'U':
5481 if (vim_isxdigit(p[1]))
5482 {
5483 int n, nr;
5484 int c = toupper(*p);
5485
5486 if (c == 'X')
5487 n = 2;
5488 else
5489 n = 4;
5490 nr = 0;
5491 while (--n >= 0 && vim_isxdigit(p[1]))
5492 {
5493 ++p;
5494 nr = (nr << 4) + hex2nr(*p);
5495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497#ifdef FEAT_MBYTE
5498 /* For "\u" store the number according to
5499 * 'encoding'. */
5500 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 else
5503#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005504 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 break;
5507
5508 /* octal: "\1", "\12", "\123" */
5509 case '0':
5510 case '1':
5511 case '2':
5512 case '3':
5513 case '4':
5514 case '5':
5515 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 case '7': *name = *p++ - '0';
5517 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 *name = (*name << 3) + *p++ - '0';
5520 if (*p >= '0' && *p <= '7')
5521 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005523 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 break;
5525
5526 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (extra != 0)
5529 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005530 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 break;
5532 }
5533 /* FALLTHROUGH */
5534
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 break;
5537 }
5538 }
5539 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 *arg = p + 1;
5545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 return OK;
5547}
5548
5549/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 * Return OK or FAIL.
5552 */
5553 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 int evaluate;
5558{
5559 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005560 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005561 int reduce = 0;
5562
5563 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005565 */
5566 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005569 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005571 break;
5572 ++reduce;
5573 ++p;
5574 }
5575 }
5576
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005578 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005580 return FAIL;
5581 }
5582
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005584 if (!evaluate)
5585 {
5586 *arg = p + 1;
5587 return OK;
5588 }
5589
5590 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 */
5593 str = alloc((unsigned)((p - *arg) - reduce));
5594 if (str == NULL)
5595 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 rettv->v_type = VAR_STRING;
5597 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005598
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005604 break;
5605 ++p;
5606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005608 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005610 *arg = p + 1;
5611
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005612 return OK;
5613}
5614
5615/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 * Allocate a variable for a List and fill it from "*arg".
5617 * Return OK or FAIL.
5618 */
5619 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 int evaluate;
5624{
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 list_T *l = NULL;
5626 typval_T tv;
5627 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628
5629 if (evaluate)
5630 {
5631 l = list_alloc();
5632 if (l == NULL)
5633 return FAIL;
5634 }
5635
5636 *arg = skipwhite(*arg + 1);
5637 while (**arg != ']' && **arg != NUL)
5638 {
5639 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5640 goto failret;
5641 if (evaluate)
5642 {
5643 item = listitem_alloc();
5644 if (item != NULL)
5645 {
5646 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005647 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 list_append(l, item);
5649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005650 else
5651 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 }
5653
5654 if (**arg == ']')
5655 break;
5656 if (**arg != ',')
5657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659 goto failret;
5660 }
5661 *arg = skipwhite(*arg + 1);
5662 }
5663
5664 if (**arg != ']')
5665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667failret:
5668 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005669 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 return FAIL;
5671 }
5672
5673 *arg = skipwhite(*arg + 1);
5674 if (evaluate)
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_LIST;
5677 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 ++l->lv_refcount;
5679 }
5680
5681 return OK;
5682}
5683
5684/*
5685 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005686 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005688 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689list_alloc()
5690{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005691 list_T *l;
5692
5693 l = (list_T *)alloc_clear(sizeof(list_T));
5694 if (l != NULL)
5695 {
5696 /* Prepend the list to the list of lists for garbage collection. */
5697 if (first_list != NULL)
5698 first_list->lv_used_prev = l;
5699 l->lv_used_prev = NULL;
5700 l->lv_used_next = first_list;
5701 first_list = l;
5702 }
5703 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704}
5705
5706/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005707 * Allocate an empty list for a return value.
5708 * Returns OK or FAIL.
5709 */
5710 static int
5711rettv_list_alloc(rettv)
5712 typval_T *rettv;
5713{
5714 list_T *l = list_alloc();
5715
5716 if (l == NULL)
5717 return FAIL;
5718
5719 rettv->vval.v_list = l;
5720 rettv->v_type = VAR_LIST;
5721 ++l->lv_refcount;
5722 return OK;
5723}
5724
5725/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 * Unreference a list: decrement the reference count and free it when it
5727 * becomes zero.
5728 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005729 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005733 if (l != NULL && --l->lv_refcount <= 0)
5734 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735}
5736
5737/*
5738 * Free a list, including all items it points to.
5739 * Ignores the reference count.
5740 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005741 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005742list_free(l, recurse)
5743 list_T *l;
5744 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745{
Bram Moolenaar33570922005-01-25 22:26:29 +00005746 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005748 /* Remove the list from the list of lists for garbage collection. */
5749 if (l->lv_used_prev == NULL)
5750 first_list = l->lv_used_next;
5751 else
5752 l->lv_used_prev->lv_used_next = l->lv_used_next;
5753 if (l->lv_used_next != NULL)
5754 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5755
Bram Moolenaard9fba312005-06-26 22:34:35 +00005756 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005758 /* Remove the item before deleting it. */
5759 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005760 if (recurse || (item->li_tv.v_type != VAR_LIST
5761 && item->li_tv.v_type != VAR_DICT))
5762 clear_tv(&item->li_tv);
5763 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 }
5765 vim_free(l);
5766}
5767
5768/*
5769 * Allocate a list item.
5770 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005771 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772listitem_alloc()
5773{
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775}
5776
5777/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005778 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 */
5780 static void
5781listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005782 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005784 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785 vim_free(item);
5786}
5787
5788/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005789 * Remove a list item from a List and free it. Also clears the value.
5790 */
5791 static void
5792listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005793 list_T *l;
5794 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005795{
5796 list_remove(l, item, item);
5797 listitem_free(item);
5798}
5799
5800/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 * Get the number of items in a list.
5802 */
5803 static long
5804list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 if (l == NULL)
5808 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005809 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810}
5811
5812/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005813 * Return TRUE when two lists have exactly the same values.
5814 */
5815 static int
5816list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 list_T *l1;
5818 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005819 int ic; /* ignore case for strings */
5820{
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005822
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005823 if (l1 == NULL || l2 == NULL)
5824 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005825 if (l1 == l2)
5826 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005827 if (list_len(l1) != list_len(l2))
5828 return FALSE;
5829
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005830 for (item1 = l1->lv_first, item2 = l2->lv_first;
5831 item1 != NULL && item2 != NULL;
5832 item1 = item1->li_next, item2 = item2->li_next)
5833 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5834 return FALSE;
5835 return item1 == NULL && item2 == NULL;
5836}
5837
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005838#if defined(FEAT_PYTHON) || defined(PROTO)
5839/*
5840 * Return the dictitem that an entry in a hashtable points to.
5841 */
5842 dictitem_T *
5843dict_lookup(hi)
5844 hashitem_T *hi;
5845{
5846 return HI2DI(hi);
5847}
5848#endif
5849
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005850/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005851 * Return TRUE when two dictionaries have exactly the same key/values.
5852 */
5853 static int
5854dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 dict_T *d1;
5856 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005857 int ic; /* ignore case for strings */
5858{
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 hashitem_T *hi;
5860 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005861 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005862
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005863 if (d1 == NULL || d2 == NULL)
5864 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005865 if (d1 == d2)
5866 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005867 if (dict_len(d1) != dict_len(d2))
5868 return FALSE;
5869
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005870 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005873 if (!HASHITEM_EMPTY(hi))
5874 {
5875 item2 = dict_find(d2, hi->hi_key, -1);
5876 if (item2 == NULL)
5877 return FALSE;
5878 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5879 return FALSE;
5880 --todo;
5881 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005882 }
5883 return TRUE;
5884}
5885
5886/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005888 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005889 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890 */
5891 static int
5892tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 typval_T *tv1;
5894 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005895 int ic; /* ignore case */
5896{
5897 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005898 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005899 static int recursive = 0; /* cach recursive loops */
5900 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005901
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005902 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005903 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005904 /* Catch lists and dicts that have an endless loop by limiting
5905 * recursiveness to 1000. We guess they are equal then. */
5906 if (recursive >= 1000)
5907 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005908
5909 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005910 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005911 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005912 ++recursive;
5913 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5914 --recursive;
5915 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005916
5917 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005918 ++recursive;
5919 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5920 --recursive;
5921 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005922
5923 case VAR_FUNC:
5924 return (tv1->vval.v_string != NULL
5925 && tv2->vval.v_string != NULL
5926 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5927
5928 case VAR_NUMBER:
5929 return tv1->vval.v_number == tv2->vval.v_number;
5930
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005931#ifdef FEAT_FLOAT
5932 case VAR_FLOAT:
5933 return tv1->vval.v_float == tv2->vval.v_float;
5934#endif
5935
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005936 case VAR_STRING:
5937 s1 = get_tv_string_buf(tv1, buf1);
5938 s2 = get_tv_string_buf(tv2, buf2);
5939 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005940 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005941
5942 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005943 return TRUE;
5944}
5945
5946/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 * Locate item with index "n" in list "l" and return it.
5948 * A negative index is counted from the end; -1 is the last item.
5949 * Returns NULL when "n" is out of range.
5950 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005953 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 long n;
5955{
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 long idx;
5958
5959 if (l == NULL)
5960 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005961
5962 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005964 n = l->lv_len + n;
5965
5966 /* Check for index out of range. */
5967 if (n < 0 || n >= l->lv_len)
5968 return NULL;
5969
5970 /* When there is a cached index may start search from there. */
5971 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005973 if (n < l->lv_idx / 2)
5974 {
5975 /* closest to the start of the list */
5976 item = l->lv_first;
5977 idx = 0;
5978 }
5979 else if (n > (l->lv_idx + l->lv_len) / 2)
5980 {
5981 /* closest to the end of the list */
5982 item = l->lv_last;
5983 idx = l->lv_len - 1;
5984 }
5985 else
5986 {
5987 /* closest to the cached index */
5988 item = l->lv_idx_item;
5989 idx = l->lv_idx;
5990 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 }
5992 else
5993 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005994 if (n < l->lv_len / 2)
5995 {
5996 /* closest to the start of the list */
5997 item = l->lv_first;
5998 idx = 0;
5999 }
6000 else
6001 {
6002 /* closest to the end of the list */
6003 item = l->lv_last;
6004 idx = l->lv_len - 1;
6005 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006007
6008 while (n > idx)
6009 {
6010 /* search forward */
6011 item = item->li_next;
6012 ++idx;
6013 }
6014 while (n < idx)
6015 {
6016 /* search backward */
6017 item = item->li_prev;
6018 --idx;
6019 }
6020
6021 /* cache the used index */
6022 l->lv_idx = idx;
6023 l->lv_idx_item = item;
6024
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 return item;
6026}
6027
6028/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006029 * Get list item "l[idx]" as a number.
6030 */
6031 static long
6032list_find_nr(l, idx, errorp)
6033 list_T *l;
6034 long idx;
6035 int *errorp; /* set to TRUE when something wrong */
6036{
6037 listitem_T *li;
6038
6039 li = list_find(l, idx);
6040 if (li == NULL)
6041 {
6042 if (errorp != NULL)
6043 *errorp = TRUE;
6044 return -1L;
6045 }
6046 return get_tv_number_chk(&li->li_tv, errorp);
6047}
6048
6049/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006050 * Locate "item" list "l" and return its index.
6051 * Returns -1 when "item" is not in the list.
6052 */
6053 static long
6054list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 list_T *l;
6056 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006057{
6058 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006060
6061 if (l == NULL)
6062 return -1;
6063 idx = 0;
6064 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6065 ++idx;
6066 if (li == NULL)
6067 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006068 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006069}
6070
6071/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 * Append item "item" to the end of list "l".
6073 */
6074 static void
6075list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 list_T *l;
6077 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078{
6079 if (l->lv_last == NULL)
6080 {
6081 /* empty list */
6082 l->lv_first = item;
6083 l->lv_last = item;
6084 item->li_prev = NULL;
6085 }
6086 else
6087 {
6088 l->lv_last->li_next = item;
6089 item->li_prev = l->lv_last;
6090 l->lv_last = item;
6091 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006092 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 item->li_next = NULL;
6094}
6095
6096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099 */
6100 static int
6101list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 list_T *l;
6103 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006104{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006105 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106
Bram Moolenaar05159a02005-02-26 23:04:13 +00006107 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006109 copy_tv(tv, &li->li_tv);
6110 list_append(l, li);
6111 return OK;
6112}
6113
6114/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006115 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006116 * Return FAIL when out of memory.
6117 */
6118 int
6119list_append_dict(list, dict)
6120 list_T *list;
6121 dict_T *dict;
6122{
6123 listitem_T *li = listitem_alloc();
6124
6125 if (li == NULL)
6126 return FAIL;
6127 li->li_tv.v_type = VAR_DICT;
6128 li->li_tv.v_lock = 0;
6129 li->li_tv.vval.v_dict = dict;
6130 list_append(list, li);
6131 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006132 return OK;
6133}
6134
6135/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006136 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006137 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006138 * Returns FAIL when out of memory.
6139 */
6140 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006141list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006142 list_T *l;
6143 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006144 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006145{
6146 listitem_T *li = listitem_alloc();
6147
6148 if (li == NULL)
6149 return FAIL;
6150 list_append(l, li);
6151 li->li_tv.v_type = VAR_STRING;
6152 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006153 if (str == NULL)
6154 li->li_tv.vval.v_string = NULL;
6155 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006156 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006157 return FAIL;
6158 return OK;
6159}
6160
6161/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006162 * Append "n" to list "l".
6163 * Returns FAIL when out of memory.
6164 */
6165 static int
6166list_append_number(l, n)
6167 list_T *l;
6168 varnumber_T n;
6169{
6170 listitem_T *li;
6171
6172 li = listitem_alloc();
6173 if (li == NULL)
6174 return FAIL;
6175 li->li_tv.v_type = VAR_NUMBER;
6176 li->li_tv.v_lock = 0;
6177 li->li_tv.vval.v_number = n;
6178 list_append(l, li);
6179 return OK;
6180}
6181
6182/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006183 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * If "item" is NULL append at the end.
6185 * Return FAIL when out of memory.
6186 */
6187 static int
6188list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 list_T *l;
6190 typval_T *tv;
6191 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194
6195 if (ni == NULL)
6196 return FAIL;
6197 copy_tv(tv, &ni->li_tv);
6198 if (item == NULL)
6199 /* Append new item at end of list. */
6200 list_append(l, ni);
6201 else
6202 {
6203 /* Insert new item before existing item. */
6204 ni->li_prev = item->li_prev;
6205 ni->li_next = item;
6206 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006207 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006208 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006209 ++l->lv_idx;
6210 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006211 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006212 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006213 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006214 l->lv_idx_item = NULL;
6215 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006217 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006218 }
6219 return OK;
6220}
6221
6222/*
6223 * Extend "l1" with "l2".
6224 * If "bef" is NULL append at the end, otherwise insert before this item.
6225 * Returns FAIL when out of memory.
6226 */
6227 static int
6228list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 list_T *l1;
6230 list_T *l2;
6231 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006232{
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006234 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006235
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006236 /* We also quit the loop when we have inserted the original item count of
6237 * the list, avoid a hang when we extend a list with itself. */
6238 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6240 return FAIL;
6241 return OK;
6242}
6243
6244/*
6245 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6246 * Return FAIL when out of memory.
6247 */
6248 static int
6249list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 list_T *l1;
6251 list_T *l2;
6252 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253{
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006255
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006256 if (l1 == NULL || l2 == NULL)
6257 return FAIL;
6258
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006260 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 if (l == NULL)
6262 return FAIL;
6263 tv->v_type = VAR_LIST;
6264 tv->vval.v_list = l;
6265
6266 /* append all items from the second list */
6267 return list_extend(l, l2, NULL);
6268}
6269
6270/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006271 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006273 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274 * Returns NULL when out of memory.
6275 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006277list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006280 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281{
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 list_T *copy;
6283 listitem_T *item;
6284 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285
6286 if (orig == NULL)
6287 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288
6289 copy = list_alloc();
6290 if (copy != NULL)
6291 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006292 if (copyID != 0)
6293 {
6294 /* Do this before adding the items, because one of the items may
6295 * refer back to this list. */
6296 orig->lv_copyID = copyID;
6297 orig->lv_copylist = copy;
6298 }
6299 for (item = orig->lv_first; item != NULL && !got_int;
6300 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 {
6302 ni = listitem_alloc();
6303 if (ni == NULL)
6304 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006306 {
6307 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6308 {
6309 vim_free(ni);
6310 break;
6311 }
6312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006314 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 list_append(copy, ni);
6316 }
6317 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006318 if (item != NULL)
6319 {
6320 list_unref(copy);
6321 copy = NULL;
6322 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 }
6324
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325 return copy;
6326}
6327
6328/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006329 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006330 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006333list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 list_T *l;
6335 listitem_T *item;
6336 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337{
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006340 /* notify watchers */
6341 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006343 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006344 list_fix_watch(l, ip);
6345 if (ip == item2)
6346 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006348
6349 if (item2->li_next == NULL)
6350 l->lv_last = item->li_prev;
6351 else
6352 item2->li_next->li_prev = item->li_prev;
6353 if (item->li_prev == NULL)
6354 l->lv_first = item2->li_next;
6355 else
6356 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006357 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358}
6359
6360/*
6361 * Return an allocated string with the string representation of a list.
6362 * May return NULL.
6363 */
6364 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006365list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006367 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368{
6369 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370
6371 if (tv->vval.v_list == NULL)
6372 return NULL;
6373 ga_init2(&ga, (int)sizeof(char), 80);
6374 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006375 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 {
6377 vim_free(ga.ga_data);
6378 return NULL;
6379 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380 ga_append(&ga, ']');
6381 ga_append(&ga, NUL);
6382 return (char_u *)ga.ga_data;
6383}
6384
6385/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006386 * Join list "l" into a string in "*gap", using separator "sep".
6387 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006388 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006389 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006390 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006391list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006392 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006394 char_u *sep;
6395 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006396 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006397{
6398 int first = TRUE;
6399 char_u *tofree;
6400 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006402 char_u *s;
6403
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006405 {
6406 if (first)
6407 first = FALSE;
6408 else
6409 ga_concat(gap, sep);
6410
6411 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006412 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006413 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006414 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006415 if (s != NULL)
6416 ga_concat(gap, s);
6417 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006418 if (s == NULL)
6419 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006420 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006421 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006422}
6423
6424/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006425 * Garbage collection for lists and dictionaries.
6426 *
6427 * We use reference counts to be able to free most items right away when they
6428 * are no longer used. But for composite items it's possible that it becomes
6429 * unused while the reference count is > 0: When there is a recursive
6430 * reference. Example:
6431 * :let l = [1, 2, 3]
6432 * :let d = {9: l}
6433 * :let l[1] = d
6434 *
6435 * Since this is quite unusual we handle this with garbage collection: every
6436 * once in a while find out which lists and dicts are not referenced from any
6437 * variable.
6438 *
6439 * Here is a good reference text about garbage collection (refers to Python
6440 * but it applies to all reference-counting mechanisms):
6441 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006442 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006443
6444/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006445 * Do garbage collection for lists and dicts.
6446 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006447 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006448 int
6449garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006450{
6451 dict_T *dd;
6452 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006453 int copyID = ++current_copyID;
6454 buf_T *buf;
6455 win_T *wp;
6456 int i;
6457 funccall_T *fc;
6458 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006459#ifdef FEAT_WINDOWS
6460 tabpage_T *tp;
6461#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006462
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006463 /* Only do this once. */
6464 want_garbage_collect = FALSE;
6465 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006466 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006467
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006468 /*
6469 * 1. Go through all accessible variables and mark all lists and dicts
6470 * with copyID.
6471 */
6472 /* script-local variables */
6473 for (i = 1; i <= ga_scripts.ga_len; ++i)
6474 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6475
6476 /* buffer-local variables */
6477 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6478 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6479
6480 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006481 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006482 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6483
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006484#ifdef FEAT_WINDOWS
6485 /* tabpage-local variables */
6486 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6487 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6488#endif
6489
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006490 /* global variables */
6491 set_ref_in_ht(&globvarht, copyID);
6492
6493 /* function-local variables */
6494 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6495 {
6496 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6497 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6498 }
6499
6500 /*
6501 * 2. Go through the list of dicts and free items without the copyID.
6502 */
6503 for (dd = first_dict; dd != NULL; )
6504 if (dd->dv_copyID != copyID)
6505 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006506 /* Free the Dictionary and ordinary items it contains, but don't
6507 * recurse into Lists and Dictionaries, they will be in the list
6508 * of dicts or list of lists. */
6509 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006510 did_free = TRUE;
6511
6512 /* restart, next dict may also have been freed */
6513 dd = first_dict;
6514 }
6515 else
6516 dd = dd->dv_used_next;
6517
6518 /*
6519 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006520 * But don't free a list that has a watcher (used in a for loop), these
6521 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006522 */
6523 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006524 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006525 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006526 /* Free the List and ordinary items it contains, but don't recurse
6527 * into Lists and Dictionaries, they will be in the list of dicts
6528 * or list of lists. */
6529 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006530 did_free = TRUE;
6531
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006532 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006533 ll = first_list;
6534 }
6535 else
6536 ll = ll->lv_used_next;
6537
6538 return did_free;
6539}
6540
6541/*
6542 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6543 */
6544 static void
6545set_ref_in_ht(ht, copyID)
6546 hashtab_T *ht;
6547 int copyID;
6548{
6549 int todo;
6550 hashitem_T *hi;
6551
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006552 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006553 for (hi = ht->ht_array; todo > 0; ++hi)
6554 if (!HASHITEM_EMPTY(hi))
6555 {
6556 --todo;
6557 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6558 }
6559}
6560
6561/*
6562 * Mark all lists and dicts referenced through list "l" with "copyID".
6563 */
6564 static void
6565set_ref_in_list(l, copyID)
6566 list_T *l;
6567 int copyID;
6568{
6569 listitem_T *li;
6570
6571 for (li = l->lv_first; li != NULL; li = li->li_next)
6572 set_ref_in_item(&li->li_tv, copyID);
6573}
6574
6575/*
6576 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6577 */
6578 static void
6579set_ref_in_item(tv, copyID)
6580 typval_T *tv;
6581 int copyID;
6582{
6583 dict_T *dd;
6584 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006585
6586 switch (tv->v_type)
6587 {
6588 case VAR_DICT:
6589 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006590 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006591 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006592 /* Didn't see this dict yet. */
6593 dd->dv_copyID = copyID;
6594 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006595 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006596 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006597
6598 case VAR_LIST:
6599 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006600 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006601 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006602 /* Didn't see this list yet. */
6603 ll->lv_copyID = copyID;
6604 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006605 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006606 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006607 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006608 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006609}
6610
6611/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006612 * Allocate an empty header for a dictionary.
6613 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006614 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006615dict_alloc()
6616{
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006618
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006620 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006621 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006622 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006623 if (first_dict != NULL)
6624 first_dict->dv_used_prev = d;
6625 d->dv_used_next = first_dict;
6626 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006627 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006628
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006630 d->dv_lock = 0;
6631 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006632 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006633 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006634 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006635}
6636
6637/*
6638 * Unreference a Dictionary: decrement the reference count and free it when it
6639 * becomes zero.
6640 */
6641 static void
6642dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006643 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006645 if (d != NULL && --d->dv_refcount <= 0)
6646 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647}
6648
6649/*
6650 * Free a Dictionary, including all items it contains.
6651 * Ignores the reference count.
6652 */
6653 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006654dict_free(d, recurse)
6655 dict_T *d;
6656 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006657{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006658 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006660 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006662 /* Remove the dict from the list of dicts for garbage collection. */
6663 if (d->dv_used_prev == NULL)
6664 first_dict = d->dv_used_next;
6665 else
6666 d->dv_used_prev->dv_used_next = d->dv_used_next;
6667 if (d->dv_used_next != NULL)
6668 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6669
6670 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006671 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006672 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006675 if (!HASHITEM_EMPTY(hi))
6676 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006677 /* Remove the item before deleting it, just in case there is
6678 * something recursive causing trouble. */
6679 di = HI2DI(hi);
6680 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006681 if (recurse || (di->di_tv.v_type != VAR_LIST
6682 && di->di_tv.v_type != VAR_DICT))
6683 clear_tv(&di->di_tv);
6684 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006685 --todo;
6686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006689 vim_free(d);
6690}
6691
6692/*
6693 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006694 * The "key" is copied to the new item.
6695 * Note that the value of the item "di_tv" still needs to be initialized!
6696 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006697 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006698 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006699dictitem_alloc(key)
6700 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006701{
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006704 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006705 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006706 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006707 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006708 di->di_flags = 0;
6709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006710 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711}
6712
6713/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006714 * Make a copy of a Dictionary item.
6715 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006716 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006717dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006718 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719{
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006721
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006722 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6723 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006724 if (di != NULL)
6725 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006726 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006727 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006728 copy_tv(&org->di_tv, &di->di_tv);
6729 }
6730 return di;
6731}
6732
6733/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006734 * Remove item "item" from Dictionary "dict" and free it.
6735 */
6736 static void
6737dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006738 dict_T *dict;
6739 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006740{
Bram Moolenaar33570922005-01-25 22:26:29 +00006741 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006742
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006744 if (HASHITEM_EMPTY(hi))
6745 EMSG2(_(e_intern2), "dictitem_remove()");
6746 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006747 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006748 dictitem_free(item);
6749}
6750
6751/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752 * Free a dict item. Also clears the value.
6753 */
6754 static void
6755dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006757{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758 clear_tv(&item->di_tv);
6759 vim_free(item);
6760}
6761
6762/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006763 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6764 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006765 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006766 * Returns NULL when out of memory.
6767 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006768 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006770 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006772 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006773{
Bram Moolenaar33570922005-01-25 22:26:29 +00006774 dict_T *copy;
6775 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006776 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006777 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006778
6779 if (orig == NULL)
6780 return NULL;
6781
6782 copy = dict_alloc();
6783 if (copy != NULL)
6784 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006785 if (copyID != 0)
6786 {
6787 orig->dv_copyID = copyID;
6788 orig->dv_copydict = copy;
6789 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006790 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006791 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006792 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006793 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006794 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006795 --todo;
6796
6797 di = dictitem_alloc(hi->hi_key);
6798 if (di == NULL)
6799 break;
6800 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006801 {
6802 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6803 copyID) == FAIL)
6804 {
6805 vim_free(di);
6806 break;
6807 }
6808 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006809 else
6810 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6811 if (dict_add(copy, di) == FAIL)
6812 {
6813 dictitem_free(di);
6814 break;
6815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006817 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006818
Bram Moolenaare9a41262005-01-15 22:18:47 +00006819 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006820 if (todo > 0)
6821 {
6822 dict_unref(copy);
6823 copy = NULL;
6824 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825 }
6826
6827 return copy;
6828}
6829
6830/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006831 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006832 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006833 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006834 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006835dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006836 dict_T *d;
6837 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838{
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006840}
6841
Bram Moolenaar8c711452005-01-14 21:53:12 +00006842/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006843 * Add a number or string entry to dictionary "d".
6844 * When "str" is NULL use number "nr", otherwise use "str".
6845 * Returns FAIL when out of memory and when key already exists.
6846 */
6847 int
6848dict_add_nr_str(d, key, nr, str)
6849 dict_T *d;
6850 char *key;
6851 long nr;
6852 char_u *str;
6853{
6854 dictitem_T *item;
6855
6856 item = dictitem_alloc((char_u *)key);
6857 if (item == NULL)
6858 return FAIL;
6859 item->di_tv.v_lock = 0;
6860 if (str == NULL)
6861 {
6862 item->di_tv.v_type = VAR_NUMBER;
6863 item->di_tv.vval.v_number = nr;
6864 }
6865 else
6866 {
6867 item->di_tv.v_type = VAR_STRING;
6868 item->di_tv.vval.v_string = vim_strsave(str);
6869 }
6870 if (dict_add(d, item) == FAIL)
6871 {
6872 dictitem_free(item);
6873 return FAIL;
6874 }
6875 return OK;
6876}
6877
6878/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 * Get the number of items in a Dictionary.
6880 */
6881 static long
6882dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006883 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006884{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006885 if (d == NULL)
6886 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006887 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006888}
6889
6890/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891 * Find item "key[len]" in Dictionary "d".
6892 * If "len" is negative use strlen(key).
6893 * Returns NULL when not found.
6894 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006896dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006898 char_u *key;
6899 int len;
6900{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006901#define AKEYLEN 200
6902 char_u buf[AKEYLEN];
6903 char_u *akey;
6904 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006907 if (len < 0)
6908 akey = key;
6909 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006910 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006911 tofree = akey = vim_strnsave(key, len);
6912 if (akey == NULL)
6913 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006914 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006915 else
6916 {
6917 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006918 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006919 akey = buf;
6920 }
6921
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006923 vim_free(tofree);
6924 if (HASHITEM_EMPTY(hi))
6925 return NULL;
6926 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006927}
6928
6929/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006930 * Get a string item from a dictionary.
6931 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006932 * Returns NULL if the entry doesn't exist or out of memory.
6933 */
6934 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006935get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006936 dict_T *d;
6937 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006938 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006939{
6940 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006941 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006942
6943 di = dict_find(d, key, -1);
6944 if (di == NULL)
6945 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006946 s = get_tv_string(&di->di_tv);
6947 if (save && s != NULL)
6948 s = vim_strsave(s);
6949 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006950}
6951
6952/*
6953 * Get a number item from a dictionary.
6954 * Returns 0 if the entry doesn't exist or out of memory.
6955 */
6956 long
6957get_dict_number(d, key)
6958 dict_T *d;
6959 char_u *key;
6960{
6961 dictitem_T *di;
6962
6963 di = dict_find(d, key, -1);
6964 if (di == NULL)
6965 return 0;
6966 return get_tv_number(&di->di_tv);
6967}
6968
6969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 * Return an allocated string with the string representation of a Dictionary.
6971 * May return NULL.
6972 */
6973 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006974dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006976 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977{
6978 garray_T ga;
6979 int first = TRUE;
6980 char_u *tofree;
6981 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006985 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006986
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006987 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006988 return NULL;
6989 ga_init2(&ga, (int)sizeof(char), 80);
6990 ga_append(&ga, '{');
6991
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006992 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006993 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006994 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006995 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006996 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997 --todo;
6998
6999 if (first)
7000 first = FALSE;
7001 else
7002 ga_concat(&ga, (char_u *)", ");
7003
7004 tofree = string_quote(hi->hi_key, FALSE);
7005 if (tofree != NULL)
7006 {
7007 ga_concat(&ga, tofree);
7008 vim_free(tofree);
7009 }
7010 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007011 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012 if (s != NULL)
7013 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007015 if (s == NULL)
7016 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007018 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 if (todo > 0)
7020 {
7021 vim_free(ga.ga_data);
7022 return NULL;
7023 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024
7025 ga_append(&ga, '}');
7026 ga_append(&ga, NUL);
7027 return (char_u *)ga.ga_data;
7028}
7029
7030/*
7031 * Allocate a variable for a Dictionary and fill it from "*arg".
7032 * Return OK or FAIL. Returns NOTDONE for {expr}.
7033 */
7034 static int
7035get_dict_tv(arg, rettv, evaluate)
7036 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038 int evaluate;
7039{
Bram Moolenaar33570922005-01-25 22:26:29 +00007040 dict_T *d = NULL;
7041 typval_T tvkey;
7042 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007043 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007044 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007046 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047
7048 /*
7049 * First check if it's not a curly-braces thing: {expr}.
7050 * Must do this without evaluating, otherwise a function may be called
7051 * twice. Unfortunately this means we need to call eval1() twice for the
7052 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007053 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007055 if (*start != '}')
7056 {
7057 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7058 return FAIL;
7059 if (*start == '}')
7060 return NOTDONE;
7061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062
7063 if (evaluate)
7064 {
7065 d = dict_alloc();
7066 if (d == NULL)
7067 return FAIL;
7068 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 tvkey.v_type = VAR_UNKNOWN;
7070 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007071
7072 *arg = skipwhite(*arg + 1);
7073 while (**arg != '}' && **arg != NUL)
7074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 goto failret;
7077 if (**arg != ':')
7078 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007079 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 goto failret;
7082 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007083 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007085 key = get_tv_string_buf_chk(&tvkey, buf);
7086 if (key == NULL || *key == NUL)
7087 {
7088 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7089 if (key != NULL)
7090 EMSG(_(e_emptykey));
7091 clear_tv(&tvkey);
7092 goto failret;
7093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095
7096 *arg = skipwhite(*arg + 1);
7097 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7098 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007099 if (evaluate)
7100 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101 goto failret;
7102 }
7103 if (evaluate)
7104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 if (item != NULL)
7107 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007108 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110 clear_tv(&tv);
7111 goto failret;
7112 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 item = dictitem_alloc(key);
7114 clear_tv(&tvkey);
7115 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007118 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 if (dict_add(d, item) == FAIL)
7120 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007121 }
7122 }
7123
7124 if (**arg == '}')
7125 break;
7126 if (**arg != ',')
7127 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007128 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007129 goto failret;
7130 }
7131 *arg = skipwhite(*arg + 1);
7132 }
7133
7134 if (**arg != '}')
7135 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007136 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137failret:
7138 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007139 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007140 return FAIL;
7141 }
7142
7143 *arg = skipwhite(*arg + 1);
7144 if (evaluate)
7145 {
7146 rettv->v_type = VAR_DICT;
7147 rettv->vval.v_dict = d;
7148 ++d->dv_refcount;
7149 }
7150
7151 return OK;
7152}
7153
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007155 * Return a string with the string representation of a variable.
7156 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007157 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007158 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007159 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007160 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007161 */
7162 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007163echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007165 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007166 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007167 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007168{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 static int recurse = 0;
7170 char_u *r = NULL;
7171
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007174 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175 *tofree = NULL;
7176 return NULL;
7177 }
7178 ++recurse;
7179
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007180 switch (tv->v_type)
7181 {
7182 case VAR_FUNC:
7183 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 r = tv->vval.v_string;
7185 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007186
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007187 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007188 if (tv->vval.v_list == NULL)
7189 {
7190 *tofree = NULL;
7191 r = NULL;
7192 }
7193 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7194 {
7195 *tofree = NULL;
7196 r = (char_u *)"[...]";
7197 }
7198 else
7199 {
7200 tv->vval.v_list->lv_copyID = copyID;
7201 *tofree = list2string(tv, copyID);
7202 r = *tofree;
7203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007205
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007207 if (tv->vval.v_dict == NULL)
7208 {
7209 *tofree = NULL;
7210 r = NULL;
7211 }
7212 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7213 {
7214 *tofree = NULL;
7215 r = (char_u *)"{...}";
7216 }
7217 else
7218 {
7219 tv->vval.v_dict->dv_copyID = copyID;
7220 *tofree = dict2string(tv, copyID);
7221 r = *tofree;
7222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225 case VAR_STRING:
7226 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 *tofree = NULL;
7228 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007229 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007230
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007231#ifdef FEAT_FLOAT
7232 case VAR_FLOAT:
7233 *tofree = NULL;
7234 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7235 r = numbuf;
7236 break;
7237#endif
7238
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007239 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007240 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007241 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007242 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007243
7244 --recurse;
7245 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007246}
7247
7248/*
7249 * Return a string with the string representation of a variable.
7250 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7251 * "numbuf" is used for a number.
7252 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007253 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007254 */
7255 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007256tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007258 char_u **tofree;
7259 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007260 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007261{
7262 switch (tv->v_type)
7263 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007264 case VAR_FUNC:
7265 *tofree = string_quote(tv->vval.v_string, TRUE);
7266 return *tofree;
7267 case VAR_STRING:
7268 *tofree = string_quote(tv->vval.v_string, FALSE);
7269 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007270#ifdef FEAT_FLOAT
7271 case VAR_FLOAT:
7272 *tofree = NULL;
7273 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7274 return numbuf;
7275#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007277 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007280 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007281 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007282 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007283 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284}
7285
7286/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 * Return string "str" in ' quotes, doubling ' characters.
7288 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007290 */
7291 static char_u *
7292string_quote(str, function)
7293 char_u *str;
7294 int function;
7295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007297 char_u *p, *r, *s;
7298
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 len = (function ? 13 : 3);
7300 if (str != NULL)
7301 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007302 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 for (p = str; *p != NUL; mb_ptr_adv(p))
7304 if (*p == '\'')
7305 ++len;
7306 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007307 s = r = alloc(len);
7308 if (r != NULL)
7309 {
7310 if (function)
7311 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007313 r += 10;
7314 }
7315 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 if (str != NULL)
7318 for (p = str; *p != NUL; )
7319 {
7320 if (*p == '\'')
7321 *r++ = '\'';
7322 MB_COPY_CHAR(p, r);
7323 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007325 if (function)
7326 *r++ = ')';
7327 *r++ = NUL;
7328 }
7329 return s;
7330}
7331
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007332#ifdef FEAT_FLOAT
7333/*
7334 * Convert the string "text" to a floating point number.
7335 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7336 * this always uses a decimal point.
7337 * Returns the length of the text that was consumed.
7338 */
7339 static int
7340string2float(text, value)
7341 char_u *text;
7342 float_T *value; /* result stored here */
7343{
7344 char *s = (char *)text;
7345 float_T f;
7346
7347 f = strtod(s, &s);
7348 *value = f;
7349 return (int)((char_u *)s - text);
7350}
7351#endif
7352
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 * Get the value of an environment variable.
7355 * "arg" is pointing to the '$'. It is advanced to after the name.
7356 * If the environment variable was not set, silently assume it is empty.
7357 * Always return OK.
7358 */
7359 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007360get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 int evaluate;
7364{
7365 char_u *string = NULL;
7366 int len;
7367 int cc;
7368 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007369 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
7371 ++*arg;
7372 name = *arg;
7373 len = get_env_len(arg);
7374 if (evaluate)
7375 {
7376 if (len != 0)
7377 {
7378 cc = name[len];
7379 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007380 /* first try vim_getenv(), fast for normal environment vars */
7381 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007383 {
7384 if (!mustfree)
7385 string = vim_strsave(string);
7386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 else
7388 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007389 if (mustfree)
7390 vim_free(string);
7391
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 /* next try expanding things like $VIM and ${HOME} */
7393 string = expand_env_save(name - 1);
7394 if (string != NULL && *string == '$')
7395 {
7396 vim_free(string);
7397 string = NULL;
7398 }
7399 }
7400 name[len] = cc;
7401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007402 rettv->v_type = VAR_STRING;
7403 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
7405
7406 return OK;
7407}
7408
7409/*
7410 * Array with names and number of arguments of all internal functions
7411 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7412 */
7413static struct fst
7414{
7415 char *f_name; /* function name */
7416 char f_min_argc; /* minimal number of arguments */
7417 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007418 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007419 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420} functions[] =
7421{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007422#ifdef FEAT_FLOAT
7423 {"abs", 1, 1, f_abs},
7424#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007425 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {"append", 2, 2, f_append},
7427 {"argc", 0, 0, f_argc},
7428 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007429 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007430#ifdef FEAT_FLOAT
7431 {"atan", 1, 1, f_atan},
7432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007434 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {"bufexists", 1, 1, f_bufexists},
7436 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7437 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7438 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7439 {"buflisted", 1, 1, f_buflisted},
7440 {"bufloaded", 1, 1, f_bufloaded},
7441 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007442 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {"bufwinnr", 1, 1, f_bufwinnr},
7444 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007445 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007446 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007447#ifdef FEAT_FLOAT
7448 {"ceil", 1, 1, f_ceil},
7449#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007450 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {"char2nr", 1, 1, f_char2nr},
7452 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007453 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007455#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007456 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007457 {"complete_add", 1, 1, f_complete_add},
7458 {"complete_check", 0, 0, f_complete_check},
7459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007462#ifdef FEAT_FLOAT
7463 {"cos", 1, 1, f_cos},
7464#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007465 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007467 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007468 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 {"delete", 1, 1, f_delete},
7470 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007471 {"diff_filler", 1, 1, f_diff_filler},
7472 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007473 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007475 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 {"eventhandler", 0, 0, f_eventhandler},
7477 {"executable", 1, 1, f_executable},
7478 {"exists", 1, 1, f_exists},
7479 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007480 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007481 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7483 {"filereadable", 1, 1, f_filereadable},
7484 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007485 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007486 {"finddir", 1, 3, f_finddir},
7487 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007488#ifdef FEAT_FLOAT
7489 {"float2nr", 1, 1, f_float2nr},
7490 {"floor", 1, 1, f_floor},
7491#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007492 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 {"fnamemodify", 2, 2, f_fnamemodify},
7494 {"foldclosed", 1, 1, f_foldclosed},
7495 {"foldclosedend", 1, 1, f_foldclosedend},
7496 {"foldlevel", 1, 1, f_foldlevel},
7497 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007498 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007500 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007501 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007502 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007503 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 {"getbufvar", 2, 2, f_getbufvar},
7505 {"getchar", 0, 1, f_getchar},
7506 {"getcharmod", 0, 0, f_getcharmod},
7507 {"getcmdline", 0, 0, f_getcmdline},
7508 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007509 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007511 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007512 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {"getfsize", 1, 1, f_getfsize},
7514 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007515 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007516 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007517 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007518 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007519 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007520 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007521 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007522 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007524 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {"getwinposx", 0, 0, f_getwinposx},
7526 {"getwinposy", 0, 0, f_getwinposy},
7527 {"getwinvar", 2, 2, f_getwinvar},
7528 {"glob", 1, 1, f_glob},
7529 {"globpath", 2, 2, f_globpath},
7530 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007532 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007533 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7535 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7536 {"histadd", 2, 2, f_histadd},
7537 {"histdel", 1, 2, f_histdel},
7538 {"histget", 1, 2, f_histget},
7539 {"histnr", 1, 1, f_histnr},
7540 {"hlID", 1, 1, f_hlID},
7541 {"hlexists", 1, 1, f_hlexists},
7542 {"hostname", 0, 0, f_hostname},
7543 {"iconv", 3, 3, f_iconv},
7544 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007545 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007546 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007548 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 {"inputrestore", 0, 0, f_inputrestore},
7550 {"inputsave", 0, 0, f_inputsave},
7551 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007552 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007554 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007556 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007559 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {"libcall", 3, 3, f_libcall},
7561 {"libcallnr", 3, 3, f_libcallnr},
7562 {"line", 1, 1, f_line},
7563 {"line2byte", 1, 1, f_line2byte},
7564 {"lispindent", 1, 1, f_lispindent},
7565 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007566#ifdef FEAT_FLOAT
7567 {"log10", 1, 1, f_log10},
7568#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007569 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007570 {"maparg", 1, 3, f_maparg},
7571 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007572 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007573 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007574 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007575 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007576 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007577 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007578 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007579 {"max", 1, 1, f_max},
7580 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007581#ifdef vim_mkdir
7582 {"mkdir", 1, 3, f_mkdir},
7583#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007584 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {"nextnonblank", 1, 1, f_nextnonblank},
7586 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007587 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007588#ifdef FEAT_FLOAT
7589 {"pow", 2, 2, f_pow},
7590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007592 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007593 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007595 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007596 {"reltime", 0, 2, f_reltime},
7597 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {"remote_expr", 2, 3, f_remote_expr},
7599 {"remote_foreground", 1, 1, f_remote_foreground},
7600 {"remote_peek", 1, 2, f_remote_peek},
7601 {"remote_read", 1, 1, f_remote_read},
7602 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007603 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007605 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007607 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007608#ifdef FEAT_FLOAT
7609 {"round", 1, 1, f_round},
7610#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007611 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007612 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007613 {"searchpair", 3, 7, f_searchpair},
7614 {"searchpairpos", 3, 7, f_searchpairpos},
7615 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {"server2client", 2, 2, f_server2client},
7617 {"serverlist", 0, 0, f_serverlist},
7618 {"setbufvar", 3, 3, f_setbufvar},
7619 {"setcmdpos", 1, 1, f_setcmdpos},
7620 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007621 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007622 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007623 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007624 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007626 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007628 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007630#ifdef FEAT_FLOAT
7631 {"sin", 1, 1, f_sin},
7632#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007633 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007634 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007635 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007636 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007637 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007638#ifdef FEAT_FLOAT
7639 {"sqrt", 1, 1, f_sqrt},
7640 {"str2float", 1, 1, f_str2float},
7641#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007642 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643#ifdef HAVE_STRFTIME
7644 {"strftime", 1, 2, f_strftime},
7645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007647 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {"strlen", 1, 1, f_strlen},
7649 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007650 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {"strtrans", 1, 1, f_strtrans},
7652 {"submatch", 1, 1, f_submatch},
7653 {"substitute", 4, 4, f_substitute},
7654 {"synID", 3, 3, f_synID},
7655 {"synIDattr", 2, 3, f_synIDattr},
7656 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007657 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007658 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007659 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007660 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007661 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007662 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007663 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007665 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {"tolower", 1, 1, f_tolower},
7667 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007668 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007669#ifdef FEAT_FLOAT
7670 {"trunc", 1, 1, f_trunc},
7671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {"virtcol", 1, 1, f_virtcol},
7675 {"visualmode", 0, 1, f_visualmode},
7676 {"winbufnr", 1, 1, f_winbufnr},
7677 {"wincol", 0, 0, f_wincol},
7678 {"winheight", 1, 1, f_winheight},
7679 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007680 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007682 {"winrestview", 1, 1, f_winrestview},
7683 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007685 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686};
7687
7688#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7689
7690/*
7691 * Function given to ExpandGeneric() to obtain the list of internal
7692 * or user defined function names.
7693 */
7694 char_u *
7695get_function_name(xp, idx)
7696 expand_T *xp;
7697 int idx;
7698{
7699 static int intidx = -1;
7700 char_u *name;
7701
7702 if (idx == 0)
7703 intidx = -1;
7704 if (intidx < 0)
7705 {
7706 name = get_user_func_name(xp, idx);
7707 if (name != NULL)
7708 return name;
7709 }
7710 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7711 {
7712 STRCPY(IObuff, functions[intidx].f_name);
7713 STRCAT(IObuff, "(");
7714 if (functions[intidx].f_max_argc == 0)
7715 STRCAT(IObuff, ")");
7716 return IObuff;
7717 }
7718
7719 return NULL;
7720}
7721
7722/*
7723 * Function given to ExpandGeneric() to obtain the list of internal or
7724 * user defined variable or function names.
7725 */
7726/*ARGSUSED*/
7727 char_u *
7728get_expr_name(xp, idx)
7729 expand_T *xp;
7730 int idx;
7731{
7732 static int intidx = -1;
7733 char_u *name;
7734
7735 if (idx == 0)
7736 intidx = -1;
7737 if (intidx < 0)
7738 {
7739 name = get_function_name(xp, idx);
7740 if (name != NULL)
7741 return name;
7742 }
7743 return get_user_var_name(xp, ++intidx);
7744}
7745
7746#endif /* FEAT_CMDL_COMPL */
7747
7748/*
7749 * Find internal function in table above.
7750 * Return index, or -1 if not found
7751 */
7752 static int
7753find_internal_func(name)
7754 char_u *name; /* name of the function */
7755{
7756 int first = 0;
7757 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7758 int cmp;
7759 int x;
7760
7761 /*
7762 * Find the function name in the table. Binary search.
7763 */
7764 while (first <= last)
7765 {
7766 x = first + ((unsigned)(last - first) >> 1);
7767 cmp = STRCMP(name, functions[x].f_name);
7768 if (cmp < 0)
7769 last = x - 1;
7770 else if (cmp > 0)
7771 first = x + 1;
7772 else
7773 return x;
7774 }
7775 return -1;
7776}
7777
7778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7780 * name it contains, otherwise return "name".
7781 */
7782 static char_u *
7783deref_func_name(name, lenp)
7784 char_u *name;
7785 int *lenp;
7786{
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 int cc;
7789
7790 cc = name[*lenp];
7791 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007792 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797 {
7798 *lenp = 0;
7799 return (char_u *)""; /* just in case */
7800 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007801 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 }
7804
7805 return name;
7806}
7807
7808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 * Allocate a variable for the result of a function.
7810 * Return OK or FAIL.
7811 */
7812 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7814 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 char_u *name; /* name of the function */
7816 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 char_u **arg; /* argument, pointing to the '(' */
7819 linenr_T firstline; /* first line of range */
7820 linenr_T lastline; /* last line of range */
7821 int *doesrange; /* return: function handled range */
7822 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
7825 char_u *argp;
7826 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007827 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 int argcount = 0; /* number of arguments found */
7829
7830 /*
7831 * Get the arguments.
7832 */
7833 argp = *arg;
7834 while (argcount < MAX_FUNC_ARGS)
7835 {
7836 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7837 if (*argp == ')' || *argp == ',' || *argp == NUL)
7838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7840 {
7841 ret = FAIL;
7842 break;
7843 }
7844 ++argcount;
7845 if (*argp != ',')
7846 break;
7847 }
7848 if (*argp == ')')
7849 ++argp;
7850 else
7851 ret = FAIL;
7852
7853 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007854 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 {
7858 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007859 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007861 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
7864 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866
7867 *arg = skipwhite(argp);
7868 return ret;
7869}
7870
7871
7872/*
7873 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007874 * Return OK when the function can't be called, FAIL otherwise.
7875 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 */
7877 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 char_u *name; /* name of the function */
7881 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007884 typval_T *argvars; /* vars for arguments, must have "argcount"
7885 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 linenr_T firstline; /* first line of range */
7887 linenr_T lastline; /* last line of range */
7888 int *doesrange; /* return: function handled range */
7889 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891{
7892 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893#define ERROR_UNKNOWN 0
7894#define ERROR_TOOMANY 1
7895#define ERROR_TOOFEW 2
7896#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007897#define ERROR_DICT 4
7898#define ERROR_NONE 5
7899#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 int error = ERROR_NONE;
7901 int i;
7902 int llen;
7903 ufunc_T *fp;
7904 int cc;
7905#define FLEN_FIXED 40
7906 char_u fname_buf[FLEN_FIXED + 1];
7907 char_u *fname;
7908
7909 /*
7910 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7911 * Change <SNR>123_name() to K_SNR 123_name().
7912 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7913 */
7914 cc = name[len];
7915 name[len] = NUL;
7916 llen = eval_fname_script(name);
7917 if (llen > 0)
7918 {
7919 fname_buf[0] = K_SPECIAL;
7920 fname_buf[1] = KS_EXTRA;
7921 fname_buf[2] = (int)KE_SNR;
7922 i = 3;
7923 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7924 {
7925 if (current_SID <= 0)
7926 error = ERROR_SCRIPT;
7927 else
7928 {
7929 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7930 i = (int)STRLEN(fname_buf);
7931 }
7932 }
7933 if (i + STRLEN(name + llen) < FLEN_FIXED)
7934 {
7935 STRCPY(fname_buf + i, name + llen);
7936 fname = fname_buf;
7937 }
7938 else
7939 {
7940 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7941 if (fname == NULL)
7942 error = ERROR_OTHER;
7943 else
7944 {
7945 mch_memmove(fname, fname_buf, (size_t)i);
7946 STRCPY(fname + i, name + llen);
7947 }
7948 }
7949 }
7950 else
7951 fname = name;
7952
7953 *doesrange = FALSE;
7954
7955
7956 /* execute the function if no errors detected and executing */
7957 if (evaluate && error == ERROR_NONE)
7958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 error = ERROR_UNKNOWN;
7961
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007962 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {
7964 /*
7965 * User defined function.
7966 */
7967 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007968
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007970 /* Trigger FuncUndefined event, may load the function. */
7971 if (fp == NULL
7972 && apply_autocmds(EVENT_FUNCUNDEFINED,
7973 fname, fname, TRUE, NULL)
7974 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007976 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 fp = find_func(fname);
7978 }
7979#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007980 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007981 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007982 {
7983 /* loaded a package, search for the function again */
7984 fp = find_func(fname);
7985 }
7986
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (fp != NULL)
7988 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007989 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007991 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007993 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007995 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007996 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 else
7998 {
7999 /*
8000 * Call the user function.
8001 * Save and restore search patterns, script variables and
8002 * redo buffer.
8003 */
8004 save_search_patterns();
8005 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008006 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008007 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008008 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008009 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8010 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8011 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008012 /* Function was unreferenced while being used, free it
8013 * now. */
8014 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 restoreRedobuff();
8016 restore_search_patterns();
8017 error = ERROR_NONE;
8018 }
8019 }
8020 }
8021 else
8022 {
8023 /*
8024 * Find the function name in the table, call its implementation.
8025 */
8026 i = find_internal_func(fname);
8027 if (i >= 0)
8028 {
8029 if (argcount < functions[i].f_min_argc)
8030 error = ERROR_TOOFEW;
8031 else if (argcount > functions[i].f_max_argc)
8032 error = ERROR_TOOMANY;
8033 else
8034 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008035 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 error = ERROR_NONE;
8038 }
8039 }
8040 }
8041 /*
8042 * The function call (or "FuncUndefined" autocommand sequence) might
8043 * have been aborted by an error, an interrupt, or an explicitly thrown
8044 * exception that has not been caught so far. This situation can be
8045 * tested for by calling aborting(). For an error in an internal
8046 * function or for the "E132" error in call_user_func(), however, the
8047 * throw point at which the "force_abort" flag (temporarily reset by
8048 * emsg()) is normally updated has not been reached yet. We need to
8049 * update that flag first to make aborting() reliable.
8050 */
8051 update_force_abort();
8052 }
8053 if (error == ERROR_NONE)
8054 ret = OK;
8055
8056 /*
8057 * Report an error unless the argument evaluation or function call has been
8058 * cancelled due to an aborting error, an interrupt, or an exception.
8059 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008060 if (!aborting())
8061 {
8062 switch (error)
8063 {
8064 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008065 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008066 break;
8067 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008068 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 break;
8070 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008071 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008072 name);
8073 break;
8074 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008075 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 name);
8077 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008079 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008080 name);
8081 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008082 }
8083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
8085 name[len] = cc;
8086 if (fname != name && fname != fname_buf)
8087 vim_free(fname);
8088
8089 return ret;
8090}
8091
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008092/*
8093 * Give an error message with a function name. Handle <SNR> things.
8094 */
8095 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008096emsg_funcname(ermsg, name)
8097 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008098 char_u *name;
8099{
8100 char_u *p;
8101
8102 if (*name == K_SPECIAL)
8103 p = concat_str((char_u *)"<SNR>", name + 3);
8104 else
8105 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008106 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008107 if (p != name)
8108 vim_free(p);
8109}
8110
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008111/*
8112 * Return TRUE for a non-zero Number and a non-empty String.
8113 */
8114 static int
8115non_zero_arg(argvars)
8116 typval_T *argvars;
8117{
8118 return ((argvars[0].v_type == VAR_NUMBER
8119 && argvars[0].vval.v_number != 0)
8120 || (argvars[0].v_type == VAR_STRING
8121 && argvars[0].vval.v_string != NULL
8122 && *argvars[0].vval.v_string != NUL));
8123}
8124
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125/*********************************************
8126 * Implementation of the built-in functions
8127 */
8128
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129#ifdef FEAT_FLOAT
8130/*
8131 * "abs(expr)" function
8132 */
8133 static void
8134f_abs(argvars, rettv)
8135 typval_T *argvars;
8136 typval_T *rettv;
8137{
8138 if (argvars[0].v_type == VAR_FLOAT)
8139 {
8140 rettv->v_type = VAR_FLOAT;
8141 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8142 }
8143 else
8144 {
8145 varnumber_T n;
8146 int error = FALSE;
8147
8148 n = get_tv_number_chk(&argvars[0], &error);
8149 if (error)
8150 rettv->vval.v_number = -1;
8151 else if (n > 0)
8152 rettv->vval.v_number = n;
8153 else
8154 rettv->vval.v_number = -n;
8155 }
8156}
8157#endif
8158
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008160 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 */
8162 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008163f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 typval_T *argvars;
8165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
Bram Moolenaar33570922005-01-25 22:26:29 +00008167 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008172 if ((l = argvars[0].vval.v_list) != NULL
8173 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8174 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008175 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008176 }
8177 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008178 EMSG(_(e_listreq));
8179}
8180
8181/*
8182 * "append(lnum, string/list)" function
8183 */
8184 static void
8185f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008186 typval_T *argvars;
8187 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008188{
8189 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008190 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 list_T *l = NULL;
8192 listitem_T *li = NULL;
8193 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008194 long added = 0;
8195
Bram Moolenaar0d660222005-01-07 21:51:51 +00008196 lnum = get_tv_lnum(argvars);
8197 if (lnum >= 0
8198 && lnum <= curbuf->b_ml.ml_line_count
8199 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008200 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008202 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008203 l = argvars[1].vval.v_list;
8204 if (l == NULL)
8205 return;
8206 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008208 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008209 for (;;)
8210 {
8211 if (l == NULL)
8212 tv = &argvars[1]; /* append a string */
8213 else if (li == NULL)
8214 break; /* end of list */
8215 else
8216 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008217 line = get_tv_string_chk(tv);
8218 if (line == NULL) /* type error */
8219 {
8220 rettv->vval.v_number = 1; /* Failed */
8221 break;
8222 }
8223 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008224 ++added;
8225 if (l == NULL)
8226 break;
8227 li = li->li_next;
8228 }
8229
8230 appended_lines_mark(lnum, added);
8231 if (curwin->w_cursor.lnum > lnum)
8232 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008234 else
8235 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236}
8237
8238/*
8239 * "argc()" function
8240 */
8241/* ARGSUSED */
8242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008244 typval_T *argvars;
8245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248}
8249
8250/*
8251 * "argidx()" function
8252 */
8253/* ARGSUSED */
8254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 typval_T *argvars;
8257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008259 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260}
8261
8262/*
8263 * "argv(nr)" function
8264 */
8265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 typval_T *argvars;
8268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
8270 int idx;
8271
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008272 if (argvars[0].v_type != VAR_UNKNOWN)
8273 {
8274 idx = get_tv_number_chk(&argvars[0], NULL);
8275 if (idx >= 0 && idx < ARGCOUNT)
8276 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8277 else
8278 rettv->vval.v_string = NULL;
8279 rettv->v_type = VAR_STRING;
8280 }
8281 else if (rettv_list_alloc(rettv) == OK)
8282 for (idx = 0; idx < ARGCOUNT; ++idx)
8283 list_append_string(rettv->vval.v_list,
8284 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285}
8286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008287#ifdef FEAT_FLOAT
8288static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8289
8290/*
8291 * Get the float value of "argvars[0]" into "f".
8292 * Returns FAIL when the argument is not a Number or Float.
8293 */
8294 static int
8295get_float_arg(argvars, f)
8296 typval_T *argvars;
8297 float_T *f;
8298{
8299 if (argvars[0].v_type == VAR_FLOAT)
8300 {
8301 *f = argvars[0].vval.v_float;
8302 return OK;
8303 }
8304 if (argvars[0].v_type == VAR_NUMBER)
8305 {
8306 *f = (float_T)argvars[0].vval.v_number;
8307 return OK;
8308 }
8309 EMSG(_("E808: Number or Float required"));
8310 return FAIL;
8311}
8312
8313/*
8314 * "atan()" function
8315 */
8316 static void
8317f_atan(argvars, rettv)
8318 typval_T *argvars;
8319 typval_T *rettv;
8320{
8321 float_T f;
8322
8323 rettv->v_type = VAR_FLOAT;
8324 if (get_float_arg(argvars, &f) == OK)
8325 rettv->vval.v_float = atan(f);
8326 else
8327 rettv->vval.v_float = 0.0;
8328}
8329#endif
8330
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331/*
8332 * "browse(save, title, initdir, default)" function
8333 */
8334/* ARGSUSED */
8335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 typval_T *argvars;
8338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340#ifdef FEAT_BROWSE
8341 int save;
8342 char_u *title;
8343 char_u *initdir;
8344 char_u *defname;
8345 char_u buf[NUMBUFLEN];
8346 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008347 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008349 save = get_tv_number_chk(&argvars[0], &error);
8350 title = get_tv_string_chk(&argvars[1]);
8351 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8352 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008354 if (error || title == NULL || initdir == NULL || defname == NULL)
8355 rettv->vval.v_string = NULL;
8356 else
8357 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008358 do_browse(save ? BROWSE_SAVE : 0,
8359 title, defname, NULL, initdir, NULL, curbuf);
8360#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008362#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008364}
8365
8366/*
8367 * "browsedir(title, initdir)" function
8368 */
8369/* ARGSUSED */
8370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *argvars;
8373 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008374{
8375#ifdef FEAT_BROWSE
8376 char_u *title;
8377 char_u *initdir;
8378 char_u buf[NUMBUFLEN];
8379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008380 title = get_tv_string_chk(&argvars[0]);
8381 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008383 if (title == NULL || initdir == NULL)
8384 rettv->vval.v_string = NULL;
8385 else
8386 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008387 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392}
8393
Bram Moolenaar33570922005-01-25 22:26:29 +00008394static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008395
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396/*
8397 * Find a buffer by number or exact name.
8398 */
8399 static buf_T *
8400find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008401 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008405 if (avar->v_type == VAR_NUMBER)
8406 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008407 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008409 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008410 if (buf == NULL)
8411 {
8412 /* No full path name match, try a match with a URL or a "nofile"
8413 * buffer, these don't use the full path. */
8414 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8415 if (buf->b_fname != NULL
8416 && (path_with_url(buf->b_fname)
8417#ifdef FEAT_QUICKFIX
8418 || bt_nofile(buf)
8419#endif
8420 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008421 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008422 break;
8423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 }
8425 return buf;
8426}
8427
8428/*
8429 * "bufexists(expr)" function
8430 */
8431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008432f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 typval_T *argvars;
8434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008436 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437}
8438
8439/*
8440 * "buflisted(expr)" function
8441 */
8442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008443f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008444 typval_T *argvars;
8445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446{
8447 buf_T *buf;
8448
8449 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451}
8452
8453/*
8454 * "bufloaded(expr)" function
8455 */
8456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 typval_T *argvars;
8459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 buf_T *buf;
8462
8463 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465}
8466
Bram Moolenaar33570922005-01-25 22:26:29 +00008467static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469/*
8470 * Get buffer by number or pattern.
8471 */
8472 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 int save_magic;
8478 char_u *save_cpo;
8479 buf_T *buf;
8480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481 if (tv->v_type == VAR_NUMBER)
8482 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008483 if (tv->v_type != VAR_STRING)
8484 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (name == NULL || *name == NUL)
8486 return curbuf;
8487 if (name[0] == '$' && name[1] == NUL)
8488 return lastbuf;
8489
8490 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8491 save_magic = p_magic;
8492 p_magic = TRUE;
8493 save_cpo = p_cpo;
8494 p_cpo = (char_u *)"";
8495
8496 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8497 TRUE, FALSE));
8498
8499 p_magic = save_magic;
8500 p_cpo = save_cpo;
8501
8502 /* If not found, try expanding the name, like done for bufexists(). */
8503 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505
8506 return buf;
8507}
8508
8509/*
8510 * "bufname(expr)" function
8511 */
8512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 typval_T *argvars;
8515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
8517 buf_T *buf;
8518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008519 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 buf = get_buf_tv(&argvars[0]);
8522 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008524 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 --emsg_off;
8528}
8529
8530/*
8531 * "bufnr(expr)" function
8532 */
8533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008534f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008535 typval_T *argvars;
8536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008539 int error = FALSE;
8540 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008542 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008544 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008545 --emsg_off;
8546
8547 /* If the buffer isn't found and the second argument is not zero create a
8548 * new buffer. */
8549 if (buf == NULL
8550 && argvars[1].v_type != VAR_UNKNOWN
8551 && get_tv_number_chk(&argvars[1], &error) != 0
8552 && !error
8553 && (name = get_tv_string_chk(&argvars[0])) != NULL
8554 && !error)
8555 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008558 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561}
8562
8563/*
8564 * "bufwinnr(nr)" function
8565 */
8566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008568 typval_T *argvars;
8569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571#ifdef FEAT_WINDOWS
8572 win_T *wp;
8573 int winnr = 0;
8574#endif
8575 buf_T *buf;
8576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008577 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#ifdef FEAT_WINDOWS
8581 for (wp = firstwin; wp; wp = wp->w_next)
8582 {
8583 ++winnr;
8584 if (wp->w_buffer == buf)
8585 break;
8586 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590#endif
8591 --emsg_off;
8592}
8593
8594/*
8595 * "byte2line(byte)" function
8596 */
8597/*ARGSUSED*/
8598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *argvars;
8601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605#else
8606 long boff = 0;
8607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 (linenr_T)0, &boff);
8614#endif
8615}
8616
8617/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008618 * "byteidx()" function
8619 */
8620/*ARGSUSED*/
8621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *argvars;
8624 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008625{
8626#ifdef FEAT_MBYTE
8627 char_u *t;
8628#endif
8629 char_u *str;
8630 long idx;
8631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008632 str = get_tv_string_chk(&argvars[0]);
8633 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008636 return;
8637
8638#ifdef FEAT_MBYTE
8639 t = str;
8640 for ( ; idx > 0; idx--)
8641 {
8642 if (*t == NUL) /* EOL reached */
8643 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008644 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008645 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008646 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008647#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008648 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008650#endif
8651}
8652
8653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008654 * "call(func, arglist)" function
8655 */
8656 static void
8657f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008660{
8661 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008662 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008663 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008665 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008667
8668 rettv->vval.v_number = 0;
8669 if (argvars[1].v_type != VAR_LIST)
8670 {
8671 EMSG(_(e_listreq));
8672 return;
8673 }
8674 if (argvars[1].vval.v_list == NULL)
8675 return;
8676
8677 if (argvars[0].v_type == VAR_FUNC)
8678 func = argvars[0].vval.v_string;
8679 else
8680 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 if (*func == NUL)
8682 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008683
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684 if (argvars[2].v_type != VAR_UNKNOWN)
8685 {
8686 if (argvars[2].v_type != VAR_DICT)
8687 {
8688 EMSG(_(e_dictreq));
8689 return;
8690 }
8691 selfdict = argvars[2].vval.v_dict;
8692 }
8693
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008694 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8695 item = item->li_next)
8696 {
8697 if (argc == MAX_FUNC_ARGS)
8698 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008699 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008700 break;
8701 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008702 /* Make a copy of each argument. This is needed to be able to set
8703 * v_lock to VAR_FIXED in the copy without changing the original list.
8704 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008705 copy_tv(&item->li_tv, &argv[argc++]);
8706 }
8707
8708 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008709 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008710 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8711 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008712
8713 /* Free the arguments. */
8714 while (argc > 0)
8715 clear_tv(&argv[--argc]);
8716}
8717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008718#ifdef FEAT_FLOAT
8719/*
8720 * "ceil({float})" function
8721 */
8722 static void
8723f_ceil(argvars, rettv)
8724 typval_T *argvars;
8725 typval_T *rettv;
8726{
8727 float_T f;
8728
8729 rettv->v_type = VAR_FLOAT;
8730 if (get_float_arg(argvars, &f) == OK)
8731 rettv->vval.v_float = ceil(f);
8732 else
8733 rettv->vval.v_float = 0.0;
8734}
8735#endif
8736
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008737/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008738 * "changenr()" function
8739 */
8740/*ARGSUSED*/
8741 static void
8742f_changenr(argvars, rettv)
8743 typval_T *argvars;
8744 typval_T *rettv;
8745{
8746 rettv->vval.v_number = curbuf->b_u_seq_cur;
8747}
8748
8749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 * "char2nr(string)" function
8751 */
8752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T *argvars;
8755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757#ifdef FEAT_MBYTE
8758 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008759 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 else
8761#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764
8765/*
8766 * "cindent(lnum)" function
8767 */
8768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008770 typval_T *argvars;
8771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773#ifdef FEAT_CINDENT
8774 pos_T pos;
8775 linenr_T lnum;
8776
8777 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8780 {
8781 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 curwin->w_cursor = pos;
8784 }
8785 else
8786#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
8790/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008791 * "clearmatches()" function
8792 */
8793/*ARGSUSED*/
8794 static void
8795f_clearmatches(argvars, rettv)
8796 typval_T *argvars;
8797 typval_T *rettv;
8798{
8799#ifdef FEAT_SEARCH_EXTRA
8800 clear_matches(curwin);
8801#endif
8802}
8803
8804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 * "col(string)" function
8806 */
8807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 typval_T *argvars;
8810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812 colnr_T col = 0;
8813 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008814 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008816 fp = var2fpos(&argvars[0], FALSE, &fnum);
8817 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {
8819 if (fp->col == MAXCOL)
8820 {
8821 /* '> can be MAXCOL, get the length of the line then */
8822 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008823 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 else
8825 col = MAXCOL;
8826 }
8827 else
8828 {
8829 col = fp->col + 1;
8830#ifdef FEAT_VIRTUALEDIT
8831 /* col(".") when the cursor is on the NUL at the end of the line
8832 * because of "coladd" can be seen as an extra column. */
8833 if (virtual_active() && fp == &curwin->w_cursor)
8834 {
8835 char_u *p = ml_get_cursor();
8836
8837 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8838 curwin->w_virtcol - curwin->w_cursor.coladd))
8839 {
8840# ifdef FEAT_MBYTE
8841 int l;
8842
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008843 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 col += l;
8845# else
8846 if (*p != NUL && p[1] == NUL)
8847 ++col;
8848# endif
8849 }
8850 }
8851#endif
8852 }
8853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855}
8856
Bram Moolenaar572cb562005-08-05 21:35:02 +00008857#if defined(FEAT_INS_EXPAND)
8858/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008859 * "complete()" function
8860 */
8861/*ARGSUSED*/
8862 static void
8863f_complete(argvars, rettv)
8864 typval_T *argvars;
8865 typval_T *rettv;
8866{
8867 int startcol;
8868
8869 if ((State & INSERT) == 0)
8870 {
8871 EMSG(_("E785: complete() can only be used in Insert mode"));
8872 return;
8873 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008874
8875 /* Check for undo allowed here, because if something was already inserted
8876 * the line was already saved for undo and this check isn't done. */
8877 if (!undo_allowed())
8878 return;
8879
Bram Moolenaarade00832006-03-10 21:46:58 +00008880 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8881 {
8882 EMSG(_(e_invarg));
8883 return;
8884 }
8885
8886 startcol = get_tv_number_chk(&argvars[0], NULL);
8887 if (startcol <= 0)
8888 return;
8889
8890 set_completion(startcol - 1, argvars[1].vval.v_list);
8891}
8892
8893/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008894 * "complete_add()" function
8895 */
8896/*ARGSUSED*/
8897 static void
8898f_complete_add(argvars, rettv)
8899 typval_T *argvars;
8900 typval_T *rettv;
8901{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008902 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008903}
8904
8905/*
8906 * "complete_check()" function
8907 */
8908/*ARGSUSED*/
8909 static void
8910f_complete_check(argvars, rettv)
8911 typval_T *argvars;
8912 typval_T *rettv;
8913{
8914 int saved = RedrawingDisabled;
8915
8916 RedrawingDisabled = 0;
8917 ins_compl_check_keys(0);
8918 rettv->vval.v_number = compl_interrupted;
8919 RedrawingDisabled = saved;
8920}
8921#endif
8922
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923/*
8924 * "confirm(message, buttons[, default [, type]])" function
8925 */
8926/*ARGSUSED*/
8927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008928f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008929 typval_T *argvars;
8930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8933 char_u *message;
8934 char_u *buttons = NULL;
8935 char_u buf[NUMBUFLEN];
8936 char_u buf2[NUMBUFLEN];
8937 int def = 1;
8938 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 char_u *typestr;
8940 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 message = get_tv_string_chk(&argvars[0]);
8943 if (message == NULL)
8944 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008945 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8948 if (buttons == NULL)
8949 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008953 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8956 if (typestr == NULL)
8957 error = TRUE;
8958 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008960 switch (TOUPPER_ASC(*typestr))
8961 {
8962 case 'E': type = VIM_ERROR; break;
8963 case 'Q': type = VIM_QUESTION; break;
8964 case 'I': type = VIM_INFO; break;
8965 case 'W': type = VIM_WARNING; break;
8966 case 'G': type = VIM_GENERIC; break;
8967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 }
8969 }
8970 }
8971 }
8972
8973 if (buttons == NULL || *buttons == NUL)
8974 buttons = (char_u *)_("&Ok");
8975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 if (error)
8977 rettv->vval.v_number = 0;
8978 else
8979 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 def, NULL);
8981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#endif
8984}
8985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008986/*
8987 * "copy()" function
8988 */
8989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *argvars;
8992 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008994 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008997#ifdef FEAT_FLOAT
8998/*
8999 * "cos()" function
9000 */
9001 static void
9002f_cos(argvars, rettv)
9003 typval_T *argvars;
9004 typval_T *rettv;
9005{
9006 float_T f;
9007
9008 rettv->v_type = VAR_FLOAT;
9009 if (get_float_arg(argvars, &f) == OK)
9010 rettv->vval.v_float = cos(f);
9011 else
9012 rettv->vval.v_float = 0.0;
9013}
9014#endif
9015
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009017 * "count()" function
9018 */
9019 static void
9020f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 typval_T *argvars;
9022 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009023{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009024 long n = 0;
9025 int ic = FALSE;
9026
Bram Moolenaare9a41262005-01-15 22:18:47 +00009027 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009028 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 listitem_T *li;
9030 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009031 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009032
Bram Moolenaare9a41262005-01-15 22:18:47 +00009033 if ((l = argvars[0].vval.v_list) != NULL)
9034 {
9035 li = l->lv_first;
9036 if (argvars[2].v_type != VAR_UNKNOWN)
9037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 int error = FALSE;
9039
9040 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 if (argvars[3].v_type != VAR_UNKNOWN)
9042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 idx = get_tv_number_chk(&argvars[3], &error);
9044 if (!error)
9045 {
9046 li = list_find(l, idx);
9047 if (li == NULL)
9048 EMSGN(_(e_listidx), idx);
9049 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 if (error)
9052 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009053 }
9054
9055 for ( ; li != NULL; li = li->li_next)
9056 if (tv_equal(&li->li_tv, &argvars[1], ic))
9057 ++n;
9058 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009059 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009060 else if (argvars[0].v_type == VAR_DICT)
9061 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 int todo;
9063 dict_T *d;
9064 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009066 if ((d = argvars[0].vval.v_dict) != NULL)
9067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009068 int error = FALSE;
9069
Bram Moolenaare9a41262005-01-15 22:18:47 +00009070 if (argvars[2].v_type != VAR_UNKNOWN)
9071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 if (argvars[3].v_type != VAR_UNKNOWN)
9074 EMSG(_(e_invarg));
9075 }
9076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009077 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009079 {
9080 if (!HASHITEM_EMPTY(hi))
9081 {
9082 --todo;
9083 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9084 ++n;
9085 }
9086 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009087 }
9088 }
9089 else
9090 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009091 rettv->vval.v_number = n;
9092}
9093
9094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9096 *
9097 * Checks the existence of a cscope connection.
9098 */
9099/*ARGSUSED*/
9100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 typval_T *argvars;
9103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105#ifdef FEAT_CSCOPE
9106 int num = 0;
9107 char_u *dbpath = NULL;
9108 char_u *prepend = NULL;
9109 char_u buf[NUMBUFLEN];
9110
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009111 if (argvars[0].v_type != VAR_UNKNOWN
9112 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 num = (int)get_tv_number(&argvars[0]);
9115 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009116 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 }
9119
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123#endif
9124}
9125
9126/*
9127 * "cursor(lnum, col)" function
9128 *
9129 * Moves the cursor to the specified line and column
9130 */
9131/*ARGSUSED*/
9132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 typval_T *argvars;
9135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009138#ifdef FEAT_VIRTUALEDIT
9139 long coladd = 0;
9140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
Bram Moolenaara5525202006-03-02 22:52:09 +00009142 if (argvars[1].v_type == VAR_UNKNOWN)
9143 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009144 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009145
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009146 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009147 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009148 line = pos.lnum;
9149 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009150#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009151 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009152#endif
9153 }
9154 else
9155 {
9156 line = get_tv_lnum(argvars);
9157 col = get_tv_number_chk(&argvars[1], NULL);
9158#ifdef FEAT_VIRTUALEDIT
9159 if (argvars[2].v_type != VAR_UNKNOWN)
9160 coladd = get_tv_number_chk(&argvars[2], NULL);
9161#endif
9162 }
9163 if (line < 0 || col < 0
9164#ifdef FEAT_VIRTUALEDIT
9165 || coladd < 0
9166#endif
9167 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009168 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 if (line > 0)
9170 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (col > 0)
9172 curwin->w_cursor.col = col - 1;
9173#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009174 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175#endif
9176
9177 /* Make sure the cursor is in a valid position. */
9178 check_cursor();
9179#ifdef FEAT_MBYTE
9180 /* Correct cursor for multi-byte character. */
9181 if (has_mbyte)
9182 mb_adjust_cursor();
9183#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009184
9185 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186}
9187
9188/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009189 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 */
9191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009193 typval_T *argvars;
9194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009196 int noref = 0;
9197
9198 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009200 if (noref < 0 || noref > 1)
9201 EMSG(_(e_invarg));
9202 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009203 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204}
9205
9206/*
9207 * "delete()" function
9208 */
9209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009211 typval_T *argvars;
9212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
9214 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218}
9219
9220/*
9221 * "did_filetype()" function
9222 */
9223/*ARGSUSED*/
9224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009225f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009226 typval_T *argvars;
9227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228{
9229#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233#endif
9234}
9235
9236/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009237 * "diff_filler()" function
9238 */
9239/*ARGSUSED*/
9240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009241f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 typval_T *argvars;
9243 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009244{
9245#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009246 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009247#endif
9248}
9249
9250/*
9251 * "diff_hlID()" function
9252 */
9253/*ARGSUSED*/
9254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009256 typval_T *argvars;
9257 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009258{
9259#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009261 static linenr_T prev_lnum = 0;
9262 static int changedtick = 0;
9263 static int fnum = 0;
9264 static int change_start = 0;
9265 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009266 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009267 int filler_lines;
9268 int col;
9269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 if (lnum < 0) /* ignore type error in {lnum} arg */
9271 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009272 if (lnum != prev_lnum
9273 || changedtick != curbuf->b_changedtick
9274 || fnum != curbuf->b_fnum)
9275 {
9276 /* New line, buffer, change: need to get the values. */
9277 filler_lines = diff_check(curwin, lnum);
9278 if (filler_lines < 0)
9279 {
9280 if (filler_lines == -1)
9281 {
9282 change_start = MAXCOL;
9283 change_end = -1;
9284 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9285 hlID = HLF_ADD; /* added line */
9286 else
9287 hlID = HLF_CHD; /* changed line */
9288 }
9289 else
9290 hlID = HLF_ADD; /* added line */
9291 }
9292 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009293 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009294 prev_lnum = lnum;
9295 changedtick = curbuf->b_changedtick;
9296 fnum = curbuf->b_fnum;
9297 }
9298
9299 if (hlID == HLF_CHD || hlID == HLF_TXD)
9300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009302 if (col >= change_start && col <= change_end)
9303 hlID = HLF_TXD; /* changed text */
9304 else
9305 hlID = HLF_CHD; /* changed line */
9306 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009307 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009308#endif
9309}
9310
9311/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009312 * "empty({expr})" function
9313 */
9314 static void
9315f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 typval_T *argvars;
9317 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009318{
9319 int n;
9320
9321 switch (argvars[0].v_type)
9322 {
9323 case VAR_STRING:
9324 case VAR_FUNC:
9325 n = argvars[0].vval.v_string == NULL
9326 || *argvars[0].vval.v_string == NUL;
9327 break;
9328 case VAR_NUMBER:
9329 n = argvars[0].vval.v_number == 0;
9330 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009331#ifdef FEAT_FLOAT
9332 case VAR_FLOAT:
9333 n = argvars[0].vval.v_float == 0.0;
9334 break;
9335#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009336 case VAR_LIST:
9337 n = argvars[0].vval.v_list == NULL
9338 || argvars[0].vval.v_list->lv_first == NULL;
9339 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009340 case VAR_DICT:
9341 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009342 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009343 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009344 default:
9345 EMSG2(_(e_intern2), "f_empty()");
9346 n = 0;
9347 }
9348
9349 rettv->vval.v_number = n;
9350}
9351
9352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 * "escape({string}, {chars})" function
9354 */
9355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009356f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009357 typval_T *argvars;
9358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359{
9360 char_u buf[NUMBUFLEN];
9361
Bram Moolenaar758711c2005-02-02 23:11:38 +00009362 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9363 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365}
9366
9367/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009368 * "eval()" function
9369 */
9370/*ARGSUSED*/
9371 static void
9372f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009375{
9376 char_u *s;
9377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 s = get_tv_string_chk(&argvars[0]);
9379 if (s != NULL)
9380 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9383 {
9384 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009385 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009387 else if (*s != NUL)
9388 EMSG(_(e_trailing));
9389}
9390
9391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 * "eventhandler()" function
9393 */
9394/*ARGSUSED*/
9395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *argvars;
9398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
9403/*
9404 * "executable()" function
9405 */
9406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 typval_T *argvars;
9409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412}
9413
9414/*
9415 * "exists()" function
9416 */
9417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
9422 char_u *p;
9423 char_u *name;
9424 int n = FALSE;
9425 int len = 0;
9426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 if (*p == '$') /* environment variable */
9429 {
9430 /* first try "normal" environment variables (fast) */
9431 if (mch_getenv(p + 1) != NULL)
9432 n = TRUE;
9433 else
9434 {
9435 /* try expanding things like $VIM and ${HOME} */
9436 p = expand_env_save(p);
9437 if (p != NULL && *p != '$')
9438 n = TRUE;
9439 vim_free(p);
9440 }
9441 }
9442 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009445 if (*skipwhite(p) != NUL)
9446 n = FALSE; /* trailing garbage */
9447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 else if (*p == '*') /* internal or user defined function */
9449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009450 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 }
9452 else if (*p == ':')
9453 {
9454 n = cmd_exists(p + 1);
9455 }
9456 else if (*p == '#')
9457 {
9458#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009459 if (p[1] == '#')
9460 n = autocmd_supported(p + 2);
9461 else
9462 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463#endif
9464 }
9465 else /* internal variable */
9466 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009467 char_u *tofree;
9468 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009470 /* get_name_len() takes care of expanding curly braces */
9471 name = p;
9472 len = get_name_len(&p, &tofree, TRUE, FALSE);
9473 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009475 if (tofree != NULL)
9476 name = tofree;
9477 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9478 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009480 /* handle d.key, l[idx], f(expr) */
9481 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9482 if (n)
9483 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 }
9485 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009486 if (*p != NUL)
9487 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009489 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 }
9491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
9495/*
9496 * "expand()" function
9497 */
9498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 typval_T *argvars;
9501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
9503 char_u *s;
9504 int len;
9505 char_u *errormsg;
9506 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9507 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009508 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 rettv->v_type = VAR_STRING;
9511 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (*s == '%' || *s == '#' || *s == '<')
9513 {
9514 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009515 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 --emsg_off;
9517 }
9518 else
9519 {
9520 /* When the optional second argument is non-zero, don't remove matches
9521 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009522 if (argvars[1].v_type != VAR_UNKNOWN
9523 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 if (!error)
9526 {
9527 ExpandInit(&xpc);
9528 xpc.xp_context = EXPAND_FILES;
9529 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 }
9531 else
9532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 }
9534}
9535
9536/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009537 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009538 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009539 */
9540 static void
9541f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 typval_T *argvars;
9543 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009544{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009545 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009546 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009547 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 list_T *l1, *l2;
9549 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009550 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009551 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009552
Bram Moolenaare9a41262005-01-15 22:18:47 +00009553 l1 = argvars[0].vval.v_list;
9554 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009555 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9556 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009557 {
9558 if (argvars[2].v_type != VAR_UNKNOWN)
9559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 before = get_tv_number_chk(&argvars[2], &error);
9561 if (error)
9562 return; /* type error; errmsg already given */
9563
Bram Moolenaar758711c2005-02-02 23:11:38 +00009564 if (before == l1->lv_len)
9565 item = NULL;
9566 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009568 item = list_find(l1, before);
9569 if (item == NULL)
9570 {
9571 EMSGN(_(e_listidx), before);
9572 return;
9573 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009574 }
9575 }
9576 else
9577 item = NULL;
9578 list_extend(l1, l2, item);
9579
Bram Moolenaare9a41262005-01-15 22:18:47 +00009580 copy_tv(&argvars[0], rettv);
9581 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009583 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9584 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009585 dict_T *d1, *d2;
9586 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009587 char_u *action;
9588 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009589 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009590 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009591
9592 d1 = argvars[0].vval.v_dict;
9593 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009594 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9595 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009596 {
9597 /* Check the third argument. */
9598 if (argvars[2].v_type != VAR_UNKNOWN)
9599 {
9600 static char *(av[]) = {"keep", "force", "error"};
9601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 action = get_tv_string_chk(&argvars[2]);
9603 if (action == NULL)
9604 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 for (i = 0; i < 3; ++i)
9606 if (STRCMP(action, av[i]) == 0)
9607 break;
9608 if (i == 3)
9609 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009610 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 return;
9612 }
9613 }
9614 else
9615 action = (char_u *)"force";
9616
9617 /* Go over all entries in the second dict and add them to the
9618 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009619 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009620 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009622 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009624 --todo;
9625 di1 = dict_find(d1, hi2->hi_key, -1);
9626 if (di1 == NULL)
9627 {
9628 di1 = dictitem_copy(HI2DI(hi2));
9629 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9630 dictitem_free(di1);
9631 }
9632 else if (*action == 'e')
9633 {
9634 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9635 break;
9636 }
9637 else if (*action == 'f')
9638 {
9639 clear_tv(&di1->di_tv);
9640 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 }
9643 }
9644
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 copy_tv(&argvars[0], rettv);
9646 }
9647 }
9648 else
9649 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009650}
9651
9652/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009653 * "feedkeys()" function
9654 */
9655/*ARGSUSED*/
9656 static void
9657f_feedkeys(argvars, rettv)
9658 typval_T *argvars;
9659 typval_T *rettv;
9660{
9661 int remap = TRUE;
9662 char_u *keys, *flags;
9663 char_u nbuf[NUMBUFLEN];
9664 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009665 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009666
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009667 /* This is not allowed in the sandbox. If the commands would still be
9668 * executed in the sandbox it would be OK, but it probably happens later,
9669 * when "sandbox" is no longer set. */
9670 if (check_secure())
9671 return;
9672
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009673 rettv->vval.v_number = 0;
9674 keys = get_tv_string(&argvars[0]);
9675 if (*keys != NUL)
9676 {
9677 if (argvars[1].v_type != VAR_UNKNOWN)
9678 {
9679 flags = get_tv_string_buf(&argvars[1], nbuf);
9680 for ( ; *flags != NUL; ++flags)
9681 {
9682 switch (*flags)
9683 {
9684 case 'n': remap = FALSE; break;
9685 case 'm': remap = TRUE; break;
9686 case 't': typed = TRUE; break;
9687 }
9688 }
9689 }
9690
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009691 /* Need to escape K_SPECIAL and CSI before putting the string in the
9692 * typeahead buffer. */
9693 keys_esc = vim_strsave_escape_csi(keys);
9694 if (keys_esc != NULL)
9695 {
9696 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009697 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009698 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009699 if (vgetc_busy)
9700 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009701 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009702 }
9703}
9704
9705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 * "filereadable()" function
9707 */
9708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *argvars;
9711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009713 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 char_u *p;
9715 int n;
9716
Bram Moolenaarc236c162008-07-13 17:41:49 +00009717#ifndef O_NONBLOCK
9718# define O_NONBLOCK 0
9719#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009721 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9722 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 {
9724 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009725 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 }
9727 else
9728 n = FALSE;
9729
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009734 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 * rights to write into.
9736 */
9737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009742 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009745static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009746
9747 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009748findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009749 typval_T *argvars;
9750 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009751 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009752{
9753#ifdef FEAT_SEARCHPATH
9754 char_u *fname;
9755 char_u *fresult = NULL;
9756 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9757 char_u *p;
9758 char_u pathbuf[NUMBUFLEN];
9759 int count = 1;
9760 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009761 int error = FALSE;
9762#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009763
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009764 rettv->vval.v_string = NULL;
9765 rettv->v_type = VAR_STRING;
9766
9767#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009769
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009770 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9773 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009774 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 else
9776 {
9777 if (*p != NUL)
9778 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009780 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009781 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009782 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009783 }
9784
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009785 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9786 error = TRUE;
9787
9788 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 do
9791 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009792 if (rettv->v_type == VAR_STRING)
9793 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 fresult = find_file_in_path_option(first ? fname : NULL,
9795 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009796 0, first, path,
9797 find_what,
9798 curbuf->b_ffname,
9799 find_what == FINDFILE_DIR
9800 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009801 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009802
9803 if (fresult != NULL && rettv->v_type == VAR_LIST)
9804 list_append_string(rettv->vval.v_list, fresult, -1);
9805
9806 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009807 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009808
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009809 if (rettv->v_type == VAR_STRING)
9810 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009811#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009812}
9813
Bram Moolenaar33570922005-01-25 22:26:29 +00009814static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9815static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009816
9817/*
9818 * Implementation of map() and filter().
9819 */
9820 static void
9821filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 typval_T *argvars;
9823 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009824 int map;
9825{
9826 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009827 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009828 listitem_T *li, *nli;
9829 list_T *l = NULL;
9830 dictitem_T *di;
9831 hashtab_T *ht;
9832 hashitem_T *hi;
9833 dict_T *d = NULL;
9834 typval_T save_val;
9835 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009836 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009837 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009838 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009839 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009840
9841 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009842 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009843 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009844 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009845 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009846 return;
9847 }
9848 else if (argvars[0].v_type == VAR_DICT)
9849 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009850 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009851 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009852 return;
9853 }
9854 else
9855 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009856 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009857 return;
9858 }
9859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009860 expr = get_tv_string_buf_chk(&argvars[1], buf);
9861 /* On type errors, the preceding call has already displayed an error
9862 * message. Avoid a misleading error message for an empty string that
9863 * was not passed as argument. */
9864 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009866 prepare_vimvar(VV_VAL, &save_val);
9867 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009868
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009869 /* We reset "did_emsg" to be able to detect whether an error
9870 * occurred during evaluation of the expression. */
9871 save_did_emsg = did_emsg;
9872 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 prepare_vimvar(VV_KEY, &save_key);
9877 vimvars[VV_KEY].vv_type = VAR_STRING;
9878
9879 ht = &d->dv_hashtab;
9880 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009881 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 if (!HASHITEM_EMPTY(hi))
9885 {
9886 --todo;
9887 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009888 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 break;
9890 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009891 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009892 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 break;
9894 if (!map && rem)
9895 dictitem_remove(d, di);
9896 clear_tv(&vimvars[VV_KEY].vv_tv);
9897 }
9898 }
9899 hash_unlock(ht);
9900
9901 restore_vimvar(VV_KEY, &save_key);
9902 }
9903 else
9904 {
9905 for (li = l->lv_first; li != NULL; li = nli)
9906 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009907 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009908 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009909 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009910 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009911 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009912 break;
9913 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009915 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009916 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009919
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009920 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009922
9923 copy_tv(&argvars[0], rettv);
9924}
9925
9926 static int
9927filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 char_u *expr;
9930 int map;
9931 int *remp;
9932{
Bram Moolenaar33570922005-01-25 22:26:29 +00009933 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009935 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009936
Bram Moolenaar33570922005-01-25 22:26:29 +00009937 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 s = expr;
9939 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009940 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009941 if (*s != NUL) /* check for trailing chars after expr */
9942 {
9943 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009944 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009945 }
9946 if (map)
9947 {
9948 /* map(): replace the list item value */
9949 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009950 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009951 *tv = rettv;
9952 }
9953 else
9954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 int error = FALSE;
9956
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009959 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009960 /* On type error, nothing has been removed; return FAIL to stop the
9961 * loop. The error message was given by get_tv_number_chk(). */
9962 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009963 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009964 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009965 retval = OK;
9966theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009968 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009969}
9970
9971/*
9972 * "filter()" function
9973 */
9974 static void
9975f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *argvars;
9977 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009978{
9979 filter_map(argvars, rettv, FALSE);
9980}
9981
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009982/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009983 * "finddir({fname}[, {path}[, {count}]])" function
9984 */
9985 static void
9986f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009989{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009990 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009991}
9992
9993/*
9994 * "findfile({fname}[, {path}[, {count}]])" function
9995 */
9996 static void
9997f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009998 typval_T *argvars;
9999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010000{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010001 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010002}
10003
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010004#ifdef FEAT_FLOAT
10005/*
10006 * "float2nr({float})" function
10007 */
10008 static void
10009f_float2nr(argvars, rettv)
10010 typval_T *argvars;
10011 typval_T *rettv;
10012{
10013 float_T f;
10014
10015 if (get_float_arg(argvars, &f) == OK)
10016 {
10017 if (f < -0x7fffffff)
10018 rettv->vval.v_number = -0x7fffffff;
10019 else if (f > 0x7fffffff)
10020 rettv->vval.v_number = 0x7fffffff;
10021 else
10022 rettv->vval.v_number = (varnumber_T)f;
10023 }
10024 else
10025 rettv->vval.v_number = 0;
10026}
10027
10028/*
10029 * "floor({float})" function
10030 */
10031 static void
10032f_floor(argvars, rettv)
10033 typval_T *argvars;
10034 typval_T *rettv;
10035{
10036 float_T f;
10037
10038 rettv->v_type = VAR_FLOAT;
10039 if (get_float_arg(argvars, &f) == OK)
10040 rettv->vval.v_float = floor(f);
10041 else
10042 rettv->vval.v_float = 0.0;
10043}
10044#endif
10045
Bram Moolenaar0d660222005-01-07 21:51:51 +000010046/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010047 * "fnameescape({string})" function
10048 */
10049 static void
10050f_fnameescape(argvars, rettv)
10051 typval_T *argvars;
10052 typval_T *rettv;
10053{
10054 rettv->vval.v_string = vim_strsave_fnameescape(
10055 get_tv_string(&argvars[0]), FALSE);
10056 rettv->v_type = VAR_STRING;
10057}
10058
10059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 * "fnamemodify({fname}, {mods})" function
10061 */
10062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010064 typval_T *argvars;
10065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066{
10067 char_u *fname;
10068 char_u *mods;
10069 int usedlen = 0;
10070 int len;
10071 char_u *fbuf = NULL;
10072 char_u buf[NUMBUFLEN];
10073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 fname = get_tv_string_chk(&argvars[0]);
10075 mods = get_tv_string_buf_chk(&argvars[1], buf);
10076 if (fname == NULL || mods == NULL)
10077 fname = NULL;
10078 else
10079 {
10080 len = (int)STRLEN(fname);
10081 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 vim_free(fbuf);
10090}
10091
Bram Moolenaar33570922005-01-25 22:26:29 +000010092static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
10094/*
10095 * "foldclosed()" function
10096 */
10097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010099 typval_T *argvars;
10100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 int end;
10102{
10103#ifdef FEAT_FOLDING
10104 linenr_T lnum;
10105 linenr_T first, last;
10106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10109 {
10110 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10111 {
10112 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 return;
10117 }
10118 }
10119#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121}
10122
10123/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010124 * "foldclosed()" function
10125 */
10126 static void
10127f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010128 typval_T *argvars;
10129 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010130{
10131 foldclosed_both(argvars, rettv, FALSE);
10132}
10133
10134/*
10135 * "foldclosedend()" function
10136 */
10137 static void
10138f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010139 typval_T *argvars;
10140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010141{
10142 foldclosed_both(argvars, rettv, TRUE);
10143}
10144
10145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 * "foldlevel()" function
10147 */
10148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010150 typval_T *argvars;
10151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
10153#ifdef FEAT_FOLDING
10154 linenr_T lnum;
10155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 else
10160#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162}
10163
10164/*
10165 * "foldtext()" function
10166 */
10167/*ARGSUSED*/
10168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010170 typval_T *argvars;
10171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172{
10173#ifdef FEAT_FOLDING
10174 linenr_T lnum;
10175 char_u *s;
10176 char_u *r;
10177 int len;
10178 char *txt;
10179#endif
10180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->v_type = VAR_STRING;
10182 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010184 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10185 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10186 <= curbuf->b_ml.ml_line_count
10187 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 {
10189 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010190 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10191 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 {
10193 if (!linewhite(lnum))
10194 break;
10195 ++lnum;
10196 }
10197
10198 /* Find interesting text in this line. */
10199 s = skipwhite(ml_get(lnum));
10200 /* skip C comment-start */
10201 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010204 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010206 {
10207 s = skipwhite(ml_get(lnum + 1));
10208 if (*s == '*')
10209 s = skipwhite(s + 1);
10210 }
10211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 txt = _("+-%s%3ld lines: ");
10213 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 + 20 /* for %3ld */
10216 + STRLEN(s))); /* concatenated */
10217 if (r != NULL)
10218 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10220 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10221 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 len = (int)STRLEN(r);
10223 STRCAT(r, s);
10224 /* remove 'foldmarker' and 'commentstring' */
10225 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 }
10228 }
10229#endif
10230}
10231
10232/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010233 * "foldtextresult(lnum)" function
10234 */
10235/*ARGSUSED*/
10236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 typval_T *argvars;
10239 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010240{
10241#ifdef FEAT_FOLDING
10242 linenr_T lnum;
10243 char_u *text;
10244 char_u buf[51];
10245 foldinfo_T foldinfo;
10246 int fold_count;
10247#endif
10248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 rettv->v_type = VAR_STRING;
10250 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010251#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010252 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 /* treat illegal types and illegal string values for {lnum} the same */
10254 if (lnum < 0)
10255 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010256 fold_count = foldedCount(curwin, lnum, &foldinfo);
10257 if (fold_count > 0)
10258 {
10259 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10260 &foldinfo, buf);
10261 if (text == buf)
10262 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010264 }
10265#endif
10266}
10267
10268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 * "foreground()" function
10270 */
10271/*ARGSUSED*/
10272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010273f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010274 typval_T *argvars;
10275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#ifdef FEAT_GUI
10279 if (gui.in_use)
10280 gui_mch_set_foreground();
10281#else
10282# ifdef WIN32
10283 win32_set_foreground();
10284# endif
10285#endif
10286}
10287
10288/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010289 * "function()" function
10290 */
10291/*ARGSUSED*/
10292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010294 typval_T *argvars;
10295 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010296{
10297 char_u *s;
10298
Bram Moolenaara7043832005-01-21 11:56:39 +000010299 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010300 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010301 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010302 EMSG2(_(e_invarg2), s);
10303 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010304 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010305 else
10306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010307 rettv->vval.v_string = vim_strsave(s);
10308 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010309 }
10310}
10311
10312/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010313 * "garbagecollect()" function
10314 */
10315/*ARGSUSED*/
10316 static void
10317f_garbagecollect(argvars, rettv)
10318 typval_T *argvars;
10319 typval_T *rettv;
10320{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010321 /* This is postponed until we are back at the toplevel, because we may be
10322 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10323 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010324
10325 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10326 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010327}
10328
10329/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010330 * "get()" function
10331 */
10332 static void
10333f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010334 typval_T *argvars;
10335 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010336{
Bram Moolenaar33570922005-01-25 22:26:29 +000010337 listitem_T *li;
10338 list_T *l;
10339 dictitem_T *di;
10340 dict_T *d;
10341 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010342
Bram Moolenaare9a41262005-01-15 22:18:47 +000010343 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010344 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010345 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010347 int error = FALSE;
10348
10349 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10350 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010352 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010353 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354 else if (argvars[0].v_type == VAR_DICT)
10355 {
10356 if ((d = argvars[0].vval.v_dict) != NULL)
10357 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010358 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010359 if (di != NULL)
10360 tv = &di->di_tv;
10361 }
10362 }
10363 else
10364 EMSG2(_(e_listdictarg), "get()");
10365
10366 if (tv == NULL)
10367 {
10368 if (argvars[2].v_type == VAR_UNKNOWN)
10369 rettv->vval.v_number = 0;
10370 else
10371 copy_tv(&argvars[2], rettv);
10372 }
10373 else
10374 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010375}
10376
Bram Moolenaar342337a2005-07-21 21:11:17 +000010377static 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 +000010378
10379/*
10380 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010381 * Return a range (from start to end) of lines in rettv from the specified
10382 * buffer.
10383 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010384 */
10385 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010386get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010387 buf_T *buf;
10388 linenr_T start;
10389 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010390 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010391 typval_T *rettv;
10392{
10393 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010394
Bram Moolenaar342337a2005-07-21 21:11:17 +000010395 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010396 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010397 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010398 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010399 }
10400 else
10401 rettv->vval.v_number = 0;
10402
10403 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10404 return;
10405
10406 if (!retlist)
10407 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010408 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10409 p = ml_get_buf(buf, start, FALSE);
10410 else
10411 p = (char_u *)"";
10412
10413 rettv->v_type = VAR_STRING;
10414 rettv->vval.v_string = vim_strsave(p);
10415 }
10416 else
10417 {
10418 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010419 return;
10420
10421 if (start < 1)
10422 start = 1;
10423 if (end > buf->b_ml.ml_line_count)
10424 end = buf->b_ml.ml_line_count;
10425 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010426 if (list_append_string(rettv->vval.v_list,
10427 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010428 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010429 }
10430}
10431
10432/*
10433 * "getbufline()" function
10434 */
10435 static void
10436f_getbufline(argvars, rettv)
10437 typval_T *argvars;
10438 typval_T *rettv;
10439{
10440 linenr_T lnum;
10441 linenr_T end;
10442 buf_T *buf;
10443
10444 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10445 ++emsg_off;
10446 buf = get_buf_tv(&argvars[0]);
10447 --emsg_off;
10448
Bram Moolenaar661b1822005-07-28 22:36:45 +000010449 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010450 if (argvars[2].v_type == VAR_UNKNOWN)
10451 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010452 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010453 end = get_tv_lnum_buf(&argvars[2], buf);
10454
Bram Moolenaar342337a2005-07-21 21:11:17 +000010455 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010456}
10457
Bram Moolenaar0d660222005-01-07 21:51:51 +000010458/*
10459 * "getbufvar()" function
10460 */
10461 static void
10462f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 typval_T *argvars;
10464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010465{
10466 buf_T *buf;
10467 buf_T *save_curbuf;
10468 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010469 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10472 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010473 ++emsg_off;
10474 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010475
10476 rettv->v_type = VAR_STRING;
10477 rettv->vval.v_string = NULL;
10478
10479 if (buf != NULL && varname != NULL)
10480 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010481 /* set curbuf to be our buf, temporarily */
10482 save_curbuf = curbuf;
10483 curbuf = buf;
10484
Bram Moolenaar0d660222005-01-07 21:51:51 +000010485 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010486 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010487 else
10488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 if (*varname == NUL)
10490 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10491 * scope prefix before the NUL byte is required by
10492 * find_var_in_ht(). */
10493 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010494 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010495 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010496 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010497 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010498 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010499
10500 /* restore previous notion of curbuf */
10501 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010502 }
10503
10504 --emsg_off;
10505}
10506
10507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 * "getchar()" function
10509 */
10510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010512 typval_T *argvars;
10513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010518 /* Position the cursor. Needed after a message that ends in a space. */
10519 windgoto(msg_row, msg_col);
10520
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 ++no_mapping;
10522 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010523 for (;;)
10524 {
10525 if (argvars[0].v_type == VAR_UNKNOWN)
10526 /* getchar(): blocking wait. */
10527 n = safe_vgetc();
10528 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10529 /* getchar(1): only check if char avail */
10530 n = vpeekc();
10531 else if (error || vpeekc() == NUL)
10532 /* illegal argument or getchar(0) and no char avail: return zero */
10533 n = 0;
10534 else
10535 /* getchar(0) and char avail: return char */
10536 n = safe_vgetc();
10537 if (n == K_IGNORE)
10538 continue;
10539 break;
10540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 --no_mapping;
10542 --allow_keys;
10543
Bram Moolenaar219b8702006-11-01 14:32:36 +000010544 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10545 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10546 vimvars[VV_MOUSE_COL].vv_nr = 0;
10547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 if (IS_SPECIAL(n) || mod_mask != 0)
10550 {
10551 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10552 int i = 0;
10553
10554 /* Turn a special key into three bytes, plus modifier. */
10555 if (mod_mask != 0)
10556 {
10557 temp[i++] = K_SPECIAL;
10558 temp[i++] = KS_MODIFIER;
10559 temp[i++] = mod_mask;
10560 }
10561 if (IS_SPECIAL(n))
10562 {
10563 temp[i++] = K_SPECIAL;
10564 temp[i++] = K_SECOND(n);
10565 temp[i++] = K_THIRD(n);
10566 }
10567#ifdef FEAT_MBYTE
10568 else if (has_mbyte)
10569 i += (*mb_char2bytes)(n, temp + i);
10570#endif
10571 else
10572 temp[i++] = n;
10573 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 rettv->v_type = VAR_STRING;
10575 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010576
10577#ifdef FEAT_MOUSE
10578 if (n == K_LEFTMOUSE
10579 || n == K_LEFTMOUSE_NM
10580 || n == K_LEFTDRAG
10581 || n == K_LEFTRELEASE
10582 || n == K_LEFTRELEASE_NM
10583 || n == K_MIDDLEMOUSE
10584 || n == K_MIDDLEDRAG
10585 || n == K_MIDDLERELEASE
10586 || n == K_RIGHTMOUSE
10587 || n == K_RIGHTDRAG
10588 || n == K_RIGHTRELEASE
10589 || n == K_X1MOUSE
10590 || n == K_X1DRAG
10591 || n == K_X1RELEASE
10592 || n == K_X2MOUSE
10593 || n == K_X2DRAG
10594 || n == K_X2RELEASE
10595 || n == K_MOUSEDOWN
10596 || n == K_MOUSEUP)
10597 {
10598 int row = mouse_row;
10599 int col = mouse_col;
10600 win_T *win;
10601 linenr_T lnum;
10602# ifdef FEAT_WINDOWS
10603 win_T *wp;
10604# endif
10605 int n = 1;
10606
10607 if (row >= 0 && col >= 0)
10608 {
10609 /* Find the window at the mouse coordinates and compute the
10610 * text position. */
10611 win = mouse_find_win(&row, &col);
10612 (void)mouse_comp_pos(win, &row, &col, &lnum);
10613# ifdef FEAT_WINDOWS
10614 for (wp = firstwin; wp != win; wp = wp->w_next)
10615 ++n;
10616# endif
10617 vimvars[VV_MOUSE_WIN].vv_nr = n;
10618 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10619 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10620 }
10621 }
10622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 }
10624}
10625
10626/*
10627 * "getcharmod()" function
10628 */
10629/*ARGSUSED*/
10630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *argvars;
10633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636}
10637
10638/*
10639 * "getcmdline()" function
10640 */
10641/*ARGSUSED*/
10642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643f_getcmdline(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->v_type = VAR_STRING;
10648 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649}
10650
10651/*
10652 * "getcmdpos()" function
10653 */
10654/*ARGSUSED*/
10655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010657 typval_T *argvars;
10658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661}
10662
10663/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010664 * "getcmdtype()" function
10665 */
10666/*ARGSUSED*/
10667 static void
10668f_getcmdtype(argvars, rettv)
10669 typval_T *argvars;
10670 typval_T *rettv;
10671{
10672 rettv->v_type = VAR_STRING;
10673 rettv->vval.v_string = alloc(2);
10674 if (rettv->vval.v_string != NULL)
10675 {
10676 rettv->vval.v_string[0] = get_cmdline_type();
10677 rettv->vval.v_string[1] = NUL;
10678 }
10679}
10680
10681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 * "getcwd()" function
10683 */
10684/*ARGSUSED*/
10685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 typval_T *argvars;
10688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
10690 char_u cwd[MAXPATHL];
10691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 else
10696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010699 if (rettv->vval.v_string != NULL)
10700 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701#endif
10702 }
10703}
10704
10705/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010706 * "getfontname()" function
10707 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010708/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010711 typval_T *argvars;
10712 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 rettv->v_type = VAR_STRING;
10715 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010716#ifdef FEAT_GUI
10717 if (gui.in_use)
10718 {
10719 GuiFont font;
10720 char_u *name = NULL;
10721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010722 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010723 {
10724 /* Get the "Normal" font. Either the name saved by
10725 * hl_set_font_name() or from the font ID. */
10726 font = gui.norm_font;
10727 name = hl_get_font_name();
10728 }
10729 else
10730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010732 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10733 return;
10734 font = gui_mch_get_font(name, FALSE);
10735 if (font == NOFONT)
10736 return; /* Invalid font name, return empty string. */
10737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010739 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010740 gui_mch_free_font(font);
10741 }
10742#endif
10743}
10744
10745/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010746 * "getfperm({fname})" function
10747 */
10748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 typval_T *argvars;
10751 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010752{
10753 char_u *fname;
10754 struct stat st;
10755 char_u *perm = NULL;
10756 char_u flags[] = "rwx";
10757 int i;
10758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010762 if (mch_stat((char *)fname, &st) >= 0)
10763 {
10764 perm = vim_strsave((char_u *)"---------");
10765 if (perm != NULL)
10766 {
10767 for (i = 0; i < 9; i++)
10768 {
10769 if (st.st_mode & (1 << (8 - i)))
10770 perm[i] = flags[i % 3];
10771 }
10772 }
10773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010775}
10776
10777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 * "getfsize({fname})" function
10779 */
10780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010782 typval_T *argvars;
10783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784{
10785 char_u *fname;
10786 struct stat st;
10787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791
10792 if (mch_stat((char *)fname, &st) >= 0)
10793 {
10794 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010795 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010799
10800 /* non-perfect check for overflow */
10801 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10802 rettv->vval.v_number = -2;
10803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 }
10805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807}
10808
10809/*
10810 * "getftime({fname})" function
10811 */
10812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 typval_T *argvars;
10815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
10817 char_u *fname;
10818 struct stat st;
10819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821
10822 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826}
10827
10828/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010829 * "getftype({fname})" function
10830 */
10831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010832f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 typval_T *argvars;
10834 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010835{
10836 char_u *fname;
10837 struct stat st;
10838 char_u *type = NULL;
10839 char *t;
10840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010844 if (mch_lstat((char *)fname, &st) >= 0)
10845 {
10846#ifdef S_ISREG
10847 if (S_ISREG(st.st_mode))
10848 t = "file";
10849 else if (S_ISDIR(st.st_mode))
10850 t = "dir";
10851# ifdef S_ISLNK
10852 else if (S_ISLNK(st.st_mode))
10853 t = "link";
10854# endif
10855# ifdef S_ISBLK
10856 else if (S_ISBLK(st.st_mode))
10857 t = "bdev";
10858# endif
10859# ifdef S_ISCHR
10860 else if (S_ISCHR(st.st_mode))
10861 t = "cdev";
10862# endif
10863# ifdef S_ISFIFO
10864 else if (S_ISFIFO(st.st_mode))
10865 t = "fifo";
10866# endif
10867# ifdef S_ISSOCK
10868 else if (S_ISSOCK(st.st_mode))
10869 t = "fifo";
10870# endif
10871 else
10872 t = "other";
10873#else
10874# ifdef S_IFMT
10875 switch (st.st_mode & S_IFMT)
10876 {
10877 case S_IFREG: t = "file"; break;
10878 case S_IFDIR: t = "dir"; break;
10879# ifdef S_IFLNK
10880 case S_IFLNK: t = "link"; break;
10881# endif
10882# ifdef S_IFBLK
10883 case S_IFBLK: t = "bdev"; break;
10884# endif
10885# ifdef S_IFCHR
10886 case S_IFCHR: t = "cdev"; break;
10887# endif
10888# ifdef S_IFIFO
10889 case S_IFIFO: t = "fifo"; break;
10890# endif
10891# ifdef S_IFSOCK
10892 case S_IFSOCK: t = "socket"; break;
10893# endif
10894 default: t = "other";
10895 }
10896# else
10897 if (mch_isdir(fname))
10898 t = "dir";
10899 else
10900 t = "file";
10901# endif
10902#endif
10903 type = vim_strsave((char_u *)t);
10904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010906}
10907
10908/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010909 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010910 */
10911 static void
10912f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010913 typval_T *argvars;
10914 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010915{
10916 linenr_T lnum;
10917 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010918 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010919
10920 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010921 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010922 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010923 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010924 retlist = FALSE;
10925 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010926 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010927 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010929 retlist = TRUE;
10930 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010931
Bram Moolenaar342337a2005-07-21 21:11:17 +000010932 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010933}
10934
10935/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010936 * "getmatches()" function
10937 */
10938/*ARGSUSED*/
10939 static void
10940f_getmatches(argvars, rettv)
10941 typval_T *argvars;
10942 typval_T *rettv;
10943{
10944#ifdef FEAT_SEARCH_EXTRA
10945 dict_T *dict;
10946 matchitem_T *cur = curwin->w_match_head;
10947
10948 rettv->vval.v_number = 0;
10949
10950 if (rettv_list_alloc(rettv) == OK)
10951 {
10952 while (cur != NULL)
10953 {
10954 dict = dict_alloc();
10955 if (dict == NULL)
10956 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010957 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10958 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10959 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10960 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10961 list_append_dict(rettv->vval.v_list, dict);
10962 cur = cur->next;
10963 }
10964 }
10965#endif
10966}
10967
10968/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010969 * "getpid()" function
10970 */
10971/*ARGSUSED*/
10972 static void
10973f_getpid(argvars, rettv)
10974 typval_T *argvars;
10975 typval_T *rettv;
10976{
10977 rettv->vval.v_number = mch_get_pid();
10978}
10979
10980/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010981 * "getpos(string)" function
10982 */
10983 static void
10984f_getpos(argvars, rettv)
10985 typval_T *argvars;
10986 typval_T *rettv;
10987{
10988 pos_T *fp;
10989 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010990 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010991
10992 if (rettv_list_alloc(rettv) == OK)
10993 {
10994 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010995 fp = var2fpos(&argvars[0], TRUE, &fnum);
10996 if (fnum != -1)
10997 list_append_number(l, (varnumber_T)fnum);
10998 else
10999 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011000 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11001 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011002 list_append_number(l, (fp != NULL)
11003 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011004 : (varnumber_T)0);
11005 list_append_number(l,
11006#ifdef FEAT_VIRTUALEDIT
11007 (fp != NULL) ? (varnumber_T)fp->coladd :
11008#endif
11009 (varnumber_T)0);
11010 }
11011 else
11012 rettv->vval.v_number = FALSE;
11013}
11014
11015/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011016 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011017 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000011018/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000011019 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011020f_getqflist(argvars, rettv)
11021 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011022 typval_T *rettv;
11023{
11024#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011025 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011026#endif
11027
Bram Moolenaar77f66d62007-02-20 02:16:18 +000011028 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011029#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011030 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011031 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011032 wp = NULL;
11033 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11034 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011035 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011036 if (wp == NULL)
11037 return;
11038 }
11039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011040 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011041 }
11042#endif
11043}
11044
11045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 * "getreg()" function
11047 */
11048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011050 typval_T *argvars;
11051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052{
11053 char_u *strregname;
11054 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011055 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011058 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 strregname = get_tv_string_chk(&argvars[0]);
11061 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011062 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011066 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 regname = (strregname == NULL ? '"' : *strregname);
11068 if (regname == 0)
11069 regname = '"';
11070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 rettv->vval.v_string = error ? NULL :
11073 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074}
11075
11076/*
11077 * "getregtype()" function
11078 */
11079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011081 typval_T *argvars;
11082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083{
11084 char_u *strregname;
11085 int regname;
11086 char_u buf[NUMBUFLEN + 2];
11087 long reglen = 0;
11088
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011089 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 {
11091 strregname = get_tv_string_chk(&argvars[0]);
11092 if (strregname == NULL) /* type error; errmsg already given */
11093 {
11094 rettv->v_type = VAR_STRING;
11095 rettv->vval.v_string = NULL;
11096 return;
11097 }
11098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 else
11100 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102
11103 regname = (strregname == NULL ? '"' : *strregname);
11104 if (regname == 0)
11105 regname = '"';
11106
11107 buf[0] = NUL;
11108 buf[1] = NUL;
11109 switch (get_reg_type(regname, &reglen))
11110 {
11111 case MLINE: buf[0] = 'V'; break;
11112 case MCHAR: buf[0] = 'v'; break;
11113#ifdef FEAT_VISUAL
11114 case MBLOCK:
11115 buf[0] = Ctrl_V;
11116 sprintf((char *)buf + 1, "%ld", reglen + 1);
11117 break;
11118#endif
11119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 rettv->v_type = VAR_STRING;
11121 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122}
11123
11124/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011125 * "gettabwinvar()" function
11126 */
11127 static void
11128f_gettabwinvar(argvars, rettv)
11129 typval_T *argvars;
11130 typval_T *rettv;
11131{
11132 getwinvar(argvars, rettv, 1);
11133}
11134
11135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 * "getwinposx()" function
11137 */
11138/*ARGSUSED*/
11139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *argvars;
11142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145#ifdef FEAT_GUI
11146 if (gui.in_use)
11147 {
11148 int x, y;
11149
11150 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 }
11153#endif
11154}
11155
11156/*
11157 * "getwinposy()" function
11158 */
11159/*ARGSUSED*/
11160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 typval_T *argvars;
11163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166#ifdef FEAT_GUI
11167 if (gui.in_use)
11168 {
11169 int x, y;
11170
11171 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 }
11174#endif
11175}
11176
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011177/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011178 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011179 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011180 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011181find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011182 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011183 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011184{
11185#ifdef FEAT_WINDOWS
11186 win_T *wp;
11187#endif
11188 int nr;
11189
11190 nr = get_tv_number_chk(vp, NULL);
11191
11192#ifdef FEAT_WINDOWS
11193 if (nr < 0)
11194 return NULL;
11195 if (nr == 0)
11196 return curwin;
11197
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011198 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11199 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011200 if (--nr <= 0)
11201 break;
11202 return wp;
11203#else
11204 if (nr == 0 || nr == 1)
11205 return curwin;
11206 return NULL;
11207#endif
11208}
11209
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210/*
11211 * "getwinvar()" function
11212 */
11213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011215 typval_T *argvars;
11216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011218 getwinvar(argvars, rettv, 0);
11219}
11220
11221/*
11222 * getwinvar() and gettabwinvar()
11223 */
11224 static void
11225getwinvar(argvars, rettv, off)
11226 typval_T *argvars;
11227 typval_T *rettv;
11228 int off; /* 1 for gettabwinvar() */
11229{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 win_T *win, *oldcurwin;
11231 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011233 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011235#ifdef FEAT_WINDOWS
11236 if (off == 1)
11237 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11238 else
11239 tp = curtab;
11240#endif
11241 win = find_win_by_nr(&argvars[off], tp);
11242 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011243 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->v_type = VAR_STRING;
11246 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247
11248 if (win != NULL && varname != NULL)
11249 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011250 /* Set curwin to be our win, temporarily. Also set curbuf, so
11251 * that we can get buffer-local options. */
11252 oldcurwin = curwin;
11253 curwin = win;
11254 curbuf = win->w_buffer;
11255
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 else
11259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011260 if (*varname == NUL)
11261 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11262 * scope prefix before the NUL byte is required by
11263 * find_var_in_ht(). */
11264 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011266 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011268 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011270
11271 /* restore previous notion of curwin */
11272 curwin = oldcurwin;
11273 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 }
11275
11276 --emsg_off;
11277}
11278
11279/*
11280 * "glob()" function
11281 */
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 expand_T xpc;
11288
11289 ExpandInit(&xpc);
11290 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291 rettv->v_type = VAR_STRING;
11292 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294}
11295
11296/*
11297 * "globpath()" function
11298 */
11299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011301 typval_T *argvars;
11302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303{
11304 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 if (file == NULL)
11309 rettv->vval.v_string = NULL;
11310 else
11311 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312}
11313
11314/*
11315 * "has()" function
11316 */
11317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
11322 int i;
11323 char_u *name;
11324 int n = FALSE;
11325 static char *(has_list[]) =
11326 {
11327#ifdef AMIGA
11328 "amiga",
11329# ifdef FEAT_ARP
11330 "arp",
11331# endif
11332#endif
11333#ifdef __BEOS__
11334 "beos",
11335#endif
11336#ifdef MSDOS
11337# ifdef DJGPP
11338 "dos32",
11339# else
11340 "dos16",
11341# endif
11342#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011343#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 "mac",
11345#endif
11346#if defined(MACOS_X_UNIX)
11347 "macunix",
11348#endif
11349#ifdef OS2
11350 "os2",
11351#endif
11352#ifdef __QNX__
11353 "qnx",
11354#endif
11355#ifdef RISCOS
11356 "riscos",
11357#endif
11358#ifdef UNIX
11359 "unix",
11360#endif
11361#ifdef VMS
11362 "vms",
11363#endif
11364#ifdef WIN16
11365 "win16",
11366#endif
11367#ifdef WIN32
11368 "win32",
11369#endif
11370#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11371 "win32unix",
11372#endif
11373#ifdef WIN64
11374 "win64",
11375#endif
11376#ifdef EBCDIC
11377 "ebcdic",
11378#endif
11379#ifndef CASE_INSENSITIVE_FILENAME
11380 "fname_case",
11381#endif
11382#ifdef FEAT_ARABIC
11383 "arabic",
11384#endif
11385#ifdef FEAT_AUTOCMD
11386 "autocmd",
11387#endif
11388#ifdef FEAT_BEVAL
11389 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011390# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11391 "balloon_multiline",
11392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393#endif
11394#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11395 "builtin_terms",
11396# ifdef ALL_BUILTIN_TCAPS
11397 "all_builtin_terms",
11398# endif
11399#endif
11400#ifdef FEAT_BYTEOFF
11401 "byte_offset",
11402#endif
11403#ifdef FEAT_CINDENT
11404 "cindent",
11405#endif
11406#ifdef FEAT_CLIENTSERVER
11407 "clientserver",
11408#endif
11409#ifdef FEAT_CLIPBOARD
11410 "clipboard",
11411#endif
11412#ifdef FEAT_CMDL_COMPL
11413 "cmdline_compl",
11414#endif
11415#ifdef FEAT_CMDHIST
11416 "cmdline_hist",
11417#endif
11418#ifdef FEAT_COMMENTS
11419 "comments",
11420#endif
11421#ifdef FEAT_CRYPT
11422 "cryptv",
11423#endif
11424#ifdef FEAT_CSCOPE
11425 "cscope",
11426#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011427#ifdef CURSOR_SHAPE
11428 "cursorshape",
11429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430#ifdef DEBUG
11431 "debug",
11432#endif
11433#ifdef FEAT_CON_DIALOG
11434 "dialog_con",
11435#endif
11436#ifdef FEAT_GUI_DIALOG
11437 "dialog_gui",
11438#endif
11439#ifdef FEAT_DIFF
11440 "diff",
11441#endif
11442#ifdef FEAT_DIGRAPHS
11443 "digraphs",
11444#endif
11445#ifdef FEAT_DND
11446 "dnd",
11447#endif
11448#ifdef FEAT_EMACS_TAGS
11449 "emacs_tags",
11450#endif
11451 "eval", /* always present, of course! */
11452#ifdef FEAT_EX_EXTRA
11453 "ex_extra",
11454#endif
11455#ifdef FEAT_SEARCH_EXTRA
11456 "extra_search",
11457#endif
11458#ifdef FEAT_FKMAP
11459 "farsi",
11460#endif
11461#ifdef FEAT_SEARCHPATH
11462 "file_in_path",
11463#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011464#if defined(UNIX) && !defined(USE_SYSTEM)
11465 "filterpipe",
11466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467#ifdef FEAT_FIND_ID
11468 "find_in_path",
11469#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011470#ifdef FEAT_FLOAT
11471 "float",
11472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473#ifdef FEAT_FOLDING
11474 "folding",
11475#endif
11476#ifdef FEAT_FOOTER
11477 "footer",
11478#endif
11479#if !defined(USE_SYSTEM) && defined(UNIX)
11480 "fork",
11481#endif
11482#ifdef FEAT_GETTEXT
11483 "gettext",
11484#endif
11485#ifdef FEAT_GUI
11486 "gui",
11487#endif
11488#ifdef FEAT_GUI_ATHENA
11489# ifdef FEAT_GUI_NEXTAW
11490 "gui_neXtaw",
11491# else
11492 "gui_athena",
11493# endif
11494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495#ifdef FEAT_GUI_GTK
11496 "gui_gtk",
11497# ifdef HAVE_GTK2
11498 "gui_gtk2",
11499# endif
11500#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011501#ifdef FEAT_GUI_GNOME
11502 "gui_gnome",
11503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504#ifdef FEAT_GUI_MAC
11505 "gui_mac",
11506#endif
11507#ifdef FEAT_GUI_MOTIF
11508 "gui_motif",
11509#endif
11510#ifdef FEAT_GUI_PHOTON
11511 "gui_photon",
11512#endif
11513#ifdef FEAT_GUI_W16
11514 "gui_win16",
11515#endif
11516#ifdef FEAT_GUI_W32
11517 "gui_win32",
11518#endif
11519#ifdef FEAT_HANGULIN
11520 "hangul_input",
11521#endif
11522#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11523 "iconv",
11524#endif
11525#ifdef FEAT_INS_EXPAND
11526 "insert_expand",
11527#endif
11528#ifdef FEAT_JUMPLIST
11529 "jumplist",
11530#endif
11531#ifdef FEAT_KEYMAP
11532 "keymap",
11533#endif
11534#ifdef FEAT_LANGMAP
11535 "langmap",
11536#endif
11537#ifdef FEAT_LIBCALL
11538 "libcall",
11539#endif
11540#ifdef FEAT_LINEBREAK
11541 "linebreak",
11542#endif
11543#ifdef FEAT_LISP
11544 "lispindent",
11545#endif
11546#ifdef FEAT_LISTCMDS
11547 "listcmds",
11548#endif
11549#ifdef FEAT_LOCALMAP
11550 "localmap",
11551#endif
11552#ifdef FEAT_MENU
11553 "menu",
11554#endif
11555#ifdef FEAT_SESSION
11556 "mksession",
11557#endif
11558#ifdef FEAT_MODIFY_FNAME
11559 "modify_fname",
11560#endif
11561#ifdef FEAT_MOUSE
11562 "mouse",
11563#endif
11564#ifdef FEAT_MOUSESHAPE
11565 "mouseshape",
11566#endif
11567#if defined(UNIX) || defined(VMS)
11568# ifdef FEAT_MOUSE_DEC
11569 "mouse_dec",
11570# endif
11571# ifdef FEAT_MOUSE_GPM
11572 "mouse_gpm",
11573# endif
11574# ifdef FEAT_MOUSE_JSB
11575 "mouse_jsbterm",
11576# endif
11577# ifdef FEAT_MOUSE_NET
11578 "mouse_netterm",
11579# endif
11580# ifdef FEAT_MOUSE_PTERM
11581 "mouse_pterm",
11582# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011583# ifdef FEAT_SYSMOUSE
11584 "mouse_sysmouse",
11585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586# ifdef FEAT_MOUSE_XTERM
11587 "mouse_xterm",
11588# endif
11589#endif
11590#ifdef FEAT_MBYTE
11591 "multi_byte",
11592#endif
11593#ifdef FEAT_MBYTE_IME
11594 "multi_byte_ime",
11595#endif
11596#ifdef FEAT_MULTI_LANG
11597 "multi_lang",
11598#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011599#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011600#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011601 "mzscheme",
11602#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604#ifdef FEAT_OLE
11605 "ole",
11606#endif
11607#ifdef FEAT_OSFILETYPE
11608 "osfiletype",
11609#endif
11610#ifdef FEAT_PATH_EXTRA
11611 "path_extra",
11612#endif
11613#ifdef FEAT_PERL
11614#ifndef DYNAMIC_PERL
11615 "perl",
11616#endif
11617#endif
11618#ifdef FEAT_PYTHON
11619#ifndef DYNAMIC_PYTHON
11620 "python",
11621#endif
11622#endif
11623#ifdef FEAT_POSTSCRIPT
11624 "postscript",
11625#endif
11626#ifdef FEAT_PRINTER
11627 "printer",
11628#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011629#ifdef FEAT_PROFILE
11630 "profile",
11631#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011632#ifdef FEAT_RELTIME
11633 "reltime",
11634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635#ifdef FEAT_QUICKFIX
11636 "quickfix",
11637#endif
11638#ifdef FEAT_RIGHTLEFT
11639 "rightleft",
11640#endif
11641#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11642 "ruby",
11643#endif
11644#ifdef FEAT_SCROLLBIND
11645 "scrollbind",
11646#endif
11647#ifdef FEAT_CMDL_INFO
11648 "showcmd",
11649 "cmdline_info",
11650#endif
11651#ifdef FEAT_SIGNS
11652 "signs",
11653#endif
11654#ifdef FEAT_SMARTINDENT
11655 "smartindent",
11656#endif
11657#ifdef FEAT_SNIFF
11658 "sniff",
11659#endif
11660#ifdef FEAT_STL_OPT
11661 "statusline",
11662#endif
11663#ifdef FEAT_SUN_WORKSHOP
11664 "sun_workshop",
11665#endif
11666#ifdef FEAT_NETBEANS_INTG
11667 "netbeans_intg",
11668#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011669#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011670 "spell",
11671#endif
11672#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 "syntax",
11674#endif
11675#if defined(USE_SYSTEM) || !defined(UNIX)
11676 "system",
11677#endif
11678#ifdef FEAT_TAG_BINS
11679 "tag_binary",
11680#endif
11681#ifdef FEAT_TAG_OLDSTATIC
11682 "tag_old_static",
11683#endif
11684#ifdef FEAT_TAG_ANYWHITE
11685 "tag_any_white",
11686#endif
11687#ifdef FEAT_TCL
11688# ifndef DYNAMIC_TCL
11689 "tcl",
11690# endif
11691#endif
11692#ifdef TERMINFO
11693 "terminfo",
11694#endif
11695#ifdef FEAT_TERMRESPONSE
11696 "termresponse",
11697#endif
11698#ifdef FEAT_TEXTOBJ
11699 "textobjects",
11700#endif
11701#ifdef HAVE_TGETENT
11702 "tgetent",
11703#endif
11704#ifdef FEAT_TITLE
11705 "title",
11706#endif
11707#ifdef FEAT_TOOLBAR
11708 "toolbar",
11709#endif
11710#ifdef FEAT_USR_CMDS
11711 "user-commands", /* was accidentally included in 5.4 */
11712 "user_commands",
11713#endif
11714#ifdef FEAT_VIMINFO
11715 "viminfo",
11716#endif
11717#ifdef FEAT_VERTSPLIT
11718 "vertsplit",
11719#endif
11720#ifdef FEAT_VIRTUALEDIT
11721 "virtualedit",
11722#endif
11723#ifdef FEAT_VISUAL
11724 "visual",
11725#endif
11726#ifdef FEAT_VISUALEXTRA
11727 "visualextra",
11728#endif
11729#ifdef FEAT_VREPLACE
11730 "vreplace",
11731#endif
11732#ifdef FEAT_WILDIGN
11733 "wildignore",
11734#endif
11735#ifdef FEAT_WILDMENU
11736 "wildmenu",
11737#endif
11738#ifdef FEAT_WINDOWS
11739 "windows",
11740#endif
11741#ifdef FEAT_WAK
11742 "winaltkeys",
11743#endif
11744#ifdef FEAT_WRITEBACKUP
11745 "writebackup",
11746#endif
11747#ifdef FEAT_XIM
11748 "xim",
11749#endif
11750#ifdef FEAT_XFONTSET
11751 "xfontset",
11752#endif
11753#ifdef USE_XSMP
11754 "xsmp",
11755#endif
11756#ifdef USE_XSMP_INTERACT
11757 "xsmp_interact",
11758#endif
11759#ifdef FEAT_XCLIPBOARD
11760 "xterm_clipboard",
11761#endif
11762#ifdef FEAT_XTERM_SAVE
11763 "xterm_save",
11764#endif
11765#if defined(UNIX) && defined(FEAT_X11)
11766 "X11",
11767#endif
11768 NULL
11769 };
11770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 for (i = 0; has_list[i] != NULL; ++i)
11773 if (STRICMP(name, has_list[i]) == 0)
11774 {
11775 n = TRUE;
11776 break;
11777 }
11778
11779 if (n == FALSE)
11780 {
11781 if (STRNICMP(name, "patch", 5) == 0)
11782 n = has_patch(atoi((char *)name + 5));
11783 else if (STRICMP(name, "vim_starting") == 0)
11784 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011785#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11786 else if (STRICMP(name, "balloon_multiline") == 0)
11787 n = multiline_balloon_available();
11788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789#ifdef DYNAMIC_TCL
11790 else if (STRICMP(name, "tcl") == 0)
11791 n = tcl_enabled(FALSE);
11792#endif
11793#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11794 else if (STRICMP(name, "iconv") == 0)
11795 n = iconv_enabled(FALSE);
11796#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011797#ifdef DYNAMIC_MZSCHEME
11798 else if (STRICMP(name, "mzscheme") == 0)
11799 n = mzscheme_enabled(FALSE);
11800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801#ifdef DYNAMIC_RUBY
11802 else if (STRICMP(name, "ruby") == 0)
11803 n = ruby_enabled(FALSE);
11804#endif
11805#ifdef DYNAMIC_PYTHON
11806 else if (STRICMP(name, "python") == 0)
11807 n = python_enabled(FALSE);
11808#endif
11809#ifdef DYNAMIC_PERL
11810 else if (STRICMP(name, "perl") == 0)
11811 n = perl_enabled(FALSE);
11812#endif
11813#ifdef FEAT_GUI
11814 else if (STRICMP(name, "gui_running") == 0)
11815 n = (gui.in_use || gui.starting);
11816# ifdef FEAT_GUI_W32
11817 else if (STRICMP(name, "gui_win32s") == 0)
11818 n = gui_is_win32s();
11819# endif
11820# ifdef FEAT_BROWSE
11821 else if (STRICMP(name, "browse") == 0)
11822 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11823# endif
11824#endif
11825#ifdef FEAT_SYN_HL
11826 else if (STRICMP(name, "syntax_items") == 0)
11827 n = syntax_present(curbuf);
11828#endif
11829#if defined(WIN3264)
11830 else if (STRICMP(name, "win95") == 0)
11831 n = mch_windows95();
11832#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011833#ifdef FEAT_NETBEANS_INTG
11834 else if (STRICMP(name, "netbeans_enabled") == 0)
11835 n = usingNetbeans;
11836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 }
11838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840}
11841
11842/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 * "has_key()" function
11844 */
11845 static void
11846f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *argvars;
11848 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011849{
11850 rettv->vval.v_number = 0;
11851 if (argvars[0].v_type != VAR_DICT)
11852 {
11853 EMSG(_(e_dictreq));
11854 return;
11855 }
11856 if (argvars[0].vval.v_dict == NULL)
11857 return;
11858
11859 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011860 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011861}
11862
11863/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011864 * "haslocaldir()" function
11865 */
11866/*ARGSUSED*/
11867 static void
11868f_haslocaldir(argvars, rettv)
11869 typval_T *argvars;
11870 typval_T *rettv;
11871{
11872 rettv->vval.v_number = (curwin->w_localdir != NULL);
11873}
11874
11875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 * "hasmapto()" function
11877 */
11878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 typval_T *argvars;
11881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882{
11883 char_u *name;
11884 char_u *mode;
11885 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011886 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011889 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 mode = (char_u *)"nvo";
11891 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011894 if (argvars[2].v_type != VAR_UNKNOWN)
11895 abbr = get_tv_number(&argvars[2]);
11896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897
Bram Moolenaar2c932302006-03-18 21:42:09 +000011898 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902}
11903
11904/*
11905 * "histadd()" function
11906 */
11907/*ARGSUSED*/
11908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011910 typval_T *argvars;
11911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912{
11913#ifdef FEAT_CMDHIST
11914 int histype;
11915 char_u *str;
11916 char_u buf[NUMBUFLEN];
11917#endif
11918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 if (check_restricted() || check_secure())
11921 return;
11922#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011923 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11924 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 if (histype >= 0)
11926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 if (*str != NUL)
11929 {
11930 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 return;
11933 }
11934 }
11935#endif
11936}
11937
11938/*
11939 * "histdel()" function
11940 */
11941/*ARGSUSED*/
11942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 typval_T *argvars;
11945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946{
11947#ifdef FEAT_CMDHIST
11948 int n;
11949 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011950 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011952 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11953 if (str == NULL)
11954 n = 0;
11955 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011958 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011960 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 else
11963 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 get_tv_string_buf(&argvars[1], buf));
11966 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969#endif
11970}
11971
11972/*
11973 * "histget()" function
11974 */
11975/*ARGSUSED*/
11976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011978 typval_T *argvars;
11979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980{
11981#ifdef FEAT_CMDHIST
11982 int type;
11983 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11987 if (str == NULL)
11988 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 {
11991 type = get_histtype(str);
11992 if (argvars[1].v_type == VAR_UNKNOWN)
11993 idx = get_history_idx(type);
11994 else
11995 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11996 /* -1 on type error */
11997 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003}
12004
12005/*
12006 * "histnr()" function
12007 */
12008/*ARGSUSED*/
12009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T *argvars;
12012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013{
12014 int i;
12015
12016#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012017 char_u *history = get_tv_string_chk(&argvars[0]);
12018
12019 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (i >= HIST_CMD && i < HIST_COUNT)
12021 i = get_history_idx(i);
12022 else
12023#endif
12024 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026}
12027
12028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 * "highlightID(name)" function
12030 */
12031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012033 typval_T *argvars;
12034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037}
12038
12039/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040 * "highlight_exists()" function
12041 */
12042 static void
12043f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 typval_T *argvars;
12045 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012046{
12047 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12048}
12049
12050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 * "hostname()" function
12052 */
12053/*ARGSUSED*/
12054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012056 typval_T *argvars;
12057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058{
12059 char_u hostname[256];
12060
12061 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->v_type = VAR_STRING;
12063 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
12067 * iconv() function
12068 */
12069/*ARGSUSED*/
12070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012071f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012072 typval_T *argvars;
12073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074{
12075#ifdef FEAT_MBYTE
12076 char_u buf1[NUMBUFLEN];
12077 char_u buf2[NUMBUFLEN];
12078 char_u *from, *to, *str;
12079 vimconv_T vimconv;
12080#endif
12081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->v_type = VAR_STRING;
12083 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084
12085#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086 str = get_tv_string(&argvars[0]);
12087 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12088 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 vimconv.vc_type = CONV_NONE;
12090 convert_setup(&vimconv, from, to);
12091
12092 /* If the encodings are equal, no conversion needed. */
12093 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097
12098 convert_setup(&vimconv, NULL, NULL);
12099 vim_free(from);
12100 vim_free(to);
12101#endif
12102}
12103
12104/*
12105 * "indent()" function
12106 */
12107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 typval_T *argvars;
12110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111{
12112 linenr_T lnum;
12113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119}
12120
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012121/*
12122 * "index()" function
12123 */
12124 static void
12125f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 typval_T *argvars;
12127 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012128{
Bram Moolenaar33570922005-01-25 22:26:29 +000012129 list_T *l;
12130 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012131 long idx = 0;
12132 int ic = FALSE;
12133
12134 rettv->vval.v_number = -1;
12135 if (argvars[0].v_type != VAR_LIST)
12136 {
12137 EMSG(_(e_listreq));
12138 return;
12139 }
12140 l = argvars[0].vval.v_list;
12141 if (l != NULL)
12142 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012143 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012144 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 int error = FALSE;
12147
Bram Moolenaar758711c2005-02-02 23:11:38 +000012148 /* Start at specified item. Use the cached index that list_find()
12149 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012150 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012151 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012152 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 ic = get_tv_number_chk(&argvars[3], &error);
12154 if (error)
12155 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012156 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012157
Bram Moolenaar758711c2005-02-02 23:11:38 +000012158 for ( ; item != NULL; item = item->li_next, ++idx)
12159 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012160 {
12161 rettv->vval.v_number = idx;
12162 break;
12163 }
12164 }
12165}
12166
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167static int inputsecret_flag = 0;
12168
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012169static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12170
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012172 * This function is used by f_input() and f_inputdialog() functions. The third
12173 * argument to f_input() specifies the type of completion to use at the
12174 * prompt. The third argument to f_inputdialog() specifies the value to return
12175 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 */
12177 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012178get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012179 typval_T *argvars;
12180 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012181 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012183 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184 char_u *p = NULL;
12185 int c;
12186 char_u buf[NUMBUFLEN];
12187 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012189 int xp_type = EXPAND_NOTHING;
12190 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012193 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194
12195#ifdef NO_CONSOLE_INPUT
12196 /* While starting up, there is no place to enter text. */
12197 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199#endif
12200
12201 cmd_silent = FALSE; /* Want to see the prompt. */
12202 if (prompt != NULL)
12203 {
12204 /* Only the part of the message after the last NL is considered as
12205 * prompt for the command line */
12206 p = vim_strrchr(prompt, '\n');
12207 if (p == NULL)
12208 p = prompt;
12209 else
12210 {
12211 ++p;
12212 c = *p;
12213 *p = NUL;
12214 msg_start();
12215 msg_clr_eos();
12216 msg_puts_attr(prompt, echo_attr);
12217 msg_didout = FALSE;
12218 msg_starthere();
12219 *p = c;
12220 }
12221 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223 if (argvars[1].v_type != VAR_UNKNOWN)
12224 {
12225 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12226 if (defstr != NULL)
12227 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012229 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012230 {
12231 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012232 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012233 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012234
Bram Moolenaar4463f292005-09-25 22:20:24 +000012235 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012236
Bram Moolenaar4463f292005-09-25 22:20:24 +000012237 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12238 if (xp_name == NULL)
12239 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012240
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012241 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012242
Bram Moolenaar4463f292005-09-25 22:20:24 +000012243 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12244 &xp_arg) == FAIL)
12245 return;
12246 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012247 }
12248
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012249 if (defstr != NULL)
12250 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012251 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12252 xp_type, xp_arg);
12253
12254 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012256 /* since the user typed this, no need to wait for return */
12257 need_wait_return = FALSE;
12258 msg_didout = FALSE;
12259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 cmd_silent = cmd_silent_save;
12261}
12262
12263/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012264 * "input()" function
12265 * Also handles inputsecret() when inputsecret is set.
12266 */
12267 static void
12268f_input(argvars, rettv)
12269 typval_T *argvars;
12270 typval_T *rettv;
12271{
12272 get_user_input(argvars, rettv, FALSE);
12273}
12274
12275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 * "inputdialog()" function
12277 */
12278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012280 typval_T *argvars;
12281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282{
12283#if defined(FEAT_GUI_TEXTDIALOG)
12284 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12285 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12286 {
12287 char_u *message;
12288 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012289 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 message = get_tv_string_chk(&argvars[0]);
12292 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012293 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012294 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 else
12296 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012297 if (message != NULL && defstr != NULL
12298 && do_dialog(VIM_QUESTION, NULL, message,
12299 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 else
12302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012303 if (message != NULL && defstr != NULL
12304 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012305 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012306 rettv->vval.v_string = vim_strsave(
12307 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012309 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 }
12313 else
12314#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012315 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316}
12317
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012318/*
12319 * "inputlist()" function
12320 */
12321 static void
12322f_inputlist(argvars, rettv)
12323 typval_T *argvars;
12324 typval_T *rettv;
12325{
12326 listitem_T *li;
12327 int selected;
12328 int mouse_used;
12329
12330 rettv->vval.v_number = 0;
12331#ifdef NO_CONSOLE_INPUT
12332 /* While starting up, there is no place to enter text. */
12333 if (no_console_input())
12334 return;
12335#endif
12336 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12337 {
12338 EMSG2(_(e_listarg), "inputlist()");
12339 return;
12340 }
12341
12342 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012343 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012344 lines_left = Rows; /* avoid more prompt */
12345 msg_scroll = TRUE;
12346 msg_clr_eos();
12347
12348 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12349 {
12350 msg_puts(get_tv_string(&li->li_tv));
12351 msg_putchar('\n');
12352 }
12353
12354 /* Ask for choice. */
12355 selected = prompt_for_number(&mouse_used);
12356 if (mouse_used)
12357 selected -= lines_left;
12358
12359 rettv->vval.v_number = selected;
12360}
12361
12362
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12364
12365/*
12366 * "inputrestore()" function
12367 */
12368/*ARGSUSED*/
12369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012371 typval_T *argvars;
12372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373{
12374 if (ga_userinput.ga_len > 0)
12375 {
12376 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12378 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 }
12381 else if (p_verbose > 1)
12382 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012383 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 }
12386}
12387
12388/*
12389 * "inputsave()" function
12390 */
12391/*ARGSUSED*/
12392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012394 typval_T *argvars;
12395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012397 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 if (ga_grow(&ga_userinput, 1) == OK)
12399 {
12400 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12401 + ga_userinput.ga_len);
12402 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 }
12405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407}
12408
12409/*
12410 * "inputsecret()" function
12411 */
12412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012414 typval_T *argvars;
12415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416{
12417 ++cmdline_star;
12418 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 --cmdline_star;
12421 --inputsecret_flag;
12422}
12423
12424/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012425 * "insert()" function
12426 */
12427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012429 typval_T *argvars;
12430 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012431{
12432 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012433 listitem_T *item;
12434 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012436
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012437 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012438 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012439 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012440 else if ((l = argvars[0].vval.v_list) != NULL
12441 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012442 {
12443 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012444 before = get_tv_number_chk(&argvars[2], &error);
12445 if (error)
12446 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012447
Bram Moolenaar758711c2005-02-02 23:11:38 +000012448 if (before == l->lv_len)
12449 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012450 else
12451 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012452 item = list_find(l, before);
12453 if (item == NULL)
12454 {
12455 EMSGN(_(e_listidx), before);
12456 l = NULL;
12457 }
12458 }
12459 if (l != NULL)
12460 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012461 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012462 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012463 }
12464 }
12465}
12466
12467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 * "isdirectory()" function
12469 */
12470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012472 typval_T *argvars;
12473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476}
12477
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012478/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012479 * "islocked()" function
12480 */
12481 static void
12482f_islocked(argvars, rettv)
12483 typval_T *argvars;
12484 typval_T *rettv;
12485{
12486 lval_T lv;
12487 char_u *end;
12488 dictitem_T *di;
12489
12490 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012491 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12492 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012493 if (end != NULL && lv.ll_name != NULL)
12494 {
12495 if (*end != NUL)
12496 EMSG(_(e_trailing));
12497 else
12498 {
12499 if (lv.ll_tv == NULL)
12500 {
12501 if (check_changedtick(lv.ll_name))
12502 rettv->vval.v_number = 1; /* always locked */
12503 else
12504 {
12505 di = find_var(lv.ll_name, NULL);
12506 if (di != NULL)
12507 {
12508 /* Consider a variable locked when:
12509 * 1. the variable itself is locked
12510 * 2. the value of the variable is locked.
12511 * 3. the List or Dict value is locked.
12512 */
12513 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12514 || tv_islocked(&di->di_tv));
12515 }
12516 }
12517 }
12518 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012519 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012520 else if (lv.ll_newkey != NULL)
12521 EMSG2(_(e_dictkey), lv.ll_newkey);
12522 else if (lv.ll_list != NULL)
12523 /* List item. */
12524 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12525 else
12526 /* Dictionary item. */
12527 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12528 }
12529 }
12530
12531 clear_lval(&lv);
12532}
12533
Bram Moolenaar33570922005-01-25 22:26:29 +000012534static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012535
12536/*
12537 * Turn a dict into a list:
12538 * "what" == 0: list of keys
12539 * "what" == 1: list of values
12540 * "what" == 2: list of items
12541 */
12542 static void
12543dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 typval_T *argvars;
12545 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012546 int what;
12547{
Bram Moolenaar33570922005-01-25 22:26:29 +000012548 list_T *l2;
12549 dictitem_T *di;
12550 hashitem_T *hi;
12551 listitem_T *li;
12552 listitem_T *li2;
12553 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012554 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012555
12556 rettv->vval.v_number = 0;
12557 if (argvars[0].v_type != VAR_DICT)
12558 {
12559 EMSG(_(e_dictreq));
12560 return;
12561 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012562 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012563 return;
12564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012565 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012566 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012568 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012571 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012573 --todo;
12574 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012575
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012576 li = listitem_alloc();
12577 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012578 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012579 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012580
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012581 if (what == 0)
12582 {
12583 /* keys() */
12584 li->li_tv.v_type = VAR_STRING;
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_string = vim_strsave(di->di_key);
12587 }
12588 else if (what == 1)
12589 {
12590 /* values() */
12591 copy_tv(&di->di_tv, &li->li_tv);
12592 }
12593 else
12594 {
12595 /* items() */
12596 l2 = list_alloc();
12597 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012598 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012599 li->li_tv.vval.v_list = l2;
12600 if (l2 == NULL)
12601 break;
12602 ++l2->lv_refcount;
12603
12604 li2 = listitem_alloc();
12605 if (li2 == NULL)
12606 break;
12607 list_append(l2, li2);
12608 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012609 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012610 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12611
12612 li2 = listitem_alloc();
12613 if (li2 == NULL)
12614 break;
12615 list_append(l2, li2);
12616 copy_tv(&di->di_tv, &li2->li_tv);
12617 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012618 }
12619 }
12620}
12621
12622/*
12623 * "items(dict)" function
12624 */
12625 static void
12626f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012627 typval_T *argvars;
12628 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012629{
12630 dict_list(argvars, rettv, 2);
12631}
12632
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012634 * "join()" function
12635 */
12636 static void
12637f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T *argvars;
12639 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012640{
12641 garray_T ga;
12642 char_u *sep;
12643
12644 rettv->vval.v_number = 0;
12645 if (argvars[0].v_type != VAR_LIST)
12646 {
12647 EMSG(_(e_listreq));
12648 return;
12649 }
12650 if (argvars[0].vval.v_list == NULL)
12651 return;
12652 if (argvars[1].v_type == VAR_UNKNOWN)
12653 sep = (char_u *)" ";
12654 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012655 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012656
12657 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012658
12659 if (sep != NULL)
12660 {
12661 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012662 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012663 ga_append(&ga, NUL);
12664 rettv->vval.v_string = (char_u *)ga.ga_data;
12665 }
12666 else
12667 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012668}
12669
12670/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012671 * "keys()" function
12672 */
12673 static void
12674f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012675 typval_T *argvars;
12676 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012677{
12678 dict_list(argvars, rettv, 0);
12679}
12680
12681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 * "last_buffer_nr()" function.
12683 */
12684/*ARGSUSED*/
12685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 typval_T *argvars;
12688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689{
12690 int n = 0;
12691 buf_T *buf;
12692
12693 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12694 if (n < buf->b_fnum)
12695 n = buf->b_fnum;
12696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012698}
12699
12700/*
12701 * "len()" function
12702 */
12703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 typval_T *argvars;
12706 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012707{
12708 switch (argvars[0].v_type)
12709 {
12710 case VAR_STRING:
12711 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->vval.v_number = (varnumber_T)STRLEN(
12713 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012714 break;
12715 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012717 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012718 case VAR_DICT:
12719 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12720 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012721 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012722 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012723 break;
12724 }
12725}
12726
Bram Moolenaar33570922005-01-25 22:26:29 +000012727static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012728
12729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012731 typval_T *argvars;
12732 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012733 int type;
12734{
12735#ifdef FEAT_LIBCALL
12736 char_u *string_in;
12737 char_u **string_result;
12738 int nr_result;
12739#endif
12740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012742 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012746
12747 if (check_restricted() || check_secure())
12748 return;
12749
12750#ifdef FEAT_LIBCALL
12751 /* The first two args must be strings, otherwise its meaningless */
12752 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12753 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012754 string_in = NULL;
12755 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012756 string_in = argvars[2].vval.v_string;
12757 if (type == VAR_NUMBER)
12758 string_result = NULL;
12759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012761 if (mch_libcall(argvars[0].vval.v_string,
12762 argvars[1].vval.v_string,
12763 string_in,
12764 argvars[2].vval.v_number,
12765 string_result,
12766 &nr_result) == OK
12767 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012769 }
12770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771}
12772
12773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012774 * "libcall()" function
12775 */
12776 static void
12777f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012778 typval_T *argvars;
12779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012780{
12781 libcall_common(argvars, rettv, VAR_STRING);
12782}
12783
12784/*
12785 * "libcallnr()" function
12786 */
12787 static void
12788f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012789 typval_T *argvars;
12790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012791{
12792 libcall_common(argvars, rettv, VAR_NUMBER);
12793}
12794
12795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 * "line(string)" function
12797 */
12798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012800 typval_T *argvars;
12801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802{
12803 linenr_T lnum = 0;
12804 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012805 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012807 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 if (fp != NULL)
12809 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811}
12812
12813/*
12814 * "line2byte(lnum)" function
12815 */
12816/*ARGSUSED*/
12817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *argvars;
12820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821{
12822#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824#else
12825 linenr_T lnum;
12826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12832 if (rettv->vval.v_number >= 0)
12833 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834#endif
12835}
12836
12837/*
12838 * "lispindent(lnum)" function
12839 */
12840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012842 typval_T *argvars;
12843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844{
12845#ifdef FEAT_LISP
12846 pos_T pos;
12847 linenr_T lnum;
12848
12849 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12852 {
12853 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 curwin->w_cursor = pos;
12856 }
12857 else
12858#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860}
12861
12862/*
12863 * "localtime()" function
12864 */
12865/*ARGSUSED*/
12866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012868 typval_T *argvars;
12869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872}
12873
Bram Moolenaar33570922005-01-25 22:26:29 +000012874static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875
12876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 typval_T *argvars;
12879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 int exact;
12881{
12882 char_u *keys;
12883 char_u *which;
12884 char_u buf[NUMBUFLEN];
12885 char_u *keys_buf = NULL;
12886 char_u *rhs;
12887 int mode;
12888 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012889 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890
12891 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 rettv->v_type = VAR_STRING;
12893 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 if (*keys == NUL)
12897 return;
12898
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012902 if (argvars[2].v_type != VAR_UNKNOWN)
12903 abbr = get_tv_number(&argvars[2]);
12904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 else
12906 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907 if (which == NULL)
12908 return;
12909
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 mode = get_map_mode(&which, 0);
12911
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012912 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012913 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 vim_free(keys_buf);
12915 if (rhs != NULL)
12916 {
12917 ga_init(&ga);
12918 ga.ga_itemsize = 1;
12919 ga.ga_growsize = 40;
12920
12921 while (*rhs != NUL)
12922 ga_concat(&ga, str2special(&rhs, FALSE));
12923
12924 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012925 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926 }
12927}
12928
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012929#ifdef FEAT_FLOAT
12930/*
12931 * "log10()" function
12932 */
12933 static void
12934f_log10(argvars, rettv)
12935 typval_T *argvars;
12936 typval_T *rettv;
12937{
12938 float_T f;
12939
12940 rettv->v_type = VAR_FLOAT;
12941 if (get_float_arg(argvars, &f) == OK)
12942 rettv->vval.v_float = log10(f);
12943 else
12944 rettv->vval.v_float = 0.0;
12945}
12946#endif
12947
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012949 * "map()" function
12950 */
12951 static void
12952f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012953 typval_T *argvars;
12954 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012955{
12956 filter_map(argvars, rettv, TRUE);
12957}
12958
12959/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 */
12962 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012963f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012964 typval_T *argvars;
12965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012967 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968}
12969
12970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972 */
12973 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012975 typval_T *argvars;
12976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012978 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979}
12980
Bram Moolenaar33570922005-01-25 22:26:29 +000012981static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982
12983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012985 typval_T *argvars;
12986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 int type;
12988{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012989 char_u *str = NULL;
12990 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991 char_u *pat;
12992 regmatch_T regmatch;
12993 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012994 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995 char_u *save_cpo;
12996 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012997 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012998 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012999 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013000 list_T *l = NULL;
13001 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013002 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013003 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004
13005 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13006 save_cpo = p_cpo;
13007 p_cpo = (char_u *)"";
13008
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013009 rettv->vval.v_number = -1;
13010 if (type == 3)
13011 {
13012 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013013 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013014 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013015 }
13016 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018 rettv->v_type = VAR_STRING;
13019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013022 if (argvars[0].v_type == VAR_LIST)
13023 {
13024 if ((l = argvars[0].vval.v_list) == NULL)
13025 goto theend;
13026 li = l->lv_first;
13027 }
13028 else
13029 expr = str = get_tv_string(&argvars[0]);
13030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13032 if (pat == NULL)
13033 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013034
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013035 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013037 int error = FALSE;
13038
13039 start = get_tv_number_chk(&argvars[2], &error);
13040 if (error)
13041 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013042 if (l != NULL)
13043 {
13044 li = list_find(l, start);
13045 if (li == NULL)
13046 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013047 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013048 }
13049 else
13050 {
13051 if (start < 0)
13052 start = 0;
13053 if (start > (long)STRLEN(str))
13054 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013055 /* When "count" argument is there ignore matches before "start",
13056 * otherwise skip part of the string. Differs when pattern is "^"
13057 * or "\<". */
13058 if (argvars[3].v_type != VAR_UNKNOWN)
13059 startcol = start;
13060 else
13061 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013062 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013064 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013065 nth = get_tv_number_chk(&argvars[3], &error);
13066 if (error)
13067 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 }
13069
13070 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13071 if (regmatch.regprog != NULL)
13072 {
13073 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013074
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013075 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013076 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013077 if (l != NULL)
13078 {
13079 if (li == NULL)
13080 {
13081 match = FALSE;
13082 break;
13083 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013084 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013085 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013086 if (str == NULL)
13087 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013088 }
13089
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013090 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013091
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013092 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013093 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013094 if (l == NULL && !match)
13095 break;
13096
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013097 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013098 if (l != NULL)
13099 {
13100 li = li->li_next;
13101 ++idx;
13102 }
13103 else
13104 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013105#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013106 startcol = (colnr_T)(regmatch.startp[0]
13107 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013108#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013109 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013110#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013111 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013112 }
13113
13114 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013116 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013117 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013118 int i;
13119
13120 /* return list with matched string and submatches */
13121 for (i = 0; i < NSUBEXP; ++i)
13122 {
13123 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013124 {
13125 if (list_append_string(rettv->vval.v_list,
13126 (char_u *)"", 0) == FAIL)
13127 break;
13128 }
13129 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013130 regmatch.startp[i],
13131 (int)(regmatch.endp[i] - regmatch.startp[i]))
13132 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013133 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013134 }
13135 }
13136 else if (type == 2)
13137 {
13138 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013139 if (l != NULL)
13140 copy_tv(&li->li_tv, rettv);
13141 else
13142 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013144 }
13145 else if (l != NULL)
13146 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 else
13148 {
13149 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013150 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 (varnumber_T)(regmatch.startp[0] - str);
13152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013155 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 }
13157 }
13158 vim_free(regmatch.regprog);
13159 }
13160
13161theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013162 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 p_cpo = save_cpo;
13164}
13165
13166/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013167 * "match()" function
13168 */
13169 static void
13170f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013171 typval_T *argvars;
13172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013173{
13174 find_some_match(argvars, rettv, 1);
13175}
13176
13177/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013178 * "matchadd()" function
13179 */
13180 static void
13181f_matchadd(argvars, rettv)
13182 typval_T *argvars;
13183 typval_T *rettv;
13184{
13185#ifdef FEAT_SEARCH_EXTRA
13186 char_u buf[NUMBUFLEN];
13187 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13188 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13189 int prio = 10; /* default priority */
13190 int id = -1;
13191 int error = FALSE;
13192
13193 rettv->vval.v_number = -1;
13194
13195 if (grp == NULL || pat == NULL)
13196 return;
13197 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013198 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013199 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013200 if (argvars[3].v_type != VAR_UNKNOWN)
13201 id = get_tv_number_chk(&argvars[3], &error);
13202 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013203 if (error == TRUE)
13204 return;
13205 if (id >= 1 && id <= 3)
13206 {
13207 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13208 return;
13209 }
13210
13211 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13212#endif
13213}
13214
13215/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013216 * "matcharg()" function
13217 */
13218 static void
13219f_matcharg(argvars, rettv)
13220 typval_T *argvars;
13221 typval_T *rettv;
13222{
13223 if (rettv_list_alloc(rettv) == OK)
13224 {
13225#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013226 int id = get_tv_number(&argvars[0]);
13227 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013228
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013229 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013230 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013231 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13232 {
13233 list_append_string(rettv->vval.v_list,
13234 syn_id2name(m->hlg_id), -1);
13235 list_append_string(rettv->vval.v_list, m->pattern, -1);
13236 }
13237 else
13238 {
13239 list_append_string(rettv->vval.v_list, NUL, -1);
13240 list_append_string(rettv->vval.v_list, NUL, -1);
13241 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013242 }
13243#endif
13244 }
13245}
13246
13247/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013248 * "matchdelete()" function
13249 */
13250 static void
13251f_matchdelete(argvars, rettv)
13252 typval_T *argvars;
13253 typval_T *rettv;
13254{
13255#ifdef FEAT_SEARCH_EXTRA
13256 rettv->vval.v_number = match_delete(curwin,
13257 (int)get_tv_number(&argvars[0]), TRUE);
13258#endif
13259}
13260
13261/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013262 * "matchend()" function
13263 */
13264 static void
13265f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013266 typval_T *argvars;
13267 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013268{
13269 find_some_match(argvars, rettv, 0);
13270}
13271
13272/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013273 * "matchlist()" function
13274 */
13275 static void
13276f_matchlist(argvars, rettv)
13277 typval_T *argvars;
13278 typval_T *rettv;
13279{
13280 find_some_match(argvars, rettv, 3);
13281}
13282
13283/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013284 * "matchstr()" function
13285 */
13286 static void
13287f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 typval_T *argvars;
13289 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013290{
13291 find_some_match(argvars, rettv, 2);
13292}
13293
Bram Moolenaar33570922005-01-25 22:26:29 +000013294static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013295
13296 static void
13297max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 typval_T *argvars;
13299 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013300 int domax;
13301{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013302 long n = 0;
13303 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013305
13306 if (argvars[0].v_type == VAR_LIST)
13307 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013308 list_T *l;
13309 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013310
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013311 l = argvars[0].vval.v_list;
13312 if (l != NULL)
13313 {
13314 li = l->lv_first;
13315 if (li != NULL)
13316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013318 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013319 {
13320 li = li->li_next;
13321 if (li == NULL)
13322 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013323 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013324 if (domax ? i > n : i < n)
13325 n = i;
13326 }
13327 }
13328 }
13329 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013330 else if (argvars[0].v_type == VAR_DICT)
13331 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013333 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013335 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013336
13337 d = argvars[0].vval.v_dict;
13338 if (d != NULL)
13339 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013340 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013341 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013342 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013343 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013345 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013346 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013347 if (first)
13348 {
13349 n = i;
13350 first = FALSE;
13351 }
13352 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013353 n = i;
13354 }
13355 }
13356 }
13357 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013358 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013359 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013360 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013361}
13362
13363/*
13364 * "max()" function
13365 */
13366 static void
13367f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 typval_T *argvars;
13369 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013370{
13371 max_min(argvars, rettv, TRUE);
13372}
13373
13374/*
13375 * "min()" function
13376 */
13377 static void
13378f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *argvars;
13380 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013381{
13382 max_min(argvars, rettv, FALSE);
13383}
13384
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013385static int mkdir_recurse __ARGS((char_u *dir, int prot));
13386
13387/*
13388 * Create the directory in which "dir" is located, and higher levels when
13389 * needed.
13390 */
13391 static int
13392mkdir_recurse(dir, prot)
13393 char_u *dir;
13394 int prot;
13395{
13396 char_u *p;
13397 char_u *updir;
13398 int r = FAIL;
13399
13400 /* Get end of directory name in "dir".
13401 * We're done when it's "/" or "c:/". */
13402 p = gettail_sep(dir);
13403 if (p <= get_past_head(dir))
13404 return OK;
13405
13406 /* If the directory exists we're done. Otherwise: create it.*/
13407 updir = vim_strnsave(dir, (int)(p - dir));
13408 if (updir == NULL)
13409 return FAIL;
13410 if (mch_isdir(updir))
13411 r = OK;
13412 else if (mkdir_recurse(updir, prot) == OK)
13413 r = vim_mkdir_emsg(updir, prot);
13414 vim_free(updir);
13415 return r;
13416}
13417
13418#ifdef vim_mkdir
13419/*
13420 * "mkdir()" function
13421 */
13422 static void
13423f_mkdir(argvars, rettv)
13424 typval_T *argvars;
13425 typval_T *rettv;
13426{
13427 char_u *dir;
13428 char_u buf[NUMBUFLEN];
13429 int prot = 0755;
13430
13431 rettv->vval.v_number = FAIL;
13432 if (check_restricted() || check_secure())
13433 return;
13434
13435 dir = get_tv_string_buf(&argvars[0], buf);
13436 if (argvars[1].v_type != VAR_UNKNOWN)
13437 {
13438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013439 prot = get_tv_number_chk(&argvars[2], NULL);
13440 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013441 mkdir_recurse(dir, prot);
13442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013443 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013444}
13445#endif
13446
Bram Moolenaar0d660222005-01-07 21:51:51 +000013447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 * "mode()" function
13449 */
13450/*ARGSUSED*/
13451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013456 char_u buf[3];
13457
13458 buf[1] = NUL;
13459 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460
13461#ifdef FEAT_VISUAL
13462 if (VIsual_active)
13463 {
13464 if (VIsual_select)
13465 buf[0] = VIsual_mode + 's' - 'v';
13466 else
13467 buf[0] = VIsual_mode;
13468 }
13469 else
13470#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013471 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13472 || State == CONFIRM)
13473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013475 if (State == ASKMORE)
13476 buf[1] = 'm';
13477 else if (State == CONFIRM)
13478 buf[1] = '?';
13479 }
13480 else if (State == EXTERNCMD)
13481 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 else if (State & INSERT)
13483 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013484#ifdef FEAT_VREPLACE
13485 if (State & VREPLACE_FLAG)
13486 {
13487 buf[0] = 'R';
13488 buf[1] = 'v';
13489 }
13490 else
13491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 if (State & REPLACE_FLAG)
13493 buf[0] = 'R';
13494 else
13495 buf[0] = 'i';
13496 }
13497 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013500 if (exmode_active)
13501 buf[1] = 'v';
13502 }
13503 else if (exmode_active)
13504 {
13505 buf[0] = 'c';
13506 buf[1] = 'e';
13507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013511 if (finish_op)
13512 buf[1] = 'o';
13513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013515 /* Clear out the minor mode when the argument is not a non-zero number or
13516 * non-empty string. */
13517 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013518 buf[1] = NUL;
13519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 rettv->vval.v_string = vim_strsave(buf);
13521 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522}
13523
13524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525 * "nextnonblank()" function
13526 */
13527 static void
13528f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013529 typval_T *argvars;
13530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013531{
13532 linenr_T lnum;
13533
13534 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013537 {
13538 lnum = 0;
13539 break;
13540 }
13541 if (*skipwhite(ml_get(lnum)) != NUL)
13542 break;
13543 }
13544 rettv->vval.v_number = lnum;
13545}
13546
13547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 * "nr2char()" function
13549 */
13550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 typval_T *argvars;
13553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554{
13555 char_u buf[NUMBUFLEN];
13556
13557#ifdef FEAT_MBYTE
13558 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 else
13561#endif
13562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 buf[1] = NUL;
13565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->v_type = VAR_STRING;
13567 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013568}
13569
13570/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013571 * "pathshorten()" function
13572 */
13573 static void
13574f_pathshorten(argvars, rettv)
13575 typval_T *argvars;
13576 typval_T *rettv;
13577{
13578 char_u *p;
13579
13580 rettv->v_type = VAR_STRING;
13581 p = get_tv_string_chk(&argvars[0]);
13582 if (p == NULL)
13583 rettv->vval.v_string = NULL;
13584 else
13585 {
13586 p = vim_strsave(p);
13587 rettv->vval.v_string = p;
13588 if (p != NULL)
13589 shorten_dir(p);
13590 }
13591}
13592
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013593#ifdef FEAT_FLOAT
13594/*
13595 * "pow()" function
13596 */
13597 static void
13598f_pow(argvars, rettv)
13599 typval_T *argvars;
13600 typval_T *rettv;
13601{
13602 float_T fx, fy;
13603
13604 rettv->v_type = VAR_FLOAT;
13605 if (get_float_arg(argvars, &fx) == OK
13606 && get_float_arg(&argvars[1], &fy) == OK)
13607 rettv->vval.v_float = pow(fx, fy);
13608 else
13609 rettv->vval.v_float = 0.0;
13610}
13611#endif
13612
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013613/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013614 * "prevnonblank()" function
13615 */
13616 static void
13617f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013618 typval_T *argvars;
13619 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013620{
13621 linenr_T lnum;
13622
13623 lnum = get_tv_lnum(argvars);
13624 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13625 lnum = 0;
13626 else
13627 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13628 --lnum;
13629 rettv->vval.v_number = lnum;
13630}
13631
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013632#ifdef HAVE_STDARG_H
13633/* This dummy va_list is here because:
13634 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13635 * - locally in the function results in a "used before set" warning
13636 * - using va_start() to initialize it gives "function with fixed args" error */
13637static va_list ap;
13638#endif
13639
Bram Moolenaar8c711452005-01-14 21:53:12 +000013640/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013641 * "printf()" function
13642 */
13643 static void
13644f_printf(argvars, rettv)
13645 typval_T *argvars;
13646 typval_T *rettv;
13647{
13648 rettv->v_type = VAR_STRING;
13649 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013650#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013651 {
13652 char_u buf[NUMBUFLEN];
13653 int len;
13654 char_u *s;
13655 int saved_did_emsg = did_emsg;
13656 char *fmt;
13657
13658 /* Get the required length, allocate the buffer and do it for real. */
13659 did_emsg = FALSE;
13660 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013661 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013662 if (!did_emsg)
13663 {
13664 s = alloc(len + 1);
13665 if (s != NULL)
13666 {
13667 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013668 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013669 }
13670 }
13671 did_emsg |= saved_did_emsg;
13672 }
13673#endif
13674}
13675
13676/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013677 * "pumvisible()" function
13678 */
13679/*ARGSUSED*/
13680 static void
13681f_pumvisible(argvars, rettv)
13682 typval_T *argvars;
13683 typval_T *rettv;
13684{
13685 rettv->vval.v_number = 0;
13686#ifdef FEAT_INS_EXPAND
13687 if (pum_visible())
13688 rettv->vval.v_number = 1;
13689#endif
13690}
13691
13692/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013693 * "range()" function
13694 */
13695 static void
13696f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013697 typval_T *argvars;
13698 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013699{
13700 long start;
13701 long end;
13702 long stride = 1;
13703 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013707 if (argvars[1].v_type == VAR_UNKNOWN)
13708 {
13709 end = start - 1;
13710 start = 0;
13711 }
13712 else
13713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013714 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013717 }
13718
13719 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013720 if (error)
13721 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013722 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013723 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013724 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013725 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013726 else
13727 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013728 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013729 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013730 if (list_append_number(rettv->vval.v_list,
13731 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013732 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013733 }
13734}
13735
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013736/*
13737 * "readfile()" function
13738 */
13739 static void
13740f_readfile(argvars, rettv)
13741 typval_T *argvars;
13742 typval_T *rettv;
13743{
13744 int binary = FALSE;
13745 char_u *fname;
13746 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013747 listitem_T *li;
13748#define FREAD_SIZE 200 /* optimized for text lines */
13749 char_u buf[FREAD_SIZE];
13750 int readlen; /* size of last fread() */
13751 int buflen; /* nr of valid chars in buf[] */
13752 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13753 int tolist; /* first byte in buf[] still to be put in list */
13754 int chop; /* how many CR to chop off */
13755 char_u *prev = NULL; /* previously read bytes, if any */
13756 int prevlen = 0; /* length of "prev" if not NULL */
13757 char_u *s;
13758 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013759 long maxline = MAXLNUM;
13760 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013761
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013762 if (argvars[1].v_type != VAR_UNKNOWN)
13763 {
13764 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13765 binary = TRUE;
13766 if (argvars[2].v_type != VAR_UNKNOWN)
13767 maxline = get_tv_number(&argvars[2]);
13768 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013769
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013770 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013771 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013772
13773 /* Always open the file in binary mode, library functions have a mind of
13774 * their own about CR-LF conversion. */
13775 fname = get_tv_string(&argvars[0]);
13776 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13777 {
13778 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13779 return;
13780 }
13781
13782 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013783 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013784 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013785 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013786 buflen = filtd + readlen;
13787 tolist = 0;
13788 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13789 {
13790 if (buf[filtd] == '\n' || readlen <= 0)
13791 {
13792 /* Only when in binary mode add an empty list item when the
13793 * last line ends in a '\n'. */
13794 if (!binary && readlen == 0 && filtd == 0)
13795 break;
13796
13797 /* Found end-of-line or end-of-file: add a text line to the
13798 * list. */
13799 chop = 0;
13800 if (!binary)
13801 while (filtd - chop - 1 >= tolist
13802 && buf[filtd - chop - 1] == '\r')
13803 ++chop;
13804 len = filtd - tolist - chop;
13805 if (prev == NULL)
13806 s = vim_strnsave(buf + tolist, len);
13807 else
13808 {
13809 s = alloc((unsigned)(prevlen + len + 1));
13810 if (s != NULL)
13811 {
13812 mch_memmove(s, prev, prevlen);
13813 vim_free(prev);
13814 prev = NULL;
13815 mch_memmove(s + prevlen, buf + tolist, len);
13816 s[prevlen + len] = NUL;
13817 }
13818 }
13819 tolist = filtd + 1;
13820
13821 li = listitem_alloc();
13822 if (li == NULL)
13823 {
13824 vim_free(s);
13825 break;
13826 }
13827 li->li_tv.v_type = VAR_STRING;
13828 li->li_tv.v_lock = 0;
13829 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013830 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013831
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013832 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013833 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013834 if (readlen <= 0)
13835 break;
13836 }
13837 else if (buf[filtd] == NUL)
13838 buf[filtd] = '\n';
13839 }
13840 if (readlen <= 0)
13841 break;
13842
13843 if (tolist == 0)
13844 {
13845 /* "buf" is full, need to move text to an allocated buffer */
13846 if (prev == NULL)
13847 {
13848 prev = vim_strnsave(buf, buflen);
13849 prevlen = buflen;
13850 }
13851 else
13852 {
13853 s = alloc((unsigned)(prevlen + buflen));
13854 if (s != NULL)
13855 {
13856 mch_memmove(s, prev, prevlen);
13857 mch_memmove(s + prevlen, buf, buflen);
13858 vim_free(prev);
13859 prev = s;
13860 prevlen += buflen;
13861 }
13862 }
13863 filtd = 0;
13864 }
13865 else
13866 {
13867 mch_memmove(buf, buf + tolist, buflen - tolist);
13868 filtd -= tolist;
13869 }
13870 }
13871
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013872 /*
13873 * For a negative line count use only the lines at the end of the file,
13874 * free the rest.
13875 */
13876 if (maxline < 0)
13877 while (cnt > -maxline)
13878 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013879 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013880 --cnt;
13881 }
13882
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013883 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013884 fclose(fd);
13885}
13886
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013887#if defined(FEAT_RELTIME)
13888static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13889
13890/*
13891 * Convert a List to proftime_T.
13892 * Return FAIL when there is something wrong.
13893 */
13894 static int
13895list2proftime(arg, tm)
13896 typval_T *arg;
13897 proftime_T *tm;
13898{
13899 long n1, n2;
13900 int error = FALSE;
13901
13902 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13903 || arg->vval.v_list->lv_len != 2)
13904 return FAIL;
13905 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13906 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13907# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013908 tm->HighPart = n1;
13909 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013910# else
13911 tm->tv_sec = n1;
13912 tm->tv_usec = n2;
13913# endif
13914 return error ? FAIL : OK;
13915}
13916#endif /* FEAT_RELTIME */
13917
13918/*
13919 * "reltime()" function
13920 */
13921 static void
13922f_reltime(argvars, rettv)
13923 typval_T *argvars;
13924 typval_T *rettv;
13925{
13926#ifdef FEAT_RELTIME
13927 proftime_T res;
13928 proftime_T start;
13929
13930 if (argvars[0].v_type == VAR_UNKNOWN)
13931 {
13932 /* No arguments: get current time. */
13933 profile_start(&res);
13934 }
13935 else if (argvars[1].v_type == VAR_UNKNOWN)
13936 {
13937 if (list2proftime(&argvars[0], &res) == FAIL)
13938 return;
13939 profile_end(&res);
13940 }
13941 else
13942 {
13943 /* Two arguments: compute the difference. */
13944 if (list2proftime(&argvars[0], &start) == FAIL
13945 || list2proftime(&argvars[1], &res) == FAIL)
13946 return;
13947 profile_sub(&res, &start);
13948 }
13949
13950 if (rettv_list_alloc(rettv) == OK)
13951 {
13952 long n1, n2;
13953
13954# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013955 n1 = res.HighPart;
13956 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013957# else
13958 n1 = res.tv_sec;
13959 n2 = res.tv_usec;
13960# endif
13961 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13962 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13963 }
13964#endif
13965}
13966
13967/*
13968 * "reltimestr()" function
13969 */
13970 static void
13971f_reltimestr(argvars, rettv)
13972 typval_T *argvars;
13973 typval_T *rettv;
13974{
13975#ifdef FEAT_RELTIME
13976 proftime_T tm;
13977#endif
13978
13979 rettv->v_type = VAR_STRING;
13980 rettv->vval.v_string = NULL;
13981#ifdef FEAT_RELTIME
13982 if (list2proftime(&argvars[0], &tm) == OK)
13983 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13984#endif
13985}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013986
Bram Moolenaar0d660222005-01-07 21:51:51 +000013987#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13988static void make_connection __ARGS((void));
13989static int check_connection __ARGS((void));
13990
13991 static void
13992make_connection()
13993{
13994 if (X_DISPLAY == NULL
13995# ifdef FEAT_GUI
13996 && !gui.in_use
13997# endif
13998 )
13999 {
14000 x_force_connect = TRUE;
14001 setup_term_clip();
14002 x_force_connect = FALSE;
14003 }
14004}
14005
14006 static int
14007check_connection()
14008{
14009 make_connection();
14010 if (X_DISPLAY == NULL)
14011 {
14012 EMSG(_("E240: No connection to Vim server"));
14013 return FAIL;
14014 }
14015 return OK;
14016}
14017#endif
14018
14019#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014020static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014021
14022 static void
14023remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014024 typval_T *argvars;
14025 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014026 int expr;
14027{
14028 char_u *server_name;
14029 char_u *keys;
14030 char_u *r = NULL;
14031 char_u buf[NUMBUFLEN];
14032# ifdef WIN32
14033 HWND w;
14034# else
14035 Window w;
14036# endif
14037
14038 if (check_restricted() || check_secure())
14039 return;
14040
14041# ifdef FEAT_X11
14042 if (check_connection() == FAIL)
14043 return;
14044# endif
14045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 server_name = get_tv_string_chk(&argvars[0]);
14047 if (server_name == NULL)
14048 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014049 keys = get_tv_string_buf(&argvars[1], buf);
14050# ifdef WIN32
14051 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14052# else
14053 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14054 < 0)
14055# endif
14056 {
14057 if (r != NULL)
14058 EMSG(r); /* sending worked but evaluation failed */
14059 else
14060 EMSG2(_("E241: Unable to send to %s"), server_name);
14061 return;
14062 }
14063
14064 rettv->vval.v_string = r;
14065
14066 if (argvars[2].v_type != VAR_UNKNOWN)
14067 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014068 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014069 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014072 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014073 v.di_tv.v_type = VAR_STRING;
14074 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075 idvar = get_tv_string_chk(&argvars[2]);
14076 if (idvar != NULL)
14077 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014078 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014079 }
14080}
14081#endif
14082
14083/*
14084 * "remote_expr()" function
14085 */
14086/*ARGSUSED*/
14087 static void
14088f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014089 typval_T *argvars;
14090 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091{
14092 rettv->v_type = VAR_STRING;
14093 rettv->vval.v_string = NULL;
14094#ifdef FEAT_CLIENTSERVER
14095 remote_common(argvars, rettv, TRUE);
14096#endif
14097}
14098
14099/*
14100 * "remote_foreground()" function
14101 */
14102/*ARGSUSED*/
14103 static void
14104f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 typval_T *argvars;
14106 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107{
14108 rettv->vval.v_number = 0;
14109#ifdef FEAT_CLIENTSERVER
14110# ifdef WIN32
14111 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 {
14113 char_u *server_name = get_tv_string_chk(&argvars[0]);
14114
14115 if (server_name != NULL)
14116 serverForeground(server_name);
14117 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118# else
14119 /* Send a foreground() expression to the server. */
14120 argvars[1].v_type = VAR_STRING;
14121 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14122 argvars[2].v_type = VAR_UNKNOWN;
14123 remote_common(argvars, rettv, TRUE);
14124 vim_free(argvars[1].vval.v_string);
14125# endif
14126#endif
14127}
14128
14129/*ARGSUSED*/
14130 static void
14131f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 typval_T *argvars;
14133 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014134{
14135#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014136 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014137 char_u *s = NULL;
14138# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014139 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014140# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014142
14143 if (check_restricted() || check_secure())
14144 {
14145 rettv->vval.v_number = -1;
14146 return;
14147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 serverid = get_tv_string_chk(&argvars[0]);
14149 if (serverid == NULL)
14150 {
14151 rettv->vval.v_number = -1;
14152 return; /* type error; errmsg already given */
14153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014154# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014155 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156 if (n == 0)
14157 rettv->vval.v_number = -1;
14158 else
14159 {
14160 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14161 rettv->vval.v_number = (s != NULL);
14162 }
14163# else
14164 rettv->vval.v_number = 0;
14165 if (check_connection() == FAIL)
14166 return;
14167
14168 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014170# endif
14171
14172 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 char_u *retvar;
14175
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 v.di_tv.v_type = VAR_STRING;
14177 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 retvar = get_tv_string_chk(&argvars[1]);
14179 if (retvar != NULL)
14180 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014181 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014182 }
14183#else
14184 rettv->vval.v_number = -1;
14185#endif
14186}
14187
14188/*ARGSUSED*/
14189 static void
14190f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193{
14194 char_u *r = NULL;
14195
14196#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 char_u *serverid = get_tv_string_chk(&argvars[0]);
14198
14199 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014200 {
14201# ifdef WIN32
14202 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014203 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014204
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014205 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014206 if (n != 0)
14207 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14208 if (r == NULL)
14209# else
14210 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014212# endif
14213 EMSG(_("E277: Unable to read a server reply"));
14214 }
14215#endif
14216 rettv->v_type = VAR_STRING;
14217 rettv->vval.v_string = r;
14218}
14219
14220/*
14221 * "remote_send()" function
14222 */
14223/*ARGSUSED*/
14224 static void
14225f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 typval_T *argvars;
14227 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014228{
14229 rettv->v_type = VAR_STRING;
14230 rettv->vval.v_string = NULL;
14231#ifdef FEAT_CLIENTSERVER
14232 remote_common(argvars, rettv, FALSE);
14233#endif
14234}
14235
14236/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014237 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014238 */
14239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014241 typval_T *argvars;
14242 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014243{
Bram Moolenaar33570922005-01-25 22:26:29 +000014244 list_T *l;
14245 listitem_T *item, *item2;
14246 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014247 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014248 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014249 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 dict_T *d;
14251 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014252
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014253 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014254 if (argvars[0].v_type == VAR_DICT)
14255 {
14256 if (argvars[2].v_type != VAR_UNKNOWN)
14257 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014258 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014259 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 key = get_tv_string_chk(&argvars[1]);
14262 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264 di = dict_find(d, key, -1);
14265 if (di == NULL)
14266 EMSG2(_(e_dictkey), key);
14267 else
14268 {
14269 *rettv = di->di_tv;
14270 init_tv(&di->di_tv);
14271 dictitem_remove(d, di);
14272 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014273 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014274 }
14275 }
14276 else if (argvars[0].v_type != VAR_LIST)
14277 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014278 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014279 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014281 int error = FALSE;
14282
14283 idx = get_tv_number_chk(&argvars[1], &error);
14284 if (error)
14285 ; /* type error: do nothing, errmsg already given */
14286 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014287 EMSGN(_(e_listidx), idx);
14288 else
14289 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014290 if (argvars[2].v_type == VAR_UNKNOWN)
14291 {
14292 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014293 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014294 *rettv = item->li_tv;
14295 vim_free(item);
14296 }
14297 else
14298 {
14299 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 end = get_tv_number_chk(&argvars[2], &error);
14301 if (error)
14302 ; /* type error: do nothing */
14303 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014304 EMSGN(_(e_listidx), end);
14305 else
14306 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014307 int cnt = 0;
14308
14309 for (li = item; li != NULL; li = li->li_next)
14310 {
14311 ++cnt;
14312 if (li == item2)
14313 break;
14314 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014315 if (li == NULL) /* didn't find "item2" after "item" */
14316 EMSG(_(e_invrange));
14317 else
14318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014319 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014320 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014321 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014322 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014323 l->lv_first = item;
14324 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014325 item->li_prev = NULL;
14326 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014327 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014328 }
14329 }
14330 }
14331 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014332 }
14333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334}
14335
14336/*
14337 * "rename({from}, {to})" function
14338 */
14339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014341 typval_T *argvars;
14342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343{
14344 char_u buf[NUMBUFLEN];
14345
14346 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014349 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14350 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351}
14352
14353/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014354 * "repeat()" function
14355 */
14356/*ARGSUSED*/
14357 static void
14358f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *argvars;
14360 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014361{
14362 char_u *p;
14363 int n;
14364 int slen;
14365 int len;
14366 char_u *r;
14367 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014368
14369 n = get_tv_number(&argvars[1]);
14370 if (argvars[0].v_type == VAR_LIST)
14371 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014372 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014373 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014374 if (list_extend(rettv->vval.v_list,
14375 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014376 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014377 }
14378 else
14379 {
14380 p = get_tv_string(&argvars[0]);
14381 rettv->v_type = VAR_STRING;
14382 rettv->vval.v_string = NULL;
14383
14384 slen = (int)STRLEN(p);
14385 len = slen * n;
14386 if (len <= 0)
14387 return;
14388
14389 r = alloc(len + 1);
14390 if (r != NULL)
14391 {
14392 for (i = 0; i < n; i++)
14393 mch_memmove(r + i * slen, p, (size_t)slen);
14394 r[len] = NUL;
14395 }
14396
14397 rettv->vval.v_string = r;
14398 }
14399}
14400
14401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 * "resolve()" function
14403 */
14404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014406 typval_T *argvars;
14407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408{
14409 char_u *p;
14410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412#ifdef FEAT_SHORTCUT
14413 {
14414 char_u *v = NULL;
14415
14416 v = mch_resolve_shortcut(p);
14417 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014420 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 }
14422#else
14423# ifdef HAVE_READLINK
14424 {
14425 char_u buf[MAXPATHL + 1];
14426 char_u *cpy;
14427 int len;
14428 char_u *remain = NULL;
14429 char_u *q;
14430 int is_relative_to_current = FALSE;
14431 int has_trailing_pathsep = FALSE;
14432 int limit = 100;
14433
14434 p = vim_strsave(p);
14435
14436 if (p[0] == '.' && (vim_ispathsep(p[1])
14437 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14438 is_relative_to_current = TRUE;
14439
14440 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014441 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442 has_trailing_pathsep = TRUE;
14443
14444 q = getnextcomp(p);
14445 if (*q != NUL)
14446 {
14447 /* Separate the first path component in "p", and keep the
14448 * remainder (beginning with the path separator). */
14449 remain = vim_strsave(q - 1);
14450 q[-1] = NUL;
14451 }
14452
14453 for (;;)
14454 {
14455 for (;;)
14456 {
14457 len = readlink((char *)p, (char *)buf, MAXPATHL);
14458 if (len <= 0)
14459 break;
14460 buf[len] = NUL;
14461
14462 if (limit-- == 0)
14463 {
14464 vim_free(p);
14465 vim_free(remain);
14466 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 goto fail;
14469 }
14470
14471 /* Ensure that the result will have a trailing path separator
14472 * if the argument has one. */
14473 if (remain == NULL && has_trailing_pathsep)
14474 add_pathsep(buf);
14475
14476 /* Separate the first path component in the link value and
14477 * concatenate the remainders. */
14478 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14479 if (*q != NUL)
14480 {
14481 if (remain == NULL)
14482 remain = vim_strsave(q - 1);
14483 else
14484 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014485 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 if (cpy != NULL)
14487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 vim_free(remain);
14489 remain = cpy;
14490 }
14491 }
14492 q[-1] = NUL;
14493 }
14494
14495 q = gettail(p);
14496 if (q > p && *q == NUL)
14497 {
14498 /* Ignore trailing path separator. */
14499 q[-1] = NUL;
14500 q = gettail(p);
14501 }
14502 if (q > p && !mch_isFullName(buf))
14503 {
14504 /* symlink is relative to directory of argument */
14505 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14506 if (cpy != NULL)
14507 {
14508 STRCPY(cpy, p);
14509 STRCPY(gettail(cpy), buf);
14510 vim_free(p);
14511 p = cpy;
14512 }
14513 }
14514 else
14515 {
14516 vim_free(p);
14517 p = vim_strsave(buf);
14518 }
14519 }
14520
14521 if (remain == NULL)
14522 break;
14523
14524 /* Append the first path component of "remain" to "p". */
14525 q = getnextcomp(remain + 1);
14526 len = q - remain - (*q != NUL);
14527 cpy = vim_strnsave(p, STRLEN(p) + len);
14528 if (cpy != NULL)
14529 {
14530 STRNCAT(cpy, remain, len);
14531 vim_free(p);
14532 p = cpy;
14533 }
14534 /* Shorten "remain". */
14535 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014536 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 else
14538 {
14539 vim_free(remain);
14540 remain = NULL;
14541 }
14542 }
14543
14544 /* If the result is a relative path name, make it explicitly relative to
14545 * the current directory if and only if the argument had this form. */
14546 if (!vim_ispathsep(*p))
14547 {
14548 if (is_relative_to_current
14549 && *p != NUL
14550 && !(p[0] == '.'
14551 && (p[1] == NUL
14552 || vim_ispathsep(p[1])
14553 || (p[1] == '.'
14554 && (p[2] == NUL
14555 || vim_ispathsep(p[2]))))))
14556 {
14557 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014558 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 if (cpy != NULL)
14560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 vim_free(p);
14562 p = cpy;
14563 }
14564 }
14565 else if (!is_relative_to_current)
14566 {
14567 /* Strip leading "./". */
14568 q = p;
14569 while (q[0] == '.' && vim_ispathsep(q[1]))
14570 q += 2;
14571 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014572 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 }
14574 }
14575
14576 /* Ensure that the result will have no trailing path separator
14577 * if the argument had none. But keep "/" or "//". */
14578 if (!has_trailing_pathsep)
14579 {
14580 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014581 if (after_pathsep(p, q))
14582 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 }
14584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 }
14587# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589# endif
14590#endif
14591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593
14594#ifdef HAVE_READLINK
14595fail:
14596#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598}
14599
14600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 */
14603 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014605 typval_T *argvars;
14606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607{
Bram Moolenaar33570922005-01-25 22:26:29 +000014608 list_T *l;
14609 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611 rettv->vval.v_number = 0;
14612 if (argvars[0].v_type != VAR_LIST)
14613 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014614 else if ((l = argvars[0].vval.v_list) != NULL
14615 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616 {
14617 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014618 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014619 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620 while (li != NULL)
14621 {
14622 ni = li->li_prev;
14623 list_append(l, li);
14624 li = ni;
14625 }
14626 rettv->vval.v_list = l;
14627 rettv->v_type = VAR_LIST;
14628 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014629 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631}
14632
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014633#define SP_NOMOVE 0x01 /* don't move cursor */
14634#define SP_REPEAT 0x02 /* repeat to find outer pair */
14635#define SP_RETCOUNT 0x04 /* return matchcount */
14636#define SP_SETPCMARK 0x08 /* set previous context mark */
14637#define SP_START 0x10 /* accept match at start position */
14638#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14639#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014640
Bram Moolenaar33570922005-01-25 22:26:29 +000014641static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642
14643/*
14644 * Get flags for a search function.
14645 * Possibly sets "p_ws".
14646 * Returns BACKWARD, FORWARD or zero (for an error).
14647 */
14648 static int
14649get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014651 int *flagsp;
14652{
14653 int dir = FORWARD;
14654 char_u *flags;
14655 char_u nbuf[NUMBUFLEN];
14656 int mask;
14657
14658 if (varp->v_type != VAR_UNKNOWN)
14659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 flags = get_tv_string_buf_chk(varp, nbuf);
14661 if (flags == NULL)
14662 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014663 while (*flags != NUL)
14664 {
14665 switch (*flags)
14666 {
14667 case 'b': dir = BACKWARD; break;
14668 case 'w': p_ws = TRUE; break;
14669 case 'W': p_ws = FALSE; break;
14670 default: mask = 0;
14671 if (flagsp != NULL)
14672 switch (*flags)
14673 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014674 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014675 case 'e': mask = SP_END; break;
14676 case 'm': mask = SP_RETCOUNT; break;
14677 case 'n': mask = SP_NOMOVE; break;
14678 case 'p': mask = SP_SUBPAT; break;
14679 case 'r': mask = SP_REPEAT; break;
14680 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681 }
14682 if (mask == 0)
14683 {
14684 EMSG2(_(e_invarg2), flags);
14685 dir = 0;
14686 }
14687 else
14688 *flagsp |= mask;
14689 }
14690 if (dir == 0)
14691 break;
14692 ++flags;
14693 }
14694 }
14695 return dir;
14696}
14697
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014699 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014701 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014702search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014703 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014704 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014705 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014707 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 char_u *pat;
14709 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014710 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 int save_p_ws = p_ws;
14712 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014713 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014714 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014715 proftime_T tm;
14716#ifdef FEAT_RELTIME
14717 long time_limit = 0;
14718#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014719 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014720 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014723 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014724 if (dir == 0)
14725 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014726 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014727 if (flags & SP_START)
14728 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014729 if (flags & SP_END)
14730 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014731
Bram Moolenaar76929292008-01-06 19:07:36 +000014732 /* Optional arguments: line number to stop searching and timeout. */
14733 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014734 {
14735 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14736 if (lnum_stop < 0)
14737 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014738#ifdef FEAT_RELTIME
14739 if (argvars[3].v_type != VAR_UNKNOWN)
14740 {
14741 time_limit = get_tv_number_chk(&argvars[3], NULL);
14742 if (time_limit < 0)
14743 goto theend;
14744 }
14745#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014746 }
14747
Bram Moolenaar76929292008-01-06 19:07:36 +000014748#ifdef FEAT_RELTIME
14749 /* Set the time limit, if there is one. */
14750 profile_setlimit(time_limit, &tm);
14751#endif
14752
Bram Moolenaar231334e2005-07-25 20:46:57 +000014753 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014754 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014755 * Check to make sure only those flags are set.
14756 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14757 * flags cannot be set. Check for that condition also.
14758 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014759 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014760 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014762 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014763 goto theend;
14764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014766 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014767 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014768 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014769 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014771 if (flags & SP_SUBPAT)
14772 retval = subpatnum;
14773 else
14774 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014775 if (flags & SP_SETPCMARK)
14776 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014778 if (match_pos != NULL)
14779 {
14780 /* Store the match cursor position */
14781 match_pos->lnum = pos.lnum;
14782 match_pos->col = pos.col + 1;
14783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 /* "/$" will put the cursor after the end of the line, may need to
14785 * correct that here */
14786 check_cursor();
14787 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014788
14789 /* If 'n' flag is used: restore cursor position. */
14790 if (flags & SP_NOMOVE)
14791 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014792 else
14793 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014794theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014796
14797 return retval;
14798}
14799
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014800#ifdef FEAT_FLOAT
14801/*
14802 * "round({float})" function
14803 */
14804 static void
14805f_round(argvars, rettv)
14806 typval_T *argvars;
14807 typval_T *rettv;
14808{
14809 float_T f;
14810
14811 rettv->v_type = VAR_FLOAT;
14812 if (get_float_arg(argvars, &f) == OK)
14813 /* round() is not in C90, use ceil() or floor() instead. */
14814 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14815 else
14816 rettv->vval.v_float = 0.0;
14817}
14818#endif
14819
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014820/*
14821 * "search()" function
14822 */
14823 static void
14824f_search(argvars, rettv)
14825 typval_T *argvars;
14826 typval_T *rettv;
14827{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014828 int flags = 0;
14829
14830 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831}
14832
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014834 * "searchdecl()" function
14835 */
14836 static void
14837f_searchdecl(argvars, rettv)
14838 typval_T *argvars;
14839 typval_T *rettv;
14840{
14841 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014842 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014843 int error = FALSE;
14844 char_u *name;
14845
14846 rettv->vval.v_number = 1; /* default: FAIL */
14847
14848 name = get_tv_string_chk(&argvars[0]);
14849 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014850 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014851 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014852 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14853 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14854 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014855 if (!error && name != NULL)
14856 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014857 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014858}
14859
14860/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014861 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014863 static int
14864searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014865 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014866 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867{
14868 char_u *spat, *mpat, *epat;
14869 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871 int dir;
14872 int flags = 0;
14873 char_u nbuf1[NUMBUFLEN];
14874 char_u nbuf2[NUMBUFLEN];
14875 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014876 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014877 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014878 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014881 spat = get_tv_string_chk(&argvars[0]);
14882 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14883 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14884 if (spat == NULL || mpat == NULL || epat == NULL)
14885 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 /* Handle the optional fourth argument: flags */
14888 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014889 if (dir == 0)
14890 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014891
14892 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014893 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14894 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014895 if ((flags & (SP_END | SP_SUBPAT)) != 0
14896 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014897 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014898 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014899 goto theend;
14900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014902 /* Using 'r' implies 'W', otherwise it doesn't work. */
14903 if (flags & SP_REPEAT)
14904 p_ws = FALSE;
14905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014906 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014907 if (argvars[3].v_type == VAR_UNKNOWN
14908 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 skip = (char_u *)"";
14910 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014912 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014913 if (argvars[5].v_type != VAR_UNKNOWN)
14914 {
14915 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14916 if (lnum_stop < 0)
14917 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014918#ifdef FEAT_RELTIME
14919 if (argvars[6].v_type != VAR_UNKNOWN)
14920 {
14921 time_limit = get_tv_number_chk(&argvars[6], NULL);
14922 if (time_limit < 0)
14923 goto theend;
14924 }
14925#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014926 }
14927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014928 if (skip == NULL)
14929 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014931 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014932 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014933
14934theend:
14935 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014936
14937 return retval;
14938}
14939
14940/*
14941 * "searchpair()" function
14942 */
14943 static void
14944f_searchpair(argvars, rettv)
14945 typval_T *argvars;
14946 typval_T *rettv;
14947{
14948 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14949}
14950
14951/*
14952 * "searchpairpos()" function
14953 */
14954 static void
14955f_searchpairpos(argvars, rettv)
14956 typval_T *argvars;
14957 typval_T *rettv;
14958{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014959 pos_T match_pos;
14960 int lnum = 0;
14961 int col = 0;
14962
14963 rettv->vval.v_number = 0;
14964
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014965 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014966 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014967
14968 if (searchpair_cmn(argvars, &match_pos) > 0)
14969 {
14970 lnum = match_pos.lnum;
14971 col = match_pos.col;
14972 }
14973
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014974 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14975 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014976}
14977
14978/*
14979 * Search for a start/middle/end thing.
14980 * Used by searchpair(), see its documentation for the details.
14981 * Returns 0 or -1 for no match,
14982 */
14983 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014984do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14985 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014986 char_u *spat; /* start pattern */
14987 char_u *mpat; /* middle pattern */
14988 char_u *epat; /* end pattern */
14989 int dir; /* BACKWARD or FORWARD */
14990 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014991 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014992 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014993 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014994 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014995{
14996 char_u *save_cpo;
14997 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14998 long retval = 0;
14999 pos_T pos;
15000 pos_T firstpos;
15001 pos_T foundpos;
15002 pos_T save_cursor;
15003 pos_T save_pos;
15004 int n;
15005 int r;
15006 int nest = 1;
15007 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015008 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015009 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015010
15011 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15012 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015013 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015014
Bram Moolenaar76929292008-01-06 19:07:36 +000015015#ifdef FEAT_RELTIME
15016 /* Set the time limit, if there is one. */
15017 profile_setlimit(time_limit, &tm);
15018#endif
15019
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015020 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15021 * start/middle/end (pat3, for the top pair). */
15022 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15023 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15024 if (pat2 == NULL || pat3 == NULL)
15025 goto theend;
15026 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15027 if (*mpat == NUL)
15028 STRCPY(pat3, pat2);
15029 else
15030 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15031 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015032 if (flags & SP_START)
15033 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015034
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 save_cursor = curwin->w_cursor;
15036 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015037 clearpos(&firstpos);
15038 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 pat = pat3;
15040 for (;;)
15041 {
15042 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015043 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15045 /* didn't find it or found the first match again: FAIL */
15046 break;
15047
15048 if (firstpos.lnum == 0)
15049 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015050 if (equalpos(pos, foundpos))
15051 {
15052 /* Found the same position again. Can happen with a pattern that
15053 * has "\zs" at the end and searching backwards. Advance one
15054 * character and try again. */
15055 if (dir == BACKWARD)
15056 decl(&pos);
15057 else
15058 incl(&pos);
15059 }
15060 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015062 /* clear the start flag to avoid getting stuck here */
15063 options &= ~SEARCH_START;
15064
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 /* If the skip pattern matches, ignore this match. */
15066 if (*skip != NUL)
15067 {
15068 save_pos = curwin->w_cursor;
15069 curwin->w_cursor = pos;
15070 r = eval_to_bool(skip, &err, NULL, FALSE);
15071 curwin->w_cursor = save_pos;
15072 if (err)
15073 {
15074 /* Evaluating {skip} caused an error, break here. */
15075 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015076 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 break;
15078 }
15079 if (r)
15080 continue;
15081 }
15082
15083 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15084 {
15085 /* Found end when searching backwards or start when searching
15086 * forward: nested pair. */
15087 ++nest;
15088 pat = pat2; /* nested, don't search for middle */
15089 }
15090 else
15091 {
15092 /* Found end when searching forward or start when searching
15093 * backward: end of (nested) pair; or found middle in outer pair. */
15094 if (--nest == 1)
15095 pat = pat3; /* outer level, search for middle */
15096 }
15097
15098 if (nest == 0)
15099 {
15100 /* Found the match: return matchcount or line number. */
15101 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015102 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015104 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015105 if (flags & SP_SETPCMARK)
15106 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 curwin->w_cursor = pos;
15108 if (!(flags & SP_REPEAT))
15109 break;
15110 nest = 1; /* search for next unmatched */
15111 }
15112 }
15113
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015114 if (match_pos != NULL)
15115 {
15116 /* Store the match cursor position */
15117 match_pos->lnum = curwin->w_cursor.lnum;
15118 match_pos->col = curwin->w_cursor.col + 1;
15119 }
15120
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015122 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123 curwin->w_cursor = save_cursor;
15124
15125theend:
15126 vim_free(pat2);
15127 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015128 if (p_cpo == empty_option)
15129 p_cpo = save_cpo;
15130 else
15131 /* Darn, evaluating the {skip} expression changed the value. */
15132 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015133
15134 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135}
15136
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015137/*
15138 * "searchpos()" function
15139 */
15140 static void
15141f_searchpos(argvars, rettv)
15142 typval_T *argvars;
15143 typval_T *rettv;
15144{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015145 pos_T match_pos;
15146 int lnum = 0;
15147 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015148 int n;
15149 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015150
15151 rettv->vval.v_number = 0;
15152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015153 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015154 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015155
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015156 n = search_cmn(argvars, &match_pos, &flags);
15157 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015158 {
15159 lnum = match_pos.lnum;
15160 col = match_pos.col;
15161 }
15162
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015163 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15164 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015165 if (flags & SP_SUBPAT)
15166 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015167}
15168
15169
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170/*ARGSUSED*/
15171 static void
15172f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015173 typval_T *argvars;
15174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176#ifdef FEAT_CLIENTSERVER
15177 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015178 char_u *server = get_tv_string_chk(&argvars[0]);
15179 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 if (server == NULL || reply == NULL)
15183 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184 if (check_restricted() || check_secure())
15185 return;
15186# ifdef FEAT_X11
15187 if (check_connection() == FAIL)
15188 return;
15189# endif
15190
15191 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193 EMSG(_("E258: Unable to send to client"));
15194 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015196 rettv->vval.v_number = 0;
15197#else
15198 rettv->vval.v_number = -1;
15199#endif
15200}
15201
15202/*ARGSUSED*/
15203 static void
15204f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 typval_T *argvars;
15206 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207{
15208 char_u *r = NULL;
15209
15210#ifdef FEAT_CLIENTSERVER
15211# ifdef WIN32
15212 r = serverGetVimNames();
15213# else
15214 make_connection();
15215 if (X_DISPLAY != NULL)
15216 r = serverGetVimNames(X_DISPLAY);
15217# endif
15218#endif
15219 rettv->v_type = VAR_STRING;
15220 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221}
15222
15223/*
15224 * "setbufvar()" function
15225 */
15226/*ARGSUSED*/
15227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015228f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015229 typval_T *argvars;
15230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231{
15232 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 char_u nbuf[NUMBUFLEN];
15237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 rettv->vval.v_number = 0;
15239
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 if (check_restricted() || check_secure())
15241 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015242 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15243 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015244 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245 varp = &argvars[2];
15246
15247 if (buf != NULL && varname != NULL && varp != NULL)
15248 {
15249 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251
15252 if (*varname == '&')
15253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 long numval;
15255 char_u *strval;
15256 int error = FALSE;
15257
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015259 numval = get_tv_number_chk(varp, &error);
15260 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 if (!error && strval != NULL)
15262 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 }
15264 else
15265 {
15266 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15267 if (bufvarname != NULL)
15268 {
15269 STRCPY(bufvarname, "b:");
15270 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015271 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 vim_free(bufvarname);
15273 }
15274 }
15275
15276 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279}
15280
15281/*
15282 * "setcmdpos()" function
15283 */
15284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015285f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015286 typval_T *argvars;
15287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015289 int pos = (int)get_tv_number(&argvars[0]) - 1;
15290
15291 if (pos >= 0)
15292 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293}
15294
15295/*
15296 * "setline()" function
15297 */
15298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015299f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015300 typval_T *argvars;
15301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302{
15303 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015304 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015305 list_T *l = NULL;
15306 listitem_T *li = NULL;
15307 long added = 0;
15308 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015310 lnum = get_tv_lnum(&argvars[0]);
15311 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015313 l = argvars[1].vval.v_list;
15314 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015316 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015317 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015318
15319 rettv->vval.v_number = 0; /* OK */
15320 for (;;)
15321 {
15322 if (l != NULL)
15323 {
15324 /* list argument, get next string */
15325 if (li == NULL)
15326 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015327 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015328 li = li->li_next;
15329 }
15330
15331 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015332 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015333 break;
15334 if (lnum <= curbuf->b_ml.ml_line_count)
15335 {
15336 /* existing line, replace it */
15337 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15338 {
15339 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015340 if (lnum == curwin->w_cursor.lnum)
15341 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015342 rettv->vval.v_number = 0; /* OK */
15343 }
15344 }
15345 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15346 {
15347 /* lnum is one past the last line, append the line */
15348 ++added;
15349 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15350 rettv->vval.v_number = 0; /* OK */
15351 }
15352
15353 if (l == NULL) /* only one string argument */
15354 break;
15355 ++lnum;
15356 }
15357
15358 if (added > 0)
15359 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360}
15361
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015362static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15363
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015365 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015366 */
15367/*ARGSUSED*/
15368 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015369set_qf_ll_list(wp, list_arg, action_arg, rettv)
15370 win_T *wp;
15371 typval_T *list_arg;
15372 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015373 typval_T *rettv;
15374{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015375#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015376 char_u *act;
15377 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015378#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015379
Bram Moolenaar2641f772005-03-25 21:58:17 +000015380 rettv->vval.v_number = -1;
15381
15382#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015383 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015384 EMSG(_(e_listreq));
15385 else
15386 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015387 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015388
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015389 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015390 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015391 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015392 if (act == NULL)
15393 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015394 if (*act == 'a' || *act == 'r')
15395 action = *act;
15396 }
15397
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015398 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015399 rettv->vval.v_number = 0;
15400 }
15401#endif
15402}
15403
15404/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015405 * "setloclist()" function
15406 */
15407/*ARGSUSED*/
15408 static void
15409f_setloclist(argvars, rettv)
15410 typval_T *argvars;
15411 typval_T *rettv;
15412{
15413 win_T *win;
15414
15415 rettv->vval.v_number = -1;
15416
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015417 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015418 if (win != NULL)
15419 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15420}
15421
15422/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015423 * "setmatches()" function
15424 */
15425 static void
15426f_setmatches(argvars, rettv)
15427 typval_T *argvars;
15428 typval_T *rettv;
15429{
15430#ifdef FEAT_SEARCH_EXTRA
15431 list_T *l;
15432 listitem_T *li;
15433 dict_T *d;
15434
15435 rettv->vval.v_number = -1;
15436 if (argvars[0].v_type != VAR_LIST)
15437 {
15438 EMSG(_(e_listreq));
15439 return;
15440 }
15441 if ((l = argvars[0].vval.v_list) != NULL)
15442 {
15443
15444 /* To some extent make sure that we are dealing with a list from
15445 * "getmatches()". */
15446 li = l->lv_first;
15447 while (li != NULL)
15448 {
15449 if (li->li_tv.v_type != VAR_DICT
15450 || (d = li->li_tv.vval.v_dict) == NULL)
15451 {
15452 EMSG(_(e_invarg));
15453 return;
15454 }
15455 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15456 && dict_find(d, (char_u *)"pattern", -1) != NULL
15457 && dict_find(d, (char_u *)"priority", -1) != NULL
15458 && dict_find(d, (char_u *)"id", -1) != NULL))
15459 {
15460 EMSG(_(e_invarg));
15461 return;
15462 }
15463 li = li->li_next;
15464 }
15465
15466 clear_matches(curwin);
15467 li = l->lv_first;
15468 while (li != NULL)
15469 {
15470 d = li->li_tv.vval.v_dict;
15471 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15472 get_dict_string(d, (char_u *)"pattern", FALSE),
15473 (int)get_dict_number(d, (char_u *)"priority"),
15474 (int)get_dict_number(d, (char_u *)"id"));
15475 li = li->li_next;
15476 }
15477 rettv->vval.v_number = 0;
15478 }
15479#endif
15480}
15481
15482/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015483 * "setpos()" function
15484 */
15485/*ARGSUSED*/
15486 static void
15487f_setpos(argvars, rettv)
15488 typval_T *argvars;
15489 typval_T *rettv;
15490{
15491 pos_T pos;
15492 int fnum;
15493 char_u *name;
15494
Bram Moolenaar08250432008-02-13 11:42:46 +000015495 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015496 name = get_tv_string_chk(argvars);
15497 if (name != NULL)
15498 {
15499 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15500 {
15501 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015502 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015503 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015504 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015505 if (fnum == curbuf->b_fnum)
15506 {
15507 curwin->w_cursor = pos;
15508 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015509 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015510 }
15511 else
15512 EMSG(_(e_invarg));
15513 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015514 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15515 {
15516 /* set mark */
15517 if (setmark_pos(name[1], &pos, fnum) == OK)
15518 rettv->vval.v_number = 0;
15519 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015520 else
15521 EMSG(_(e_invarg));
15522 }
15523 }
15524}
15525
15526/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015527 * "setqflist()" function
15528 */
15529/*ARGSUSED*/
15530 static void
15531f_setqflist(argvars, rettv)
15532 typval_T *argvars;
15533 typval_T *rettv;
15534{
15535 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15536}
15537
15538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 * "setreg()" function
15540 */
15541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015543 typval_T *argvars;
15544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545{
15546 int regname;
15547 char_u *strregname;
15548 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015549 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 int append;
15551 char_u yank_type;
15552 long block_len;
15553
15554 block_len = -1;
15555 yank_type = MAUTO;
15556 append = FALSE;
15557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015558 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015561 if (strregname == NULL)
15562 return; /* type error; errmsg already given */
15563 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 if (regname == 0 || regname == '@')
15565 regname = '"';
15566 else if (regname == '=')
15567 return;
15568
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015569 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015571 stropt = get_tv_string_chk(&argvars[2]);
15572 if (stropt == NULL)
15573 return; /* type error */
15574 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 switch (*stropt)
15576 {
15577 case 'a': case 'A': /* append */
15578 append = TRUE;
15579 break;
15580 case 'v': case 'c': /* character-wise selection */
15581 yank_type = MCHAR;
15582 break;
15583 case 'V': case 'l': /* line-wise selection */
15584 yank_type = MLINE;
15585 break;
15586#ifdef FEAT_VISUAL
15587 case 'b': case Ctrl_V: /* block-wise selection */
15588 yank_type = MBLOCK;
15589 if (VIM_ISDIGIT(stropt[1]))
15590 {
15591 ++stropt;
15592 block_len = getdigits(&stropt) - 1;
15593 --stropt;
15594 }
15595 break;
15596#endif
15597 }
15598 }
15599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 strval = get_tv_string_chk(&argvars[1]);
15601 if (strval != NULL)
15602 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015604 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605}
15606
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015607/*
15608 * "settabwinvar()" function
15609 */
15610 static void
15611f_settabwinvar(argvars, rettv)
15612 typval_T *argvars;
15613 typval_T *rettv;
15614{
15615 setwinvar(argvars, rettv, 1);
15616}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617
15618/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015619 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015622f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015623 typval_T *argvars;
15624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015626 setwinvar(argvars, rettv, 0);
15627}
15628
15629/*
15630 * "setwinvar()" and "settabwinvar()" functions
15631 */
15632 static void
15633setwinvar(argvars, rettv, off)
15634 typval_T *argvars;
15635 typval_T *rettv;
15636 int off;
15637{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 win_T *win;
15639#ifdef FEAT_WINDOWS
15640 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015641 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642#endif
15643 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015644 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015646 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 rettv->vval.v_number = 0;
15649
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 if (check_restricted() || check_secure())
15651 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015652
15653#ifdef FEAT_WINDOWS
15654 if (off == 1)
15655 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15656 else
15657 tp = curtab;
15658#endif
15659 win = find_win_by_nr(&argvars[off], tp);
15660 varname = get_tv_string_chk(&argvars[off + 1]);
15661 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662
15663 if (win != NULL && varname != NULL && varp != NULL)
15664 {
15665#ifdef FEAT_WINDOWS
15666 /* set curwin to be our win, temporarily */
15667 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015668 save_curtab = curtab;
15669 goto_tabpage_tp(tp);
15670 if (!win_valid(win))
15671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 curwin = win;
15673 curbuf = curwin->w_buffer;
15674#endif
15675
15676 if (*varname == '&')
15677 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015678 long numval;
15679 char_u *strval;
15680 int error = FALSE;
15681
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015683 numval = get_tv_number_chk(varp, &error);
15684 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 if (!error && strval != NULL)
15686 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 }
15688 else
15689 {
15690 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15691 if (winvarname != NULL)
15692 {
15693 STRCPY(winvarname, "w:");
15694 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015695 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696 vim_free(winvarname);
15697 }
15698 }
15699
15700#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015701 /* Restore current tabpage and window, if still valid (autocomands can
15702 * make them invalid). */
15703 if (valid_tabpage(save_curtab))
15704 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 if (win_valid(save_curwin))
15706 {
15707 curwin = save_curwin;
15708 curbuf = curwin->w_buffer;
15709 }
15710#endif
15711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712}
15713
15714/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015715 * "shellescape({string})" function
15716 */
15717 static void
15718f_shellescape(argvars, rettv)
15719 typval_T *argvars;
15720 typval_T *rettv;
15721{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015722 rettv->vval.v_string = vim_strsave_shellescape(
15723 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015724 rettv->v_type = VAR_STRING;
15725}
15726
15727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 */
15730 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015732 typval_T *argvars;
15733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736
Bram Moolenaar0d660222005-01-07 21:51:51 +000015737 p = get_tv_string(&argvars[0]);
15738 rettv->vval.v_string = vim_strsave(p);
15739 simplify_filename(rettv->vval.v_string); /* simplify in place */
15740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741}
15742
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015743#ifdef FEAT_FLOAT
15744/*
15745 * "sin()" function
15746 */
15747 static void
15748f_sin(argvars, rettv)
15749 typval_T *argvars;
15750 typval_T *rettv;
15751{
15752 float_T f;
15753
15754 rettv->v_type = VAR_FLOAT;
15755 if (get_float_arg(argvars, &f) == OK)
15756 rettv->vval.v_float = sin(f);
15757 else
15758 rettv->vval.v_float = 0.0;
15759}
15760#endif
15761
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762static int
15763#ifdef __BORLANDC__
15764 _RTLENTRYF
15765#endif
15766 item_compare __ARGS((const void *s1, const void *s2));
15767static int
15768#ifdef __BORLANDC__
15769 _RTLENTRYF
15770#endif
15771 item_compare2 __ARGS((const void *s1, const void *s2));
15772
15773static int item_compare_ic;
15774static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015775static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776#define ITEM_COMPARE_FAIL 999
15777
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015779 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015781 static int
15782#ifdef __BORLANDC__
15783_RTLENTRYF
15784#endif
15785item_compare(s1, s2)
15786 const void *s1;
15787 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015789 char_u *p1, *p2;
15790 char_u *tofree1, *tofree2;
15791 int res;
15792 char_u numbuf1[NUMBUFLEN];
15793 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015795 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15796 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015797 if (p1 == NULL)
15798 p1 = (char_u *)"";
15799 if (p2 == NULL)
15800 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015801 if (item_compare_ic)
15802 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 res = STRCMP(p1, p2);
15805 vim_free(tofree1);
15806 vim_free(tofree2);
15807 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808}
15809
15810 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015811#ifdef __BORLANDC__
15812_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015814item_compare2(s1, s2)
15815 const void *s1;
15816 const void *s2;
15817{
15818 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015820 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 /* shortcut after failure in previous call; compare all items equal */
15824 if (item_compare_func_err)
15825 return 0;
15826
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015827 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15828 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15830 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015831
15832 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015833 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015834 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835 clear_tv(&argv[0]);
15836 clear_tv(&argv[1]);
15837
15838 if (res == FAIL)
15839 res = ITEM_COMPARE_FAIL;
15840 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 /* return value has wrong type */
15842 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15843 if (item_compare_func_err)
15844 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845 clear_tv(&rettv);
15846 return res;
15847}
15848
15849/*
15850 * "sort({list})" function
15851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015853f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015854 typval_T *argvars;
15855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856{
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 list_T *l;
15858 listitem_T *li;
15859 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015860 long len;
15861 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 rettv->vval.v_number = 0;
15864 if (argvars[0].v_type != VAR_LIST)
15865 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 else
15867 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015868 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015869 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870 return;
15871 rettv->vval.v_list = l;
15872 rettv->v_type = VAR_LIST;
15873 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874
Bram Moolenaar0d660222005-01-07 21:51:51 +000015875 len = list_len(l);
15876 if (len <= 1)
15877 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878
Bram Moolenaar0d660222005-01-07 21:51:51 +000015879 item_compare_ic = FALSE;
15880 item_compare_func = NULL;
15881 if (argvars[1].v_type != VAR_UNKNOWN)
15882 {
15883 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015884 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015885 else
15886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015887 int error = FALSE;
15888
15889 i = get_tv_number_chk(&argvars[1], &error);
15890 if (error)
15891 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015892 if (i == 1)
15893 item_compare_ic = TRUE;
15894 else
15895 item_compare_func = get_tv_string(&argvars[1]);
15896 }
15897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898
Bram Moolenaar0d660222005-01-07 21:51:51 +000015899 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015900 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015901 if (ptrs == NULL)
15902 return;
15903 i = 0;
15904 for (li = l->lv_first; li != NULL; li = li->li_next)
15905 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015907 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908 /* test the compare function */
15909 if (item_compare_func != NULL
15910 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15911 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015912 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015914 {
15915 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917 item_compare_func == NULL ? item_compare : item_compare2);
15918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 if (!item_compare_func_err)
15920 {
15921 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015922 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015923 l->lv_len = 0;
15924 for (i = 0; i < len; ++i)
15925 list_append(l, ptrs[i]);
15926 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015927 }
15928
15929 vim_free(ptrs);
15930 }
15931}
15932
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015933/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015934 * "soundfold({word})" function
15935 */
15936 static void
15937f_soundfold(argvars, rettv)
15938 typval_T *argvars;
15939 typval_T *rettv;
15940{
15941 char_u *s;
15942
15943 rettv->v_type = VAR_STRING;
15944 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015945#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015946 rettv->vval.v_string = eval_soundfold(s);
15947#else
15948 rettv->vval.v_string = vim_strsave(s);
15949#endif
15950}
15951
15952/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015953 * "spellbadword()" function
15954 */
15955/* ARGSUSED */
15956 static void
15957f_spellbadword(argvars, rettv)
15958 typval_T *argvars;
15959 typval_T *rettv;
15960{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015961 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015962 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015963 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015964
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015965 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015966 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015967
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015968#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015969 if (argvars[0].v_type == VAR_UNKNOWN)
15970 {
15971 /* Find the start and length of the badly spelled word. */
15972 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15973 if (len != 0)
15974 word = ml_get_cursor();
15975 }
15976 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15977 {
15978 char_u *str = get_tv_string_chk(&argvars[0]);
15979 int capcol = -1;
15980
15981 if (str != NULL)
15982 {
15983 /* Check the argument for spelling. */
15984 while (*str != NUL)
15985 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015986 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015987 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015988 {
15989 word = str;
15990 break;
15991 }
15992 str += len;
15993 }
15994 }
15995 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015996#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015997
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015998 list_append_string(rettv->vval.v_list, word, len);
15999 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016000 attr == HLF_SPB ? "bad" :
16001 attr == HLF_SPR ? "rare" :
16002 attr == HLF_SPL ? "local" :
16003 attr == HLF_SPC ? "caps" :
16004 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016005}
16006
16007/*
16008 * "spellsuggest()" function
16009 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016010/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016011 static void
16012f_spellsuggest(argvars, rettv)
16013 typval_T *argvars;
16014 typval_T *rettv;
16015{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016016#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016017 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016018 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016019 int maxcount;
16020 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016021 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016022 listitem_T *li;
16023 int need_capital = FALSE;
16024#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016025
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016026 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016027 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016028
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016029#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016030 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16031 {
16032 str = get_tv_string(&argvars[0]);
16033 if (argvars[1].v_type != VAR_UNKNOWN)
16034 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016035 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016036 if (maxcount <= 0)
16037 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016038 if (argvars[2].v_type != VAR_UNKNOWN)
16039 {
16040 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16041 if (typeerr)
16042 return;
16043 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016044 }
16045 else
16046 maxcount = 25;
16047
Bram Moolenaar4770d092006-01-12 23:22:24 +000016048 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016049
16050 for (i = 0; i < ga.ga_len; ++i)
16051 {
16052 str = ((char_u **)ga.ga_data)[i];
16053
16054 li = listitem_alloc();
16055 if (li == NULL)
16056 vim_free(str);
16057 else
16058 {
16059 li->li_tv.v_type = VAR_STRING;
16060 li->li_tv.v_lock = 0;
16061 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016062 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016063 }
16064 }
16065 ga_clear(&ga);
16066 }
16067#endif
16068}
16069
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016071f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016072 typval_T *argvars;
16073 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016074{
16075 char_u *str;
16076 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016077 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016078 regmatch_T regmatch;
16079 char_u patbuf[NUMBUFLEN];
16080 char_u *save_cpo;
16081 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016082 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016083 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016084 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085
16086 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16087 save_cpo = p_cpo;
16088 p_cpo = (char_u *)"";
16089
16090 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016091 if (argvars[1].v_type != VAR_UNKNOWN)
16092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016093 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16094 if (pat == NULL)
16095 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016096 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016098 }
16099 if (pat == NULL || *pat == NUL)
16100 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016101
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016102 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016104 if (typeerr)
16105 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16108 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016111 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016112 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016113 if (*str == NUL)
16114 match = FALSE; /* empty item at the end */
16115 else
16116 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117 if (match)
16118 end = regmatch.startp[0];
16119 else
16120 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016121 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16122 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016123 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016124 if (list_append_string(rettv->vval.v_list, str,
16125 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016126 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127 }
16128 if (!match)
16129 break;
16130 /* Advance to just after the match. */
16131 if (regmatch.endp[0] > str)
16132 col = 0;
16133 else
16134 {
16135 /* Don't get stuck at the same match. */
16136#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016137 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016138#else
16139 col = 1;
16140#endif
16141 }
16142 str = regmatch.endp[0];
16143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144
Bram Moolenaar0d660222005-01-07 21:51:51 +000016145 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147
Bram Moolenaar0d660222005-01-07 21:51:51 +000016148 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149}
16150
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016151#ifdef FEAT_FLOAT
16152/*
16153 * "sqrt()" function
16154 */
16155 static void
16156f_sqrt(argvars, rettv)
16157 typval_T *argvars;
16158 typval_T *rettv;
16159{
16160 float_T f;
16161
16162 rettv->v_type = VAR_FLOAT;
16163 if (get_float_arg(argvars, &f) == OK)
16164 rettv->vval.v_float = sqrt(f);
16165 else
16166 rettv->vval.v_float = 0.0;
16167}
16168
16169/*
16170 * "str2float()" function
16171 */
16172 static void
16173f_str2float(argvars, rettv)
16174 typval_T *argvars;
16175 typval_T *rettv;
16176{
16177 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16178
16179 if (*p == '+')
16180 p = skipwhite(p + 1);
16181 (void)string2float(p, &rettv->vval.v_float);
16182 rettv->v_type = VAR_FLOAT;
16183}
16184#endif
16185
Bram Moolenaar2c932302006-03-18 21:42:09 +000016186/*
16187 * "str2nr()" function
16188 */
16189 static void
16190f_str2nr(argvars, rettv)
16191 typval_T *argvars;
16192 typval_T *rettv;
16193{
16194 int base = 10;
16195 char_u *p;
16196 long n;
16197
16198 if (argvars[1].v_type != VAR_UNKNOWN)
16199 {
16200 base = get_tv_number(&argvars[1]);
16201 if (base != 8 && base != 10 && base != 16)
16202 {
16203 EMSG(_(e_invarg));
16204 return;
16205 }
16206 }
16207
16208 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016209 if (*p == '+')
16210 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016211 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16212 rettv->vval.v_number = n;
16213}
16214
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215#ifdef HAVE_STRFTIME
16216/*
16217 * "strftime({format}[, {time}])" function
16218 */
16219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016221 typval_T *argvars;
16222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223{
16224 char_u result_buf[256];
16225 struct tm *curtime;
16226 time_t seconds;
16227 char_u *p;
16228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016231 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016232 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 seconds = time(NULL);
16234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 curtime = localtime(&seconds);
16237 /* MSVC returns NULL for an invalid value of seconds. */
16238 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016239 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 else
16241 {
16242# ifdef FEAT_MBYTE
16243 vimconv_T conv;
16244 char_u *enc;
16245
16246 conv.vc_type = CONV_NONE;
16247 enc = enc_locale();
16248 convert_setup(&conv, p_enc, enc);
16249 if (conv.vc_type != CONV_NONE)
16250 p = string_convert(&conv, p, NULL);
16251# endif
16252 if (p != NULL)
16253 (void)strftime((char *)result_buf, sizeof(result_buf),
16254 (char *)p, curtime);
16255 else
16256 result_buf[0] = NUL;
16257
16258# ifdef FEAT_MBYTE
16259 if (conv.vc_type != CONV_NONE)
16260 vim_free(p);
16261 convert_setup(&conv, enc, p_enc);
16262 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016263 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 else
16265# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016266 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267
16268# ifdef FEAT_MBYTE
16269 /* Release conversion descriptors */
16270 convert_setup(&conv, NULL, NULL);
16271 vim_free(enc);
16272# endif
16273 }
16274}
16275#endif
16276
16277/*
16278 * "stridx()" function
16279 */
16280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016281f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 typval_T *argvars;
16283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284{
16285 char_u buf[NUMBUFLEN];
16286 char_u *needle;
16287 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016288 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016290 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 needle = get_tv_string_chk(&argvars[1]);
16293 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016295 if (needle == NULL || haystack == NULL)
16296 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297
Bram Moolenaar33570922005-01-25 22:26:29 +000016298 if (argvars[2].v_type != VAR_UNKNOWN)
16299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016300 int error = FALSE;
16301
16302 start_idx = get_tv_number_chk(&argvars[2], &error);
16303 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016305 if (start_idx >= 0)
16306 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016307 }
16308
16309 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16310 if (pos != NULL)
16311 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312}
16313
16314/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016315 * "string()" function
16316 */
16317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 typval_T *argvars;
16320 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016321{
16322 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016323 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016326 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016327 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016328 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016329 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330}
16331
16332/*
16333 * "strlen()" function
16334 */
16335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016336f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016337 typval_T *argvars;
16338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016340 rettv->vval.v_number = (varnumber_T)(STRLEN(
16341 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342}
16343
16344/*
16345 * "strpart()" function
16346 */
16347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016348f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016349 typval_T *argvars;
16350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351{
16352 char_u *p;
16353 int n;
16354 int len;
16355 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016356 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016358 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 slen = (int)STRLEN(p);
16360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016361 n = get_tv_number_chk(&argvars[1], &error);
16362 if (error)
16363 len = 0;
16364 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016365 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 else
16367 len = slen - n; /* default len: all bytes that are available. */
16368
16369 /*
16370 * Only return the overlap between the specified part and the actual
16371 * string.
16372 */
16373 if (n < 0)
16374 {
16375 len += n;
16376 n = 0;
16377 }
16378 else if (n > slen)
16379 n = slen;
16380 if (len < 0)
16381 len = 0;
16382 else if (n + len > slen)
16383 len = slen - n;
16384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016385 rettv->v_type = VAR_STRING;
16386 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387}
16388
16389/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390 * "strridx()" function
16391 */
16392 static void
16393f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 typval_T *argvars;
16395 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396{
16397 char_u buf[NUMBUFLEN];
16398 char_u *needle;
16399 char_u *haystack;
16400 char_u *rest;
16401 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016402 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 needle = get_tv_string_chk(&argvars[1]);
16405 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406
16407 rettv->vval.v_number = -1;
16408 if (needle == NULL || haystack == NULL)
16409 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016410
16411 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016412 if (argvars[2].v_type != VAR_UNKNOWN)
16413 {
16414 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016415 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016416 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016418 }
16419 else
16420 end_idx = haystack_len;
16421
Bram Moolenaar0d660222005-01-07 21:51:51 +000016422 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016423 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016425 lastmatch = haystack + end_idx;
16426 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016428 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429 for (rest = haystack; *rest != '\0'; ++rest)
16430 {
16431 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016432 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433 break;
16434 lastmatch = rest;
16435 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016436 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437
16438 if (lastmatch == NULL)
16439 rettv->vval.v_number = -1;
16440 else
16441 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16442}
16443
16444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 * "strtrans()" function
16446 */
16447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 typval_T *argvars;
16450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016452 rettv->v_type = VAR_STRING;
16453 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454}
16455
16456/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 * "submatch()" function
16458 */
16459 static void
16460f_submatch(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 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016465 rettv->vval.v_string =
16466 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016467}
16468
16469/*
16470 * "substitute()" function
16471 */
16472 static void
16473f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016474 typval_T *argvars;
16475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016476{
16477 char_u patbuf[NUMBUFLEN];
16478 char_u subbuf[NUMBUFLEN];
16479 char_u flagsbuf[NUMBUFLEN];
16480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 char_u *str = get_tv_string_chk(&argvars[0]);
16482 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16483 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16484 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16485
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16488 rettv->vval.v_string = NULL;
16489 else
16490 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016491}
16492
16493/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016494 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 */
16496/*ARGSUSED*/
16497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016498f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016499 typval_T *argvars;
16500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501{
16502 int id = 0;
16503#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016504 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 long col;
16506 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016507 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 lnum = get_tv_lnum(argvars); /* -1 on type error */
16510 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16511 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016513 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016514 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016515 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516#endif
16517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016518 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519}
16520
16521/*
16522 * "synIDattr(id, what [, mode])" function
16523 */
16524/*ARGSUSED*/
16525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016526f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016527 typval_T *argvars;
16528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529{
16530 char_u *p = NULL;
16531#ifdef FEAT_SYN_HL
16532 int id;
16533 char_u *what;
16534 char_u *mode;
16535 char_u modebuf[NUMBUFLEN];
16536 int modec;
16537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538 id = get_tv_number(&argvars[0]);
16539 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016540 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 modec = TOLOWER_ASC(mode[0]);
16544 if (modec != 't' && modec != 'c'
16545#ifdef FEAT_GUI
16546 && modec != 'g'
16547#endif
16548 )
16549 modec = 0; /* replace invalid with current */
16550 }
16551 else
16552 {
16553#ifdef FEAT_GUI
16554 if (gui.in_use)
16555 modec = 'g';
16556 else
16557#endif
16558 if (t_colors > 1)
16559 modec = 'c';
16560 else
16561 modec = 't';
16562 }
16563
16564
16565 switch (TOLOWER_ASC(what[0]))
16566 {
16567 case 'b':
16568 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16569 p = highlight_color(id, what, modec);
16570 else /* bold */
16571 p = highlight_has_attr(id, HL_BOLD, modec);
16572 break;
16573
16574 case 'f': /* fg[#] */
16575 p = highlight_color(id, what, modec);
16576 break;
16577
16578 case 'i':
16579 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16580 p = highlight_has_attr(id, HL_INVERSE, modec);
16581 else /* italic */
16582 p = highlight_has_attr(id, HL_ITALIC, modec);
16583 break;
16584
16585 case 'n': /* name */
16586 p = get_highlight_name(NULL, id - 1);
16587 break;
16588
16589 case 'r': /* reverse */
16590 p = highlight_has_attr(id, HL_INVERSE, modec);
16591 break;
16592
16593 case 's': /* standout */
16594 p = highlight_has_attr(id, HL_STANDOUT, modec);
16595 break;
16596
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016597 case 'u':
16598 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16599 /* underline */
16600 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16601 else
16602 /* undercurl */
16603 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 break;
16605 }
16606
16607 if (p != NULL)
16608 p = vim_strsave(p);
16609#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610 rettv->v_type = VAR_STRING;
16611 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612}
16613
16614/*
16615 * "synIDtrans(id)" function
16616 */
16617/*ARGSUSED*/
16618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016619f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016620 typval_T *argvars;
16621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622{
16623 int id;
16624
16625#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627
16628 if (id > 0)
16629 id = syn_get_final_id(id);
16630 else
16631#endif
16632 id = 0;
16633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635}
16636
16637/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016638 * "synstack(lnum, col)" function
16639 */
16640/*ARGSUSED*/
16641 static void
16642f_synstack(argvars, rettv)
16643 typval_T *argvars;
16644 typval_T *rettv;
16645{
16646#ifdef FEAT_SYN_HL
16647 long lnum;
16648 long col;
16649 int i;
16650 int id;
16651#endif
16652
16653 rettv->v_type = VAR_LIST;
16654 rettv->vval.v_list = NULL;
16655
16656#ifdef FEAT_SYN_HL
16657 lnum = get_tv_lnum(argvars); /* -1 on type error */
16658 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16659
16660 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
16661 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
16662 && rettv_list_alloc(rettv) != FAIL)
16663 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016664 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016665 for (i = 0; ; ++i)
16666 {
16667 id = syn_get_stack_item(i);
16668 if (id < 0)
16669 break;
16670 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16671 break;
16672 }
16673 }
16674#endif
16675}
16676
16677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 * "system()" function
16679 */
16680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016681f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016682 typval_T *argvars;
16683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016685 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016687 char_u *infile = NULL;
16688 char_u buf[NUMBUFLEN];
16689 int err = FALSE;
16690 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016692 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016693 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016694
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016695 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016696 {
16697 /*
16698 * Write the string to a temp file, to be used for input of the shell
16699 * command.
16700 */
16701 if ((infile = vim_tempname('i')) == NULL)
16702 {
16703 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016704 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016705 }
16706
16707 fd = mch_fopen((char *)infile, WRITEBIN);
16708 if (fd == NULL)
16709 {
16710 EMSG2(_(e_notopen), infile);
16711 goto done;
16712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016713 p = get_tv_string_buf_chk(&argvars[1], buf);
16714 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016715 {
16716 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016718 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016719 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16720 err = TRUE;
16721 if (fclose(fd) != 0)
16722 err = TRUE;
16723 if (err)
16724 {
16725 EMSG(_("E677: Error writing temp file"));
16726 goto done;
16727 }
16728 }
16729
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016730 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16731 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016732
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733#ifdef USE_CR
16734 /* translate <CR> 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;
16738
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016739 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 {
16741 if (*s == CAR)
16742 *s = NL;
16743 }
16744 }
16745#else
16746# ifdef USE_CRNL
16747 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016748 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 {
16750 char_u *s, *d;
16751
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016752 d = res;
16753 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 {
16755 if (s[0] == CAR && s[1] == NL)
16756 ++s;
16757 *d++ = *s;
16758 }
16759 *d = NUL;
16760 }
16761# endif
16762#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016763
16764done:
16765 if (infile != NULL)
16766 {
16767 mch_remove(infile);
16768 vim_free(infile);
16769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 rettv->v_type = VAR_STRING;
16771 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772}
16773
16774/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016775 * "tabpagebuflist()" function
16776 */
16777/* ARGSUSED */
16778 static void
16779f_tabpagebuflist(argvars, rettv)
16780 typval_T *argvars;
16781 typval_T *rettv;
16782{
16783#ifndef FEAT_WINDOWS
16784 rettv->vval.v_number = 0;
16785#else
16786 tabpage_T *tp;
16787 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016788
16789 if (argvars[0].v_type == VAR_UNKNOWN)
16790 wp = firstwin;
16791 else
16792 {
16793 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16794 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016795 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016796 }
16797 if (wp == NULL)
16798 rettv->vval.v_number = 0;
16799 else
16800 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016801 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016802 rettv->vval.v_number = 0;
16803 else
16804 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016805 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016806 if (list_append_number(rettv->vval.v_list,
16807 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016808 break;
16809 }
16810 }
16811#endif
16812}
16813
16814
16815/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016816 * "tabpagenr()" function
16817 */
16818/* ARGSUSED */
16819 static void
16820f_tabpagenr(argvars, rettv)
16821 typval_T *argvars;
16822 typval_T *rettv;
16823{
16824 int nr = 1;
16825#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016826 char_u *arg;
16827
16828 if (argvars[0].v_type != VAR_UNKNOWN)
16829 {
16830 arg = get_tv_string_chk(&argvars[0]);
16831 nr = 0;
16832 if (arg != NULL)
16833 {
16834 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016835 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016836 else
16837 EMSG2(_(e_invexpr2), arg);
16838 }
16839 }
16840 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016841 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016842#endif
16843 rettv->vval.v_number = nr;
16844}
16845
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016846
16847#ifdef FEAT_WINDOWS
16848static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16849
16850/*
16851 * Common code for tabpagewinnr() and winnr().
16852 */
16853 static int
16854get_winnr(tp, argvar)
16855 tabpage_T *tp;
16856 typval_T *argvar;
16857{
16858 win_T *twin;
16859 int nr = 1;
16860 win_T *wp;
16861 char_u *arg;
16862
16863 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16864 if (argvar->v_type != VAR_UNKNOWN)
16865 {
16866 arg = get_tv_string_chk(argvar);
16867 if (arg == NULL)
16868 nr = 0; /* type error; errmsg already given */
16869 else if (STRCMP(arg, "$") == 0)
16870 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16871 else if (STRCMP(arg, "#") == 0)
16872 {
16873 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16874 if (twin == NULL)
16875 nr = 0;
16876 }
16877 else
16878 {
16879 EMSG2(_(e_invexpr2), arg);
16880 nr = 0;
16881 }
16882 }
16883
16884 if (nr > 0)
16885 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16886 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016887 {
16888 if (wp == NULL)
16889 {
16890 /* didn't find it in this tabpage */
16891 nr = 0;
16892 break;
16893 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016894 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016895 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016896 return nr;
16897}
16898#endif
16899
16900/*
16901 * "tabpagewinnr()" function
16902 */
16903/* ARGSUSED */
16904 static void
16905f_tabpagewinnr(argvars, rettv)
16906 typval_T *argvars;
16907 typval_T *rettv;
16908{
16909 int nr = 1;
16910#ifdef FEAT_WINDOWS
16911 tabpage_T *tp;
16912
16913 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16914 if (tp == NULL)
16915 nr = 0;
16916 else
16917 nr = get_winnr(tp, &argvars[1]);
16918#endif
16919 rettv->vval.v_number = nr;
16920}
16921
16922
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016923/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016924 * "tagfiles()" function
16925 */
16926/*ARGSUSED*/
16927 static void
16928f_tagfiles(argvars, rettv)
16929 typval_T *argvars;
16930 typval_T *rettv;
16931{
16932 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016933 tagname_T tn;
16934 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016935
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016936 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016937 {
16938 rettv->vval.v_number = 0;
16939 return;
16940 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016942 for (first = TRUE; ; first = FALSE)
16943 if (get_tagfname(&tn, first, fname) == FAIL
16944 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016945 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016946 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016947}
16948
16949/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016950 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016951 */
16952 static void
16953f_taglist(argvars, rettv)
16954 typval_T *argvars;
16955 typval_T *rettv;
16956{
16957 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016958
16959 tag_pattern = get_tv_string(&argvars[0]);
16960
16961 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 if (*tag_pattern == NUL)
16963 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016964
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016965 if (rettv_list_alloc(rettv) == OK)
16966 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016967}
16968
16969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 * "tempname()" function
16971 */
16972/*ARGSUSED*/
16973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016974f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016975 typval_T *argvars;
16976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977{
16978 static int x = 'A';
16979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016980 rettv->v_type = VAR_STRING;
16981 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982
16983 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16984 * names. Skip 'I' and 'O', they are used for shell redirection. */
16985 do
16986 {
16987 if (x == 'Z')
16988 x = '0';
16989 else if (x == '9')
16990 x = 'A';
16991 else
16992 {
16993#ifdef EBCDIC
16994 if (x == 'I')
16995 x = 'J';
16996 else if (x == 'R')
16997 x = 'S';
16998 else
16999#endif
17000 ++x;
17001 }
17002 } while (x == 'I' || x == 'O');
17003}
17004
17005/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017006 * "test(list)" function: Just checking the walls...
17007 */
17008/*ARGSUSED*/
17009 static void
17010f_test(argvars, rettv)
17011 typval_T *argvars;
17012 typval_T *rettv;
17013{
17014 /* Used for unit testing. Change the code below to your liking. */
17015#if 0
17016 listitem_T *li;
17017 list_T *l;
17018 char_u *bad, *good;
17019
17020 if (argvars[0].v_type != VAR_LIST)
17021 return;
17022 l = argvars[0].vval.v_list;
17023 if (l == NULL)
17024 return;
17025 li = l->lv_first;
17026 if (li == NULL)
17027 return;
17028 bad = get_tv_string(&li->li_tv);
17029 li = li->li_next;
17030 if (li == NULL)
17031 return;
17032 good = get_tv_string(&li->li_tv);
17033 rettv->vval.v_number = test_edit_score(bad, good);
17034#endif
17035}
17036
17037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 * "tolower(string)" function
17039 */
17040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017041f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017042 typval_T *argvars;
17043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044{
17045 char_u *p;
17046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047 p = vim_strsave(get_tv_string(&argvars[0]));
17048 rettv->v_type = VAR_STRING;
17049 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050
17051 if (p != NULL)
17052 while (*p != NUL)
17053 {
17054#ifdef FEAT_MBYTE
17055 int l;
17056
17057 if (enc_utf8)
17058 {
17059 int c, lc;
17060
17061 c = utf_ptr2char(p);
17062 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017063 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 /* TODO: reallocate string when byte count changes. */
17065 if (utf_char2len(lc) == l)
17066 utf_char2bytes(lc, p);
17067 p += l;
17068 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017069 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 p += l; /* skip multi-byte character */
17071 else
17072#endif
17073 {
17074 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17075 ++p;
17076 }
17077 }
17078}
17079
17080/*
17081 * "toupper(string)" function
17082 */
17083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017084f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 typval_T *argvars;
17086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017088 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017089 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090}
17091
17092/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017093 * "tr(string, fromstr, tostr)" function
17094 */
17095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017096f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017097 typval_T *argvars;
17098 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017099{
17100 char_u *instr;
17101 char_u *fromstr;
17102 char_u *tostr;
17103 char_u *p;
17104#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017105 int inlen;
17106 int fromlen;
17107 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017108 int idx;
17109 char_u *cpstr;
17110 int cplen;
17111 int first = TRUE;
17112#endif
17113 char_u buf[NUMBUFLEN];
17114 char_u buf2[NUMBUFLEN];
17115 garray_T ga;
17116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017118 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17119 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017120
17121 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017122 rettv->v_type = VAR_STRING;
17123 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017124 if (fromstr == NULL || tostr == NULL)
17125 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017126 ga_init2(&ga, (int)sizeof(char), 80);
17127
17128#ifdef FEAT_MBYTE
17129 if (!has_mbyte)
17130#endif
17131 /* not multi-byte: fromstr and tostr must be the same length */
17132 if (STRLEN(fromstr) != STRLEN(tostr))
17133 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017134#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017135error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017136#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017137 EMSG2(_(e_invarg2), fromstr);
17138 ga_clear(&ga);
17139 return;
17140 }
17141
17142 /* fromstr and tostr have to contain the same number of chars */
17143 while (*instr != NUL)
17144 {
17145#ifdef FEAT_MBYTE
17146 if (has_mbyte)
17147 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017148 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017149 cpstr = instr;
17150 cplen = inlen;
17151 idx = 0;
17152 for (p = fromstr; *p != NUL; p += fromlen)
17153 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017154 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017155 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17156 {
17157 for (p = tostr; *p != NUL; p += tolen)
17158 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017159 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017160 if (idx-- == 0)
17161 {
17162 cplen = tolen;
17163 cpstr = p;
17164 break;
17165 }
17166 }
17167 if (*p == NUL) /* tostr is shorter than fromstr */
17168 goto error;
17169 break;
17170 }
17171 ++idx;
17172 }
17173
17174 if (first && cpstr == instr)
17175 {
17176 /* Check that fromstr and tostr have the same number of
17177 * (multi-byte) characters. Done only once when a character
17178 * of instr doesn't appear in fromstr. */
17179 first = FALSE;
17180 for (p = tostr; *p != NUL; p += tolen)
17181 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017182 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017183 --idx;
17184 }
17185 if (idx != 0)
17186 goto error;
17187 }
17188
17189 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017190 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017191 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017192
17193 instr += inlen;
17194 }
17195 else
17196#endif
17197 {
17198 /* When not using multi-byte chars we can do it faster. */
17199 p = vim_strchr(fromstr, *instr);
17200 if (p != NULL)
17201 ga_append(&ga, tostr[p - fromstr]);
17202 else
17203 ga_append(&ga, *instr);
17204 ++instr;
17205 }
17206 }
17207
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017208 /* add a terminating NUL */
17209 ga_grow(&ga, 1);
17210 ga_append(&ga, NUL);
17211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017213}
17214
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017215#ifdef FEAT_FLOAT
17216/*
17217 * "trunc({float})" function
17218 */
17219 static void
17220f_trunc(argvars, rettv)
17221 typval_T *argvars;
17222 typval_T *rettv;
17223{
17224 float_T f;
17225
17226 rettv->v_type = VAR_FLOAT;
17227 if (get_float_arg(argvars, &f) == OK)
17228 /* trunc() is not in C90, use floor() or ceil() instead. */
17229 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17230 else
17231 rettv->vval.v_float = 0.0;
17232}
17233#endif
17234
Bram Moolenaar8299df92004-07-10 09:47:34 +000017235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 * "type(expr)" function
17237 */
17238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017240 typval_T *argvars;
17241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017243 int n;
17244
17245 switch (argvars[0].v_type)
17246 {
17247 case VAR_NUMBER: n = 0; break;
17248 case VAR_STRING: n = 1; break;
17249 case VAR_FUNC: n = 2; break;
17250 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017251 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017252#ifdef FEAT_FLOAT
17253 case VAR_FLOAT: n = 5; break;
17254#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017255 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17256 }
17257 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258}
17259
17260/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017261 * "values(dict)" function
17262 */
17263 static void
17264f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017265 typval_T *argvars;
17266 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017267{
17268 dict_list(argvars, rettv, 1);
17269}
17270
17271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 * "virtcol(string)" function
17273 */
17274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T *argvars;
17277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278{
17279 colnr_T vcol = 0;
17280 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017281 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017283 fp = var2fpos(&argvars[0], FALSE, &fnum);
17284 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17285 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 {
17287 getvvcol(curwin, fp, NULL, NULL, &vcol);
17288 ++vcol;
17289 }
17290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017291 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292}
17293
17294/*
17295 * "visualmode()" function
17296 */
17297/*ARGSUSED*/
17298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 typval_T *argvars;
17301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302{
17303#ifdef FEAT_VISUAL
17304 char_u str[2];
17305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 str[0] = curbuf->b_visual_mode_eval;
17308 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310
17311 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017312 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 curbuf->b_visual_mode_eval = NUL;
17314#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316#endif
17317}
17318
17319/*
17320 * "winbufnr(nr)" function
17321 */
17322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017323f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017324 typval_T *argvars;
17325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326{
17327 win_T *wp;
17328
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017329 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017333 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
17336/*
17337 * "wincol()" function
17338 */
17339/*ARGSUSED*/
17340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017342 typval_T *argvars;
17343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344{
17345 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017346 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347}
17348
17349/*
17350 * "winheight(nr)" function
17351 */
17352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017353f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 typval_T *argvars;
17355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356{
17357 win_T *wp;
17358
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017359 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017361 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364}
17365
17366/*
17367 * "winline()" function
17368 */
17369/*ARGSUSED*/
17370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371f_winline(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017376 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377}
17378
17379/*
17380 * "winnr()" function
17381 */
17382/* ARGSUSED */
17383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017384f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017385 typval_T *argvars;
17386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387{
17388 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017389
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017391 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394}
17395
17396/*
17397 * "winrestcmd()" function
17398 */
17399/* ARGSUSED */
17400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 typval_T *argvars;
17403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404{
17405#ifdef FEAT_WINDOWS
17406 win_T *wp;
17407 int winnr = 1;
17408 garray_T ga;
17409 char_u buf[50];
17410
17411 ga_init2(&ga, (int)sizeof(char), 70);
17412 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17413 {
17414 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17415 ga_concat(&ga, buf);
17416# ifdef FEAT_VERTSPLIT
17417 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17418 ga_concat(&ga, buf);
17419# endif
17420 ++winnr;
17421 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017422 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017426 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017428 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429}
17430
17431/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017432 * "winrestview()" function
17433 */
17434/* ARGSUSED */
17435 static void
17436f_winrestview(argvars, rettv)
17437 typval_T *argvars;
17438 typval_T *rettv;
17439{
17440 dict_T *dict;
17441
17442 if (argvars[0].v_type != VAR_DICT
17443 || (dict = argvars[0].vval.v_dict) == NULL)
17444 EMSG(_(e_invarg));
17445 else
17446 {
17447 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17448 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17449#ifdef FEAT_VIRTUALEDIT
17450 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17451#endif
17452 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017453 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017454
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017455 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017456#ifdef FEAT_DIFF
17457 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17458#endif
17459 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17460 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17461
17462 check_cursor();
17463 changed_cline_bef_curs();
17464 invalidate_botline();
17465 redraw_later(VALID);
17466
17467 if (curwin->w_topline == 0)
17468 curwin->w_topline = 1;
17469 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17470 curwin->w_topline = curbuf->b_ml.ml_line_count;
17471#ifdef FEAT_DIFF
17472 check_topfill(curwin, TRUE);
17473#endif
17474 }
17475}
17476
17477/*
17478 * "winsaveview()" function
17479 */
17480/* ARGSUSED */
17481 static void
17482f_winsaveview(argvars, rettv)
17483 typval_T *argvars;
17484 typval_T *rettv;
17485{
17486 dict_T *dict;
17487
17488 dict = dict_alloc();
17489 if (dict == NULL)
17490 return;
17491 rettv->v_type = VAR_DICT;
17492 rettv->vval.v_dict = dict;
17493 ++dict->dv_refcount;
17494
17495 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17496 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17497#ifdef FEAT_VIRTUALEDIT
17498 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17499#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017500 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017501 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17502
17503 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17504#ifdef FEAT_DIFF
17505 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17506#endif
17507 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17508 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17509}
17510
17511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 * "winwidth(nr)" function
17513 */
17514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017516 typval_T *argvars;
17517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518{
17519 win_T *wp;
17520
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017521 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 else
17525#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017526 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017528 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529#endif
17530}
17531
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017533 * "writefile()" function
17534 */
17535 static void
17536f_writefile(argvars, rettv)
17537 typval_T *argvars;
17538 typval_T *rettv;
17539{
17540 int binary = FALSE;
17541 char_u *fname;
17542 FILE *fd;
17543 listitem_T *li;
17544 char_u *s;
17545 int ret = 0;
17546 int c;
17547
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017548 if (check_restricted() || check_secure())
17549 return;
17550
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017551 if (argvars[0].v_type != VAR_LIST)
17552 {
17553 EMSG2(_(e_listarg), "writefile()");
17554 return;
17555 }
17556 if (argvars[0].vval.v_list == NULL)
17557 return;
17558
17559 if (argvars[2].v_type != VAR_UNKNOWN
17560 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17561 binary = TRUE;
17562
17563 /* Always open the file in binary mode, library functions have a mind of
17564 * their own about CR-LF conversion. */
17565 fname = get_tv_string(&argvars[1]);
17566 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17567 {
17568 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17569 ret = -1;
17570 }
17571 else
17572 {
17573 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17574 li = li->li_next)
17575 {
17576 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17577 {
17578 if (*s == '\n')
17579 c = putc(NUL, fd);
17580 else
17581 c = putc(*s, fd);
17582 if (c == EOF)
17583 {
17584 ret = -1;
17585 break;
17586 }
17587 }
17588 if (!binary || li->li_next != NULL)
17589 if (putc('\n', fd) == EOF)
17590 {
17591 ret = -1;
17592 break;
17593 }
17594 if (ret < 0)
17595 {
17596 EMSG(_(e_write));
17597 break;
17598 }
17599 }
17600 fclose(fd);
17601 }
17602
17603 rettv->vval.v_number = ret;
17604}
17605
17606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017608 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 */
17610 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017611var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017613 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017614 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017616 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017618 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619
Bram Moolenaara5525202006-03-02 22:52:09 +000017620 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017621 if (varp->v_type == VAR_LIST)
17622 {
17623 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017624 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017625 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017626 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017627
17628 l = varp->vval.v_list;
17629 if (l == NULL)
17630 return NULL;
17631
17632 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017633 pos.lnum = list_find_nr(l, 0L, &error);
17634 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017635 return NULL; /* invalid line number */
17636
17637 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017638 pos.col = list_find_nr(l, 1L, &error);
17639 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017640 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017641 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017642
17643 /* We accept "$" for the column number: last column. */
17644 li = list_find(l, 1L);
17645 if (li != NULL && li->li_tv.v_type == VAR_STRING
17646 && li->li_tv.vval.v_string != NULL
17647 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17648 pos.col = len + 1;
17649
Bram Moolenaara5525202006-03-02 22:52:09 +000017650 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017651 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017652 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017653 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017654
Bram Moolenaara5525202006-03-02 22:52:09 +000017655#ifdef FEAT_VIRTUALEDIT
17656 /* Get the virtual offset. Defaults to zero. */
17657 pos.coladd = list_find_nr(l, 2L, &error);
17658 if (error)
17659 pos.coladd = 0;
17660#endif
17661
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017662 return &pos;
17663 }
17664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 name = get_tv_string_chk(varp);
17666 if (name == NULL)
17667 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017668 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017670#ifdef FEAT_VISUAL
17671 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17672 {
17673 if (VIsual_active)
17674 return &VIsual;
17675 return &curwin->w_cursor;
17676 }
17677#endif
17678 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017680 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17682 return NULL;
17683 return pp;
17684 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017685
17686#ifdef FEAT_VIRTUALEDIT
17687 pos.coladd = 0;
17688#endif
17689
Bram Moolenaar477933c2007-07-17 14:32:23 +000017690 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017691 {
17692 pos.col = 0;
17693 if (name[1] == '0') /* "w0": first visible line */
17694 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017695 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017696 pos.lnum = curwin->w_topline;
17697 return &pos;
17698 }
17699 else if (name[1] == '$') /* "w$": last visible line */
17700 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017701 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017702 pos.lnum = curwin->w_botline - 1;
17703 return &pos;
17704 }
17705 }
17706 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017708 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 {
17710 pos.lnum = curbuf->b_ml.ml_line_count;
17711 pos.col = 0;
17712 }
17713 else
17714 {
17715 pos.lnum = curwin->w_cursor.lnum;
17716 pos.col = (colnr_T)STRLEN(ml_get_curline());
17717 }
17718 return &pos;
17719 }
17720 return NULL;
17721}
17722
17723/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017724 * Convert list in "arg" into a position and optional file number.
17725 * When "fnump" is NULL there is no file number, only 3 items.
17726 * Note that the column is passed on as-is, the caller may want to decrement
17727 * it to use 1 for the first column.
17728 * Return FAIL when conversion is not possible, doesn't check the position for
17729 * validity.
17730 */
17731 static int
17732list2fpos(arg, posp, fnump)
17733 typval_T *arg;
17734 pos_T *posp;
17735 int *fnump;
17736{
17737 list_T *l = arg->vval.v_list;
17738 long i = 0;
17739 long n;
17740
Bram Moolenaarbde35262006-07-23 20:12:24 +000017741 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17742 * when "fnump" isn't NULL and "coladd" is optional. */
17743 if (arg->v_type != VAR_LIST
17744 || l == NULL
17745 || l->lv_len < (fnump == NULL ? 2 : 3)
17746 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017747 return FAIL;
17748
17749 if (fnump != NULL)
17750 {
17751 n = list_find_nr(l, i++, NULL); /* fnum */
17752 if (n < 0)
17753 return FAIL;
17754 if (n == 0)
17755 n = curbuf->b_fnum; /* current buffer */
17756 *fnump = n;
17757 }
17758
17759 n = list_find_nr(l, i++, NULL); /* lnum */
17760 if (n < 0)
17761 return FAIL;
17762 posp->lnum = n;
17763
17764 n = list_find_nr(l, i++, NULL); /* col */
17765 if (n < 0)
17766 return FAIL;
17767 posp->col = n;
17768
17769#ifdef FEAT_VIRTUALEDIT
17770 n = list_find_nr(l, i, NULL);
17771 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017772 posp->coladd = 0;
17773 else
17774 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017775#endif
17776
17777 return OK;
17778}
17779
17780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 * Get the length of an environment variable name.
17782 * Advance "arg" to the first character after the name.
17783 * Return 0 for error.
17784 */
17785 static int
17786get_env_len(arg)
17787 char_u **arg;
17788{
17789 char_u *p;
17790 int len;
17791
17792 for (p = *arg; vim_isIDc(*p); ++p)
17793 ;
17794 if (p == *arg) /* no name found */
17795 return 0;
17796
17797 len = (int)(p - *arg);
17798 *arg = p;
17799 return len;
17800}
17801
17802/*
17803 * Get the length of the name of a function or internal variable.
17804 * "arg" is advanced to the first non-white character after the name.
17805 * Return 0 if something is wrong.
17806 */
17807 static int
17808get_id_len(arg)
17809 char_u **arg;
17810{
17811 char_u *p;
17812 int len;
17813
17814 /* Find the end of the name. */
17815 for (p = *arg; eval_isnamec(*p); ++p)
17816 ;
17817 if (p == *arg) /* no name found */
17818 return 0;
17819
17820 len = (int)(p - *arg);
17821 *arg = skipwhite(p);
17822
17823 return len;
17824}
17825
17826/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017827 * Get the length of the name of a variable or function.
17828 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017830 * Return -1 if curly braces expansion failed.
17831 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 * If the name contains 'magic' {}'s, expand them and return the
17833 * expanded name in an allocated string via 'alias' - caller must free.
17834 */
17835 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017836get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 char_u **arg;
17838 char_u **alias;
17839 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017840 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841{
17842 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 char_u *p;
17844 char_u *expr_start;
17845 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846
17847 *alias = NULL; /* default to no alias */
17848
17849 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17850 && (*arg)[2] == (int)KE_SNR)
17851 {
17852 /* hard coded <SNR>, already translated */
17853 *arg += 3;
17854 return get_id_len(arg) + 3;
17855 }
17856 len = eval_fname_script(*arg);
17857 if (len > 0)
17858 {
17859 /* literal "<SID>", "s:" or "<SNR>" */
17860 *arg += len;
17861 }
17862
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017866 p = find_name_end(*arg, &expr_start, &expr_end,
17867 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 if (expr_start != NULL)
17869 {
17870 char_u *temp_string;
17871
17872 if (!evaluate)
17873 {
17874 len += (int)(p - *arg);
17875 *arg = skipwhite(p);
17876 return len;
17877 }
17878
17879 /*
17880 * Include any <SID> etc in the expanded string:
17881 * Thus the -len here.
17882 */
17883 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17884 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017885 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 *alias = temp_string;
17887 *arg = skipwhite(p);
17888 return (int)STRLEN(temp_string);
17889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890
17891 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017892 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 EMSG2(_(e_invexpr2), *arg);
17894
17895 return len;
17896}
17897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017898/*
17899 * Find the end of a variable or function name, taking care of magic braces.
17900 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17901 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017902 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017903 * Return a pointer to just after the name. Equal to "arg" if there is no
17904 * valid name.
17905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017907find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 char_u *arg;
17909 char_u **expr_start;
17910 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017911 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017913 int mb_nest = 0;
17914 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 char_u *p;
17916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017917 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017919 *expr_start = NULL;
17920 *expr_end = NULL;
17921 }
17922
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017923 /* Quick check for valid starting character. */
17924 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17925 return arg;
17926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017927 for (p = arg; *p != NUL
17928 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017929 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017930 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017931 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017932 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017934 if (*p == '\'')
17935 {
17936 /* skip over 'string' to avoid counting [ and ] inside it. */
17937 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17938 ;
17939 if (*p == NUL)
17940 break;
17941 }
17942 else if (*p == '"')
17943 {
17944 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17945 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17946 if (*p == '\\' && p[1] != NUL)
17947 ++p;
17948 if (*p == NUL)
17949 break;
17950 }
17951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017952 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017954 if (*p == '[')
17955 ++br_nest;
17956 else if (*p == ']')
17957 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017960 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017962 if (*p == '{')
17963 {
17964 mb_nest++;
17965 if (expr_start != NULL && *expr_start == NULL)
17966 *expr_start = p;
17967 }
17968 else if (*p == '}')
17969 {
17970 mb_nest--;
17971 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17972 *expr_end = p;
17973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 }
17976
17977 return p;
17978}
17979
17980/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017981 * Expands out the 'magic' {}'s in a variable/function name.
17982 * Note that this can call itself recursively, to deal with
17983 * constructs like foo{bar}{baz}{bam}
17984 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17985 * "in_start" ^
17986 * "expr_start" ^
17987 * "expr_end" ^
17988 * "in_end" ^
17989 *
17990 * Returns a new allocated string, which the caller must free.
17991 * Returns NULL for failure.
17992 */
17993 static char_u *
17994make_expanded_name(in_start, expr_start, expr_end, in_end)
17995 char_u *in_start;
17996 char_u *expr_start;
17997 char_u *expr_end;
17998 char_u *in_end;
17999{
18000 char_u c1;
18001 char_u *retval = NULL;
18002 char_u *temp_result;
18003 char_u *nextcmd = NULL;
18004
18005 if (expr_end == NULL || in_end == NULL)
18006 return NULL;
18007 *expr_start = NUL;
18008 *expr_end = NUL;
18009 c1 = *in_end;
18010 *in_end = NUL;
18011
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018012 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018013 if (temp_result != NULL && nextcmd == NULL)
18014 {
18015 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18016 + (in_end - expr_end) + 1));
18017 if (retval != NULL)
18018 {
18019 STRCPY(retval, in_start);
18020 STRCAT(retval, temp_result);
18021 STRCAT(retval, expr_end + 1);
18022 }
18023 }
18024 vim_free(temp_result);
18025
18026 *in_end = c1; /* put char back for error messages */
18027 *expr_start = '{';
18028 *expr_end = '}';
18029
18030 if (retval != NULL)
18031 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018032 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018033 if (expr_start != NULL)
18034 {
18035 /* Further expansion! */
18036 temp_result = make_expanded_name(retval, expr_start,
18037 expr_end, temp_result);
18038 vim_free(retval);
18039 retval = temp_result;
18040 }
18041 }
18042
18043 return retval;
18044}
18045
18046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018048 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 */
18050 static int
18051eval_isnamec(c)
18052 int c;
18053{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018054 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18055}
18056
18057/*
18058 * Return TRUE if character "c" can be used as the first character in a
18059 * variable or function name (excluding '{' and '}').
18060 */
18061 static int
18062eval_isnamec1(c)
18063 int c;
18064{
18065 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066}
18067
18068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 * Set number v: variable to "val".
18070 */
18071 void
18072set_vim_var_nr(idx, val)
18073 int idx;
18074 long val;
18075{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018076 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077}
18078
18079/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018080 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 */
18082 long
18083get_vim_var_nr(idx)
18084 int idx;
18085{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018086 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087}
18088
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018089/*
18090 * Get string v: variable value. Uses a static buffer, can only be used once.
18091 */
18092 char_u *
18093get_vim_var_str(idx)
18094 int idx;
18095{
18096 return get_tv_string(&vimvars[idx].vv_tv);
18097}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018098
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099/*
18100 * Set v:count, v:count1 and v:prevcount.
18101 */
18102 void
18103set_vcount(count, count1)
18104 long count;
18105 long count1;
18106{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018107 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
18108 vimvars[VV_COUNT].vv_nr = count;
18109 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110}
18111
18112/*
18113 * Set string v: variable to a copy of "val".
18114 */
18115 void
18116set_vim_var_string(idx, val, len)
18117 int idx;
18118 char_u *val;
18119 int len; /* length of "val" to use or -1 (whole string) */
18120{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018121 /* Need to do this (at least) once, since we can't initialize a union.
18122 * Will always be invoked when "v:progname" is set. */
18123 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18124
Bram Moolenaare9a41262005-01-15 22:18:47 +000018125 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018127 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018129 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018131 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132}
18133
18134/*
18135 * Set v:register if needed.
18136 */
18137 void
18138set_reg_var(c)
18139 int c;
18140{
18141 char_u regname;
18142
18143 if (c == 0 || c == ' ')
18144 regname = '"';
18145 else
18146 regname = c;
18147 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018148 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 set_vim_var_string(VV_REG, &regname, 1);
18150}
18151
18152/*
18153 * Get or set v:exception. If "oldval" == NULL, return the current value.
18154 * Otherwise, restore the value to "oldval" and return NULL.
18155 * Must always be called in pairs to save and restore v:exception! Does not
18156 * take care of memory allocations.
18157 */
18158 char_u *
18159v_exception(oldval)
18160 char_u *oldval;
18161{
18162 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018163 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
Bram Moolenaare9a41262005-01-15 22:18:47 +000018165 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 return NULL;
18167}
18168
18169/*
18170 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18171 * Otherwise, restore the value to "oldval" and return NULL.
18172 * Must always be called in pairs to save and restore v:throwpoint! Does not
18173 * take care of memory allocations.
18174 */
18175 char_u *
18176v_throwpoint(oldval)
18177 char_u *oldval;
18178{
18179 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018180 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181
Bram Moolenaare9a41262005-01-15 22:18:47 +000018182 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 return NULL;
18184}
18185
18186#if defined(FEAT_AUTOCMD) || defined(PROTO)
18187/*
18188 * Set v:cmdarg.
18189 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18190 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18191 * Must always be called in pairs!
18192 */
18193 char_u *
18194set_cmdarg(eap, oldarg)
18195 exarg_T *eap;
18196 char_u *oldarg;
18197{
18198 char_u *oldval;
18199 char_u *newval;
18200 unsigned len;
18201
Bram Moolenaare9a41262005-01-15 22:18:47 +000018202 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018203 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018205 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018206 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018207 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 }
18209
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018210 if (eap->force_bin == FORCE_BIN)
18211 len = 6;
18212 else if (eap->force_bin == FORCE_NOBIN)
18213 len = 8;
18214 else
18215 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018216
18217 if (eap->read_edit)
18218 len += 7;
18219
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018220 if (eap->force_ff != 0)
18221 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18222# ifdef FEAT_MBYTE
18223 if (eap->force_enc != 0)
18224 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018225 if (eap->bad_char != 0)
18226 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018227# endif
18228
18229 newval = alloc(len + 1);
18230 if (newval == NULL)
18231 return NULL;
18232
18233 if (eap->force_bin == FORCE_BIN)
18234 sprintf((char *)newval, " ++bin");
18235 else if (eap->force_bin == FORCE_NOBIN)
18236 sprintf((char *)newval, " ++nobin");
18237 else
18238 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018239
18240 if (eap->read_edit)
18241 STRCAT(newval, " ++edit");
18242
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018243 if (eap->force_ff != 0)
18244 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18245 eap->cmd + eap->force_ff);
18246# ifdef FEAT_MBYTE
18247 if (eap->force_enc != 0)
18248 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18249 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018250 if (eap->bad_char != 0)
18251 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18252 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018253# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018254 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018255 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256}
18257#endif
18258
18259/*
18260 * Get the value of internal variable "name".
18261 * Return OK or FAIL.
18262 */
18263 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018264get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 char_u *name;
18266 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018267 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018268 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269{
18270 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018271 typval_T *tv = NULL;
18272 typval_T atv;
18273 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275
18276 /* truncate the name, so that we can use strcmp() */
18277 cc = name[len];
18278 name[len] = NUL;
18279
18280 /*
18281 * Check for "b:changedtick".
18282 */
18283 if (STRCMP(name, "b:changedtick") == 0)
18284 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018285 atv.v_type = VAR_NUMBER;
18286 atv.vval.v_number = curbuf->b_changedtick;
18287 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 }
18289
18290 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 * Check for user-defined variables.
18292 */
18293 else
18294 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018295 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018297 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 }
18299
Bram Moolenaare9a41262005-01-15 22:18:47 +000018300 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018302 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018303 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 ret = FAIL;
18305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018307 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308
18309 name[len] = cc;
18310
18311 return ret;
18312}
18313
18314/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018315 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18316 * Also handle function call with Funcref variable: func(expr)
18317 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18318 */
18319 static int
18320handle_subscript(arg, rettv, evaluate, verbose)
18321 char_u **arg;
18322 typval_T *rettv;
18323 int evaluate; /* do more than finding the end */
18324 int verbose; /* give error messages */
18325{
18326 int ret = OK;
18327 dict_T *selfdict = NULL;
18328 char_u *s;
18329 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018330 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018331
18332 while (ret == OK
18333 && (**arg == '['
18334 || (**arg == '.' && rettv->v_type == VAR_DICT)
18335 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18336 && !vim_iswhite(*(*arg - 1)))
18337 {
18338 if (**arg == '(')
18339 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018340 /* need to copy the funcref so that we can clear rettv */
18341 functv = *rettv;
18342 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018343
18344 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018345 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018346 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018347 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18348 &len, evaluate, selfdict);
18349
18350 /* Clear the funcref afterwards, so that deleting it while
18351 * evaluating the arguments is possible (see test55). */
18352 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018353
18354 /* Stop the expression evaluation when immediately aborting on
18355 * error, or when an interrupt occurred or an exception was thrown
18356 * but not caught. */
18357 if (aborting())
18358 {
18359 if (ret == OK)
18360 clear_tv(rettv);
18361 ret = FAIL;
18362 }
18363 dict_unref(selfdict);
18364 selfdict = NULL;
18365 }
18366 else /* **arg == '[' || **arg == '.' */
18367 {
18368 dict_unref(selfdict);
18369 if (rettv->v_type == VAR_DICT)
18370 {
18371 selfdict = rettv->vval.v_dict;
18372 if (selfdict != NULL)
18373 ++selfdict->dv_refcount;
18374 }
18375 else
18376 selfdict = NULL;
18377 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18378 {
18379 clear_tv(rettv);
18380 ret = FAIL;
18381 }
18382 }
18383 }
18384 dict_unref(selfdict);
18385 return ret;
18386}
18387
18388/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018389 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018390 * value).
18391 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018392 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018393alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018394{
Bram Moolenaar33570922005-01-25 22:26:29 +000018395 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018396}
18397
18398/*
18399 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 * The string "s" must have been allocated, it is consumed.
18401 * Return NULL for out of memory, the variable otherwise.
18402 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018403 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018404alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 char_u *s;
18406{
Bram Moolenaar33570922005-01-25 22:26:29 +000018407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409 rettv = alloc_tv();
18410 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018412 rettv->v_type = VAR_STRING;
18413 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 }
18415 else
18416 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018417 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418}
18419
18420/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018421 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018423 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018424free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018425 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426{
18427 if (varp != NULL)
18428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018429 switch (varp->v_type)
18430 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018431 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018432 func_unref(varp->vval.v_string);
18433 /*FALLTHROUGH*/
18434 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018435 vim_free(varp->vval.v_string);
18436 break;
18437 case VAR_LIST:
18438 list_unref(varp->vval.v_list);
18439 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018440 case VAR_DICT:
18441 dict_unref(varp->vval.v_dict);
18442 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018443 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018444#ifdef FEAT_FLOAT
18445 case VAR_FLOAT:
18446#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018447 case VAR_UNKNOWN:
18448 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018449 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018450 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018451 break;
18452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 vim_free(varp);
18454 }
18455}
18456
18457/*
18458 * Free the memory for a variable value and set the value to NULL or 0.
18459 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018460 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463{
18464 if (varp != NULL)
18465 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018466 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018468 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018469 func_unref(varp->vval.v_string);
18470 /*FALLTHROUGH*/
18471 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018472 vim_free(varp->vval.v_string);
18473 varp->vval.v_string = NULL;
18474 break;
18475 case VAR_LIST:
18476 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018477 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018478 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018479 case VAR_DICT:
18480 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018481 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018482 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018484 varp->vval.v_number = 0;
18485 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018486#ifdef FEAT_FLOAT
18487 case VAR_FLOAT:
18488 varp->vval.v_float = 0.0;
18489 break;
18490#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018491 case VAR_UNKNOWN:
18492 break;
18493 default:
18494 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018496 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 }
18498}
18499
18500/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018501 * Set the value of a variable to NULL without freeing items.
18502 */
18503 static void
18504init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018505 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018506{
18507 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018508 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018509}
18510
18511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 * Get the number value of a variable.
18513 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018514 * For incompatible types, return 0.
18515 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18516 * caller of incompatible types: it sets *denote to TRUE if "denote"
18517 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 */
18519 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018520get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018521 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018523 int error = FALSE;
18524
18525 return get_tv_number_chk(varp, &error); /* return 0L on error */
18526}
18527
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018528 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018529get_tv_number_chk(varp, denote)
18530 typval_T *varp;
18531 int *denote;
18532{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018533 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018535 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018537 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018538 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018539#ifdef FEAT_FLOAT
18540 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018541 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018542 break;
18543#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018544 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018545 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018546 break;
18547 case VAR_STRING:
18548 if (varp->vval.v_string != NULL)
18549 vim_str2nr(varp->vval.v_string, NULL, NULL,
18550 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018551 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018552 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018553 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018554 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018555 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018556 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018558 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018559 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018562 if (denote == NULL) /* useful for values that must be unsigned */
18563 n = -1;
18564 else
18565 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018566 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567}
18568
18569/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018570 * Get the lnum from the first argument.
18571 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018572 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 */
18574 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018576 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577{
Bram Moolenaar33570922005-01-25 22:26:29 +000018578 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 linenr_T lnum;
18580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018581 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 if (lnum == 0) /* no valid number, try using line() */
18583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018584 rettv.v_type = VAR_NUMBER;
18585 f_line(argvars, &rettv);
18586 lnum = rettv.vval.v_number;
18587 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 }
18589 return lnum;
18590}
18591
18592/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018593 * Get the lnum from the first argument.
18594 * Also accepts "$", then "buf" is used.
18595 * Returns 0 on error.
18596 */
18597 static linenr_T
18598get_tv_lnum_buf(argvars, buf)
18599 typval_T *argvars;
18600 buf_T *buf;
18601{
18602 if (argvars[0].v_type == VAR_STRING
18603 && argvars[0].vval.v_string != NULL
18604 && argvars[0].vval.v_string[0] == '$'
18605 && buf != NULL)
18606 return buf->b_ml.ml_line_count;
18607 return get_tv_number_chk(&argvars[0], NULL);
18608}
18609
18610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 * Get the string value of a variable.
18612 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018613 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18614 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 * If the String variable has never been set, return an empty string.
18616 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018617 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18618 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 */
18620 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018622 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623{
18624 static char_u mybuf[NUMBUFLEN];
18625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627}
18628
18629 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018631 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018632 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018634 char_u *res = get_tv_string_buf_chk(varp, buf);
18635
18636 return res != NULL ? res : (char_u *)"";
18637}
18638
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018639 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018640get_tv_string_chk(varp)
18641 typval_T *varp;
18642{
18643 static char_u mybuf[NUMBUFLEN];
18644
18645 return get_tv_string_buf_chk(varp, mybuf);
18646}
18647
18648 static char_u *
18649get_tv_string_buf_chk(varp, buf)
18650 typval_T *varp;
18651 char_u *buf;
18652{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018653 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018655 case VAR_NUMBER:
18656 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18657 return buf;
18658 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018659 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018660 break;
18661 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018662 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018663 break;
18664 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018665 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018666 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018667#ifdef FEAT_FLOAT
18668 case VAR_FLOAT:
18669 EMSG(_("E806: using Float as a String"));
18670 break;
18671#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018672 case VAR_STRING:
18673 if (varp->vval.v_string != NULL)
18674 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018675 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018676 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018677 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018678 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018680 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681}
18682
18683/*
18684 * Find variable "name" in the list of variables.
18685 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018686 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018687 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018688 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018690 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018691find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018696 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697
Bram Moolenaara7043832005-01-21 11:56:39 +000018698 ht = find_var_ht(name, &varname);
18699 if (htp != NULL)
18700 *htp = ht;
18701 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018703 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704}
18705
18706/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018707 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018708 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018710 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018711find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018712 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018713 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018714 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018715{
Bram Moolenaar33570922005-01-25 22:26:29 +000018716 hashitem_T *hi;
18717
18718 if (*varname == NUL)
18719 {
18720 /* Must be something like "s:", otherwise "ht" would be NULL. */
18721 switch (varname[-2])
18722 {
18723 case 's': return &SCRIPT_SV(current_SID).sv_var;
18724 case 'g': return &globvars_var;
18725 case 'v': return &vimvars_var;
18726 case 'b': return &curbuf->b_bufvar;
18727 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018728#ifdef FEAT_WINDOWS
18729 case 't': return &curtab->tp_winvar;
18730#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018731 case 'l': return current_funccal == NULL
18732 ? NULL : &current_funccal->l_vars_var;
18733 case 'a': return current_funccal == NULL
18734 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018735 }
18736 return NULL;
18737 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018738
18739 hi = hash_find(ht, varname);
18740 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018741 {
18742 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018743 * worked find the variable again. Don't auto-load a script if it was
18744 * loaded already, otherwise it would be loaded every time when
18745 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018746 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018747 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018748 hi = hash_find(ht, varname);
18749 if (HASHITEM_EMPTY(hi))
18750 return NULL;
18751 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018752 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018753}
18754
18755/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018756 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018757 * Set "varname" to the start of name without ':'.
18758 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018759 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018760find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 char_u *name;
18762 char_u **varname;
18763{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018764 hashitem_T *hi;
18765
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 if (name[1] != ':')
18767 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018768 /* The name must not start with a colon or #. */
18769 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 return NULL;
18771 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018772
18773 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018774 hi = hash_find(&compat_hashtab, name);
18775 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018776 return &compat_hashtab;
18777
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018779 return &globvarht; /* global variable */
18780 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 }
18782 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018783 if (*name == 'g') /* global variable */
18784 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018785 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18786 */
18787 if (vim_strchr(name + 2, ':') != NULL
18788 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018789 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018794#ifdef FEAT_WINDOWS
18795 if (*name == 't') /* tab page variable */
18796 return &curtab->tp_vars.dv_hashtab;
18797#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 if (*name == 'v') /* v: variable */
18799 return &vimvarht;
18800 if (*name == 'a' && current_funccal != NULL) /* function argument */
18801 return &current_funccal->l_avars.dv_hashtab;
18802 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18803 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 if (*name == 's' /* script variable */
18805 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18806 return &SCRIPT_VARS(current_SID);
18807 return NULL;
18808}
18809
18810/*
18811 * Get the string value of a (global/local) variable.
18812 * Returns NULL when it doesn't exist.
18813 */
18814 char_u *
18815get_var_value(name)
18816 char_u *name;
18817{
Bram Moolenaar33570922005-01-25 22:26:29 +000018818 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819
Bram Moolenaara7043832005-01-21 11:56:39 +000018820 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 if (v == NULL)
18822 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018823 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824}
18825
18826/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 * sourcing this script and when executing functions defined in the script.
18829 */
18830 void
18831new_script_vars(id)
18832 scid_T id;
18833{
Bram Moolenaara7043832005-01-21 11:56:39 +000018834 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018835 hashtab_T *ht;
18836 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018837
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18839 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018840 /* Re-allocating ga_data means that an ht_array pointing to
18841 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018842 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018843 for (i = 1; i <= ga_scripts.ga_len; ++i)
18844 {
18845 ht = &SCRIPT_VARS(i);
18846 if (ht->ht_mask == HT_INIT_SIZE - 1)
18847 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018848 sv = &SCRIPT_SV(i);
18849 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018850 }
18851
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 while (ga_scripts.ga_len < id)
18853 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18855 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 }
18858 }
18859}
18860
18861/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018862 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18863 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 */
18865 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018866init_var_dict(dict, dict_var)
18867 dict_T *dict;
18868 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869{
Bram Moolenaar33570922005-01-25 22:26:29 +000018870 hash_init(&dict->dv_hashtab);
18871 dict->dv_refcount = 99999;
18872 dict_var->di_tv.vval.v_dict = dict;
18873 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018874 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018875 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18876 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877}
18878
18879/*
18880 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 * Frees all allocated variables and the value they contain.
18882 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 */
18884 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018885vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018886 hashtab_T *ht;
18887{
18888 vars_clear_ext(ht, TRUE);
18889}
18890
18891/*
18892 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18893 */
18894 static void
18895vars_clear_ext(ht, free_val)
18896 hashtab_T *ht;
18897 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898{
Bram Moolenaara7043832005-01-21 11:56:39 +000018899 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018900 hashitem_T *hi;
18901 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902
Bram Moolenaar33570922005-01-25 22:26:29 +000018903 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018904 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018905 for (hi = ht->ht_array; todo > 0; ++hi)
18906 {
18907 if (!HASHITEM_EMPTY(hi))
18908 {
18909 --todo;
18910
Bram Moolenaar33570922005-01-25 22:26:29 +000018911 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018912 * ht_array might change then. hash_clear() takes care of it
18913 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018914 v = HI2DI(hi);
18915 if (free_val)
18916 clear_tv(&v->di_tv);
18917 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18918 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018919 }
18920 }
18921 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018922 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923}
18924
Bram Moolenaara7043832005-01-21 11:56:39 +000018925/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 * Delete a variable from hashtab "ht" at item "hi".
18927 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018930delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018931 hashtab_T *ht;
18932 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933{
Bram Moolenaar33570922005-01-25 22:26:29 +000018934 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018935
18936 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018937 clear_tv(&di->di_tv);
18938 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939}
18940
18941/*
18942 * List the value of one internal variable.
18943 */
18944 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018945list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018946 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018948 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018950 char_u *tofree;
18951 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018952 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018953
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018954 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018955 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018956 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018957 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958}
18959
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018961list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 char_u *prefix;
18963 char_u *name;
18964 int type;
18965 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018966 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967{
Bram Moolenaar31859182007-08-14 20:41:13 +000018968 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18969 msg_start();
18970 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 if (name != NULL) /* "a:" vars don't have a name stored */
18972 msg_puts(name);
18973 msg_putchar(' ');
18974 msg_advance(22);
18975 if (type == VAR_NUMBER)
18976 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018977 else if (type == VAR_FUNC)
18978 msg_putchar('*');
18979 else if (type == VAR_LIST)
18980 {
18981 msg_putchar('[');
18982 if (*string == '[')
18983 ++string;
18984 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018985 else if (type == VAR_DICT)
18986 {
18987 msg_putchar('{');
18988 if (*string == '{')
18989 ++string;
18990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 else
18992 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018993
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018995
18996 if (type == VAR_FUNC)
18997 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018998 if (*first)
18999 {
19000 msg_clr_eos();
19001 *first = FALSE;
19002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003}
19004
19005/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 * If the variable already exists, the value is updated.
19008 * Otherwise the variable is created.
19009 */
19010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019011set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019013 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019014 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015{
Bram Moolenaar33570922005-01-25 22:26:29 +000019016 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019018 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019019 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019022 {
19023 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19024 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19025 ? name[2] : name[0]))
19026 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019027 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019028 return;
19029 }
19030 if (function_exists(name))
19031 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019032 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019033 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019034 return;
19035 }
19036 }
19037
Bram Moolenaara7043832005-01-21 11:56:39 +000019038 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019040 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019041 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019042 return;
19043 }
19044
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019045 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019046 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019048 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019049 if (var_check_ro(v->di_flags, name)
19050 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019051 return;
19052 if (v->di_tv.v_type != tv->v_type
19053 && !((v->di_tv.v_type == VAR_STRING
19054 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019055 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019056 || tv->v_type == VAR_NUMBER))
19057#ifdef FEAT_FLOAT
19058 && !((v->di_tv.v_type == VAR_NUMBER
19059 || v->di_tv.v_type == VAR_FLOAT)
19060 && (tv->v_type == VAR_NUMBER
19061 || tv->v_type == VAR_FLOAT))
19062#endif
19063 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019064 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019065 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019066 return;
19067 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019068
19069 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019070 * Handle setting internal v: variables separately: we don't change
19071 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 */
19073 if (ht == &vimvarht)
19074 {
19075 if (v->di_tv.v_type == VAR_STRING)
19076 {
19077 vim_free(v->di_tv.vval.v_string);
19078 if (copy || tv->v_type != VAR_STRING)
19079 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19080 else
19081 {
19082 /* Take over the string to avoid an extra alloc/free. */
19083 v->di_tv.vval.v_string = tv->vval.v_string;
19084 tv->vval.v_string = NULL;
19085 }
19086 }
19087 else if (v->di_tv.v_type != VAR_NUMBER)
19088 EMSG2(_(e_intern2), "set_var()");
19089 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019090 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019091 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019092 if (STRCMP(varname, "searchforward") == 0)
19093 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19094 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019095 return;
19096 }
19097
19098 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 }
19100 else /* add a new variable */
19101 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019102 /* Can't add "v:" variable. */
19103 if (ht == &vimvarht)
19104 {
19105 EMSG2(_(e_illvar), name);
19106 return;
19107 }
19108
Bram Moolenaar92124a32005-06-17 22:03:40 +000019109 /* Make sure the variable name is valid. */
19110 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019111 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19112 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019113 {
19114 EMSG2(_(e_illvar), varname);
19115 return;
19116 }
19117
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019118 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19119 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019120 if (v == NULL)
19121 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019122 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019123 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019125 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 return;
19127 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019128 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019130
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019131 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019132 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019133 else
19134 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019135 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019136 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019137 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139}
19140
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019142 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019143 * Also give an error message.
19144 */
19145 static int
19146var_check_ro(flags, name)
19147 int flags;
19148 char_u *name;
19149{
19150 if (flags & DI_FLAGS_RO)
19151 {
19152 EMSG2(_(e_readonlyvar), name);
19153 return TRUE;
19154 }
19155 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19156 {
19157 EMSG2(_(e_readonlysbx), name);
19158 return TRUE;
19159 }
19160 return FALSE;
19161}
19162
19163/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019164 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19165 * Also give an error message.
19166 */
19167 static int
19168var_check_fixed(flags, name)
19169 int flags;
19170 char_u *name;
19171{
19172 if (flags & DI_FLAGS_FIX)
19173 {
19174 EMSG2(_("E795: Cannot delete variable %s"), name);
19175 return TRUE;
19176 }
19177 return FALSE;
19178}
19179
19180/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019181 * Return TRUE if typeval "tv" is set to be locked (immutable).
19182 * Also give an error message, using "name".
19183 */
19184 static int
19185tv_check_lock(lock, name)
19186 int lock;
19187 char_u *name;
19188{
19189 if (lock & VAR_LOCKED)
19190 {
19191 EMSG2(_("E741: Value is locked: %s"),
19192 name == NULL ? (char_u *)_("Unknown") : name);
19193 return TRUE;
19194 }
19195 if (lock & VAR_FIXED)
19196 {
19197 EMSG2(_("E742: Cannot change value of %s"),
19198 name == NULL ? (char_u *)_("Unknown") : name);
19199 return TRUE;
19200 }
19201 return FALSE;
19202}
19203
19204/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019205 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019206 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019207 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019210copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019211 typval_T *from;
19212 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019214 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019215 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019216 switch (from->v_type)
19217 {
19218 case VAR_NUMBER:
19219 to->vval.v_number = from->vval.v_number;
19220 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019221#ifdef FEAT_FLOAT
19222 case VAR_FLOAT:
19223 to->vval.v_float = from->vval.v_float;
19224 break;
19225#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019226 case VAR_STRING:
19227 case VAR_FUNC:
19228 if (from->vval.v_string == NULL)
19229 to->vval.v_string = NULL;
19230 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019231 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019232 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019233 if (from->v_type == VAR_FUNC)
19234 func_ref(to->vval.v_string);
19235 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019236 break;
19237 case VAR_LIST:
19238 if (from->vval.v_list == NULL)
19239 to->vval.v_list = NULL;
19240 else
19241 {
19242 to->vval.v_list = from->vval.v_list;
19243 ++to->vval.v_list->lv_refcount;
19244 }
19245 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019246 case VAR_DICT:
19247 if (from->vval.v_dict == NULL)
19248 to->vval.v_dict = NULL;
19249 else
19250 {
19251 to->vval.v_dict = from->vval.v_dict;
19252 ++to->vval.v_dict->dv_refcount;
19253 }
19254 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019255 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019257 break;
19258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259}
19260
19261/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019262 * Make a copy of an item.
19263 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019264 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19265 * reference to an already copied list/dict can be used.
19266 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019267 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019268 static int
19269item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019270 typval_T *from;
19271 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019272 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019273 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019274{
19275 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019276 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019277
Bram Moolenaar33570922005-01-25 22:26:29 +000019278 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019279 {
19280 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019281 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019282 }
19283 ++recurse;
19284
19285 switch (from->v_type)
19286 {
19287 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019288#ifdef FEAT_FLOAT
19289 case VAR_FLOAT:
19290#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019291 case VAR_STRING:
19292 case VAR_FUNC:
19293 copy_tv(from, to);
19294 break;
19295 case VAR_LIST:
19296 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019297 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019298 if (from->vval.v_list == NULL)
19299 to->vval.v_list = NULL;
19300 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19301 {
19302 /* use the copy made earlier */
19303 to->vval.v_list = from->vval.v_list->lv_copylist;
19304 ++to->vval.v_list->lv_refcount;
19305 }
19306 else
19307 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19308 if (to->vval.v_list == NULL)
19309 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019310 break;
19311 case VAR_DICT:
19312 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019313 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019314 if (from->vval.v_dict == NULL)
19315 to->vval.v_dict = NULL;
19316 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19317 {
19318 /* use the copy made earlier */
19319 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19320 ++to->vval.v_dict->dv_refcount;
19321 }
19322 else
19323 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19324 if (to->vval.v_dict == NULL)
19325 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019326 break;
19327 default:
19328 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019329 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019330 }
19331 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019332 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019333}
19334
19335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 * ":echo expr1 ..." print each argument separated with a space, add a
19337 * newline at the end.
19338 * ":echon expr1 ..." print each argument plain.
19339 */
19340 void
19341ex_echo(eap)
19342 exarg_T *eap;
19343{
19344 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019345 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019346 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 char_u *p;
19348 int needclr = TRUE;
19349 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019350 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351
19352 if (eap->skip)
19353 ++emsg_skip;
19354 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19355 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019356 /* If eval1() causes an error message the text from the command may
19357 * still need to be cleared. E.g., "echo 22,44". */
19358 need_clr_eos = needclr;
19359
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019361 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 {
19363 /*
19364 * Report the invalid expression unless the expression evaluation
19365 * has been cancelled due to an aborting error, an interrupt, or an
19366 * exception.
19367 */
19368 if (!aborting())
19369 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019370 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371 break;
19372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019373 need_clr_eos = FALSE;
19374
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375 if (!eap->skip)
19376 {
19377 if (atstart)
19378 {
19379 atstart = FALSE;
19380 /* Call msg_start() after eval1(), evaluating the expression
19381 * may cause a message to appear. */
19382 if (eap->cmdidx == CMD_echo)
19383 msg_start();
19384 }
19385 else if (eap->cmdidx == CMD_echo)
19386 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019387 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019388 if (p != NULL)
19389 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019391 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019393 if (*p != TAB && needclr)
19394 {
19395 /* remove any text still there from the command */
19396 msg_clr_eos();
19397 needclr = FALSE;
19398 }
19399 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 }
19401 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019402 {
19403#ifdef FEAT_MBYTE
19404 if (has_mbyte)
19405 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019406 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019407
19408 (void)msg_outtrans_len_attr(p, i, echo_attr);
19409 p += i - 1;
19410 }
19411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019413 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019416 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019418 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 arg = skipwhite(arg);
19420 }
19421 eap->nextcmd = check_nextcmd(arg);
19422
19423 if (eap->skip)
19424 --emsg_skip;
19425 else
19426 {
19427 /* remove text that may still be there from the command */
19428 if (needclr)
19429 msg_clr_eos();
19430 if (eap->cmdidx == CMD_echo)
19431 msg_end();
19432 }
19433}
19434
19435/*
19436 * ":echohl {name}".
19437 */
19438 void
19439ex_echohl(eap)
19440 exarg_T *eap;
19441{
19442 int id;
19443
19444 id = syn_name2id(eap->arg);
19445 if (id == 0)
19446 echo_attr = 0;
19447 else
19448 echo_attr = syn_id2attr(id);
19449}
19450
19451/*
19452 * ":execute expr1 ..." execute the result of an expression.
19453 * ":echomsg expr1 ..." Print a message
19454 * ":echoerr expr1 ..." Print an error
19455 * Each gets spaces around each argument and a newline at the end for
19456 * echo commands
19457 */
19458 void
19459ex_execute(eap)
19460 exarg_T *eap;
19461{
19462 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 int ret = OK;
19465 char_u *p;
19466 garray_T ga;
19467 int len;
19468 int save_did_emsg;
19469
19470 ga_init2(&ga, 1, 80);
19471
19472 if (eap->skip)
19473 ++emsg_skip;
19474 while (*arg != NUL && *arg != '|' && *arg != '\n')
19475 {
19476 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 {
19479 /*
19480 * Report the invalid expression unless the expression evaluation
19481 * has been cancelled due to an aborting error, an interrupt, or an
19482 * exception.
19483 */
19484 if (!aborting())
19485 EMSG2(_(e_invexpr2), p);
19486 ret = FAIL;
19487 break;
19488 }
19489
19490 if (!eap->skip)
19491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019492 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 len = (int)STRLEN(p);
19494 if (ga_grow(&ga, len + 2) == FAIL)
19495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 ret = FAIL;
19498 break;
19499 }
19500 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 ga.ga_len += len;
19504 }
19505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019506 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507 arg = skipwhite(arg);
19508 }
19509
19510 if (ret != FAIL && ga.ga_data != NULL)
19511 {
19512 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019515 out_flush();
19516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 else if (eap->cmdidx == CMD_echoerr)
19518 {
19519 /* We don't want to abort following commands, restore did_emsg. */
19520 save_did_emsg = did_emsg;
19521 EMSG((char_u *)ga.ga_data);
19522 if (!force_abort)
19523 did_emsg = save_did_emsg;
19524 }
19525 else if (eap->cmdidx == CMD_execute)
19526 do_cmdline((char_u *)ga.ga_data,
19527 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19528 }
19529
19530 ga_clear(&ga);
19531
19532 if (eap->skip)
19533 --emsg_skip;
19534
19535 eap->nextcmd = check_nextcmd(arg);
19536}
19537
19538/*
19539 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19540 * "arg" points to the "&" or '+' when called, to "option" when returning.
19541 * Returns NULL when no option name found. Otherwise pointer to the char
19542 * after the option name.
19543 */
19544 static char_u *
19545find_option_end(arg, opt_flags)
19546 char_u **arg;
19547 int *opt_flags;
19548{
19549 char_u *p = *arg;
19550
19551 ++p;
19552 if (*p == 'g' && p[1] == ':')
19553 {
19554 *opt_flags = OPT_GLOBAL;
19555 p += 2;
19556 }
19557 else if (*p == 'l' && p[1] == ':')
19558 {
19559 *opt_flags = OPT_LOCAL;
19560 p += 2;
19561 }
19562 else
19563 *opt_flags = 0;
19564
19565 if (!ASCII_ISALPHA(*p))
19566 return NULL;
19567 *arg = p;
19568
19569 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19570 p += 4; /* termcap option */
19571 else
19572 while (ASCII_ISALPHA(*p))
19573 ++p;
19574 return p;
19575}
19576
19577/*
19578 * ":function"
19579 */
19580 void
19581ex_function(eap)
19582 exarg_T *eap;
19583{
19584 char_u *theline;
19585 int j;
19586 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 char_u *name = NULL;
19589 char_u *p;
19590 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019591 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 garray_T newargs;
19593 garray_T newlines;
19594 int varargs = FALSE;
19595 int mustend = FALSE;
19596 int flags = 0;
19597 ufunc_T *fp;
19598 int indent;
19599 int nesting;
19600 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019601 dictitem_T *v;
19602 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019603 static int func_nr = 0; /* number for nameless function */
19604 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019605 hashtab_T *ht;
19606 int todo;
19607 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019608 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609
19610 /*
19611 * ":function" without argument: list functions.
19612 */
19613 if (ends_excmd(*eap->arg))
19614 {
19615 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019616 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019617 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019618 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019619 {
19620 if (!HASHITEM_EMPTY(hi))
19621 {
19622 --todo;
19623 fp = HI2UF(hi);
19624 if (!isdigit(*fp->uf_name))
19625 list_func_head(fp, FALSE);
19626 }
19627 }
19628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 eap->nextcmd = check_nextcmd(eap->arg);
19630 return;
19631 }
19632
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019633 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019634 * ":function /pat": list functions matching pattern.
19635 */
19636 if (*eap->arg == '/')
19637 {
19638 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19639 if (!eap->skip)
19640 {
19641 regmatch_T regmatch;
19642
19643 c = *p;
19644 *p = NUL;
19645 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19646 *p = c;
19647 if (regmatch.regprog != NULL)
19648 {
19649 regmatch.rm_ic = p_ic;
19650
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019651 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019652 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19653 {
19654 if (!HASHITEM_EMPTY(hi))
19655 {
19656 --todo;
19657 fp = HI2UF(hi);
19658 if (!isdigit(*fp->uf_name)
19659 && vim_regexec(&regmatch, fp->uf_name, 0))
19660 list_func_head(fp, FALSE);
19661 }
19662 }
19663 }
19664 }
19665 if (*p == '/')
19666 ++p;
19667 eap->nextcmd = check_nextcmd(p);
19668 return;
19669 }
19670
19671 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019672 * Get the function name. There are these situations:
19673 * func normal function name
19674 * "name" == func, "fudi.fd_dict" == NULL
19675 * dict.func new dictionary entry
19676 * "name" == NULL, "fudi.fd_dict" set,
19677 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19678 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019679 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019680 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19681 * dict.func existing dict entry that's not a Funcref
19682 * "name" == NULL, "fudi.fd_dict" set,
19683 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019686 name = trans_function_name(&p, eap->skip, 0, &fudi);
19687 paren = (vim_strchr(p, '(') != NULL);
19688 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 {
19690 /*
19691 * Return on an invalid expression in braces, unless the expression
19692 * evaluation has been cancelled due to an aborting error, an
19693 * interrupt, or an exception.
19694 */
19695 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019697 if (!eap->skip && fudi.fd_newkey != NULL)
19698 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019699 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 else
19703 eap->skip = TRUE;
19704 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019705
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 /* An error in a function call during evaluation of an expression in magic
19707 * braces should not cause the function not to be defined. */
19708 saved_did_emsg = did_emsg;
19709 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710
19711 /*
19712 * ":function func" with only function name: list function.
19713 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019714 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 {
19716 if (!ends_excmd(*skipwhite(p)))
19717 {
19718 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019719 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 }
19721 eap->nextcmd = check_nextcmd(p);
19722 if (eap->nextcmd != NULL)
19723 *p = NUL;
19724 if (!eap->skip && !got_int)
19725 {
19726 fp = find_func(name);
19727 if (fp != NULL)
19728 {
19729 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019730 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019732 if (FUNCLINE(fp, j) == NULL)
19733 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 msg_putchar('\n');
19735 msg_outnum((long)(j + 1));
19736 if (j < 9)
19737 msg_putchar(' ');
19738 if (j < 99)
19739 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019740 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 out_flush(); /* show a line at a time */
19742 ui_breakcheck();
19743 }
19744 if (!got_int)
19745 {
19746 msg_putchar('\n');
19747 msg_puts((char_u *)" endfunction");
19748 }
19749 }
19750 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019751 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019753 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 }
19755
19756 /*
19757 * ":function name(arg1, arg2)" Define function.
19758 */
19759 p = skipwhite(p);
19760 if (*p != '(')
19761 {
19762 if (!eap->skip)
19763 {
19764 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019765 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 }
19767 /* attempt to continue by skipping some text */
19768 if (vim_strchr(p, '(') != NULL)
19769 p = vim_strchr(p, '(');
19770 }
19771 p = skipwhite(p + 1);
19772
19773 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19774 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19775
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019776 if (!eap->skip)
19777 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019778 /* Check the name of the function. Unless it's a dictionary function
19779 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019780 if (name != NULL)
19781 arg = name;
19782 else
19783 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019784 if (arg != NULL && (fudi.fd_di == NULL
19785 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019786 {
19787 if (*arg == K_SPECIAL)
19788 j = 3;
19789 else
19790 j = 0;
19791 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19792 : eval_isnamec(arg[j])))
19793 ++j;
19794 if (arg[j] != NUL)
19795 emsg_funcname(_(e_invarg2), arg);
19796 }
19797 }
19798
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 /*
19800 * Isolate the arguments: "arg1, arg2, ...)"
19801 */
19802 while (*p != ')')
19803 {
19804 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19805 {
19806 varargs = TRUE;
19807 p += 3;
19808 mustend = TRUE;
19809 }
19810 else
19811 {
19812 arg = p;
19813 while (ASCII_ISALNUM(*p) || *p == '_')
19814 ++p;
19815 if (arg == p || isdigit(*arg)
19816 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19817 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19818 {
19819 if (!eap->skip)
19820 EMSG2(_("E125: Illegal argument: %s"), arg);
19821 break;
19822 }
19823 if (ga_grow(&newargs, 1) == FAIL)
19824 goto erret;
19825 c = *p;
19826 *p = NUL;
19827 arg = vim_strsave(arg);
19828 if (arg == NULL)
19829 goto erret;
19830 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19831 *p = c;
19832 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 if (*p == ',')
19834 ++p;
19835 else
19836 mustend = TRUE;
19837 }
19838 p = skipwhite(p);
19839 if (mustend && *p != ')')
19840 {
19841 if (!eap->skip)
19842 EMSG2(_(e_invarg2), eap->arg);
19843 break;
19844 }
19845 }
19846 ++p; /* skip the ')' */
19847
Bram Moolenaare9a41262005-01-15 22:18:47 +000019848 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 for (;;)
19850 {
19851 p = skipwhite(p);
19852 if (STRNCMP(p, "range", 5) == 0)
19853 {
19854 flags |= FC_RANGE;
19855 p += 5;
19856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019857 else if (STRNCMP(p, "dict", 4) == 0)
19858 {
19859 flags |= FC_DICT;
19860 p += 4;
19861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 else if (STRNCMP(p, "abort", 5) == 0)
19863 {
19864 flags |= FC_ABORT;
19865 p += 5;
19866 }
19867 else
19868 break;
19869 }
19870
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019871 /* When there is a line break use what follows for the function body.
19872 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19873 if (*p == '\n')
19874 line_arg = p + 1;
19875 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 EMSG(_(e_trailing));
19877
19878 /*
19879 * Read the body of the function, until ":endfunction" is found.
19880 */
19881 if (KeyTyped)
19882 {
19883 /* Check if the function already exists, don't let the user type the
19884 * whole function before telling him it doesn't work! For a script we
19885 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019886 if (!eap->skip && !eap->forceit)
19887 {
19888 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19889 EMSG(_(e_funcdict));
19890 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019891 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019894 if (!eap->skip && did_emsg)
19895 goto erret;
19896
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 msg_putchar('\n'); /* don't overwrite the function name */
19898 cmdline_row = msg_row;
19899 }
19900
19901 indent = 2;
19902 nesting = 0;
19903 for (;;)
19904 {
19905 msg_scroll = TRUE;
19906 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019907 sourcing_lnum_off = sourcing_lnum;
19908
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019909 if (line_arg != NULL)
19910 {
19911 /* Use eap->arg, split up in parts by line breaks. */
19912 theline = line_arg;
19913 p = vim_strchr(theline, '\n');
19914 if (p == NULL)
19915 line_arg += STRLEN(line_arg);
19916 else
19917 {
19918 *p = NUL;
19919 line_arg = p + 1;
19920 }
19921 }
19922 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 theline = getcmdline(':', 0L, indent);
19924 else
19925 theline = eap->getline(':', eap->cookie, indent);
19926 if (KeyTyped)
19927 lines_left = Rows - 1;
19928 if (theline == NULL)
19929 {
19930 EMSG(_("E126: Missing :endfunction"));
19931 goto erret;
19932 }
19933
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019934 /* Detect line continuation: sourcing_lnum increased more than one. */
19935 if (sourcing_lnum > sourcing_lnum_off + 1)
19936 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19937 else
19938 sourcing_lnum_off = 0;
19939
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 if (skip_until != NULL)
19941 {
19942 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19943 * don't check for ":endfunc". */
19944 if (STRCMP(theline, skip_until) == 0)
19945 {
19946 vim_free(skip_until);
19947 skip_until = NULL;
19948 }
19949 }
19950 else
19951 {
19952 /* skip ':' and blanks*/
19953 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19954 ;
19955
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019956 /* Check for "endfunction". */
19957 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019959 if (line_arg == NULL)
19960 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 break;
19962 }
19963
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019964 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 * at "end". */
19966 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19967 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019968 else if (STRNCMP(p, "if", 2) == 0
19969 || STRNCMP(p, "wh", 2) == 0
19970 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 || STRNCMP(p, "try", 3) == 0)
19972 indent += 2;
19973
19974 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019975 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019977 if (*p == '!')
19978 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 p += eval_fname_script(p);
19980 if (ASCII_ISALPHA(*p))
19981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019982 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 if (*skipwhite(p) == '(')
19984 {
19985 ++nesting;
19986 indent += 2;
19987 }
19988 }
19989 }
19990
19991 /* Check for ":append" or ":insert". */
19992 p = skip_range(p, NULL);
19993 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19994 || (p[0] == 'i'
19995 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19996 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19997 skip_until = vim_strsave((char_u *)".");
19998
19999 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20000 arg = skipwhite(skiptowhite(p));
20001 if (arg[0] == '<' && arg[1] =='<'
20002 && ((p[0] == 'p' && p[1] == 'y'
20003 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20004 || (p[0] == 'p' && p[1] == 'e'
20005 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20006 || (p[0] == 't' && p[1] == 'c'
20007 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20008 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20009 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020010 || (p[0] == 'm' && p[1] == 'z'
20011 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 ))
20013 {
20014 /* ":python <<" continues until a dot, like ":append" */
20015 p = skipwhite(arg + 2);
20016 if (*p == NUL)
20017 skip_until = vim_strsave((char_u *)".");
20018 else
20019 skip_until = vim_strsave(p);
20020 }
20021 }
20022
20023 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020024 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020025 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020026 if (line_arg == NULL)
20027 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020029 }
20030
20031 /* Copy the line to newly allocated memory. get_one_sourceline()
20032 * allocates 250 bytes per line, this saves 80% on average. The cost
20033 * is an extra alloc/free. */
20034 p = vim_strsave(theline);
20035 if (p != NULL)
20036 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020037 if (line_arg == NULL)
20038 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020039 theline = p;
20040 }
20041
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020042 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20043
20044 /* Add NULL lines for continuation lines, so that the line count is
20045 * equal to the index in the growarray. */
20046 while (sourcing_lnum_off-- > 0)
20047 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020048
20049 /* Check for end of eap->arg. */
20050 if (line_arg != NULL && *line_arg == NUL)
20051 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 }
20053
20054 /* Don't define the function when skipping commands or when an error was
20055 * detected. */
20056 if (eap->skip || did_emsg)
20057 goto erret;
20058
20059 /*
20060 * If there are no errors, add the function
20061 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020062 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020063 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020064 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020066 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020067 emsg_funcname("E707: Function name conflicts with variable: %s",
20068 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020069 goto erret;
20070 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020071
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020072 fp = find_func(name);
20073 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020075 if (!eap->forceit)
20076 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020077 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078 goto erret;
20079 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020080 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020081 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020082 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020083 name);
20084 goto erret;
20085 }
20086 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020087 ga_clear_strings(&(fp->uf_args));
20088 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020089 vim_free(name);
20090 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 }
20093 else
20094 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020095 char numbuf[20];
20096
20097 fp = NULL;
20098 if (fudi.fd_newkey == NULL && !eap->forceit)
20099 {
20100 EMSG(_(e_funcdict));
20101 goto erret;
20102 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020103 if (fudi.fd_di == NULL)
20104 {
20105 /* Can't add a function to a locked dictionary */
20106 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20107 goto erret;
20108 }
20109 /* Can't change an existing function if it is locked */
20110 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20111 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020112
20113 /* Give the function a sequential number. Can only be used with a
20114 * Funcref! */
20115 vim_free(name);
20116 sprintf(numbuf, "%d", ++func_nr);
20117 name = vim_strsave((char_u *)numbuf);
20118 if (name == NULL)
20119 goto erret;
20120 }
20121
20122 if (fp == NULL)
20123 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020124 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020125 {
20126 int slen, plen;
20127 char_u *scriptname;
20128
20129 /* Check that the autoload name matches the script name. */
20130 j = FAIL;
20131 if (sourcing_name != NULL)
20132 {
20133 scriptname = autoload_name(name);
20134 if (scriptname != NULL)
20135 {
20136 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020137 plen = (int)STRLEN(p);
20138 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020139 if (slen > plen && fnamecmp(p,
20140 sourcing_name + slen - plen) == 0)
20141 j = OK;
20142 vim_free(scriptname);
20143 }
20144 }
20145 if (j == FAIL)
20146 {
20147 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20148 goto erret;
20149 }
20150 }
20151
20152 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 if (fp == NULL)
20154 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020155
20156 if (fudi.fd_dict != NULL)
20157 {
20158 if (fudi.fd_di == NULL)
20159 {
20160 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020161 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020162 if (fudi.fd_di == NULL)
20163 {
20164 vim_free(fp);
20165 goto erret;
20166 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020167 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20168 {
20169 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020170 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020171 goto erret;
20172 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020173 }
20174 else
20175 /* overwrite existing dict entry */
20176 clear_tv(&fudi.fd_di->di_tv);
20177 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020178 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020179 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020180 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020181
20182 /* behave like "dict" was used */
20183 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020184 }
20185
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020187 STRCPY(fp->uf_name, name);
20188 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020190 fp->uf_args = newargs;
20191 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020192#ifdef FEAT_PROFILE
20193 fp->uf_tml_count = NULL;
20194 fp->uf_tml_total = NULL;
20195 fp->uf_tml_self = NULL;
20196 fp->uf_profiling = FALSE;
20197 if (prof_def_func())
20198 func_do_profile(fp);
20199#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020200 fp->uf_varargs = varargs;
20201 fp->uf_flags = flags;
20202 fp->uf_calls = 0;
20203 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020204 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205
20206erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 ga_clear_strings(&newargs);
20208 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020209ret_free:
20210 vim_free(skip_until);
20211 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214}
20215
20216/*
20217 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020218 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020220 * flags:
20221 * TFN_INT: internal function name OK
20222 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 * Advances "pp" to just after the function name (if no error).
20224 */
20225 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020226trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 char_u **pp;
20228 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020229 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020232 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 char_u *start;
20234 char_u *end;
20235 int lead;
20236 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020238 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020239
20240 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020241 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020243
20244 /* Check for hard coded <SNR>: already translated function ID (from a user
20245 * command). */
20246 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20247 && (*pp)[2] == (int)KE_SNR)
20248 {
20249 *pp += 3;
20250 len = get_id_len(pp) + 3;
20251 return vim_strnsave(start, len);
20252 }
20253
20254 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20255 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020257 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020259
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020260 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20261 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020262 if (end == start)
20263 {
20264 if (!skip)
20265 EMSG(_("E129: Function name required"));
20266 goto theend;
20267 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020268 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020269 {
20270 /*
20271 * Report an invalid expression in braces, unless the expression
20272 * evaluation has been cancelled due to an aborting error, an
20273 * interrupt, or an exception.
20274 */
20275 if (!aborting())
20276 {
20277 if (end != NULL)
20278 EMSG2(_(e_invarg2), start);
20279 }
20280 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020281 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020282 goto theend;
20283 }
20284
20285 if (lv.ll_tv != NULL)
20286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020287 if (fdp != NULL)
20288 {
20289 fdp->fd_dict = lv.ll_dict;
20290 fdp->fd_newkey = lv.ll_newkey;
20291 lv.ll_newkey = NULL;
20292 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020293 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020294 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20295 {
20296 name = vim_strsave(lv.ll_tv->vval.v_string);
20297 *pp = end;
20298 }
20299 else
20300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020301 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20302 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020303 EMSG(_(e_funcref));
20304 else
20305 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020306 name = NULL;
20307 }
20308 goto theend;
20309 }
20310
20311 if (lv.ll_name == NULL)
20312 {
20313 /* Error found, but continue after the function name. */
20314 *pp = end;
20315 goto theend;
20316 }
20317
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020318 /* Check if the name is a Funcref. If so, use the value. */
20319 if (lv.ll_exp_name != NULL)
20320 {
20321 len = (int)STRLEN(lv.ll_exp_name);
20322 name = deref_func_name(lv.ll_exp_name, &len);
20323 if (name == lv.ll_exp_name)
20324 name = NULL;
20325 }
20326 else
20327 {
20328 len = (int)(end - *pp);
20329 name = deref_func_name(*pp, &len);
20330 if (name == *pp)
20331 name = NULL;
20332 }
20333 if (name != NULL)
20334 {
20335 name = vim_strsave(name);
20336 *pp = end;
20337 goto theend;
20338 }
20339
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020340 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020341 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020342 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020343 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20344 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20345 {
20346 /* When there was "s:" already or the name expanded to get a
20347 * leading "s:" then remove it. */
20348 lv.ll_name += 2;
20349 len -= 2;
20350 lead = 2;
20351 }
20352 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020353 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020354 {
20355 if (lead == 2) /* skip over "s:" */
20356 lv.ll_name += 2;
20357 len = (int)(end - lv.ll_name);
20358 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020359
20360 /*
20361 * Copy the function name to allocated memory.
20362 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20363 * Accept <SNR>123_name() outside a script.
20364 */
20365 if (skip)
20366 lead = 0; /* do nothing */
20367 else if (lead > 0)
20368 {
20369 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020370 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20371 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020372 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020373 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020374 if (current_SID <= 0)
20375 {
20376 EMSG(_(e_usingsid));
20377 goto theend;
20378 }
20379 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20380 lead += (int)STRLEN(sid_buf);
20381 }
20382 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020383 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020384 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020385 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020386 goto theend;
20387 }
20388 name = alloc((unsigned)(len + lead + 1));
20389 if (name != NULL)
20390 {
20391 if (lead > 0)
20392 {
20393 name[0] = K_SPECIAL;
20394 name[1] = KS_EXTRA;
20395 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020396 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020397 STRCPY(name + 3, sid_buf);
20398 }
20399 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20400 name[len + lead] = NUL;
20401 }
20402 *pp = end;
20403
20404theend:
20405 clear_lval(&lv);
20406 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407}
20408
20409/*
20410 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20411 * Return 2 if "p" starts with "s:".
20412 * Return 0 otherwise.
20413 */
20414 static int
20415eval_fname_script(p)
20416 char_u *p;
20417{
20418 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20419 || STRNICMP(p + 1, "SNR>", 4) == 0))
20420 return 5;
20421 if (p[0] == 's' && p[1] == ':')
20422 return 2;
20423 return 0;
20424}
20425
20426/*
20427 * Return TRUE if "p" starts with "<SID>" or "s:".
20428 * Only works if eval_fname_script() returned non-zero for "p"!
20429 */
20430 static int
20431eval_fname_sid(p)
20432 char_u *p;
20433{
20434 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20435}
20436
20437/*
20438 * List the head of the function: "name(arg1, arg2)".
20439 */
20440 static void
20441list_func_head(fp, indent)
20442 ufunc_T *fp;
20443 int indent;
20444{
20445 int j;
20446
20447 msg_start();
20448 if (indent)
20449 MSG_PUTS(" ");
20450 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020451 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 {
20453 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020454 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 }
20456 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020457 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020459 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 {
20461 if (j)
20462 MSG_PUTS(", ");
20463 msg_puts(FUNCARG(fp, j));
20464 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020465 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 {
20467 if (j)
20468 MSG_PUTS(", ");
20469 MSG_PUTS("...");
20470 }
20471 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020472 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020473 if (p_verbose > 0)
20474 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475}
20476
20477/*
20478 * Find a function by name, return pointer to it in ufuncs.
20479 * Return NULL for unknown function.
20480 */
20481 static ufunc_T *
20482find_func(name)
20483 char_u *name;
20484{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020485 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020487 hi = hash_find(&func_hashtab, name);
20488 if (!HASHITEM_EMPTY(hi))
20489 return HI2UF(hi);
20490 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491}
20492
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020493#if defined(EXITFREE) || defined(PROTO)
20494 void
20495free_all_functions()
20496{
20497 hashitem_T *hi;
20498
20499 /* Need to start all over every time, because func_free() may change the
20500 * hash table. */
20501 while (func_hashtab.ht_used > 0)
20502 for (hi = func_hashtab.ht_array; ; ++hi)
20503 if (!HASHITEM_EMPTY(hi))
20504 {
20505 func_free(HI2UF(hi));
20506 break;
20507 }
20508}
20509#endif
20510
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020511/*
20512 * Return TRUE if a function "name" exists.
20513 */
20514 static int
20515function_exists(name)
20516 char_u *name;
20517{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020518 char_u *nm = name;
20519 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020520 int n = FALSE;
20521
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020522 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020523 nm = skipwhite(nm);
20524
20525 /* Only accept "funcname", "funcname ", "funcname (..." and
20526 * "funcname(...", not "funcname!...". */
20527 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020528 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020529 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020530 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020531 else
20532 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020533 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020534 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020535 return n;
20536}
20537
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020538/*
20539 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020540 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020541 */
20542 static int
20543builtin_function(name)
20544 char_u *name;
20545{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020546 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20547 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020548}
20549
Bram Moolenaar05159a02005-02-26 23:04:13 +000020550#if defined(FEAT_PROFILE) || defined(PROTO)
20551/*
20552 * Start profiling function "fp".
20553 */
20554 static void
20555func_do_profile(fp)
20556 ufunc_T *fp;
20557{
20558 fp->uf_tm_count = 0;
20559 profile_zero(&fp->uf_tm_self);
20560 profile_zero(&fp->uf_tm_total);
20561 if (fp->uf_tml_count == NULL)
20562 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20563 (sizeof(int) * fp->uf_lines.ga_len));
20564 if (fp->uf_tml_total == NULL)
20565 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20566 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20567 if (fp->uf_tml_self == NULL)
20568 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20569 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20570 fp->uf_tml_idx = -1;
20571 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20572 || fp->uf_tml_self == NULL)
20573 return; /* out of memory */
20574
20575 fp->uf_profiling = TRUE;
20576}
20577
20578/*
20579 * Dump the profiling results for all functions in file "fd".
20580 */
20581 void
20582func_dump_profile(fd)
20583 FILE *fd;
20584{
20585 hashitem_T *hi;
20586 int todo;
20587 ufunc_T *fp;
20588 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020589 ufunc_T **sorttab;
20590 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020591
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020592 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000020593 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20594
Bram Moolenaar05159a02005-02-26 23:04:13 +000020595 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20596 {
20597 if (!HASHITEM_EMPTY(hi))
20598 {
20599 --todo;
20600 fp = HI2UF(hi);
20601 if (fp->uf_profiling)
20602 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020603 if (sorttab != NULL)
20604 sorttab[st_len++] = fp;
20605
Bram Moolenaar05159a02005-02-26 23:04:13 +000020606 if (fp->uf_name[0] == K_SPECIAL)
20607 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20608 else
20609 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20610 if (fp->uf_tm_count == 1)
20611 fprintf(fd, "Called 1 time\n");
20612 else
20613 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20614 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20615 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20616 fprintf(fd, "\n");
20617 fprintf(fd, "count total (s) self (s)\n");
20618
20619 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20620 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020621 if (FUNCLINE(fp, i) == NULL)
20622 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020623 prof_func_line(fd, fp->uf_tml_count[i],
20624 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020625 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20626 }
20627 fprintf(fd, "\n");
20628 }
20629 }
20630 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020631
20632 if (sorttab != NULL && st_len > 0)
20633 {
20634 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20635 prof_total_cmp);
20636 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20637 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20638 prof_self_cmp);
20639 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20640 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020641}
Bram Moolenaar73830342005-02-28 22:48:19 +000020642
20643 static void
20644prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20645 FILE *fd;
20646 ufunc_T **sorttab;
20647 int st_len;
20648 char *title;
20649 int prefer_self; /* when equal print only self time */
20650{
20651 int i;
20652 ufunc_T *fp;
20653
20654 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20655 fprintf(fd, "count total (s) self (s) function\n");
20656 for (i = 0; i < 20 && i < st_len; ++i)
20657 {
20658 fp = sorttab[i];
20659 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20660 prefer_self);
20661 if (fp->uf_name[0] == K_SPECIAL)
20662 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20663 else
20664 fprintf(fd, " %s()\n", fp->uf_name);
20665 }
20666 fprintf(fd, "\n");
20667}
20668
20669/*
20670 * Print the count and times for one function or function line.
20671 */
20672 static void
20673prof_func_line(fd, count, total, self, prefer_self)
20674 FILE *fd;
20675 int count;
20676 proftime_T *total;
20677 proftime_T *self;
20678 int prefer_self; /* when equal print only self time */
20679{
20680 if (count > 0)
20681 {
20682 fprintf(fd, "%5d ", count);
20683 if (prefer_self && profile_equal(total, self))
20684 fprintf(fd, " ");
20685 else
20686 fprintf(fd, "%s ", profile_msg(total));
20687 if (!prefer_self && profile_equal(total, self))
20688 fprintf(fd, " ");
20689 else
20690 fprintf(fd, "%s ", profile_msg(self));
20691 }
20692 else
20693 fprintf(fd, " ");
20694}
20695
20696/*
20697 * Compare function for total time sorting.
20698 */
20699 static int
20700#ifdef __BORLANDC__
20701_RTLENTRYF
20702#endif
20703prof_total_cmp(s1, s2)
20704 const void *s1;
20705 const void *s2;
20706{
20707 ufunc_T *p1, *p2;
20708
20709 p1 = *(ufunc_T **)s1;
20710 p2 = *(ufunc_T **)s2;
20711 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20712}
20713
20714/*
20715 * Compare function for self time sorting.
20716 */
20717 static int
20718#ifdef __BORLANDC__
20719_RTLENTRYF
20720#endif
20721prof_self_cmp(s1, s2)
20722 const void *s1;
20723 const void *s2;
20724{
20725 ufunc_T *p1, *p2;
20726
20727 p1 = *(ufunc_T **)s1;
20728 p2 = *(ufunc_T **)s2;
20729 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20730}
20731
Bram Moolenaar05159a02005-02-26 23:04:13 +000020732#endif
20733
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020734/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020735 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020736 * Return TRUE if a package was loaded.
20737 */
20738 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020739script_autoload(name, reload)
20740 char_u *name;
20741 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020742{
20743 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020744 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020745 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020746 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020747
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020748 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020749 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020750 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020751 return FALSE;
20752
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020753 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020754
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020755 /* Find the name in the list of previously loaded package names. Skip
20756 * "autoload/", it's always the same. */
20757 for (i = 0; i < ga_loaded.ga_len; ++i)
20758 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20759 break;
20760 if (!reload && i < ga_loaded.ga_len)
20761 ret = FALSE; /* was loaded already */
20762 else
20763 {
20764 /* Remember the name if it wasn't loaded already. */
20765 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20766 {
20767 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20768 tofree = NULL;
20769 }
20770
20771 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020772 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020773 ret = TRUE;
20774 }
20775
20776 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020777 return ret;
20778}
20779
20780/*
20781 * Return the autoload script name for a function or variable name.
20782 * Returns NULL when out of memory.
20783 */
20784 static char_u *
20785autoload_name(name)
20786 char_u *name;
20787{
20788 char_u *p;
20789 char_u *scriptname;
20790
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020791 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020792 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20793 if (scriptname == NULL)
20794 return FALSE;
20795 STRCPY(scriptname, "autoload/");
20796 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020797 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020798 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020799 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020800 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020801 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020802}
20803
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20805
20806/*
20807 * Function given to ExpandGeneric() to obtain the list of user defined
20808 * function names.
20809 */
20810 char_u *
20811get_user_func_name(xp, idx)
20812 expand_T *xp;
20813 int idx;
20814{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020815 static long_u done;
20816 static hashitem_T *hi;
20817 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818
20819 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020821 done = 0;
20822 hi = func_hashtab.ht_array;
20823 }
20824 if (done < func_hashtab.ht_used)
20825 {
20826 if (done++ > 0)
20827 ++hi;
20828 while (HASHITEM_EMPTY(hi))
20829 ++hi;
20830 fp = HI2UF(hi);
20831
20832 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20833 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834
20835 cat_func_name(IObuff, fp);
20836 if (xp->xp_context != EXPAND_USER_FUNC)
20837 {
20838 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020839 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 STRCAT(IObuff, ")");
20841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 return IObuff;
20843 }
20844 return NULL;
20845}
20846
20847#endif /* FEAT_CMDL_COMPL */
20848
20849/*
20850 * Copy the function name of "fp" to buffer "buf".
20851 * "buf" must be able to hold the function name plus three bytes.
20852 * Takes care of script-local function names.
20853 */
20854 static void
20855cat_func_name(buf, fp)
20856 char_u *buf;
20857 ufunc_T *fp;
20858{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020859 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 {
20861 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020862 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 }
20864 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020865 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866}
20867
20868/*
20869 * ":delfunction {name}"
20870 */
20871 void
20872ex_delfunction(eap)
20873 exarg_T *eap;
20874{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020875 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 char_u *p;
20877 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020878 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879
20880 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020881 name = trans_function_name(&p, eap->skip, 0, &fudi);
20882 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020884 {
20885 if (fudi.fd_dict != NULL && !eap->skip)
20886 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 if (!ends_excmd(*skipwhite(p)))
20890 {
20891 vim_free(name);
20892 EMSG(_(e_trailing));
20893 return;
20894 }
20895 eap->nextcmd = check_nextcmd(p);
20896 if (eap->nextcmd != NULL)
20897 *p = NUL;
20898
20899 if (!eap->skip)
20900 fp = find_func(name);
20901 vim_free(name);
20902
20903 if (!eap->skip)
20904 {
20905 if (fp == NULL)
20906 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020907 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 return;
20909 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020910 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 {
20912 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20913 return;
20914 }
20915
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020916 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020918 /* Delete the dict item that refers to the function, it will
20919 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020920 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020922 else
20923 func_free(fp);
20924 }
20925}
20926
20927/*
20928 * Free a function and remove it from the list of functions.
20929 */
20930 static void
20931func_free(fp)
20932 ufunc_T *fp;
20933{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020934 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020935
20936 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020937 ga_clear_strings(&(fp->uf_args));
20938 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020939#ifdef FEAT_PROFILE
20940 vim_free(fp->uf_tml_count);
20941 vim_free(fp->uf_tml_total);
20942 vim_free(fp->uf_tml_self);
20943#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020944
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020945 /* remove the function from the function hashtable */
20946 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20947 if (HASHITEM_EMPTY(hi))
20948 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020949 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020950 hash_remove(&func_hashtab, hi);
20951
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020952 vim_free(fp);
20953}
20954
20955/*
20956 * Unreference a Function: decrement the reference count and free it when it
20957 * becomes zero. Only for numbered functions.
20958 */
20959 static void
20960func_unref(name)
20961 char_u *name;
20962{
20963 ufunc_T *fp;
20964
20965 if (name != NULL && isdigit(*name))
20966 {
20967 fp = find_func(name);
20968 if (fp == NULL)
20969 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020970 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020971 {
20972 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020973 * when "uf_calls" becomes zero. */
20974 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020975 func_free(fp);
20976 }
20977 }
20978}
20979
20980/*
20981 * Count a reference to a Function.
20982 */
20983 static void
20984func_ref(name)
20985 char_u *name;
20986{
20987 ufunc_T *fp;
20988
20989 if (name != NULL && isdigit(*name))
20990 {
20991 fp = find_func(name);
20992 if (fp == NULL)
20993 EMSG2(_(e_intern2), "func_ref()");
20994 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020995 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 }
20997}
20998
20999/*
21000 * Call a user function.
21001 */
21002 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021003call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 ufunc_T *fp; /* pointer to function */
21005 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021006 typval_T *argvars; /* arguments */
21007 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 linenr_T firstline; /* first line of range */
21009 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021010 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011{
Bram Moolenaar33570922005-01-25 22:26:29 +000021012 char_u *save_sourcing_name;
21013 linenr_T save_sourcing_lnum;
21014 scid_T save_current_SID;
21015 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021016 int save_did_emsg;
21017 static int depth = 0;
21018 dictitem_T *v;
21019 int fixvar_idx = 0; /* index in fixvar[] */
21020 int i;
21021 int ai;
21022 char_u numbuf[NUMBUFLEN];
21023 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021024#ifdef FEAT_PROFILE
21025 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021026 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028
21029 /* If depth of calling is getting too high, don't execute the function */
21030 if (depth >= p_mfd)
21031 {
21032 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021033 rettv->v_type = VAR_NUMBER;
21034 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 return;
21036 }
21037 ++depth;
21038
21039 line_breakcheck(); /* check for CTRL-C hit */
21040
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021041 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000021042 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021044 fc.rettv = rettv;
21045 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 fc.linenr = 0;
21047 fc.returned = FALSE;
21048 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021050 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 fc.dbg_tick = debug_tick;
21052
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 /*
21054 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21055 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21056 * each argument variable and saves a lot of time.
21057 */
21058 /*
21059 * Init l: variables.
21060 */
21061 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021062 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021063 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021064 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21065 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021066 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021067 name = v->di_key;
21068 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021069 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
21070 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
21071 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021072 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021073 v->di_tv.vval.v_dict = selfdict;
21074 ++selfdict->dv_refcount;
21075 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021076
Bram Moolenaar33570922005-01-25 22:26:29 +000021077 /*
21078 * Init a: variables.
21079 * Set a:0 to "argcount".
21080 * Set a:000 to a list with room for the "..." arguments.
21081 */
21082 init_var_dict(&fc.l_avars, &fc.l_avars_var);
21083 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021084 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000021085 v = &fc.fixvar[fixvar_idx++].var;
21086 STRCPY(v->di_key, "000");
21087 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21088 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21089 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021090 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 v->di_tv.vval.v_list = &fc.l_varlist;
21092 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
21093 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000021094 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021095
21096 /*
21097 * Set a:firstline to "firstline" and a:lastline to "lastline".
21098 * Set a:name to named arguments.
21099 * Set a:N to the "..." arguments.
21100 */
21101 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
21102 (varnumber_T)firstline);
21103 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
21104 (varnumber_T)lastline);
21105 for (i = 0; i < argcount; ++i)
21106 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021107 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021108 if (ai < 0)
21109 /* named argument a:name */
21110 name = FUNCARG(fp, i);
21111 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021112 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021113 /* "..." argument a:1, a:2, etc. */
21114 sprintf((char *)numbuf, "%d", ai + 1);
21115 name = numbuf;
21116 }
21117 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21118 {
21119 v = &fc.fixvar[fixvar_idx++].var;
21120 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21121 }
21122 else
21123 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021124 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21125 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021126 if (v == NULL)
21127 break;
21128 v->di_flags = DI_FLAGS_RO;
21129 }
21130 STRCPY(v->di_key, name);
21131 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21132
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021133 /* Note: the values are copied directly to avoid alloc/free.
21134 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021135 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021136 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021137
21138 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21139 {
21140 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
21141 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021142 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021143 }
21144 }
21145
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 /* Don't redraw while executing the function. */
21147 ++RedrawingDisabled;
21148 save_sourcing_name = sourcing_name;
21149 save_sourcing_lnum = sourcing_lnum;
21150 sourcing_lnum = 1;
21151 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021152 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 if (sourcing_name != NULL)
21154 {
21155 if (save_sourcing_name != NULL
21156 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21157 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21158 else
21159 STRCPY(sourcing_name, "function ");
21160 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21161
21162 if (p_verbose >= 12)
21163 {
21164 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021165 verbose_enter_scroll();
21166
Bram Moolenaar555b2802005-05-19 21:08:39 +000021167 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 if (p_verbose >= 14)
21169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021171 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021172 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021173 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174
21175 msg_puts((char_u *)"(");
21176 for (i = 0; i < argcount; ++i)
21177 {
21178 if (i > 0)
21179 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021180 if (argvars[i].v_type == VAR_NUMBER)
21181 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 else
21183 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021184 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21185 if (s != NULL)
21186 {
21187 trunc_string(s, buf, MSG_BUF_CLEN);
21188 msg_puts(buf);
21189 vim_free(tofree);
21190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 }
21192 }
21193 msg_puts((char_u *)")");
21194 }
21195 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021196
21197 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 --no_wait_return;
21199 }
21200 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021201#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021202 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021203 {
21204 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21205 func_do_profile(fp);
21206 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021207 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021208 {
21209 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021210 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021211 profile_zero(&fp->uf_tm_children);
21212 }
21213 script_prof_save(&wait_start);
21214 }
21215#endif
21216
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021218 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 save_did_emsg = did_emsg;
21220 did_emsg = FALSE;
21221
21222 /* call do_cmdline() to execute the lines */
21223 do_cmdline(NULL, get_func_line, (void *)&fc,
21224 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21225
21226 --RedrawingDisabled;
21227
21228 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021229 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021231 clear_tv(rettv);
21232 rettv->v_type = VAR_NUMBER;
21233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 }
21235
Bram Moolenaar05159a02005-02-26 23:04:13 +000021236#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021237 if (do_profiling == PROF_YES && (fp->uf_profiling
21238 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021239 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021240 profile_end(&call_start);
21241 profile_sub_wait(&wait_start, &call_start);
21242 profile_add(&fp->uf_tm_total, &call_start);
21243 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021244 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021245 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021246 profile_add(&fc.caller->func->uf_tm_children, &call_start);
21247 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021248 }
21249 }
21250#endif
21251
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 /* when being verbose, mention the return value */
21253 if (p_verbose >= 12)
21254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021256 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021259 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021260 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021261 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
21262 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021263 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021265 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021266 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021267 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021268 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021269
Bram Moolenaar555b2802005-05-19 21:08:39 +000021270 /* The value may be very long. Skip the middle part, so that we
21271 * have some idea how it starts and ends. smsg() would always
21272 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021273 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
21274 if (s != NULL)
21275 {
21276 trunc_string(s, buf, MSG_BUF_CLEN);
21277 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21278 vim_free(tofree);
21279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 }
21281 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021282
21283 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 --no_wait_return;
21285 }
21286
21287 vim_free(sourcing_name);
21288 sourcing_name = save_sourcing_name;
21289 sourcing_lnum = save_sourcing_lnum;
21290 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021291#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021292 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021293 script_prof_restore(&wait_start);
21294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295
21296 if (p_verbose >= 12 && sourcing_name != NULL)
21297 {
21298 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021299 verbose_enter_scroll();
21300
Bram Moolenaar555b2802005-05-19 21:08:39 +000021301 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021303
21304 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 --no_wait_return;
21306 }
21307
21308 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021309 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021311 /* The a: variables typevals were not allocated, only free the allocated
Bram Moolenaar33570922005-01-25 22:26:29 +000021312 * variables. */
21313 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
21314
21315 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 --depth;
21317}
21318
21319/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021320 * Add a number variable "name" to dict "dp" with value "nr".
21321 */
21322 static void
21323add_nr_var(dp, v, name, nr)
21324 dict_T *dp;
21325 dictitem_T *v;
21326 char *name;
21327 varnumber_T nr;
21328{
21329 STRCPY(v->di_key, name);
21330 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21331 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21332 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021333 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 v->di_tv.vval.v_number = nr;
21335}
21336
21337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 * ":return [expr]"
21339 */
21340 void
21341ex_return(eap)
21342 exarg_T *eap;
21343{
21344 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021345 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 int returning = FALSE;
21347
21348 if (current_funccal == NULL)
21349 {
21350 EMSG(_("E133: :return not inside a function"));
21351 return;
21352 }
21353
21354 if (eap->skip)
21355 ++emsg_skip;
21356
21357 eap->nextcmd = NULL;
21358 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021359 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 {
21361 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021364 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 }
21366 /* It's safer to return also on error. */
21367 else if (!eap->skip)
21368 {
21369 /*
21370 * Return unless the expression evaluation has been cancelled due to an
21371 * aborting error, an interrupt, or an exception.
21372 */
21373 if (!aborting())
21374 returning = do_return(eap, FALSE, TRUE, NULL);
21375 }
21376
21377 /* When skipping or the return gets pending, advance to the next command
21378 * in this line (!returning). Otherwise, ignore the rest of the line.
21379 * Following lines will be ignored by get_func_line(). */
21380 if (returning)
21381 eap->nextcmd = NULL;
21382 else if (eap->nextcmd == NULL) /* no argument */
21383 eap->nextcmd = check_nextcmd(arg);
21384
21385 if (eap->skip)
21386 --emsg_skip;
21387}
21388
21389/*
21390 * Return from a function. Possibly makes the return pending. Also called
21391 * for a pending return at the ":endtry" or after returning from an extra
21392 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021393 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021394 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 * FALSE when the return gets pending.
21396 */
21397 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021398do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 exarg_T *eap;
21400 int reanimate;
21401 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021402 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403{
21404 int idx;
21405 struct condstack *cstack = eap->cstack;
21406
21407 if (reanimate)
21408 /* Undo the return. */
21409 current_funccal->returned = FALSE;
21410
21411 /*
21412 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21413 * not in its finally clause (which then is to be executed next) is found.
21414 * In this case, make the ":return" pending for execution at the ":endtry".
21415 * Otherwise, return normally.
21416 */
21417 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21418 if (idx >= 0)
21419 {
21420 cstack->cs_pending[idx] = CSTP_RETURN;
21421
21422 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021423 /* A pending return again gets pending. "rettv" points to an
21424 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021426 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 else
21428 {
21429 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021430 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021432 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 {
21436 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021437 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021438 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 else
21440 EMSG(_(e_outofmem));
21441 }
21442 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021443 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444
21445 if (reanimate)
21446 {
21447 /* The pending return value could be overwritten by a ":return"
21448 * without argument in a finally clause; reset the default
21449 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450 current_funccal->rettv->v_type = VAR_NUMBER;
21451 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 }
21453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 }
21456 else
21457 {
21458 current_funccal->returned = TRUE;
21459
21460 /* If the return is carried out now, store the return value. For
21461 * a return immediately after reanimation, the value is already
21462 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021463 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021465 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021468 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 }
21470 }
21471
21472 return idx < 0;
21473}
21474
21475/*
21476 * Free the variable with a pending return value.
21477 */
21478 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021479discard_pending_return(rettv)
21480 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481{
Bram Moolenaar33570922005-01-25 22:26:29 +000021482 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483}
21484
21485/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021486 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 * is an allocated string. Used by report_pending() for verbose messages.
21488 */
21489 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021490get_return_cmd(rettv)
21491 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021493 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021494 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021495 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021497 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021498 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021499 if (s == NULL)
21500 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021501
21502 STRCPY(IObuff, ":return ");
21503 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21504 if (STRLEN(s) + 8 >= IOSIZE)
21505 STRCPY(IObuff + IOSIZE - 4, "...");
21506 vim_free(tofree);
21507 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508}
21509
21510/*
21511 * Get next function line.
21512 * Called by do_cmdline() to get the next line.
21513 * Returns allocated string, or NULL for end of function.
21514 */
21515/* ARGSUSED */
21516 char_u *
21517get_func_line(c, cookie, indent)
21518 int c; /* not used */
21519 void *cookie;
21520 int indent; /* not used */
21521{
Bram Moolenaar33570922005-01-25 22:26:29 +000021522 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021523 ufunc_T *fp = fcp->func;
21524 char_u *retval;
21525 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526
21527 /* If breakpoints have been added/deleted need to check for it. */
21528 if (fcp->dbg_tick != debug_tick)
21529 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021530 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 sourcing_lnum);
21532 fcp->dbg_tick = debug_tick;
21533 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021534#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021535 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021536 func_line_end(cookie);
21537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538
Bram Moolenaar05159a02005-02-26 23:04:13 +000021539 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021540 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21541 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 retval = NULL;
21543 else
21544 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021545 /* Skip NULL lines (continuation lines). */
21546 while (fcp->linenr < gap->ga_len
21547 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21548 ++fcp->linenr;
21549 if (fcp->linenr >= gap->ga_len)
21550 retval = NULL;
21551 else
21552 {
21553 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21554 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021555#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021556 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021557 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021558#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 }
21561
21562 /* Did we encounter a breakpoint? */
21563 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21564 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021565 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021567 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 sourcing_lnum);
21569 fcp->dbg_tick = debug_tick;
21570 }
21571
21572 return retval;
21573}
21574
Bram Moolenaar05159a02005-02-26 23:04:13 +000021575#if defined(FEAT_PROFILE) || defined(PROTO)
21576/*
21577 * Called when starting to read a function line.
21578 * "sourcing_lnum" must be correct!
21579 * When skipping lines it may not actually be executed, but we won't find out
21580 * until later and we need to store the time now.
21581 */
21582 void
21583func_line_start(cookie)
21584 void *cookie;
21585{
21586 funccall_T *fcp = (funccall_T *)cookie;
21587 ufunc_T *fp = fcp->func;
21588
21589 if (fp->uf_profiling && sourcing_lnum >= 1
21590 && sourcing_lnum <= fp->uf_lines.ga_len)
21591 {
21592 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021593 /* Skip continuation lines. */
21594 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21595 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021596 fp->uf_tml_execed = FALSE;
21597 profile_start(&fp->uf_tml_start);
21598 profile_zero(&fp->uf_tml_children);
21599 profile_get_wait(&fp->uf_tml_wait);
21600 }
21601}
21602
21603/*
21604 * Called when actually executing a function line.
21605 */
21606 void
21607func_line_exec(cookie)
21608 void *cookie;
21609{
21610 funccall_T *fcp = (funccall_T *)cookie;
21611 ufunc_T *fp = fcp->func;
21612
21613 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21614 fp->uf_tml_execed = TRUE;
21615}
21616
21617/*
21618 * Called when done with a function line.
21619 */
21620 void
21621func_line_end(cookie)
21622 void *cookie;
21623{
21624 funccall_T *fcp = (funccall_T *)cookie;
21625 ufunc_T *fp = fcp->func;
21626
21627 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21628 {
21629 if (fp->uf_tml_execed)
21630 {
21631 ++fp->uf_tml_count[fp->uf_tml_idx];
21632 profile_end(&fp->uf_tml_start);
21633 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021634 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021635 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21636 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021637 }
21638 fp->uf_tml_idx = -1;
21639 }
21640}
21641#endif
21642
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643/*
21644 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021645 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 */
21647 int
21648func_has_ended(cookie)
21649 void *cookie;
21650{
Bram Moolenaar33570922005-01-25 22:26:29 +000021651 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652
21653 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21654 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021655 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 || fcp->returned);
21657}
21658
21659/*
21660 * return TRUE if cookie indicates a function which "abort"s on errors.
21661 */
21662 int
21663func_has_abort(cookie)
21664 void *cookie;
21665{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021666 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667}
21668
21669#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21670typedef enum
21671{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021672 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21673 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21674 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675} var_flavour_T;
21676
21677static var_flavour_T var_flavour __ARGS((char_u *varname));
21678
21679 static var_flavour_T
21680var_flavour(varname)
21681 char_u *varname;
21682{
21683 char_u *p = varname;
21684
21685 if (ASCII_ISUPPER(*p))
21686 {
21687 while (*(++p))
21688 if (ASCII_ISLOWER(*p))
21689 return VAR_FLAVOUR_SESSION;
21690 return VAR_FLAVOUR_VIMINFO;
21691 }
21692 else
21693 return VAR_FLAVOUR_DEFAULT;
21694}
21695#endif
21696
21697#if defined(FEAT_VIMINFO) || defined(PROTO)
21698/*
21699 * Restore global vars that start with a capital from the viminfo file
21700 */
21701 int
21702read_viminfo_varlist(virp, writing)
21703 vir_T *virp;
21704 int writing;
21705{
21706 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021707 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709
21710 if (!writing && (find_viminfo_parameter('!') != NULL))
21711 {
21712 tab = vim_strchr(virp->vir_line + 1, '\t');
21713 if (tab != NULL)
21714 {
21715 *tab++ = '\0'; /* isolate the variable name */
21716 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021717 type = VAR_STRING;
21718#ifdef FEAT_FLOAT
21719 else if (*tab == 'F')
21720 type = VAR_FLOAT;
21721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722
21723 tab = vim_strchr(tab, '\t');
21724 if (tab != NULL)
21725 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021726 tv.v_type = type;
21727 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021728 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021730#ifdef FEAT_FLOAT
21731 else if (type == VAR_FLOAT)
21732 (void)string2float(tab + 1, &tv.vval.v_float);
21733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021735 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021736 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021737 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021738 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 }
21740 }
21741 }
21742
21743 return viminfo_readline(virp);
21744}
21745
21746/*
21747 * Write global vars that start with a capital to the viminfo file
21748 */
21749 void
21750write_viminfo_varlist(fp)
21751 FILE *fp;
21752{
Bram Moolenaar33570922005-01-25 22:26:29 +000021753 hashitem_T *hi;
21754 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021755 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021756 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021757 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021758 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021759 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760
21761 if (find_viminfo_parameter('!') == NULL)
21762 return;
21763
21764 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021765
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021766 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021767 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021769 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021771 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021772 this_var = HI2DI(hi);
21773 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021774 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021776 {
21777 case VAR_STRING: s = "STR"; break;
21778 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021779#ifdef FEAT_FLOAT
21780 case VAR_FLOAT: s = "FLO"; break;
21781#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021782 default: continue;
21783 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021784 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021785 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021786 if (p != NULL)
21787 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021788 vim_free(tofree);
21789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 }
21791 }
21792}
21793#endif
21794
21795#if defined(FEAT_SESSION) || defined(PROTO)
21796 int
21797store_session_globals(fd)
21798 FILE *fd;
21799{
Bram Moolenaar33570922005-01-25 22:26:29 +000021800 hashitem_T *hi;
21801 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021802 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 char_u *p, *t;
21804
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021805 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021808 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021810 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021811 this_var = HI2DI(hi);
21812 if ((this_var->di_tv.v_type == VAR_NUMBER
21813 || this_var->di_tv.v_type == VAR_STRING)
21814 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021815 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021816 /* Escape special characters with a backslash. Turn a LF and
21817 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021818 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021819 (char_u *)"\\\"\n\r");
21820 if (p == NULL) /* out of memory */
21821 break;
21822 for (t = p; *t != NUL; ++t)
21823 if (*t == '\n')
21824 *t = 'n';
21825 else if (*t == '\r')
21826 *t = 'r';
21827 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021828 this_var->di_key,
21829 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21830 : ' ',
21831 p,
21832 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21833 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021834 || put_eol(fd) == FAIL)
21835 {
21836 vim_free(p);
21837 return FAIL;
21838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 vim_free(p);
21840 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021841#ifdef FEAT_FLOAT
21842 else if (this_var->di_tv.v_type == VAR_FLOAT
21843 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21844 {
21845 float_T f = this_var->di_tv.vval.v_float;
21846 int sign = ' ';
21847
21848 if (f < 0)
21849 {
21850 f = -f;
21851 sign = '-';
21852 }
21853 if ((fprintf(fd, "let %s = %c&%f",
21854 this_var->di_key, sign, f) < 0)
21855 || put_eol(fd) == FAIL)
21856 return FAIL;
21857 }
21858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 }
21860 }
21861 return OK;
21862}
21863#endif
21864
Bram Moolenaar661b1822005-07-28 22:36:45 +000021865/*
21866 * Display script name where an item was last set.
21867 * Should only be invoked when 'verbose' is non-zero.
21868 */
21869 void
21870last_set_msg(scriptID)
21871 scid_T scriptID;
21872{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021873 char_u *p;
21874
Bram Moolenaar661b1822005-07-28 22:36:45 +000021875 if (scriptID != 0)
21876 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021877 p = home_replace_save(NULL, get_scriptname(scriptID));
21878 if (p != NULL)
21879 {
21880 verbose_enter();
21881 MSG_PUTS(_("\n\tLast set from "));
21882 MSG_PUTS(p);
21883 vim_free(p);
21884 verbose_leave();
21885 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021886 }
21887}
21888
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889#endif /* FEAT_EVAL */
21890
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021892#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893
21894#ifdef WIN3264
21895/*
21896 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21897 */
21898static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21899static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21900static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21901
21902/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021903 * Get the short path (8.3) for the filename in "fnamep".
21904 * Only works for a valid file name.
21905 * When the path gets longer "fnamep" is changed and the allocated buffer
21906 * is put in "bufp".
21907 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21908 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 */
21910 static int
21911get_short_pathname(fnamep, bufp, fnamelen)
21912 char_u **fnamep;
21913 char_u **bufp;
21914 int *fnamelen;
21915{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021916 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 char_u *newbuf;
21918
21919 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 l = GetShortPathName(*fnamep, *fnamep, len);
21921 if (l > len - 1)
21922 {
21923 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021924 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 newbuf = vim_strnsave(*fnamep, l);
21926 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021927 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928
21929 vim_free(*bufp);
21930 *fnamep = *bufp = newbuf;
21931
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021932 /* Really should always succeed, as the buffer is big enough. */
21933 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 }
21935
21936 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021937 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021941 * Get the short path (8.3) for the filename in "fname". The converted
21942 * path is returned in "bufp".
21943 *
21944 * Some of the directories specified in "fname" may not exist. This function
21945 * will shorten the existing directories at the beginning of the path and then
21946 * append the remaining non-existing path.
21947 *
21948 * fname - Pointer to the filename to shorten. On return, contains the
21949 * pointer to the shortened pathname
21950 * bufp - Pointer to an allocated buffer for the filename.
21951 * fnamelen - Length of the filename pointed to by fname
21952 *
21953 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 */
21955 static int
21956shortpath_for_invalid_fname(fname, bufp, fnamelen)
21957 char_u **fname;
21958 char_u **bufp;
21959 int *fnamelen;
21960{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021961 char_u *short_fname, *save_fname, *pbuf_unused;
21962 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021964 int old_len, len;
21965 int new_len, sfx_len;
21966 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967
21968 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021969 old_len = *fnamelen;
21970 save_fname = vim_strnsave(*fname, old_len);
21971 pbuf_unused = NULL;
21972 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021974 endp = save_fname + old_len - 1; /* Find the end of the copy */
21975 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021977 /*
21978 * Try shortening the supplied path till it succeeds by removing one
21979 * directory at a time from the tail of the path.
21980 */
21981 len = 0;
21982 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021984 /* go back one path-separator */
21985 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
21986 --endp;
21987 if (endp <= save_fname)
21988 break; /* processed the complete path */
21989
21990 /*
21991 * Replace the path separator with a NUL and try to shorten the
21992 * resulting path.
21993 */
21994 ch = *endp;
21995 *endp = 0;
21996 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000021997 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021998 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
21999 {
22000 retval = FAIL;
22001 goto theend;
22002 }
22003 *endp = ch; /* preserve the string */
22004
22005 if (len > 0)
22006 break; /* successfully shortened the path */
22007
22008 /* failed to shorten the path. Skip the path separator */
22009 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 }
22011
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022012 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022014 /*
22015 * Succeeded in shortening the path. Now concatenate the shortened
22016 * path with the remaining path at the tail.
22017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022019 /* Compute the length of the new path. */
22020 sfx_len = (int)(save_endp - endp) + 1;
22021 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022023 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022025 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022027 /* There is not enough space in the currently allocated string,
22028 * copy it to a buffer big enough. */
22029 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022031 {
22032 retval = FAIL;
22033 goto theend;
22034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 }
22036 else
22037 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022038 /* Transfer short_fname to the main buffer (it's big enough),
22039 * unless get_short_pathname() did its work in-place. */
22040 *fname = *bufp = save_fname;
22041 if (short_fname != save_fname)
22042 vim_strncpy(save_fname, short_fname, len);
22043 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022045
22046 /* concat the not-shortened part of the path */
22047 vim_strncpy(*fname + len, endp, sfx_len);
22048 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022050
22051theend:
22052 vim_free(pbuf_unused);
22053 vim_free(save_fname);
22054
22055 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056}
22057
22058/*
22059 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022060 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 */
22062 static int
22063shortpath_for_partial(fnamep, bufp, fnamelen)
22064 char_u **fnamep;
22065 char_u **bufp;
22066 int *fnamelen;
22067{
22068 int sepcount, len, tflen;
22069 char_u *p;
22070 char_u *pbuf, *tfname;
22071 int hasTilde;
22072
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022073 /* Count up the path separators from the RHS.. so we know which part
22074 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022076 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 if (vim_ispathsep(*p))
22078 ++sepcount;
22079
22080 /* Need full path first (use expand_env() to remove a "~/") */
22081 hasTilde = (**fnamep == '~');
22082 if (hasTilde)
22083 pbuf = tfname = expand_env_save(*fnamep);
22084 else
22085 pbuf = tfname = FullName_save(*fnamep, FALSE);
22086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022087 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022089 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091
22092 if (len == 0)
22093 {
22094 /* Don't have a valid filename, so shorten the rest of the
22095 * path if we can. This CAN give us invalid 8.3 filenames, but
22096 * there's not a lot of point in guessing what it might be.
22097 */
22098 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022099 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22100 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 }
22102
22103 /* Count the paths backward to find the beginning of the desired string. */
22104 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022105 {
22106#ifdef FEAT_MBYTE
22107 if (has_mbyte)
22108 p -= mb_head_off(tfname, p);
22109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 if (vim_ispathsep(*p))
22111 {
22112 if (sepcount == 0 || (hasTilde && sepcount == 1))
22113 break;
22114 else
22115 sepcount --;
22116 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 if (hasTilde)
22119 {
22120 --p;
22121 if (p >= tfname)
22122 *p = '~';
22123 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022124 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125 }
22126 else
22127 ++p;
22128
22129 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22130 vim_free(*bufp);
22131 *fnamelen = (int)STRLEN(p);
22132 *bufp = pbuf;
22133 *fnamep = p;
22134
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022135 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136}
22137#endif /* WIN3264 */
22138
22139/*
22140 * Adjust a filename, according to a string of modifiers.
22141 * *fnamep must be NUL terminated when called. When returning, the length is
22142 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022143 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 * When there is an error, *fnamep is set to NULL.
22145 */
22146 int
22147modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22148 char_u *src; /* string with modifiers */
22149 int *usedlen; /* characters after src that are used */
22150 char_u **fnamep; /* file name so far */
22151 char_u **bufp; /* buffer for allocated file name or NULL */
22152 int *fnamelen; /* length of fnamep */
22153{
22154 int valid = 0;
22155 char_u *tail;
22156 char_u *s, *p, *pbuf;
22157 char_u dirname[MAXPATHL];
22158 int c;
22159 int has_fullname = 0;
22160#ifdef WIN3264
22161 int has_shortname = 0;
22162#endif
22163
22164repeat:
22165 /* ":p" - full path/file_name */
22166 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22167 {
22168 has_fullname = 1;
22169
22170 valid |= VALID_PATH;
22171 *usedlen += 2;
22172
22173 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22174 if ((*fnamep)[0] == '~'
22175#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22176 && ((*fnamep)[1] == '/'
22177# ifdef BACKSLASH_IN_FILENAME
22178 || (*fnamep)[1] == '\\'
22179# endif
22180 || (*fnamep)[1] == NUL)
22181
22182#endif
22183 )
22184 {
22185 *fnamep = expand_env_save(*fnamep);
22186 vim_free(*bufp); /* free any allocated file name */
22187 *bufp = *fnamep;
22188 if (*fnamep == NULL)
22189 return -1;
22190 }
22191
22192 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022193 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 {
22195 if (vim_ispathsep(*p)
22196 && p[1] == '.'
22197 && (p[2] == NUL
22198 || vim_ispathsep(p[2])
22199 || (p[2] == '.'
22200 && (p[3] == NUL || vim_ispathsep(p[3])))))
22201 break;
22202 }
22203
22204 /* FullName_save() is slow, don't use it when not needed. */
22205 if (*p != NUL || !vim_isAbsName(*fnamep))
22206 {
22207 *fnamep = FullName_save(*fnamep, *p != NUL);
22208 vim_free(*bufp); /* free any allocated file name */
22209 *bufp = *fnamep;
22210 if (*fnamep == NULL)
22211 return -1;
22212 }
22213
22214 /* Append a path separator to a directory. */
22215 if (mch_isdir(*fnamep))
22216 {
22217 /* Make room for one or two extra characters. */
22218 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22219 vim_free(*bufp); /* free any allocated file name */
22220 *bufp = *fnamep;
22221 if (*fnamep == NULL)
22222 return -1;
22223 add_pathsep(*fnamep);
22224 }
22225 }
22226
22227 /* ":." - path relative to the current directory */
22228 /* ":~" - path relative to the home directory */
22229 /* ":8" - shortname path - postponed till after */
22230 while (src[*usedlen] == ':'
22231 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22232 {
22233 *usedlen += 2;
22234 if (c == '8')
22235 {
22236#ifdef WIN3264
22237 has_shortname = 1; /* Postpone this. */
22238#endif
22239 continue;
22240 }
22241 pbuf = NULL;
22242 /* Need full path first (use expand_env() to remove a "~/") */
22243 if (!has_fullname)
22244 {
22245 if (c == '.' && **fnamep == '~')
22246 p = pbuf = expand_env_save(*fnamep);
22247 else
22248 p = pbuf = FullName_save(*fnamep, FALSE);
22249 }
22250 else
22251 p = *fnamep;
22252
22253 has_fullname = 0;
22254
22255 if (p != NULL)
22256 {
22257 if (c == '.')
22258 {
22259 mch_dirname(dirname, MAXPATHL);
22260 s = shorten_fname(p, dirname);
22261 if (s != NULL)
22262 {
22263 *fnamep = s;
22264 if (pbuf != NULL)
22265 {
22266 vim_free(*bufp); /* free any allocated file name */
22267 *bufp = pbuf;
22268 pbuf = NULL;
22269 }
22270 }
22271 }
22272 else
22273 {
22274 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22275 /* Only replace it when it starts with '~' */
22276 if (*dirname == '~')
22277 {
22278 s = vim_strsave(dirname);
22279 if (s != NULL)
22280 {
22281 *fnamep = s;
22282 vim_free(*bufp);
22283 *bufp = s;
22284 }
22285 }
22286 }
22287 vim_free(pbuf);
22288 }
22289 }
22290
22291 tail = gettail(*fnamep);
22292 *fnamelen = (int)STRLEN(*fnamep);
22293
22294 /* ":h" - head, remove "/file_name", can be repeated */
22295 /* Don't remove the first "/" or "c:\" */
22296 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22297 {
22298 valid |= VALID_HEAD;
22299 *usedlen += 2;
22300 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022301 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022302 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 *fnamelen = (int)(tail - *fnamep);
22304#ifdef VMS
22305 if (*fnamelen > 0)
22306 *fnamelen += 1; /* the path separator is part of the path */
22307#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022308 if (*fnamelen == 0)
22309 {
22310 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22311 p = vim_strsave((char_u *)".");
22312 if (p == NULL)
22313 return -1;
22314 vim_free(*bufp);
22315 *bufp = *fnamep = tail = p;
22316 *fnamelen = 1;
22317 }
22318 else
22319 {
22320 while (tail > s && !after_pathsep(s, tail))
22321 mb_ptr_back(*fnamep, tail);
22322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 }
22324
22325 /* ":8" - shortname */
22326 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22327 {
22328 *usedlen += 2;
22329#ifdef WIN3264
22330 has_shortname = 1;
22331#endif
22332 }
22333
22334#ifdef WIN3264
22335 /* Check shortname after we have done 'heads' and before we do 'tails'
22336 */
22337 if (has_shortname)
22338 {
22339 pbuf = NULL;
22340 /* Copy the string if it is shortened by :h */
22341 if (*fnamelen < (int)STRLEN(*fnamep))
22342 {
22343 p = vim_strnsave(*fnamep, *fnamelen);
22344 if (p == 0)
22345 return -1;
22346 vim_free(*bufp);
22347 *bufp = *fnamep = p;
22348 }
22349
22350 /* Split into two implementations - makes it easier. First is where
22351 * there isn't a full name already, second is where there is.
22352 */
22353 if (!has_fullname && !vim_isAbsName(*fnamep))
22354 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022355 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 return -1;
22357 }
22358 else
22359 {
22360 int l;
22361
22362 /* Simple case, already have the full-name
22363 * Nearly always shorter, so try first time. */
22364 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022365 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 return -1;
22367
22368 if (l == 0)
22369 {
22370 /* Couldn't find the filename.. search the paths.
22371 */
22372 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022373 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 return -1;
22375 }
22376 *fnamelen = l;
22377 }
22378 }
22379#endif /* WIN3264 */
22380
22381 /* ":t" - tail, just the basename */
22382 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22383 {
22384 *usedlen += 2;
22385 *fnamelen -= (int)(tail - *fnamep);
22386 *fnamep = tail;
22387 }
22388
22389 /* ":e" - extension, can be repeated */
22390 /* ":r" - root, without extension, can be repeated */
22391 while (src[*usedlen] == ':'
22392 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22393 {
22394 /* find a '.' in the tail:
22395 * - for second :e: before the current fname
22396 * - otherwise: The last '.'
22397 */
22398 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22399 s = *fnamep - 2;
22400 else
22401 s = *fnamep + *fnamelen - 1;
22402 for ( ; s > tail; --s)
22403 if (s[0] == '.')
22404 break;
22405 if (src[*usedlen + 1] == 'e') /* :e */
22406 {
22407 if (s > tail)
22408 {
22409 *fnamelen += (int)(*fnamep - (s + 1));
22410 *fnamep = s + 1;
22411#ifdef VMS
22412 /* cut version from the extension */
22413 s = *fnamep + *fnamelen - 1;
22414 for ( ; s > *fnamep; --s)
22415 if (s[0] == ';')
22416 break;
22417 if (s > *fnamep)
22418 *fnamelen = s - *fnamep;
22419#endif
22420 }
22421 else if (*fnamep <= tail)
22422 *fnamelen = 0;
22423 }
22424 else /* :r */
22425 {
22426 if (s > tail) /* remove one extension */
22427 *fnamelen = (int)(s - *fnamep);
22428 }
22429 *usedlen += 2;
22430 }
22431
22432 /* ":s?pat?foo?" - substitute */
22433 /* ":gs?pat?foo?" - global substitute */
22434 if (src[*usedlen] == ':'
22435 && (src[*usedlen + 1] == 's'
22436 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22437 {
22438 char_u *str;
22439 char_u *pat;
22440 char_u *sub;
22441 int sep;
22442 char_u *flags;
22443 int didit = FALSE;
22444
22445 flags = (char_u *)"";
22446 s = src + *usedlen + 2;
22447 if (src[*usedlen + 1] == 'g')
22448 {
22449 flags = (char_u *)"g";
22450 ++s;
22451 }
22452
22453 sep = *s++;
22454 if (sep)
22455 {
22456 /* find end of pattern */
22457 p = vim_strchr(s, sep);
22458 if (p != NULL)
22459 {
22460 pat = vim_strnsave(s, (int)(p - s));
22461 if (pat != NULL)
22462 {
22463 s = p + 1;
22464 /* find end of substitution */
22465 p = vim_strchr(s, sep);
22466 if (p != NULL)
22467 {
22468 sub = vim_strnsave(s, (int)(p - s));
22469 str = vim_strnsave(*fnamep, *fnamelen);
22470 if (sub != NULL && str != NULL)
22471 {
22472 *usedlen = (int)(p + 1 - src);
22473 s = do_string_sub(str, pat, sub, flags);
22474 if (s != NULL)
22475 {
22476 *fnamep = s;
22477 *fnamelen = (int)STRLEN(s);
22478 vim_free(*bufp);
22479 *bufp = s;
22480 didit = TRUE;
22481 }
22482 }
22483 vim_free(sub);
22484 vim_free(str);
22485 }
22486 vim_free(pat);
22487 }
22488 }
22489 /* after using ":s", repeat all the modifiers */
22490 if (didit)
22491 goto repeat;
22492 }
22493 }
22494
22495 return valid;
22496}
22497
22498/*
22499 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22500 * "flags" can be "g" to do a global substitute.
22501 * Returns an allocated string, NULL for error.
22502 */
22503 char_u *
22504do_string_sub(str, pat, sub, flags)
22505 char_u *str;
22506 char_u *pat;
22507 char_u *sub;
22508 char_u *flags;
22509{
22510 int sublen;
22511 regmatch_T regmatch;
22512 int i;
22513 int do_all;
22514 char_u *tail;
22515 garray_T ga;
22516 char_u *ret;
22517 char_u *save_cpo;
22518
22519 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22520 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022521 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522
22523 ga_init2(&ga, 1, 200);
22524
22525 do_all = (flags[0] == 'g');
22526
22527 regmatch.rm_ic = p_ic;
22528 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22529 if (regmatch.regprog != NULL)
22530 {
22531 tail = str;
22532 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22533 {
22534 /*
22535 * Get some space for a temporary buffer to do the substitution
22536 * into. It will contain:
22537 * - The text up to where the match is.
22538 * - The substituted text.
22539 * - The text after the match.
22540 */
22541 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22542 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22543 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22544 {
22545 ga_clear(&ga);
22546 break;
22547 }
22548
22549 /* copy the text up to where the match is */
22550 i = (int)(regmatch.startp[0] - tail);
22551 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22552 /* add the substituted text */
22553 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22554 + ga.ga_len + i, TRUE, TRUE, FALSE);
22555 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 /* avoid getting stuck on a match with an empty string */
22557 if (tail == regmatch.endp[0])
22558 {
22559 if (*tail == NUL)
22560 break;
22561 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22562 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 }
22564 else
22565 {
22566 tail = regmatch.endp[0];
22567 if (*tail == NUL)
22568 break;
22569 }
22570 if (!do_all)
22571 break;
22572 }
22573
22574 if (ga.ga_data != NULL)
22575 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22576
22577 vim_free(regmatch.regprog);
22578 }
22579
22580 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22581 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022582 if (p_cpo == empty_option)
22583 p_cpo = save_cpo;
22584 else
22585 /* Darn, evaluating {sub} expression changed the value. */
22586 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587
22588 return ret;
22589}
22590
22591#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */