blob: fada51d6460f314e27723d73b71d869f320fa682 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
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));
408static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
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 Moolenaar33570922005-01-25 22:26:29 +0000465
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#ifdef FEAT_FLOAT
467static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
475static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
489static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000491static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000494static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000497static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
509static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000522static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
529static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
531#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000532static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000541static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000543static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000549static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000557static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000558static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000559static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000560static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000563static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000571static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000585static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000591static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603#ifdef FEAT_FLOAT
604static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
605#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000610static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000611static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000614static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618#ifdef vim_mkdir
619static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000625#ifdef FEAT_FLOAT
626static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000629static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000630static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000633static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000649static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000651static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000658static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000659static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000660static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000661static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000663static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000665static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
668static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000671static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000675#ifdef FEAT_FLOAT
676static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
678#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000679static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680#ifdef HAVE_STRFTIME
681static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
682#endif
683static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000694static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000696static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000697static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000698static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000699static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000700static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000702static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
707static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000719static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000722static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000724static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000725static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static int get_env_len __ARGS((char_u **arg));
727static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000728static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000729static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
730#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
731#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
732 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000733static 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 +0000734static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000735static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000736static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
737static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static typval_T *alloc_tv __ARGS((void));
739static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void init_tv __ARGS((typval_T *varp));
741static long get_tv_number __ARGS((typval_T *varp));
742static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000743static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static char_u *get_tv_string __ARGS((typval_T *varp));
745static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000746static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000748static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
750static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
751static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000752static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
753static 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 +0000754static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
755static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000756static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000757static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000759static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
761static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
762static int eval_fname_script __ARGS((char_u *p));
763static int eval_fname_sid __ARGS((char_u *p));
764static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static ufunc_T *find_func __ARGS((char_u *name));
766static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000767static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#ifdef FEAT_PROFILE
769static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000770static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
771static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
772static int
773# ifdef __BORLANDC__
774 _RTLENTRYF
775# endif
776 prof_total_cmp __ARGS((const void *s1, const void *s2));
777static int
778# ifdef __BORLANDC__
779 _RTLENTRYF
780# endif
781 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000783static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000785static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static void func_free __ARGS((ufunc_T *fp));
787static void func_unref __ARGS((char_u *name));
788static void func_ref __ARGS((char_u *name));
789static 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));
790static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000791static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
792static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000793static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000794static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000795static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000797/* Character used as separated in autoload function/variable names. */
798#define AUTOLOAD_CHAR '#'
799
Bram Moolenaar33570922005-01-25 22:26:29 +0000800/*
801 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000802 */
803 void
804eval_init()
805{
Bram Moolenaar33570922005-01-25 22:26:29 +0000806 int i;
807 struct vimvar *p;
808
809 init_var_dict(&globvardict, &globvars_var);
810 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000811 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000812 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000813
814 for (i = 0; i < VV_LEN; ++i)
815 {
816 p = &vimvars[i];
817 STRCPY(p->vv_di.di_key, p->vv_name);
818 if (p->vv_flags & VV_RO)
819 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
820 else if (p->vv_flags & VV_RO_SBX)
821 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
822 else
823 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000824
825 /* add to v: scope dict, unless the value is not always available */
826 if (p->vv_type != VAR_UNKNOWN)
827 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000828 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000829 /* add to compat scope dict */
830 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000833}
834
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000835#if defined(EXITFREE) || defined(PROTO)
836 void
837eval_clear()
838{
839 int i;
840 struct vimvar *p;
841
842 for (i = 0; i < VV_LEN; ++i)
843 {
844 p = &vimvars[i];
845 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000846 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000847 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000848 p->vv_di.di_tv.vval.v_string = NULL;
849 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000850 }
851 hash_clear(&vimvarht);
852 hash_clear(&compat_hashtab);
853
854 /* script-local variables */
855 for (i = 1; i <= ga_scripts.ga_len; ++i)
856 vars_clear(&SCRIPT_VARS(i));
857 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000858 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000859
860 /* global variables */
861 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000862
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000863 /* autoloaded script names */
864 ga_clear_strings(&ga_loaded);
865
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000866 /* unreferenced lists and dicts */
867 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000868
869 /* functions */
870 free_all_functions();
871 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000872}
873#endif
874
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * Return the name of the executed function.
877 */
878 char_u *
879func_name(cookie)
880 void *cookie;
881{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000882 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884
885/*
886 * Return the address holding the next breakpoint line for a funccall cookie.
887 */
888 linenr_T *
889func_breakpoint(cookie)
890 void *cookie;
891{
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893}
894
895/*
896 * Return the address holding the debug tick for a funccall cookie.
897 */
898 int *
899func_dbg_tick(cookie)
900 void *cookie;
901{
Bram Moolenaar33570922005-01-25 22:26:29 +0000902 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903}
904
905/*
906 * Return the nesting level for a funccall cookie.
907 */
908 int
909func_level(cookie)
910 void *cookie;
911{
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913}
914
915/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000916funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918/*
919 * Return TRUE when a function was ended by a ":return" command.
920 */
921 int
922current_func_returned()
923{
924 return current_funccal->returned;
925}
926
927
928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * Set an internal variable to a string value. Creates the variable if it does
930 * not already exist.
931 */
932 void
933set_internal_string_var(name, value)
934 char_u *name;
935 char_u *value;
936{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000937 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 val = vim_strsave(value);
941 if (val != NULL)
942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000944 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000946 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
949 }
950}
951
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000953static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000954static char_u *redir_endp = NULL;
955static char_u *redir_varname = NULL;
956
957/*
958 * Start recording command output to a variable
959 * Returns OK if successfully completed the setup. FAIL otherwise.
960 */
961 int
962var_redir_start(name, append)
963 char_u *name;
964 int append; /* append to an existing variable */
965{
966 int save_emsg;
967 int err;
968 typval_T tv;
969
970 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000971 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000972 {
973 EMSG(_(e_invarg));
974 return FAIL;
975 }
976
977 redir_varname = vim_strsave(name);
978 if (redir_varname == NULL)
979 return FAIL;
980
981 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
982 if (redir_lval == NULL)
983 {
984 var_redir_stop();
985 return FAIL;
986 }
987
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000988 /* The output is stored in growarray "redir_ga" until redirection ends. */
989 ga_init2(&redir_ga, (int)sizeof(char), 500);
990
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000991 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000992 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
993 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
995 {
996 if (redir_endp != NULL && *redir_endp != NUL)
997 /* Trailing characters are present after the variable name */
998 EMSG(_(e_trailing));
999 else
1000 EMSG(_(e_invarg));
1001 var_redir_stop();
1002 return FAIL;
1003 }
1004
1005 /* check if we can write to the variable: set it to or append an empty
1006 * string */
1007 save_emsg = did_emsg;
1008 did_emsg = FALSE;
1009 tv.v_type = VAR_STRING;
1010 tv.vval.v_string = (char_u *)"";
1011 if (append)
1012 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1013 else
1014 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1015 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001016 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017 if (err)
1018 {
1019 var_redir_stop();
1020 return FAIL;
1021 }
1022 if (redir_lval->ll_newkey != NULL)
1023 {
1024 /* Dictionary item was created, don't do it again. */
1025 vim_free(redir_lval->ll_newkey);
1026 redir_lval->ll_newkey = NULL;
1027 }
1028
1029 return OK;
1030}
1031
1032/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001033 * Append "value[value_len]" to the variable set by var_redir_start().
1034 * The actual appending is postponed until redirection ends, because the value
1035 * appended may in fact be the string we write to, changing it may cause freed
1036 * memory to be used:
1037 * :redir => foo
1038 * :let foo
1039 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 */
1041 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001042var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001044 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001046 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047
1048 if (redir_lval == NULL)
1049 return;
1050
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001052 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001054 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001056 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 {
1058 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001059 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 }
1061 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063}
1064
1065/*
1066 * Stop redirecting command output to a variable.
1067 */
1068 void
1069var_redir_stop()
1070{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001071 typval_T tv;
1072
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 if (redir_lval != NULL)
1074 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001075 /* Append the trailing NUL. */
1076 ga_append(&redir_ga, NUL);
1077
1078 /* Assign the text to the variable. */
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = redir_ga.ga_data;
1081 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1082 vim_free(tv.vval.v_string);
1083
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 clear_lval(redir_lval);
1085 vim_free(redir_lval);
1086 redir_lval = NULL;
1087 }
1088 vim_free(redir_varname);
1089 redir_varname = NULL;
1090}
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092# if defined(FEAT_MBYTE) || defined(PROTO)
1093 int
1094eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1095 char_u *enc_from;
1096 char_u *enc_to;
1097 char_u *fname_from;
1098 char_u *fname_to;
1099{
1100 int err = FALSE;
1101
1102 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1103 set_vim_var_string(VV_CC_TO, enc_to, -1);
1104 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1105 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1106 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1107 err = TRUE;
1108 set_vim_var_string(VV_CC_FROM, NULL, -1);
1109 set_vim_var_string(VV_CC_TO, NULL, -1);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1112
1113 if (err)
1114 return FAIL;
1115 return OK;
1116}
1117# endif
1118
1119# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1120 int
1121eval_printexpr(fname, args)
1122 char_u *fname;
1123 char_u *args;
1124{
1125 int err = FALSE;
1126
1127 set_vim_var_string(VV_FNAME_IN, fname, -1);
1128 set_vim_var_string(VV_CMDARG, args, -1);
1129 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1130 err = TRUE;
1131 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1132 set_vim_var_string(VV_CMDARG, NULL, -1);
1133
1134 if (err)
1135 {
1136 mch_remove(fname);
1137 return FAIL;
1138 }
1139 return OK;
1140}
1141# endif
1142
1143# if defined(FEAT_DIFF) || defined(PROTO)
1144 void
1145eval_diff(origfile, newfile, outfile)
1146 char_u *origfile;
1147 char_u *newfile;
1148 char_u *outfile;
1149{
1150 int err = FALSE;
1151
1152 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1153 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1154 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1155 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1156 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1157 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1158 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1159}
1160
1161 void
1162eval_patch(origfile, difffile, outfile)
1163 char_u *origfile;
1164 char_u *difffile;
1165 char_u *outfile;
1166{
1167 int err;
1168
1169 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1170 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1171 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1172 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1173 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1174 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1175 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1176}
1177# endif
1178
1179/*
1180 * Top level evaluation function, returning a boolean.
1181 * Sets "error" to TRUE if there was an error.
1182 * Return TRUE or FALSE.
1183 */
1184 int
1185eval_to_bool(arg, error, nextcmd, skip)
1186 char_u *arg;
1187 int *error;
1188 char_u **nextcmd;
1189 int skip; /* only parse, don't execute */
1190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int retval = FALSE;
1193
1194 if (skip)
1195 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else
1199 {
1200 *error = FALSE;
1201 if (!skip)
1202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001203 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207 if (skip)
1208 --emsg_skip;
1209
1210 return retval;
1211}
1212
1213/*
1214 * Top level evaluation function, returning a string. If "skip" is TRUE,
1215 * only parsing to "nextcmd" is done, without reporting errors. Return
1216 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1217 */
1218 char_u *
1219eval_to_string_skip(arg, nextcmd, skip)
1220 char_u *arg;
1221 char_u **nextcmd;
1222 int skip; /* only parse, don't execute */
1223{
Bram Moolenaar33570922005-01-25 22:26:29 +00001224 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 char_u *retval;
1226
1227 if (skip)
1228 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001229 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 retval = NULL;
1231 else
1232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 retval = vim_strsave(get_tv_string(&tv));
1234 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236 if (skip)
1237 --emsg_skip;
1238
1239 return retval;
1240}
1241
1242/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001243 * Skip over an expression at "*pp".
1244 * Return FAIL for an error, OK otherwise.
1245 */
1246 int
1247skip_expr(pp)
1248 char_u **pp;
1249{
Bram Moolenaar33570922005-01-25 22:26:29 +00001250 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001251
1252 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001253 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001254}
1255
1256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * Top level evaluation function, returning a string.
1258 * Return pointer to allocated memory, or NULL for failure.
1259 */
1260 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001261eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 char_u *arg;
1263 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001264 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001268 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 retval = NULL;
1272 else
1273 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001274 if (dolist && tv.v_type == VAR_LIST)
1275 {
1276 ga_init2(&ga, (int)sizeof(char), 80);
1277 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1278 ga_append(&ga, NUL);
1279 retval = (char_u *)ga.ga_data;
1280 }
1281 else
1282 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285
1286 return retval;
1287}
1288
1289/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001290 * Call eval_to_string() without using current local variables and using
1291 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 */
1293 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001294eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 char_u *arg;
1296 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001297 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 char_u *retval;
1300 void *save_funccalp;
1301
1302 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001303 if (use_sandbox)
1304 ++sandbox;
1305 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001306 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001307 if (use_sandbox)
1308 --sandbox;
1309 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 restore_funccal(save_funccalp);
1311 return retval;
1312}
1313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314/*
1315 * Top level evaluation function, returning a number.
1316 * Evaluates "expr" silently.
1317 * Returns -1 for an error.
1318 */
1319 int
1320eval_to_number(expr)
1321 char_u *expr;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001325 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327 ++emsg_off;
1328
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 retval = -1;
1331 else
1332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001333 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 --emsg_off;
1337
1338 return retval;
1339}
1340
Bram Moolenaara40058a2005-07-11 22:42:07 +00001341/*
1342 * Prepare v: variable "idx" to be used.
1343 * Save the current typeval in "save_tv".
1344 * When not used yet add the variable to the v: hashtable.
1345 */
1346 static void
1347prepare_vimvar(idx, save_tv)
1348 int idx;
1349 typval_T *save_tv;
1350{
1351 *save_tv = vimvars[idx].vv_tv;
1352 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1353 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1354}
1355
1356/*
1357 * Restore v: variable "idx" to typeval "save_tv".
1358 * When no longer defined, remove the variable from the v: hashtable.
1359 */
1360 static void
1361restore_vimvar(idx, save_tv)
1362 int idx;
1363 typval_T *save_tv;
1364{
1365 hashitem_T *hi;
1366
Bram Moolenaara40058a2005-07-11 22:42:07 +00001367 vimvars[idx].vv_tv = *save_tv;
1368 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1369 {
1370 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1371 if (HASHITEM_EMPTY(hi))
1372 EMSG2(_(e_intern2), "restore_vimvar()");
1373 else
1374 hash_remove(&vimvarht, hi);
1375 }
1376}
1377
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001378#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001379/*
1380 * Evaluate an expression to a list with suggestions.
1381 * For the "expr:" part of 'spellsuggest'.
1382 */
1383 list_T *
1384eval_spell_expr(badword, expr)
1385 char_u *badword;
1386 char_u *expr;
1387{
1388 typval_T save_val;
1389 typval_T rettv;
1390 list_T *list = NULL;
1391 char_u *p = skipwhite(expr);
1392
1393 /* Set "v:val" to the bad word. */
1394 prepare_vimvar(VV_VAL, &save_val);
1395 vimvars[VV_VAL].vv_type = VAR_STRING;
1396 vimvars[VV_VAL].vv_str = badword;
1397 if (p_verbose == 0)
1398 ++emsg_off;
1399
1400 if (eval1(&p, &rettv, TRUE) == OK)
1401 {
1402 if (rettv.v_type != VAR_LIST)
1403 clear_tv(&rettv);
1404 else
1405 list = rettv.vval.v_list;
1406 }
1407
1408 if (p_verbose == 0)
1409 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001410 restore_vimvar(VV_VAL, &save_val);
1411
1412 return list;
1413}
1414
1415/*
1416 * "list" is supposed to contain two items: a word and a number. Return the
1417 * word in "pp" and the number as the return value.
1418 * Return -1 if anything isn't right.
1419 * Used to get the good word and score from the eval_spell_expr() result.
1420 */
1421 int
1422get_spellword(list, pp)
1423 list_T *list;
1424 char_u **pp;
1425{
1426 listitem_T *li;
1427
1428 li = list->lv_first;
1429 if (li == NULL)
1430 return -1;
1431 *pp = get_tv_string(&li->li_tv);
1432
1433 li = li->li_next;
1434 if (li == NULL)
1435 return -1;
1436 return get_tv_number(&li->li_tv);
1437}
1438#endif
1439
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001440/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001441 * Top level evaluation function.
1442 * Returns an allocated typval_T with the result.
1443 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001444 */
1445 typval_T *
1446eval_expr(arg, nextcmd)
1447 char_u *arg;
1448 char_u **nextcmd;
1449{
1450 typval_T *tv;
1451
1452 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001453 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001454 {
1455 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001456 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001457 }
1458
1459 return tv;
1460}
1461
1462
Bram Moolenaar4f688582007-07-24 12:34:30 +00001463#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1464 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001466 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001467 * Uses argv[argc] for the function arguments. Only Number and String
1468 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001471 static int
1472call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 char_u *func;
1474 int argc;
1475 char_u **argv;
1476 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
Bram Moolenaar33570922005-01-25 22:26:29 +00001479 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 long n;
1481 int len;
1482 int i;
1483 int doesrange;
1484 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001487 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 for (i = 0; i < argc; i++)
1492 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001493 /* Pass a NULL or empty argument as an empty string */
1494 if (argv[i] == NULL || *argv[i] == NUL)
1495 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001496 argvars[i].v_type = VAR_STRING;
1497 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001498 continue;
1499 }
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 /* Recognize a number argument, the others must be strings. */
1502 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1503 if (len != 0 && len == (int)STRLEN(argv[i]))
1504 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001505 argvars[i].v_type = VAR_NUMBER;
1506 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508 else
1509 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001510 argvars[i].v_type = VAR_STRING;
1511 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 }
1514
1515 if (safe)
1516 {
1517 save_funccalp = save_funccal();
1518 ++sandbox;
1519 }
1520
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001521 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1522 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001524 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (safe)
1526 {
1527 --sandbox;
1528 restore_funccal(save_funccalp);
1529 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001530 vim_free(argvars);
1531
1532 if (ret == FAIL)
1533 clear_tv(rettv);
1534
1535 return ret;
1536}
1537
Bram Moolenaar4f688582007-07-24 12:34:30 +00001538# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001539/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001540 * Call vimL function "func" and return the result as a string.
1541 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001542 * Uses argv[argc] for the function arguments.
1543 */
1544 void *
1545call_func_retstr(func, argc, argv, safe)
1546 char_u *func;
1547 int argc;
1548 char_u **argv;
1549 int safe; /* use the sandbox */
1550{
1551 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001552 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553
1554 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1555 return NULL;
1556
1557 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 return retval;
1560}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001561# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562
Bram Moolenaar4f688582007-07-24 12:34:30 +00001563# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001565 * Call vimL function "func" and return the result as a number.
1566 * Returns -1 when calling the function fails.
1567 * Uses argv[argc] for the function arguments.
1568 */
1569 long
1570call_func_retnr(func, argc, argv, safe)
1571 char_u *func;
1572 int argc;
1573 char_u **argv;
1574 int safe; /* use the sandbox */
1575{
1576 typval_T rettv;
1577 long retval;
1578
1579 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1580 return -1;
1581
1582 retval = get_tv_number_chk(&rettv, NULL);
1583 clear_tv(&rettv);
1584 return retval;
1585}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001586# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001587
1588/*
1589 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Uses argv[argc] for the function arguments.
1591 */
1592 void *
1593call_func_retlist(func, argc, argv, safe)
1594 char_u *func;
1595 int argc;
1596 char_u **argv;
1597 int safe; /* use the sandbox */
1598{
1599 typval_T rettv;
1600
1601 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1602 return NULL;
1603
1604 if (rettv.v_type != VAR_LIST)
1605 {
1606 clear_tv(&rettv);
1607 return NULL;
1608 }
1609
1610 return rettv.vval.v_list;
1611}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612#endif
1613
Bram Moolenaar4f688582007-07-24 12:34:30 +00001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
1616 * Save the current function call pointer, and set it to NULL.
1617 * Used when executing autocommands and for ":source".
1618 */
1619 void *
1620save_funccal()
1621{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001622 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 current_funccal = NULL;
1625 return (void *)fc;
1626}
1627
1628 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001629restore_funccal(vfc)
1630 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001632 funccall_T *fc = (funccall_T *)vfc;
1633
1634 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635}
1636
Bram Moolenaar05159a02005-02-26 23:04:13 +00001637#if defined(FEAT_PROFILE) || defined(PROTO)
1638/*
1639 * Prepare profiling for entering a child or something else that is not
1640 * counted for the script/function itself.
1641 * Should always be called in pair with prof_child_exit().
1642 */
1643 void
1644prof_child_enter(tm)
1645 proftime_T *tm; /* place to store waittime */
1646{
1647 funccall_T *fc = current_funccal;
1648
1649 if (fc != NULL && fc->func->uf_profiling)
1650 profile_start(&fc->prof_child);
1651 script_prof_save(tm);
1652}
1653
1654/*
1655 * Take care of time spent in a child.
1656 * Should always be called after prof_child_enter().
1657 */
1658 void
1659prof_child_exit(tm)
1660 proftime_T *tm; /* where waittime was stored */
1661{
1662 funccall_T *fc = current_funccal;
1663
1664 if (fc != NULL && fc->func->uf_profiling)
1665 {
1666 profile_end(&fc->prof_child);
1667 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1668 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1669 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1670 }
1671 script_prof_restore(tm);
1672}
1673#endif
1674
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676#ifdef FEAT_FOLDING
1677/*
1678 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1679 * it in "*cp". Doesn't give error messages.
1680 */
1681 int
1682eval_foldexpr(arg, cp)
1683 char_u *arg;
1684 int *cp;
1685{
Bram Moolenaar33570922005-01-25 22:26:29 +00001686 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 int retval;
1688 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001689 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1690 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001693 if (use_sandbox)
1694 ++sandbox;
1695 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 retval = 0;
1699 else
1700 {
1701 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001702 if (tv.v_type == VAR_NUMBER)
1703 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001704 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 retval = 0;
1706 else
1707 {
1708 /* If the result is a string, check if there is a non-digit before
1709 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001710 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if (!VIM_ISDIGIT(*s) && *s != '-')
1712 *cp = *s++;
1713 retval = atol((char *)s);
1714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001715 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001718 if (use_sandbox)
1719 --sandbox;
1720 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 return retval;
1723}
1724#endif
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727 * ":let" list all variable values
1728 * ":let var1 var2" list variable values
1729 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 * ":let var += expr" assignment command.
1731 * ":let var -= expr" assignment command.
1732 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 */
1735 void
1736ex_let(eap)
1737 exarg_T *eap;
1738{
1739 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001741 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 int var_count = 0;
1744 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001746 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001747 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaardb552d602006-03-23 22:59:57 +00001749 argend = skip_var_list(arg, &var_count, &semicolon);
1750 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001752 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1753 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001754 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001757 /*
1758 * ":let" without "=": list variables
1759 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 if (*arg == '[')
1761 EMSG(_(e_invarg));
1762 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001764 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001765 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001767 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001768 list_glob_vars(&first);
1769 list_buf_vars(&first);
1770 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001771#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001772 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001773#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001774 list_script_vars(&first);
1775 list_func_vars(&first);
1776 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 eap->nextcmd = check_nextcmd(arg);
1779 }
1780 else
1781 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001782 op[0] = '=';
1783 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001784 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001785 {
1786 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1787 op[0] = expr[-1]; /* +=, -= or .= */
1788 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (eap->skip)
1792 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 if (eap->skip)
1795 {
1796 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 --emsg_skip;
1799 }
1800 else if (i != FAIL)
1801 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 }
1807}
1808
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809/*
1810 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1811 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 * When "nextchars" is not NULL it points to a string with characters that
1813 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1814 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 * Returns OK or FAIL;
1816 */
1817 static int
1818ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1819 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 int copy; /* copy values from "tv", don't move */
1822 int semicolon; /* from skip_var_list() */
1823 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825{
1826 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001829 listitem_T *item;
1830 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831
1832 if (*arg != '[')
1833 {
1834 /*
1835 * ":let var = expr" or ":for var in list"
1836 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 return FAIL;
1839 return OK;
1840 }
1841
1842 /*
1843 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1844 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001845 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 {
1847 EMSG(_(e_listreq));
1848 return FAIL;
1849 }
1850
1851 i = list_len(l);
1852 if (semicolon == 0 && var_count < i)
1853 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001854 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return FAIL;
1856 }
1857 if (var_count - semicolon > i)
1858 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001859 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 return FAIL;
1861 }
1862
1863 item = l->lv_first;
1864 while (*arg != ']')
1865 {
1866 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 item = item->li_next;
1869 if (arg == NULL)
1870 return FAIL;
1871
1872 arg = skipwhite(arg);
1873 if (*arg == ';')
1874 {
1875 /* Put the rest of the list (may be empty) in the var after ';'.
1876 * Create a new list for this. */
1877 l = list_alloc();
1878 if (l == NULL)
1879 return FAIL;
1880 while (item != NULL)
1881 {
1882 list_append_tv(l, &item->li_tv);
1883 item = item->li_next;
1884 }
1885
1886 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001887 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 ltv.vval.v_list = l;
1889 l->lv_refcount = 1;
1890
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1892 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 clear_tv(&ltv);
1894 if (arg == NULL)
1895 return FAIL;
1896 break;
1897 }
1898 else if (*arg != ',' && *arg != ']')
1899 {
1900 EMSG2(_(e_intern2), "ex_let_vars()");
1901 return FAIL;
1902 }
1903 }
1904
1905 return OK;
1906}
1907
1908/*
1909 * Skip over assignable variable "var" or list of variables "[var, var]".
1910 * Used for ":let varvar = expr" and ":for varvar in expr".
1911 * For "[var, var]" increment "*var_count" for each variable.
1912 * for "[var, var; var]" set "semicolon".
1913 * Return NULL for an error.
1914 */
1915 static char_u *
1916skip_var_list(arg, var_count, semicolon)
1917 char_u *arg;
1918 int *var_count;
1919 int *semicolon;
1920{
1921 char_u *p, *s;
1922
1923 if (*arg == '[')
1924 {
1925 /* "[var, var]": find the matching ']'. */
1926 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001927 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 {
1929 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1930 s = skip_var_one(p);
1931 if (s == p)
1932 {
1933 EMSG2(_(e_invarg2), p);
1934 return NULL;
1935 }
1936 ++*var_count;
1937
1938 p = skipwhite(s);
1939 if (*p == ']')
1940 break;
1941 else if (*p == ';')
1942 {
1943 if (*semicolon == 1)
1944 {
1945 EMSG(_("Double ; in list of variables"));
1946 return NULL;
1947 }
1948 *semicolon = 1;
1949 }
1950 else if (*p != ',')
1951 {
1952 EMSG2(_(e_invarg2), p);
1953 return NULL;
1954 }
1955 }
1956 return p + 1;
1957 }
1958 else
1959 return skip_var_one(arg);
1960}
1961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001963 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001964 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 static char_u *
1967skip_var_one(arg)
1968 char_u *arg;
1969{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001970 if (*arg == '@' && arg[1] != NUL)
1971 return arg + 2;
1972 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1973 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974}
1975
Bram Moolenaara7043832005-01-21 11:56:39 +00001976/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001977 * List variables for hashtab "ht" with prefix "prefix".
1978 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001981list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001983 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001984 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001985 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001986{
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 hashitem_T *hi;
1988 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 int todo;
1990
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001991 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001992 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1993 {
1994 if (!HASHITEM_EMPTY(hi))
1995 {
1996 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001997 di = HI2DI(hi);
1998 if (empty || di->di_tv.v_type != VAR_STRING
1999 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002000 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002001 }
2002 }
2003}
2004
2005/*
2006 * List global variables.
2007 */
2008 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002009list_glob_vars(first)
2010 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002011{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002012 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002013}
2014
2015/*
2016 * List buffer variables.
2017 */
2018 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002019list_buf_vars(first)
2020 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002021{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 char_u numbuf[NUMBUFLEN];
2023
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002024 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2025 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002026
2027 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002028 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2029 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002030}
2031
2032/*
2033 * List window variables.
2034 */
2035 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002036list_win_vars(first)
2037 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002038{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002039 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2040 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002041}
2042
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002043#ifdef FEAT_WINDOWS
2044/*
2045 * List tab page variables.
2046 */
2047 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002048list_tab_vars(first)
2049 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002050{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002051 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2052 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002053}
2054#endif
2055
Bram Moolenaara7043832005-01-21 11:56:39 +00002056/*
2057 * List Vim variables.
2058 */
2059 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002060list_vim_vars(first)
2061 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002063 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064}
2065
2066/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002067 * List script-local variables, if there is a script.
2068 */
2069 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002070list_script_vars(first)
2071 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002072{
2073 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002074 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2075 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002076}
2077
2078/*
2079 * List function variables, if there is a function.
2080 */
2081 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082list_func_vars(first)
2083 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002084{
2085 if (current_funccal != NULL)
2086 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002088}
2089
2090/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 * List variables in "arg".
2092 */
2093 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 exarg_T *eap;
2096 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098{
2099 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002102 char_u *name_start;
2103 char_u *arg_subsc;
2104 char_u *tofree;
2105 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106
2107 while (!ends_excmd(*arg) && !got_int)
2108 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002111 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2113 {
2114 emsg_severe = TRUE;
2115 EMSG(_(e_trailing));
2116 break;
2117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121 /* get_name_len() takes care of expanding curly braces */
2122 name_start = name = arg;
2123 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2124 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 /* This is mainly to keep test 49 working: when expanding
2127 * curly braces fails overrule the exception error message. */
2128 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 emsg_severe = TRUE;
2131 EMSG2(_(e_invarg2), arg);
2132 break;
2133 }
2134 error = TRUE;
2135 }
2136 else
2137 {
2138 if (tofree != NULL)
2139 name = tofree;
2140 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 else
2143 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144 /* handle d.key, l[idx], f(expr) */
2145 arg_subsc = arg;
2146 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002147 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002152 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 case 'g': list_glob_vars(first); break;
2155 case 'b': list_buf_vars(first); break;
2156 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 case 'v': list_vim_vars(first); break;
2161 case 's': list_script_vars(first); break;
2162 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 default:
2164 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002165 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002166 }
2167 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002168 {
2169 char_u numbuf[NUMBUFLEN];
2170 char_u *tf;
2171 int c;
2172 char_u *s;
2173
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002174 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 c = *arg;
2176 *arg = NUL;
2177 list_one_var_a((char_u *)"",
2178 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 tv.v_type,
2180 s == NULL ? (char_u *)"" : s,
2181 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182 *arg = c;
2183 vim_free(tf);
2184 }
2185 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 }
2188 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002189
2190 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192
2193 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 }
2195
2196 return arg;
2197}
2198
2199/*
2200 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2201 * Returns a pointer to the char just after the var name.
2202 * Returns NULL if there is an error.
2203 */
2204 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002207 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002209 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002210 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
2212 int c1;
2213 char_u *name;
2214 char_u *p;
2215 char_u *arg_end = NULL;
2216 int len;
2217 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002218 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219
2220 /*
2221 * ":let $VAR = expr": Set environment variable.
2222 */
2223 if (*arg == '$')
2224 {
2225 /* Find the end of the name. */
2226 ++arg;
2227 name = arg;
2228 len = get_env_len(&arg);
2229 if (len == 0)
2230 EMSG2(_(e_invarg2), name - 1);
2231 else
2232 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002233 if (op != NULL && (*op == '+' || *op == '-'))
2234 EMSG2(_(e_letwrong), op);
2235 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002236 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 EMSG(_(e_letunexp));
2238 else
2239 {
2240 c1 = name[len];
2241 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002242 p = get_tv_string_chk(tv);
2243 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 {
2245 int mustfree = FALSE;
2246 char_u *s = vim_getenv(name, &mustfree);
2247
2248 if (s != NULL)
2249 {
2250 p = tofree = concat_str(s, p);
2251 if (mustfree)
2252 vim_free(s);
2253 }
2254 }
2255 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002256 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002258 if (STRICMP(name, "HOME") == 0)
2259 init_homedir();
2260 else if (didset_vim && STRICMP(name, "VIM") == 0)
2261 didset_vim = FALSE;
2262 else if (didset_vimruntime
2263 && STRICMP(name, "VIMRUNTIME") == 0)
2264 didset_vimruntime = FALSE;
2265 arg_end = arg;
2266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
2270 }
2271 }
2272
2273 /*
2274 * ":let &option = expr": Set option value.
2275 * ":let &l:option = expr": Set local option value.
2276 * ":let &g:option = expr": Set global option value.
2277 */
2278 else if (*arg == '&')
2279 {
2280 /* Find the end of the name. */
2281 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002282 if (p == NULL || (endchars != NULL
2283 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 EMSG(_(e_letunexp));
2285 else
2286 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002287 long n;
2288 int opt_type;
2289 long numval;
2290 char_u *stringval = NULL;
2291 char_u *s;
2292
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 c1 = *p;
2294 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295
2296 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002297 s = get_tv_string_chk(tv); /* != NULL if number or string */
2298 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 {
2300 opt_type = get_option_value(arg, &numval,
2301 &stringval, opt_flags);
2302 if ((opt_type == 1 && *op == '.')
2303 || (opt_type == 0 && *op != '.'))
2304 EMSG2(_(e_letwrong), op);
2305 else
2306 {
2307 if (opt_type == 1) /* number */
2308 {
2309 if (*op == '+')
2310 n = numval + n;
2311 else
2312 n = numval - n;
2313 }
2314 else if (opt_type == 0 && stringval != NULL) /* string */
2315 {
2316 s = concat_str(stringval, s);
2317 vim_free(stringval);
2318 stringval = s;
2319 }
2320 }
2321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002322 if (s != NULL)
2323 {
2324 set_option_value(arg, n, s, opt_flags);
2325 arg_end = p;
2326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
2330 }
2331
2332 /*
2333 * ":let @r = expr": Set register contents.
2334 */
2335 else if (*arg == '@')
2336 {
2337 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 if (op != NULL && (*op == '+' || *op == '-'))
2339 EMSG2(_(e_letwrong), op);
2340 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002341 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 EMSG(_(e_letunexp));
2343 else
2344 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002345 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *s;
2347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 p = get_tv_string_chk(tv);
2349 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002351 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 if (s != NULL)
2353 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002354 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 vim_free(s);
2356 }
2357 }
2358 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002361 arg_end = arg + 1;
2362 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002363 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
2365 }
2366
2367 /*
2368 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002371 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002373 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002375 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2379 EMSG(_(e_letunexp));
2380 else
2381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 arg_end = p;
2384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 }
2388
2389 else
2390 EMSG2(_(e_invarg2), arg);
2391
2392 return arg_end;
2393}
2394
2395/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2397 */
2398 static int
2399check_changedtick(arg)
2400 char_u *arg;
2401{
2402 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2403 {
2404 EMSG2(_(e_readonlyvar), arg);
2405 return TRUE;
2406 }
2407 return FALSE;
2408}
2409
2410/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 * Get an lval: variable, Dict item or List item that can be assigned a value
2412 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2413 * "name.key", "name.key[expr]" etc.
2414 * Indexing only works if "name" is an existing List or Dictionary.
2415 * "name" points to the start of the name.
2416 * If "rettv" is not NULL it points to the value to be assigned.
2417 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2418 * wrong; must end in space or cmd separator.
2419 *
2420 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002421 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 */
2424 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002425get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002427 typval_T *rettv;
2428 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 int unlet;
2430 int skip;
2431 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002432 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 char_u *expr_start, *expr_end;
2436 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002437 dictitem_T *v;
2438 typval_T var1;
2439 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002441 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002444 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448
2449 if (skip)
2450 {
2451 /* When skipping just find the end of the name. */
2452 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002453 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 }
2455
2456 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002457 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (expr_start != NULL)
2459 {
2460 /* Don't expand the name when we already know there is an error. */
2461 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2462 && *p != '[' && *p != '.')
2463 {
2464 EMSG(_(e_trailing));
2465 return NULL;
2466 }
2467
2468 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2469 if (lp->ll_exp_name == NULL)
2470 {
2471 /* Report an invalid expression in braces, unless the
2472 * expression evaluation has been cancelled due to an
2473 * aborting error, an interrupt, or an exception. */
2474 if (!aborting() && !quiet)
2475 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002476 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 EMSG2(_(e_invarg2), name);
2478 return NULL;
2479 }
2480 }
2481 lp->ll_name = lp->ll_exp_name;
2482 }
2483 else
2484 lp->ll_name = name;
2485
2486 /* Without [idx] or .key we are done. */
2487 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2488 return p;
2489
2490 cc = *p;
2491 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002492 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 if (v == NULL && !quiet)
2494 EMSG2(_(e_undefvar), lp->ll_name);
2495 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 if (v == NULL)
2497 return NULL;
2498
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 /*
2500 * Loop until no more [idx] or .key is following.
2501 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002502 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2506 && !(lp->ll_tv->v_type == VAR_DICT
2507 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (!quiet)
2510 EMSG(_("E689: Can only index a List or Dictionary"));
2511 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (!quiet)
2516 EMSG(_("E708: [:] must come last"));
2517 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 len = -1;
2521 if (*p == '.')
2522 {
2523 key = p + 1;
2524 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2525 ;
2526 if (len == 0)
2527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (!quiet)
2529 EMSG(_(e_emptykey));
2530 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 }
2532 p = key + len;
2533 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002534 else
2535 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002537 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 if (*p == ':')
2539 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002540 else
2541 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 empty1 = FALSE;
2543 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 if (get_tv_string_chk(&var1) == NULL)
2546 {
2547 /* not a number or string */
2548 clear_tv(&var1);
2549 return NULL;
2550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 }
2552
2553 /* Optionally get the second index [ :expr]. */
2554 if (*p == ':')
2555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002560 if (!empty1)
2561 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002563 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (rettv != NULL && (rettv->v_type != VAR_LIST
2565 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (!quiet)
2568 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 if (!empty1)
2570 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 }
2573 p = skipwhite(p + 1);
2574 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 else
2577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2580 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 if (!empty1)
2582 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002585 if (get_tv_string_chk(&var2) == NULL)
2586 {
2587 /* not a number or string */
2588 if (!empty1)
2589 clear_tv(&var1);
2590 clear_tv(&var2);
2591 return NULL;
2592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002598
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 if (!empty1)
2604 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
2609
2610 /* Skip to past ']'. */
2611 ++p;
2612 }
2613
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 {
2616 if (len == -1)
2617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 if (*key == NUL)
2621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (!quiet)
2623 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 }
2627 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002628 lp->ll_list = NULL;
2629 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002630 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002633 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002637 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 if (len == -1)
2639 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 if (len == -1)
2647 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 p = NULL;
2650 break;
2651 }
2652 if (len == -1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 else
2657 {
2658 /*
2659 * Get the number and item for the only or first index of the List.
2660 */
2661 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 else
2664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002665 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 clear_tv(&var1);
2667 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_list = lp->ll_tv->vval.v_list;
2670 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2671 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002673 if (lp->ll_n1 < 0)
2674 {
2675 lp->ll_n1 = 0;
2676 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2677 }
2678 }
2679 if (lp->ll_li == NULL)
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
2685
2686 /*
2687 * May need to find the item or absolute index for the second
2688 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 * When no index given: "lp->ll_empty2" is TRUE.
2690 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2705 if (lp->ll_n1 < 0)
2706 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2707 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002713 }
2714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return p;
2716}
2717
2718/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002719 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 */
2721 static void
2722clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002723 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724{
2725 vim_free(lp->ll_exp_name);
2726 vim_free(lp->ll_newkey);
2727}
2728
2729/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002730 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002732 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 */
2734 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002736 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002738 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002740 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741{
2742 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002743 listitem_T *ri;
2744 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745
2746 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 cc = *endp;
2751 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 if (op != NULL && *op != '=')
2753 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002756 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002757 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002758 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 {
2760 if (tv_op(&tv, rettv, op) == OK)
2761 set_var(lp->ll_name, &tv, FALSE);
2762 clear_tv(&tv);
2763 }
2764 }
2765 else
2766 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002768 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002770 else if (tv_check_lock(lp->ll_newkey == NULL
2771 ? lp->ll_tv->v_lock
2772 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2773 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 else if (lp->ll_range)
2775 {
2776 /*
2777 * Assign the List values to the list items.
2778 */
2779 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002781 if (op != NULL && *op != '=')
2782 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2783 else
2784 {
2785 clear_tv(&lp->ll_li->li_tv);
2786 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2787 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 ri = ri->li_next;
2789 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2790 break;
2791 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002794 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 ri = NULL;
2797 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002798 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_li = lp->ll_li->li_next;
2801 ++lp->ll_n1;
2802 }
2803 if (ri != NULL)
2804 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002805 else if (lp->ll_empty2
2806 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 : lp->ll_n1 != lp->ll_n2)
2808 EMSG(_("E711: List value has not enough items"));
2809 }
2810 else
2811 {
2812 /*
2813 * Assign to a List or Dictionary item.
2814 */
2815 if (lp->ll_newkey != NULL)
2816 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 if (op != NULL && *op != '=')
2818 {
2819 EMSG2(_(e_letwrong), op);
2820 return;
2821 }
2822
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002824 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (di == NULL)
2826 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002827 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2828 {
2829 vim_free(di);
2830 return;
2831 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002833 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002834 else if (op != NULL && *op != '=')
2835 {
2836 tv_op(lp->ll_tv, rettv, op);
2837 return;
2838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002839 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 /*
2843 * Assign the value to the variable or list item.
2844 */
2845 if (copy)
2846 copy_tv(rettv, lp->ll_tv);
2847 else
2848 {
2849 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002850 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002852 }
2853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854}
2855
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2858 * Returns OK or FAIL.
2859 */
2860 static int
2861tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 typval_T *tv1;
2863 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864 char_u *op;
2865{
2866 long n;
2867 char_u numbuf[NUMBUFLEN];
2868 char_u *s;
2869
2870 /* Can't do anything with a Funcref or a Dict on the right. */
2871 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2872 {
2873 switch (tv1->v_type)
2874 {
2875 case VAR_DICT:
2876 case VAR_FUNC:
2877 break;
2878
2879 case VAR_LIST:
2880 if (*op != '+' || tv2->v_type != VAR_LIST)
2881 break;
2882 /* List += List */
2883 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2884 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2885 return OK;
2886
2887 case VAR_NUMBER:
2888 case VAR_STRING:
2889 if (tv2->v_type == VAR_LIST)
2890 break;
2891 if (*op == '+' || *op == '-')
2892 {
2893 /* nr += nr or nr -= nr*/
2894 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002895#ifdef FEAT_FLOAT
2896 if (tv2->v_type == VAR_FLOAT)
2897 {
2898 float_T f = n;
2899
2900 if (*op == '+')
2901 f += tv2->vval.v_float;
2902 else
2903 f -= tv2->vval.v_float;
2904 clear_tv(tv1);
2905 tv1->v_type = VAR_FLOAT;
2906 tv1->vval.v_float = f;
2907 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002909#endif
2910 {
2911 if (*op == '+')
2912 n += get_tv_number(tv2);
2913 else
2914 n -= get_tv_number(tv2);
2915 clear_tv(tv1);
2916 tv1->v_type = VAR_NUMBER;
2917 tv1->vval.v_number = n;
2918 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 }
2920 else
2921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002922 if (tv2->v_type == VAR_FLOAT)
2923 break;
2924
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 /* str .= str */
2926 s = get_tv_string(tv1);
2927 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2928 clear_tv(tv1);
2929 tv1->v_type = VAR_STRING;
2930 tv1->vval.v_string = s;
2931 }
2932 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002933
2934#ifdef FEAT_FLOAT
2935 case VAR_FLOAT:
2936 {
2937 float_T f;
2938
2939 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2940 && tv2->v_type != VAR_NUMBER
2941 && tv2->v_type != VAR_STRING))
2942 break;
2943 if (tv2->v_type == VAR_FLOAT)
2944 f = tv2->vval.v_float;
2945 else
2946 f = get_tv_number(tv2);
2947 if (*op == '+')
2948 tv1->vval.v_float += f;
2949 else
2950 tv1->vval.v_float -= f;
2951 }
2952 return OK;
2953#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 }
2955 }
2956
2957 EMSG2(_(e_letwrong), op);
2958 return FAIL;
2959}
2960
2961/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002962 * Add a watcher to a list.
2963 */
2964 static void
2965list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 list_T *l;
2967 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968{
2969 lw->lw_next = l->lv_watch;
2970 l->lv_watch = lw;
2971}
2972
2973/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002974 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 * No warning when it isn't found...
2976 */
2977 static void
2978list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002979 list_T *l;
2980 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981{
Bram Moolenaar33570922005-01-25 22:26:29 +00002982 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002983
2984 lwp = &l->lv_watch;
2985 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2986 {
2987 if (lw == lwrem)
2988 {
2989 *lwp = lw->lw_next;
2990 break;
2991 }
2992 lwp = &lw->lw_next;
2993 }
2994}
2995
2996/*
2997 * Just before removing an item from a list: advance watchers to the next
2998 * item.
2999 */
3000 static void
3001list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 list_T *l;
3003 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003004{
Bram Moolenaar33570922005-01-25 22:26:29 +00003005 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006
3007 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3008 if (lw->lw_item == item)
3009 lw->lw_item = item->li_next;
3010}
3011
3012/*
3013 * Evaluate the expression used in a ":for var in expr" command.
3014 * "arg" points to "var".
3015 * Set "*errp" to TRUE for an error, FALSE otherwise;
3016 * Return a pointer that holds the info. Null when there is an error.
3017 */
3018 void *
3019eval_for_line(arg, errp, nextcmdp, skip)
3020 char_u *arg;
3021 int *errp;
3022 char_u **nextcmdp;
3023 int skip;
3024{
Bram Moolenaar33570922005-01-25 22:26:29 +00003025 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 typval_T tv;
3028 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029
3030 *errp = TRUE; /* default: there is an error */
3031
Bram Moolenaar33570922005-01-25 22:26:29 +00003032 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003033 if (fi == NULL)
3034 return NULL;
3035
3036 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3037 if (expr == NULL)
3038 return fi;
3039
3040 expr = skipwhite(expr);
3041 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3042 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003043 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003044 return fi;
3045 }
3046
3047 if (skip)
3048 ++emsg_skip;
3049 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3050 {
3051 *errp = FALSE;
3052 if (!skip)
3053 {
3054 l = tv.vval.v_list;
3055 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003056 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003057 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003058 clear_tv(&tv);
3059 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003060 else
3061 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003062 /* No need to increment the refcount, it's already set for the
3063 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064 fi->fi_list = l;
3065 list_add_watch(l, &fi->fi_lw);
3066 fi->fi_lw.lw_item = l->lv_first;
3067 }
3068 }
3069 }
3070 if (skip)
3071 --emsg_skip;
3072
3073 return fi;
3074}
3075
3076/*
3077 * Use the first item in a ":for" list. Advance to the next.
3078 * Assign the values to the variable (list). "arg" points to the first one.
3079 * Return TRUE when a valid item was found, FALSE when at end of list or
3080 * something wrong.
3081 */
3082 int
3083next_for_item(fi_void, arg)
3084 void *fi_void;
3085 char_u *arg;
3086{
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090
3091 item = fi->fi_lw.lw_item;
3092 if (item == NULL)
3093 result = FALSE;
3094 else
3095 {
3096 fi->fi_lw.lw_item = item->li_next;
3097 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3098 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3099 }
3100 return result;
3101}
3102
3103/*
3104 * Free the structure used to store info used by ":for".
3105 */
3106 void
3107free_for_info(fi_void)
3108 void *fi_void;
3109{
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003112 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003113 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003115 list_unref(fi->fi_list);
3116 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 vim_free(fi);
3118}
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3121
3122 void
3123set_context_for_expression(xp, arg, cmdidx)
3124 expand_T *xp;
3125 char_u *arg;
3126 cmdidx_T cmdidx;
3127{
3128 int got_eq = FALSE;
3129 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 if (cmdidx == CMD_let)
3133 {
3134 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003135 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136 {
3137 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003138 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139 {
3140 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003142 if (vim_iswhite(*p))
3143 break;
3144 }
3145 return;
3146 }
3147 }
3148 else
3149 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3150 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 while ((xp->xp_pattern = vim_strpbrk(arg,
3152 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3153 {
3154 c = *xp->xp_pattern;
3155 if (c == '&')
3156 {
3157 c = xp->xp_pattern[1];
3158 if (c == '&')
3159 {
3160 ++xp->xp_pattern;
3161 xp->xp_context = cmdidx != CMD_let || got_eq
3162 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3163 }
3164 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003167 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3168 xp->xp_pattern += 2;
3169
3170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 else if (c == '$')
3173 {
3174 /* environment variable */
3175 xp->xp_context = EXPAND_ENV_VARS;
3176 }
3177 else if (c == '=')
3178 {
3179 got_eq = TRUE;
3180 xp->xp_context = EXPAND_EXPRESSION;
3181 }
3182 else if (c == '<'
3183 && xp->xp_context == EXPAND_FUNCTIONS
3184 && vim_strchr(xp->xp_pattern, '(') == NULL)
3185 {
3186 /* Function name can start with "<SNR>" */
3187 break;
3188 }
3189 else if (cmdidx != CMD_let || got_eq)
3190 {
3191 if (c == '"') /* string */
3192 {
3193 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3194 if (c == '\\' && xp->xp_pattern[1] != NUL)
3195 ++xp->xp_pattern;
3196 xp->xp_context = EXPAND_NOTHING;
3197 }
3198 else if (c == '\'') /* literal string */
3199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003200 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3202 /* skip */ ;
3203 xp->xp_context = EXPAND_NOTHING;
3204 }
3205 else if (c == '|')
3206 {
3207 if (xp->xp_pattern[1] == '|')
3208 {
3209 ++xp->xp_pattern;
3210 xp->xp_context = EXPAND_EXPRESSION;
3211 }
3212 else
3213 xp->xp_context = EXPAND_COMMANDS;
3214 }
3215 else
3216 xp->xp_context = EXPAND_EXPRESSION;
3217 }
3218 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 /* Doesn't look like something valid, expand as an expression
3220 * anyway. */
3221 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 arg = xp->xp_pattern;
3223 if (*arg != NUL)
3224 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3225 /* skip */ ;
3226 }
3227 xp->xp_pattern = arg;
3228}
3229
3230#endif /* FEAT_CMDL_COMPL */
3231
3232/*
3233 * ":1,25call func(arg1, arg2)" function call.
3234 */
3235 void
3236ex_call(eap)
3237 exarg_T *eap;
3238{
3239 char_u *arg = eap->arg;
3240 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003242 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 linenr_T lnum;
3246 int doesrange;
3247 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003250 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003251 if (fudi.fd_newkey != NULL)
3252 {
3253 /* Still need to give an error message for missing key. */
3254 EMSG2(_(e_dictkey), fudi.fd_newkey);
3255 vim_free(fudi.fd_newkey);
3256 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003257 if (tofree == NULL)
3258 return;
3259
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003260 /* Increase refcount on dictionary, it could get deleted when evaluating
3261 * the arguments. */
3262 if (fudi.fd_dict != NULL)
3263 ++fudi.fd_dict->dv_refcount;
3264
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003265 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003266 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003267 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar532c7802005-01-27 14:44:31 +00003269 /* Skip white space to allow ":call func ()". Not good, but required for
3270 * backward compatibility. */
3271 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003272 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 if (*startarg != '(')
3275 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003276 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 goto end;
3278 }
3279
3280 /*
3281 * When skipping, evaluate the function once, to find the end of the
3282 * arguments.
3283 * When the function takes a range, this is discovered after the first
3284 * call, and the loop is broken.
3285 */
3286 if (eap->skip)
3287 {
3288 ++emsg_skip;
3289 lnum = eap->line2; /* do it once, also with an invalid range */
3290 }
3291 else
3292 lnum = eap->line1;
3293 for ( ; lnum <= eap->line2; ++lnum)
3294 {
3295 if (!eap->skip && eap->addr_count > 0)
3296 {
3297 curwin->w_cursor.lnum = lnum;
3298 curwin->w_cursor.col = 0;
3299 }
3300 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003301 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003302 eap->line1, eap->line2, &doesrange,
3303 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 failed = TRUE;
3306 break;
3307 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003308
3309 /* Handle a function returning a Funcref, Dictionary or List. */
3310 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3311 {
3312 failed = TRUE;
3313 break;
3314 }
3315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003316 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (doesrange || eap->skip)
3318 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003321 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003323 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (aborting())
3325 break;
3326 }
3327 if (eap->skip)
3328 --emsg_skip;
3329
3330 if (!failed)
3331 {
3332 /* Check for trailing illegal characters and a following command. */
3333 if (!ends_excmd(*arg))
3334 {
3335 emsg_severe = TRUE;
3336 EMSG(_(e_trailing));
3337 }
3338 else
3339 eap->nextcmd = check_nextcmd(arg);
3340 }
3341
3342end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003343 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003344 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347/*
3348 * ":unlet[!] var1 ... " command.
3349 */
3350 void
3351ex_unlet(eap)
3352 exarg_T *eap;
3353{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003354 ex_unletlock(eap, eap->arg, 0);
3355}
3356
3357/*
3358 * ":lockvar" and ":unlockvar" commands
3359 */
3360 void
3361ex_lockvar(eap)
3362 exarg_T *eap;
3363{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003365 int deep = 2;
3366
3367 if (eap->forceit)
3368 deep = -1;
3369 else if (vim_isdigit(*arg))
3370 {
3371 deep = getdigits(&arg);
3372 arg = skipwhite(arg);
3373 }
3374
3375 ex_unletlock(eap, arg, deep);
3376}
3377
3378/*
3379 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3380 */
3381 static void
3382ex_unletlock(eap, argstart, deep)
3383 exarg_T *eap;
3384 char_u *argstart;
3385 int deep;
3386{
3387 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003390 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
3392 do
3393 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003394 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003395 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3396 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003397 if (lv.ll_name == NULL)
3398 error = TRUE; /* error but continue parsing */
3399 if (name_end == NULL || (!vim_iswhite(*name_end)
3400 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003402 if (name_end != NULL)
3403 {
3404 emsg_severe = TRUE;
3405 EMSG(_(e_trailing));
3406 }
3407 if (!(eap->skip || error))
3408 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 break;
3410 }
3411
3412 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003413 {
3414 if (eap->cmdidx == CMD_unlet)
3415 {
3416 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3417 error = TRUE;
3418 }
3419 else
3420 {
3421 if (do_lock_var(&lv, name_end, deep,
3422 eap->cmdidx == CMD_lockvar) == FAIL)
3423 error = TRUE;
3424 }
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003427 if (!eap->skip)
3428 clear_lval(&lv);
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 arg = skipwhite(name_end);
3431 } while (!ends_excmd(*arg));
3432
3433 eap->nextcmd = check_nextcmd(arg);
3434}
3435
Bram Moolenaar8c711452005-01-14 21:53:12 +00003436 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003437do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003438 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003439 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 int forceit;
3441{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003442 int ret = OK;
3443 int cc;
3444
3445 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003447 cc = *name_end;
3448 *name_end = NUL;
3449
3450 /* Normal name or expanded name. */
3451 if (check_changedtick(lp->ll_name))
3452 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003453 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003454 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003455 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003457 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3458 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 else if (lp->ll_range)
3460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003462
3463 /* Delete a range of List items. */
3464 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3465 {
3466 li = lp->ll_li->li_next;
3467 listitem_remove(lp->ll_list, lp->ll_li);
3468 lp->ll_li = li;
3469 ++lp->ll_n1;
3470 }
3471 }
3472 else
3473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003474 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 /* unlet a List item. */
3476 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003477 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003480 }
3481
3482 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003483}
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485/*
3486 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 */
3489 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003490do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003492 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
Bram Moolenaar33570922005-01-25 22:26:29 +00003494 hashtab_T *ht;
3495 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003496 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003497 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar33570922005-01-25 22:26:29 +00003499 ht = find_var_ht(name, &varname);
3500 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 hi = hash_find(ht, varname);
3503 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003504 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003505 di = HI2DI(hi);
3506 if (var_check_fixed(di->di_flags, name)
3507 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003508 return FAIL;
3509 delete_var(ht, hi);
3510 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 if (forceit)
3514 return OK;
3515 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 return FAIL;
3517}
3518
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519/*
3520 * Lock or unlock variable indicated by "lp".
3521 * "deep" is the levels to go (-1 for unlimited);
3522 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3523 */
3524 static int
3525do_lock_var(lp, name_end, deep, lock)
3526 lval_T *lp;
3527 char_u *name_end;
3528 int deep;
3529 int lock;
3530{
3531 int ret = OK;
3532 int cc;
3533 dictitem_T *di;
3534
3535 if (deep == 0) /* nothing to do */
3536 return OK;
3537
3538 if (lp->ll_tv == NULL)
3539 {
3540 cc = *name_end;
3541 *name_end = NUL;
3542
3543 /* Normal name or expanded name. */
3544 if (check_changedtick(lp->ll_name))
3545 ret = FAIL;
3546 else
3547 {
3548 di = find_var(lp->ll_name, NULL);
3549 if (di == NULL)
3550 ret = FAIL;
3551 else
3552 {
3553 if (lock)
3554 di->di_flags |= DI_FLAGS_LOCK;
3555 else
3556 di->di_flags &= ~DI_FLAGS_LOCK;
3557 item_lock(&di->di_tv, deep, lock);
3558 }
3559 }
3560 *name_end = cc;
3561 }
3562 else if (lp->ll_range)
3563 {
3564 listitem_T *li = lp->ll_li;
3565
3566 /* (un)lock a range of List items. */
3567 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3568 {
3569 item_lock(&li->li_tv, deep, lock);
3570 li = li->li_next;
3571 ++lp->ll_n1;
3572 }
3573 }
3574 else if (lp->ll_list != NULL)
3575 /* (un)lock a List item. */
3576 item_lock(&lp->ll_li->li_tv, deep, lock);
3577 else
3578 /* un(lock) a Dictionary item. */
3579 item_lock(&lp->ll_di->di_tv, deep, lock);
3580
3581 return ret;
3582}
3583
3584/*
3585 * Lock or unlock an item. "deep" is nr of levels to go.
3586 */
3587 static void
3588item_lock(tv, deep, lock)
3589 typval_T *tv;
3590 int deep;
3591 int lock;
3592{
3593 static int recurse = 0;
3594 list_T *l;
3595 listitem_T *li;
3596 dict_T *d;
3597 hashitem_T *hi;
3598 int todo;
3599
3600 if (recurse >= DICT_MAXNEST)
3601 {
3602 EMSG(_("E743: variable nested too deep for (un)lock"));
3603 return;
3604 }
3605 if (deep == 0)
3606 return;
3607 ++recurse;
3608
3609 /* lock/unlock the item itself */
3610 if (lock)
3611 tv->v_lock |= VAR_LOCKED;
3612 else
3613 tv->v_lock &= ~VAR_LOCKED;
3614
3615 switch (tv->v_type)
3616 {
3617 case VAR_LIST:
3618 if ((l = tv->vval.v_list) != NULL)
3619 {
3620 if (lock)
3621 l->lv_lock |= VAR_LOCKED;
3622 else
3623 l->lv_lock &= ~VAR_LOCKED;
3624 if (deep < 0 || deep > 1)
3625 /* recursive: lock/unlock the items the List contains */
3626 for (li = l->lv_first; li != NULL; li = li->li_next)
3627 item_lock(&li->li_tv, deep - 1, lock);
3628 }
3629 break;
3630 case VAR_DICT:
3631 if ((d = tv->vval.v_dict) != NULL)
3632 {
3633 if (lock)
3634 d->dv_lock |= VAR_LOCKED;
3635 else
3636 d->dv_lock &= ~VAR_LOCKED;
3637 if (deep < 0 || deep > 1)
3638 {
3639 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003640 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3642 {
3643 if (!HASHITEM_EMPTY(hi))
3644 {
3645 --todo;
3646 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3647 }
3648 }
3649 }
3650 }
3651 }
3652 --recurse;
3653}
3654
Bram Moolenaara40058a2005-07-11 22:42:07 +00003655/*
3656 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3657 * it refers to a List or Dictionary that is locked.
3658 */
3659 static int
3660tv_islocked(tv)
3661 typval_T *tv;
3662{
3663 return (tv->v_lock & VAR_LOCKED)
3664 || (tv->v_type == VAR_LIST
3665 && tv->vval.v_list != NULL
3666 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3667 || (tv->v_type == VAR_DICT
3668 && tv->vval.v_dict != NULL
3669 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3670}
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3673/*
3674 * Delete all "menutrans_" variables.
3675 */
3676 void
3677del_menutrans_vars()
3678{
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003680 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003683 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 {
3686 if (!HASHITEM_EMPTY(hi))
3687 {
3688 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3690 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 }
3692 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003693 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694}
3695#endif
3696
3697#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3698
3699/*
3700 * Local string buffer for the next two functions to store a variable name
3701 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3702 * get_user_var_name().
3703 */
3704
3705static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3706
3707static char_u *varnamebuf = NULL;
3708static int varnamebuflen = 0;
3709
3710/*
3711 * Function to concatenate a prefix and a variable name.
3712 */
3713 static char_u *
3714cat_prefix_varname(prefix, name)
3715 int prefix;
3716 char_u *name;
3717{
3718 int len;
3719
3720 len = (int)STRLEN(name) + 3;
3721 if (len > varnamebuflen)
3722 {
3723 vim_free(varnamebuf);
3724 len += 10; /* some additional space */
3725 varnamebuf = alloc(len);
3726 if (varnamebuf == NULL)
3727 {
3728 varnamebuflen = 0;
3729 return NULL;
3730 }
3731 varnamebuflen = len;
3732 }
3733 *varnamebuf = prefix;
3734 varnamebuf[1] = ':';
3735 STRCPY(varnamebuf + 2, name);
3736 return varnamebuf;
3737}
3738
3739/*
3740 * Function given to ExpandGeneric() to obtain the list of user defined
3741 * (global/buffer/window/built-in) variable names.
3742 */
3743/*ARGSUSED*/
3744 char_u *
3745get_user_var_name(xp, idx)
3746 expand_T *xp;
3747 int idx;
3748{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003749 static long_u gdone;
3750 static long_u bdone;
3751 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003752#ifdef FEAT_WINDOWS
3753 static long_u tdone;
3754#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003755 static int vidx;
3756 static hashitem_T *hi;
3757 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003760 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003762#ifdef FEAT_WINDOWS
3763 tdone = 0;
3764#endif
3765 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003766
3767 /* Global variables */
3768 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003772 else
3773 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003774 while (HASHITEM_EMPTY(hi))
3775 ++hi;
3776 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3777 return cat_prefix_varname('g', hi->hi_key);
3778 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003780
3781 /* b: variables */
3782 ht = &curbuf->b_vars.dv_hashtab;
3783 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003785 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003787 else
3788 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003789 while (HASHITEM_EMPTY(hi))
3790 ++hi;
3791 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003793 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003795 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return (char_u *)"b:changedtick";
3797 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003798
3799 /* w: variables */
3800 ht = &curwin->w_vars.dv_hashtab;
3801 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003803 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003805 else
3806 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 while (HASHITEM_EMPTY(hi))
3808 ++hi;
3809 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003811
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003812#ifdef FEAT_WINDOWS
3813 /* t: variables */
3814 ht = &curtab->tp_vars.dv_hashtab;
3815 if (tdone < ht->ht_used)
3816 {
3817 if (tdone++ == 0)
3818 hi = ht->ht_array;
3819 else
3820 ++hi;
3821 while (HASHITEM_EMPTY(hi))
3822 ++hi;
3823 return cat_prefix_varname('t', hi->hi_key);
3824 }
3825#endif
3826
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 /* v: variables */
3828 if (vidx < VV_LEN)
3829 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 vim_free(varnamebuf);
3832 varnamebuf = NULL;
3833 varnamebuflen = 0;
3834 return NULL;
3835}
3836
3837#endif /* FEAT_CMDL_COMPL */
3838
3839/*
3840 * types for expressions.
3841 */
3842typedef enum
3843{
3844 TYPE_UNKNOWN = 0
3845 , TYPE_EQUAL /* == */
3846 , TYPE_NEQUAL /* != */
3847 , TYPE_GREATER /* > */
3848 , TYPE_GEQUAL /* >= */
3849 , TYPE_SMALLER /* < */
3850 , TYPE_SEQUAL /* <= */
3851 , TYPE_MATCH /* =~ */
3852 , TYPE_NOMATCH /* !~ */
3853} exptype_T;
3854
3855/*
3856 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003857 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3859 */
3860
3861/*
3862 * Handle zero level expression.
3863 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003865 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * Return OK or FAIL.
3867 */
3868 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003869eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 char_u **nextcmd;
3873 int evaluate;
3874{
3875 int ret;
3876 char_u *p;
3877
3878 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003879 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (ret == FAIL || !ends_excmd(*p))
3881 {
3882 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003883 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 /*
3885 * Report the invalid expression unless the expression evaluation has
3886 * been cancelled due to an aborting error, an interrupt, or an
3887 * exception.
3888 */
3889 if (!aborting())
3890 EMSG2(_(e_invexpr2), arg);
3891 ret = FAIL;
3892 }
3893 if (nextcmd != NULL)
3894 *nextcmd = check_nextcmd(p);
3895
3896 return ret;
3897}
3898
3899/*
3900 * Handle top level expression:
3901 * expr1 ? expr0 : expr0
3902 *
3903 * "arg" must point to the first non-white of the expression.
3904 * "arg" is advanced to the next non-white after the recognized expression.
3905 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003906 * Note: "rettv.v_lock" is not set.
3907 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * Return OK or FAIL.
3909 */
3910 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003911eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 int evaluate;
3915{
3916 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919 /*
3920 * Get the first variable.
3921 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 return FAIL;
3924
3925 if ((*arg)[0] == '?')
3926 {
3927 result = FALSE;
3928 if (evaluate)
3929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003930 int error = FALSE;
3931
3932 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003934 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003935 if (error)
3936 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938
3939 /*
3940 * Get the second variable.
3941 */
3942 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 return FAIL;
3945
3946 /*
3947 * Check for the ":".
3948 */
3949 if ((*arg)[0] != ':')
3950 {
3951 EMSG(_("E109: Missing ':' after '?'"));
3952 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return FAIL;
3955 }
3956
3957 /*
3958 * Get the third variable.
3959 */
3960 *arg = skipwhite(*arg + 1);
3961 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3962 {
3963 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003964 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return FAIL;
3966 }
3967 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970
3971 return OK;
3972}
3973
3974/*
3975 * Handle first level expression:
3976 * expr2 || expr2 || expr2 logical OR
3977 *
3978 * "arg" must point to the first non-white of the expression.
3979 * "arg" is advanced to the next non-white after the recognized expression.
3980 *
3981 * Return OK or FAIL.
3982 */
3983 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 int evaluate;
3988{
Bram Moolenaar33570922005-01-25 22:26:29 +00003989 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 long result;
3991 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 /*
3995 * Get the first variable.
3996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 return FAIL;
3999
4000 /*
4001 * Repeat until there is no following "||".
4002 */
4003 first = TRUE;
4004 result = FALSE;
4005 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4006 {
4007 if (evaluate && first)
4008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004009 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 if (error)
4013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 first = FALSE;
4015 }
4016
4017 /*
4018 * Get the second variable.
4019 */
4020 *arg = skipwhite(*arg + 2);
4021 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4022 return FAIL;
4023
4024 /*
4025 * Compute the result.
4026 */
4027 if (evaluate && !result)
4028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004029 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 if (error)
4033 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 if (evaluate)
4036 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 rettv->v_type = VAR_NUMBER;
4038 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040 }
4041
4042 return OK;
4043}
4044
4045/*
4046 * Handle second level expression:
4047 * expr3 && expr3 && expr3 logical AND
4048 *
4049 * "arg" must point to the first non-white of the expression.
4050 * "arg" is advanced to the next non-white after the recognized expression.
4051 *
4052 * Return OK or FAIL.
4053 */
4054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int evaluate;
4059{
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 long result;
4062 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 /*
4066 * Get the first variable.
4067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 return FAIL;
4070
4071 /*
4072 * Repeat until there is no following "&&".
4073 */
4074 first = TRUE;
4075 result = TRUE;
4076 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4077 {
4078 if (evaluate && first)
4079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004080 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 if (error)
4084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 first = FALSE;
4086 }
4087
4088 /*
4089 * Get the second variable.
4090 */
4091 *arg = skipwhite(*arg + 2);
4092 if (eval4(arg, &var2, evaluate && result) == FAIL)
4093 return FAIL;
4094
4095 /*
4096 * Compute the result.
4097 */
4098 if (evaluate && result)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103 if (error)
4104 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 if (evaluate)
4107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 rettv->v_type = VAR_NUMBER;
4109 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 }
4112
4113 return OK;
4114}
4115
4116/*
4117 * Handle third level expression:
4118 * var1 == var2
4119 * var1 =~ var2
4120 * var1 != var2
4121 * var1 !~ var2
4122 * var1 > var2
4123 * var1 >= var2
4124 * var1 < var2
4125 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004126 * var1 is var2
4127 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 *
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4131 *
4132 * Return OK or FAIL.
4133 */
4134 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 int evaluate;
4139{
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u *p;
4142 int i;
4143 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004144 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int len = 2;
4146 long n1, n2;
4147 char_u *s1, *s2;
4148 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4149 regmatch_T regmatch;
4150 int ic;
4151 char_u *save_cpo;
4152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 p = *arg;
4160 switch (p[0])
4161 {
4162 case '=': if (p[1] == '=')
4163 type = TYPE_EQUAL;
4164 else if (p[1] == '~')
4165 type = TYPE_MATCH;
4166 break;
4167 case '!': if (p[1] == '=')
4168 type = TYPE_NEQUAL;
4169 else if (p[1] == '~')
4170 type = TYPE_NOMATCH;
4171 break;
4172 case '>': if (p[1] != '=')
4173 {
4174 type = TYPE_GREATER;
4175 len = 1;
4176 }
4177 else
4178 type = TYPE_GEQUAL;
4179 break;
4180 case '<': if (p[1] != '=')
4181 {
4182 type = TYPE_SMALLER;
4183 len = 1;
4184 }
4185 else
4186 type = TYPE_SEQUAL;
4187 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004188 case 'i': if (p[1] == 's')
4189 {
4190 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4191 len = 5;
4192 if (!vim_isIDc(p[len]))
4193 {
4194 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4195 type_is = TRUE;
4196 }
4197 }
4198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200
4201 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004202 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
4204 if (type != TYPE_UNKNOWN)
4205 {
4206 /* extra question mark appended: ignore case */
4207 if (p[len] == '?')
4208 {
4209 ic = TRUE;
4210 ++len;
4211 }
4212 /* extra '#' appended: match case */
4213 else if (p[len] == '#')
4214 {
4215 ic = FALSE;
4216 ++len;
4217 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004218 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 else
4220 ic = p_ic;
4221
4222 /*
4223 * Get the second variable.
4224 */
4225 *arg = skipwhite(p + len);
4226 if (eval5(arg, &var2, evaluate) == FAIL)
4227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return FAIL;
4230 }
4231
4232 if (evaluate)
4233 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004234 if (type_is && rettv->v_type != var2.v_type)
4235 {
4236 /* For "is" a different type always means FALSE, for "notis"
4237 * it means TRUE. */
4238 n1 = (type == TYPE_NEQUAL);
4239 }
4240 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4241 {
4242 if (type_is)
4243 {
4244 n1 = (rettv->v_type == var2.v_type
4245 && rettv->vval.v_list == var2.vval.v_list);
4246 if (type == TYPE_NEQUAL)
4247 n1 = !n1;
4248 }
4249 else if (rettv->v_type != var2.v_type
4250 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4251 {
4252 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004253 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004254 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004255 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004256 clear_tv(rettv);
4257 clear_tv(&var2);
4258 return FAIL;
4259 }
4260 else
4261 {
4262 /* Compare two Lists for being equal or unequal. */
4263 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4264 if (type == TYPE_NEQUAL)
4265 n1 = !n1;
4266 }
4267 }
4268
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004269 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4270 {
4271 if (type_is)
4272 {
4273 n1 = (rettv->v_type == var2.v_type
4274 && rettv->vval.v_dict == var2.vval.v_dict);
4275 if (type == TYPE_NEQUAL)
4276 n1 = !n1;
4277 }
4278 else if (rettv->v_type != var2.v_type
4279 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4280 {
4281 if (rettv->v_type != var2.v_type)
4282 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4283 else
4284 EMSG(_("E736: Invalid operation for Dictionary"));
4285 clear_tv(rettv);
4286 clear_tv(&var2);
4287 return FAIL;
4288 }
4289 else
4290 {
4291 /* Compare two Dictionaries for being equal or unequal. */
4292 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4293 if (type == TYPE_NEQUAL)
4294 n1 = !n1;
4295 }
4296 }
4297
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004298 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4299 {
4300 if (rettv->v_type != var2.v_type
4301 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4302 {
4303 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004304 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004306 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004307 clear_tv(rettv);
4308 clear_tv(&var2);
4309 return FAIL;
4310 }
4311 else
4312 {
4313 /* Compare two Funcrefs for being equal or unequal. */
4314 if (rettv->vval.v_string == NULL
4315 || var2.vval.v_string == NULL)
4316 n1 = FALSE;
4317 else
4318 n1 = STRCMP(rettv->vval.v_string,
4319 var2.vval.v_string) == 0;
4320 if (type == TYPE_NEQUAL)
4321 n1 = !n1;
4322 }
4323 }
4324
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004325#ifdef FEAT_FLOAT
4326 /*
4327 * If one of the two variables is a float, compare as a float.
4328 * When using "=~" or "!~", always compare as string.
4329 */
4330 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4331 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4332 {
4333 float_T f1, f2;
4334
4335 if (rettv->v_type == VAR_FLOAT)
4336 f1 = rettv->vval.v_float;
4337 else
4338 f1 = get_tv_number(rettv);
4339 if (var2.v_type == VAR_FLOAT)
4340 f2 = var2.vval.v_float;
4341 else
4342 f2 = get_tv_number(&var2);
4343 n1 = FALSE;
4344 switch (type)
4345 {
4346 case TYPE_EQUAL: n1 = (f1 == f2); break;
4347 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4348 case TYPE_GREATER: n1 = (f1 > f2); break;
4349 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4350 case TYPE_SMALLER: n1 = (f1 < f2); break;
4351 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4352 case TYPE_UNKNOWN:
4353 case TYPE_MATCH:
4354 case TYPE_NOMATCH: break; /* avoid gcc warning */
4355 }
4356 }
4357#endif
4358
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 /*
4360 * If one of the two variables is a number, compare as a number.
4361 * When using "=~" or "!~", always compare as string.
4362 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004363 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 n1 = get_tv_number(rettv);
4367 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 switch (type)
4369 {
4370 case TYPE_EQUAL: n1 = (n1 == n2); break;
4371 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4372 case TYPE_GREATER: n1 = (n1 > n2); break;
4373 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4374 case TYPE_SMALLER: n1 = (n1 < n2); break;
4375 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4376 case TYPE_UNKNOWN:
4377 case TYPE_MATCH:
4378 case TYPE_NOMATCH: break; /* avoid gcc warning */
4379 }
4380 }
4381 else
4382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 s1 = get_tv_string_buf(rettv, buf1);
4384 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4386 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4387 else
4388 i = 0;
4389 n1 = FALSE;
4390 switch (type)
4391 {
4392 case TYPE_EQUAL: n1 = (i == 0); break;
4393 case TYPE_NEQUAL: n1 = (i != 0); break;
4394 case TYPE_GREATER: n1 = (i > 0); break;
4395 case TYPE_GEQUAL: n1 = (i >= 0); break;
4396 case TYPE_SMALLER: n1 = (i < 0); break;
4397 case TYPE_SEQUAL: n1 = (i <= 0); break;
4398
4399 case TYPE_MATCH:
4400 case TYPE_NOMATCH:
4401 /* avoid 'l' flag in 'cpoptions' */
4402 save_cpo = p_cpo;
4403 p_cpo = (char_u *)"";
4404 regmatch.regprog = vim_regcomp(s2,
4405 RE_MAGIC + RE_STRING);
4406 regmatch.rm_ic = ic;
4407 if (regmatch.regprog != NULL)
4408 {
4409 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4410 vim_free(regmatch.regprog);
4411 if (type == TYPE_NOMATCH)
4412 n1 = !n1;
4413 }
4414 p_cpo = save_cpo;
4415 break;
4416
4417 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4418 }
4419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 clear_tv(rettv);
4421 clear_tv(&var2);
4422 rettv->v_type = VAR_NUMBER;
4423 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 }
4426
4427 return OK;
4428}
4429
4430/*
4431 * Handle fourth level expression:
4432 * + number addition
4433 * - number subtraction
4434 * . string concatenation
4435 *
4436 * "arg" must point to the first non-white of the expression.
4437 * "arg" is advanced to the next non-white after the recognized expression.
4438 *
4439 * Return OK or FAIL.
4440 */
4441 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 int evaluate;
4446{
Bram Moolenaar33570922005-01-25 22:26:29 +00004447 typval_T var2;
4448 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 int op;
4450 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004451#ifdef FEAT_FLOAT
4452 float_T f1 = 0, f2 = 0;
4453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 char_u *s1, *s2;
4455 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4456 char_u *p;
4457
4458 /*
4459 * Get the first variable.
4460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 return FAIL;
4463
4464 /*
4465 * Repeat computing, until no '+', '-' or '.' is following.
4466 */
4467 for (;;)
4468 {
4469 op = **arg;
4470 if (op != '+' && op != '-' && op != '.')
4471 break;
4472
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 if ((op != '+' || rettv->v_type != VAR_LIST)
4474#ifdef FEAT_FLOAT
4475 && (op == '.' || rettv->v_type != VAR_FLOAT)
4476#endif
4477 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004478 {
4479 /* For "list + ...", an illegal use of the first operand as
4480 * a number cannot be determined before evaluating the 2nd
4481 * operand: if this is also a list, all is ok.
4482 * For "something . ...", "something - ..." or "non-list + ...",
4483 * we know that the first operand needs to be a string or number
4484 * without evaluating the 2nd operand. So check before to avoid
4485 * side effects after an error. */
4486 if (evaluate && get_tv_string_chk(rettv) == NULL)
4487 {
4488 clear_tv(rettv);
4489 return FAIL;
4490 }
4491 }
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /*
4494 * Get the second variable.
4495 */
4496 *arg = skipwhite(*arg + 1);
4497 if (eval6(arg, &var2, evaluate) == FAIL)
4498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 return FAIL;
4501 }
4502
4503 if (evaluate)
4504 {
4505 /*
4506 * Compute the result.
4507 */
4508 if (op == '.')
4509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004510 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4511 s2 = get_tv_string_buf_chk(&var2, buf2);
4512 if (s2 == NULL) /* type error ? */
4513 {
4514 clear_tv(rettv);
4515 clear_tv(&var2);
4516 return FAIL;
4517 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 clear_tv(rettv);
4520 rettv->v_type = VAR_STRING;
4521 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004523 else if (op == '+' && rettv->v_type == VAR_LIST
4524 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 {
4526 /* concatenate Lists */
4527 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4528 &var3) == FAIL)
4529 {
4530 clear_tv(rettv);
4531 clear_tv(&var2);
4532 return FAIL;
4533 }
4534 clear_tv(rettv);
4535 *rettv = var3;
4536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 else
4538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004539 int error = FALSE;
4540
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004541#ifdef FEAT_FLOAT
4542 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004543 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004544 f1 = rettv->vval.v_float;
4545 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004546 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004547 else
4548#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004549 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004550 n1 = get_tv_number_chk(rettv, &error);
4551 if (error)
4552 {
4553 /* This can only happen for "list + non-list". For
4554 * "non-list + ..." or "something - ...", we returned
4555 * before evaluating the 2nd operand. */
4556 clear_tv(rettv);
4557 return FAIL;
4558 }
4559#ifdef FEAT_FLOAT
4560 if (var2.v_type == VAR_FLOAT)
4561 f1 = n1;
4562#endif
4563 }
4564#ifdef FEAT_FLOAT
4565 if (var2.v_type == VAR_FLOAT)
4566 {
4567 f2 = var2.vval.v_float;
4568 n2 = 0;
4569 }
4570 else
4571#endif
4572 {
4573 n2 = get_tv_number_chk(&var2, &error);
4574 if (error)
4575 {
4576 clear_tv(rettv);
4577 clear_tv(&var2);
4578 return FAIL;
4579 }
4580#ifdef FEAT_FLOAT
4581 if (rettv->v_type == VAR_FLOAT)
4582 f2 = n2;
4583#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586
4587#ifdef FEAT_FLOAT
4588 /* If there is a float on either side the result is a float. */
4589 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4590 {
4591 if (op == '+')
4592 f1 = f1 + f2;
4593 else
4594 f1 = f1 - f2;
4595 rettv->v_type = VAR_FLOAT;
4596 rettv->vval.v_float = f1;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004599#endif
4600 {
4601 if (op == '+')
4602 n1 = n1 + n2;
4603 else
4604 n1 = n1 - n2;
4605 rettv->v_type = VAR_NUMBER;
4606 rettv->vval.v_number = n1;
4607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 }
4612 return OK;
4613}
4614
4615/*
4616 * Handle fifth level expression:
4617 * * number multiplication
4618 * / number division
4619 * % number modulo
4620 *
4621 * "arg" must point to the first non-white of the expression.
4622 * "arg" is advanced to the next non-white after the recognized expression.
4623 *
4624 * Return OK or FAIL.
4625 */
4626 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int evaluate;
4631{
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 int op;
4634 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004635#ifdef FEAT_FLOAT
4636 int use_float = FALSE;
4637 float_T f1 = 0, f2;
4638#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
4641 /*
4642 * Get the first variable.
4643 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return FAIL;
4646
4647 /*
4648 * Repeat computing, until no '*', '/' or '%' is following.
4649 */
4650 for (;;)
4651 {
4652 op = **arg;
4653 if (op != '*' && op != '/' && op != '%')
4654 break;
4655
4656 if (evaluate)
4657 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004658#ifdef FEAT_FLOAT
4659 if (rettv->v_type == VAR_FLOAT)
4660 {
4661 f1 = rettv->vval.v_float;
4662 use_float = TRUE;
4663 n1 = 0;
4664 }
4665 else
4666#endif
4667 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 if (error)
4670 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 else
4673 n1 = 0;
4674
4675 /*
4676 * Get the second variable.
4677 */
4678 *arg = skipwhite(*arg + 1);
4679 if (eval7(arg, &var2, evaluate) == FAIL)
4680 return FAIL;
4681
4682 if (evaluate)
4683 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684#ifdef FEAT_FLOAT
4685 if (var2.v_type == VAR_FLOAT)
4686 {
4687 if (!use_float)
4688 {
4689 f1 = n1;
4690 use_float = TRUE;
4691 }
4692 f2 = var2.vval.v_float;
4693 n2 = 0;
4694 }
4695 else
4696#endif
4697 {
4698 n2 = get_tv_number_chk(&var2, &error);
4699 clear_tv(&var2);
4700 if (error)
4701 return FAIL;
4702#ifdef FEAT_FLOAT
4703 if (use_float)
4704 f2 = n2;
4705#endif
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 /*
4709 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if (op == '*')
4716 f1 = f1 * f2;
4717 else if (op == '/')
4718 {
4719 /* We rely on the floating point library to handle divide
4720 * by zero to result in "inf" and not a crash. */
4721 f1 = f1 / f2;
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724 {
4725 EMSG(_("E804: Cannot use % with float"));
4726 return FAIL;
4727 }
4728 rettv->v_type = VAR_FLOAT;
4729 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 if (op == '*')
4735 n1 = n1 * n2;
4736 else if (op == '/')
4737 {
4738 if (n2 == 0) /* give an error message? */
4739 {
4740 if (n1 == 0)
4741 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4742 else if (n1 < 0)
4743 n1 = -0x7fffffffL;
4744 else
4745 n1 = 0x7fffffffL;
4746 }
4747 else
4748 n1 = n1 / n2;
4749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751 {
4752 if (n2 == 0) /* give an error message? */
4753 n1 = 0;
4754 else
4755 n1 = n1 % n2;
4756 }
4757 rettv->v_type = VAR_NUMBER;
4758 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 }
4762
4763 return OK;
4764}
4765
4766/*
4767 * Handle sixth level expression:
4768 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004769 * "string" string constant
4770 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 * &option-name option value
4772 * @r register contents
4773 * identifier variable value
4774 * function() function call
4775 * $VAR environment variable
4776 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004777 * [expr, expr] List
4778 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 *
4780 * Also handle:
4781 * ! in front logical NOT
4782 * - in front unary minus
4783 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004784 * trailing [] subscript in String or List
4785 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 *
4787 * "arg" must point to the first non-white of the expression.
4788 * "arg" is advanced to the next non-white after the recognized expression.
4789 *
4790 * Return OK or FAIL.
4791 */
4792 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004793eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 int evaluate;
4797{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 long n;
4799 int len;
4800 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 char_u *start_leader, *end_leader;
4802 int ret = OK;
4803 char_u *alias;
4804
4805 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004806 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
4811 /*
4812 * Skip '!' and '-' characters. They are handled later.
4813 */
4814 start_leader = *arg;
4815 while (**arg == '!' || **arg == '-' || **arg == '+')
4816 *arg = skipwhite(*arg + 1);
4817 end_leader = *arg;
4818
4819 switch (**arg)
4820 {
4821 /*
4822 * Number constant.
4823 */
4824 case '0':
4825 case '1':
4826 case '2':
4827 case '3':
4828 case '4':
4829 case '5':
4830 case '6':
4831 case '7':
4832 case '8':
4833 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004834 {
4835#ifdef FEAT_FLOAT
4836 char_u *p = skipdigits(*arg + 1);
4837 int get_float = FALSE;
4838
4839 /* We accept a float when the format matches
4840 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4841 * strict to avoid backwards compatibility problems. */
4842 if (p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844 get_float = TRUE;
4845 p = skipdigits(p + 2);
4846 if (*p == 'e' || *p == 'E')
4847 {
4848 ++p;
4849 if (*p == '-' || *p == '+')
4850 ++p;
4851 if (!vim_isdigit(*p))
4852 get_float = FALSE;
4853 else
4854 p = skipdigits(p + 1);
4855 }
4856 if (ASCII_ISALPHA(*p) || *p == '.')
4857 get_float = FALSE;
4858 }
4859 if (get_float)
4860 {
4861 float_T f;
4862
4863 *arg += string2float(*arg, &f);
4864 if (evaluate)
4865 {
4866 rettv->v_type = VAR_FLOAT;
4867 rettv->vval.v_float = f;
4868 }
4869 }
4870 else
4871#endif
4872 {
4873 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4874 *arg += len;
4875 if (evaluate)
4876 {
4877 rettv->v_type = VAR_NUMBER;
4878 rettv->vval.v_number = n;
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * String constant: "string".
4886 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 break;
4889
4890 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004891 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 break;
4895
4896 /*
4897 * List: [expr, expr]
4898 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 break;
4901
4902 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 * Dictionary: {key: val, key: val}
4904 */
4905 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4906 break;
4907
4908 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004909 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004911 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 break;
4913
4914 /*
4915 * Environment variable: $VAR.
4916 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 break;
4919
4920 /*
4921 * Register contents: @r.
4922 */
4923 case '@': ++*arg;
4924 if (evaluate)
4925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004927 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 if (**arg != NUL)
4930 ++*arg;
4931 break;
4932
4933 /*
4934 * nested expression: (expression).
4935 */
4936 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (**arg == ')')
4939 ++*arg;
4940 else if (ret == OK)
4941 {
4942 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004943 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 ret = FAIL;
4945 }
4946 break;
4947
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 break;
4950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951
4952 if (ret == NOTDONE)
4953 {
4954 /*
4955 * Must be a variable or function name.
4956 * Can also be a curly-braces kind of name: {expr}.
4957 */
4958 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004959 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 if (alias != NULL)
4961 s = alias;
4962
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004963 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 ret = FAIL;
4965 else
4966 {
4967 if (**arg == '(') /* recursive! */
4968 {
4969 /* If "s" is the name of a variable of type VAR_FUNC
4970 * use its contents. */
4971 s = deref_func_name(s, &len);
4972
4973 /* Invoke the function. */
4974 ret = get_func_tv(s, len, rettv, arg,
4975 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004976 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004977 /* Stop the expression evaluation when immediately
4978 * aborting on error, or when an interrupt occurred or
4979 * an exception was thrown but not caught. */
4980 if (aborting())
4981 {
4982 if (ret == OK)
4983 clear_tv(rettv);
4984 ret = FAIL;
4985 }
4986 }
4987 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004988 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004989 else
4990 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 }
4992
4993 if (alias != NULL)
4994 vim_free(alias);
4995 }
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 *arg = skipwhite(*arg);
4998
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004999 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5000 * expr(expr). */
5001 if (ret == OK)
5002 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 /*
5005 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5006 */
5007 if (ret == OK && evaluate && end_leader > start_leader)
5008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005009 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 int val = 0;
5011#ifdef FEAT_FLOAT
5012 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005013
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 if (rettv->v_type == VAR_FLOAT)
5015 f = rettv->vval.v_float;
5016 else
5017#endif
5018 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005019 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005021 clear_tv(rettv);
5022 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005024 else
5025 {
5026 while (end_leader > start_leader)
5027 {
5028 --end_leader;
5029 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 {
5031#ifdef FEAT_FLOAT
5032 if (rettv->v_type == VAR_FLOAT)
5033 f = !f;
5034 else
5035#endif
5036 val = !val;
5037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005038 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005039 {
5040#ifdef FEAT_FLOAT
5041 if (rettv->v_type == VAR_FLOAT)
5042 f = -f;
5043 else
5044#endif
5045 val = -val;
5046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005047 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005048#ifdef FEAT_FLOAT
5049 if (rettv->v_type == VAR_FLOAT)
5050 {
5051 clear_tv(rettv);
5052 rettv->vval.v_float = f;
5053 }
5054 else
5055#endif
5056 {
5057 clear_tv(rettv);
5058 rettv->v_type = VAR_NUMBER;
5059 rettv->vval.v_number = val;
5060 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063
5064 return ret;
5065}
5066
5067/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005068 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5069 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5071 */
5072 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005073eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005075 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005076 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005077 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078{
5079 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005081 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 long len = -1;
5083 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 if (rettv->v_type == VAR_FUNC
5088#ifdef FEAT_FLOAT
5089 || rettv->v_type == VAR_FLOAT
5090#endif
5091 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005093 if (verbose)
5094 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005095 return FAIL;
5096 }
5097
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 /*
5101 * dict.name
5102 */
5103 key = *arg + 1;
5104 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5105 ;
5106 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005109 }
5110 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 /*
5113 * something[idx]
5114 *
5115 * Get the (first) variable from inside the [].
5116 */
5117 *arg = skipwhite(*arg + 1);
5118 if (**arg == ':')
5119 empty1 = TRUE;
5120 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5121 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5123 {
5124 /* not a number or string */
5125 clear_tv(&var1);
5126 return FAIL;
5127 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128
5129 /*
5130 * Get the second variable from inside the [:].
5131 */
5132 if (**arg == ':')
5133 {
5134 range = TRUE;
5135 *arg = skipwhite(*arg + 1);
5136 if (**arg == ']')
5137 empty2 = TRUE;
5138 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140 if (!empty1)
5141 clear_tv(&var1);
5142 return FAIL;
5143 }
5144 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5145 {
5146 /* not a number or string */
5147 if (!empty1)
5148 clear_tv(&var1);
5149 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 return FAIL;
5151 }
5152 }
5153
5154 /* Check for the ']'. */
5155 if (**arg != ']')
5156 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 if (verbose)
5158 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 clear_tv(&var1);
5160 if (range)
5161 clear_tv(&var2);
5162 return FAIL;
5163 }
5164 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 }
5166
5167 if (evaluate)
5168 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 n1 = 0;
5170 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 n1 = get_tv_number(&var1);
5173 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 }
5175 if (range)
5176 {
5177 if (empty2)
5178 n2 = -1;
5179 else
5180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 n2 = get_tv_number(&var2);
5182 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 }
5184 }
5185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 {
5188 case VAR_NUMBER:
5189 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 len = (long)STRLEN(s);
5192 if (range)
5193 {
5194 /* The resulting variable is a substring. If the indexes
5195 * are out of range the result is empty. */
5196 if (n1 < 0)
5197 {
5198 n1 = len + n1;
5199 if (n1 < 0)
5200 n1 = 0;
5201 }
5202 if (n2 < 0)
5203 n2 = len + n2;
5204 else if (n2 >= len)
5205 n2 = len;
5206 if (n1 >= len || n2 < 0 || n1 > n2)
5207 s = NULL;
5208 else
5209 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5210 }
5211 else
5212 {
5213 /* The resulting variable is a string of a single
5214 * character. If the index is too big or negative the
5215 * result is empty. */
5216 if (n1 >= len || n1 < 0)
5217 s = NULL;
5218 else
5219 s = vim_strnsave(s + n1, 1);
5220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 clear_tv(rettv);
5222 rettv->v_type = VAR_STRING;
5223 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 break;
5225
5226 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 if (n1 < 0)
5229 n1 = len + n1;
5230 if (!empty1 && (n1 < 0 || n1 >= len))
5231 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005232 /* For a range we allow invalid values and return an empty
5233 * list. A list index out of range is an error. */
5234 if (!range)
5235 {
5236 if (verbose)
5237 EMSGN(_(e_listidx), n1);
5238 return FAIL;
5239 }
5240 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 }
5242 if (range)
5243 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 list_T *l;
5245 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246
5247 if (n2 < 0)
5248 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005249 else if (n2 >= len)
5250 n2 = len - 1;
5251 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005252 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 l = list_alloc();
5254 if (l == NULL)
5255 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 n1 <= n2; ++n1)
5258 {
5259 if (list_append_tv(l, &item->li_tv) == FAIL)
5260 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005261 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 return FAIL;
5263 }
5264 item = item->li_next;
5265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 clear_tv(rettv);
5267 rettv->v_type = VAR_LIST;
5268 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005269 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 }
5271 else
5272 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005273 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 clear_tv(rettv);
5275 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 }
5277 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278
5279 case VAR_DICT:
5280 if (range)
5281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282 if (verbose)
5283 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 if (len == -1)
5285 clear_tv(&var1);
5286 return FAIL;
5287 }
5288 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 if (len == -1)
5292 {
5293 key = get_tv_string(&var1);
5294 if (*key == NUL)
5295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296 if (verbose)
5297 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 clear_tv(&var1);
5299 return FAIL;
5300 }
5301 }
5302
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005303 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005305 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005306 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 if (len == -1)
5308 clear_tv(&var1);
5309 if (item == NULL)
5310 return FAIL;
5311
5312 copy_tv(&item->di_tv, &var1);
5313 clear_tv(rettv);
5314 *rettv = var1;
5315 }
5316 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 }
5318 }
5319
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return OK;
5321}
5322
5323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 * Get an option value.
5325 * "arg" points to the '&' or '+' before the option name.
5326 * "arg" is advanced to character after the option name.
5327 * Return OK or FAIL.
5328 */
5329 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005330get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005332 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 int evaluate;
5334{
5335 char_u *option_end;
5336 long numval;
5337 char_u *stringval;
5338 int opt_type;
5339 int c;
5340 int working = (**arg == '+'); /* has("+option") */
5341 int ret = OK;
5342 int opt_flags;
5343
5344 /*
5345 * Isolate the option name and find its value.
5346 */
5347 option_end = find_option_end(arg, &opt_flags);
5348 if (option_end == NULL)
5349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005350 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 EMSG2(_("E112: Option name missing: %s"), *arg);
5352 return FAIL;
5353 }
5354
5355 if (!evaluate)
5356 {
5357 *arg = option_end;
5358 return OK;
5359 }
5360
5361 c = *option_end;
5362 *option_end = NUL;
5363 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 if (opt_type == -3) /* invalid name */
5367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 EMSG2(_("E113: Unknown option: %s"), *arg);
5370 ret = FAIL;
5371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 if (opt_type == -2) /* hidden string option */
5375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 rettv->v_type = VAR_STRING;
5377 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379 else if (opt_type == -1) /* hidden number option */
5380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 rettv->v_type = VAR_NUMBER;
5382 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384 else if (opt_type == 1) /* number option */
5385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 rettv->v_type = VAR_NUMBER;
5387 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 else /* string option */
5390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 rettv->v_type = VAR_STRING;
5392 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 }
5395 else if (working && (opt_type == -2 || opt_type == -1))
5396 ret = FAIL;
5397
5398 *option_end = c; /* put back for error messages */
5399 *arg = option_end;
5400
5401 return ret;
5402}
5403
5404/*
5405 * Allocate a variable for a string constant.
5406 * Return OK or FAIL.
5407 */
5408 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 int evaluate;
5413{
5414 char_u *p;
5415 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 int extra = 0;
5417
5418 /*
5419 * Find the end of the string, skipping backslashed characters.
5420 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005421 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
5423 if (*p == '\\' && p[1] != NUL)
5424 {
5425 ++p;
5426 /* A "\<x>" form occupies at least 4 characters, and produces up
5427 * to 6 characters: reserve space for 2 extra */
5428 if (*p == '<')
5429 extra += 2;
5430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432
5433 if (*p != '"')
5434 {
5435 EMSG2(_("E114: Missing quote: %s"), *arg);
5436 return FAIL;
5437 }
5438
5439 /* If only parsing, set *arg and return here */
5440 if (!evaluate)
5441 {
5442 *arg = p + 1;
5443 return OK;
5444 }
5445
5446 /*
5447 * Copy the string into allocated memory, handling backslashed
5448 * characters.
5449 */
5450 name = alloc((unsigned)(p - *arg + extra));
5451 if (name == NULL)
5452 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 rettv->v_type = VAR_STRING;
5454 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 if (*p == '\\')
5459 {
5460 switch (*++p)
5461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462 case 'b': *name++ = BS; ++p; break;
5463 case 'e': *name++ = ESC; ++p; break;
5464 case 'f': *name++ = FF; ++p; break;
5465 case 'n': *name++ = NL; ++p; break;
5466 case 'r': *name++ = CAR; ++p; break;
5467 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
5469 case 'X': /* hex: "\x1", "\x12" */
5470 case 'x':
5471 case 'u': /* Unicode: "\u0023" */
5472 case 'U':
5473 if (vim_isxdigit(p[1]))
5474 {
5475 int n, nr;
5476 int c = toupper(*p);
5477
5478 if (c == 'X')
5479 n = 2;
5480 else
5481 n = 4;
5482 nr = 0;
5483 while (--n >= 0 && vim_isxdigit(p[1]))
5484 {
5485 ++p;
5486 nr = (nr << 4) + hex2nr(*p);
5487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489#ifdef FEAT_MBYTE
5490 /* For "\u" store the number according to
5491 * 'encoding'. */
5492 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 else
5495#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 break;
5499
5500 /* octal: "\1", "\12", "\123" */
5501 case '0':
5502 case '1':
5503 case '2':
5504 case '3':
5505 case '4':
5506 case '5':
5507 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005508 case '7': *name = *p++ - '0';
5509 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 *name = (*name << 3) + *p++ - '0';
5512 if (*p >= '0' && *p <= '7')
5513 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 break;
5517
5518 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (extra != 0)
5521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 break;
5524 }
5525 /* FALLTHROUGH */
5526
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 break;
5529 }
5530 }
5531 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 *arg = p + 1;
5537
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return OK;
5539}
5540
5541/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005542 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 * Return OK or FAIL.
5544 */
5545 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 int evaluate;
5550{
5551 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005552 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005553 int reduce = 0;
5554
5555 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005556 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005557 */
5558 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5559 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005560 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005563 break;
5564 ++reduce;
5565 ++p;
5566 }
5567 }
5568
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005570 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005572 return FAIL;
5573 }
5574
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 if (!evaluate)
5577 {
5578 *arg = p + 1;
5579 return OK;
5580 }
5581
5582 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005584 */
5585 str = alloc((unsigned)((p - *arg) - reduce));
5586 if (str == NULL)
5587 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 rettv->v_type = VAR_STRING;
5589 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005590
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596 break;
5597 ++p;
5598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005602 *arg = p + 1;
5603
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005604 return OK;
5605}
5606
5607/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 * Allocate a variable for a List and fill it from "*arg".
5609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 int evaluate;
5616{
Bram Moolenaar33570922005-01-25 22:26:29 +00005617 list_T *l = NULL;
5618 typval_T tv;
5619 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620
5621 if (evaluate)
5622 {
5623 l = list_alloc();
5624 if (l == NULL)
5625 return FAIL;
5626 }
5627
5628 *arg = skipwhite(*arg + 1);
5629 while (**arg != ']' && **arg != NUL)
5630 {
5631 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5632 goto failret;
5633 if (evaluate)
5634 {
5635 item = listitem_alloc();
5636 if (item != NULL)
5637 {
5638 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005639 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 list_append(l, item);
5641 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005642 else
5643 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 }
5645
5646 if (**arg == ']')
5647 break;
5648 if (**arg != ',')
5649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 goto failret;
5652 }
5653 *arg = skipwhite(*arg + 1);
5654 }
5655
5656 if (**arg != ']')
5657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005659failret:
5660 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005661 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 return FAIL;
5663 }
5664
5665 *arg = skipwhite(*arg + 1);
5666 if (evaluate)
5667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 rettv->v_type = VAR_LIST;
5669 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 ++l->lv_refcount;
5671 }
5672
5673 return OK;
5674}
5675
5676/*
5677 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005678 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005680 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005681list_alloc()
5682{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005683 list_T *l;
5684
5685 l = (list_T *)alloc_clear(sizeof(list_T));
5686 if (l != NULL)
5687 {
5688 /* Prepend the list to the list of lists for garbage collection. */
5689 if (first_list != NULL)
5690 first_list->lv_used_prev = l;
5691 l->lv_used_prev = NULL;
5692 l->lv_used_next = first_list;
5693 first_list = l;
5694 }
5695 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696}
5697
5698/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005699 * Allocate an empty list for a return value.
5700 * Returns OK or FAIL.
5701 */
5702 static int
5703rettv_list_alloc(rettv)
5704 typval_T *rettv;
5705{
5706 list_T *l = list_alloc();
5707
5708 if (l == NULL)
5709 return FAIL;
5710
5711 rettv->vval.v_list = l;
5712 rettv->v_type = VAR_LIST;
5713 ++l->lv_refcount;
5714 return OK;
5715}
5716
5717/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 * Unreference a list: decrement the reference count and free it when it
5719 * becomes zero.
5720 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005721 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005725 if (l != NULL && --l->lv_refcount <= 0)
5726 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727}
5728
5729/*
5730 * Free a list, including all items it points to.
5731 * Ignores the reference count.
5732 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005733 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005734list_free(l, recurse)
5735 list_T *l;
5736 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737{
Bram Moolenaar33570922005-01-25 22:26:29 +00005738 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005740 /* Remove the list from the list of lists for garbage collection. */
5741 if (l->lv_used_prev == NULL)
5742 first_list = l->lv_used_next;
5743 else
5744 l->lv_used_prev->lv_used_next = l->lv_used_next;
5745 if (l->lv_used_next != NULL)
5746 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5747
Bram Moolenaard9fba312005-06-26 22:34:35 +00005748 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005750 /* Remove the item before deleting it. */
5751 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005752 if (recurse || (item->li_tv.v_type != VAR_LIST
5753 && item->li_tv.v_type != VAR_DICT))
5754 clear_tv(&item->li_tv);
5755 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 }
5757 vim_free(l);
5758}
5759
5760/*
5761 * Allocate a list item.
5762 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005763 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764listitem_alloc()
5765{
Bram Moolenaar33570922005-01-25 22:26:29 +00005766 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767}
5768
5769/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005770 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 */
5772 static void
5773listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005776 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777 vim_free(item);
5778}
5779
5780/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005781 * Remove a list item from a List and free it. Also clears the value.
5782 */
5783 static void
5784listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 list_T *l;
5786 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005787{
5788 list_remove(l, item, item);
5789 listitem_free(item);
5790}
5791
5792/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 * Get the number of items in a list.
5794 */
5795 static long
5796list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005797 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 if (l == NULL)
5800 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005801 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802}
5803
5804/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005805 * Return TRUE when two lists have exactly the same values.
5806 */
5807 static int
5808list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 list_T *l1;
5810 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005811 int ic; /* ignore case for strings */
5812{
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005814
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005815 if (l1 == l2)
5816 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005817 if (list_len(l1) != list_len(l2))
5818 return FALSE;
5819
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005820 for (item1 = l1->lv_first, item2 = l2->lv_first;
5821 item1 != NULL && item2 != NULL;
5822 item1 = item1->li_next, item2 = item2->li_next)
5823 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5824 return FALSE;
5825 return item1 == NULL && item2 == NULL;
5826}
5827
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005828#if defined(FEAT_PYTHON) || defined(PROTO)
5829/*
5830 * Return the dictitem that an entry in a hashtable points to.
5831 */
5832 dictitem_T *
5833dict_lookup(hi)
5834 hashitem_T *hi;
5835{
5836 return HI2DI(hi);
5837}
5838#endif
5839
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005841 * Return TRUE when two dictionaries have exactly the same key/values.
5842 */
5843 static int
5844dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 dict_T *d1;
5846 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005847 int ic; /* ignore case for strings */
5848{
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 hashitem_T *hi;
5850 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005852
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005853 if (d1 == d2)
5854 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005855 if (dict_len(d1) != dict_len(d2))
5856 return FALSE;
5857
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005858 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005860 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005861 if (!HASHITEM_EMPTY(hi))
5862 {
5863 item2 = dict_find(d2, hi->hi_key, -1);
5864 if (item2 == NULL)
5865 return FALSE;
5866 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5867 return FALSE;
5868 --todo;
5869 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005870 }
5871 return TRUE;
5872}
5873
5874/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005875 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005876 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005877 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005878 */
5879 static int
5880tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 typval_T *tv1;
5882 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 int ic; /* ignore case */
5884{
5885 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005886 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005887 static int recursive = 0; /* cach recursive loops */
5888 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005890 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005891 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005892 /* Catch lists and dicts that have an endless loop by limiting
5893 * recursiveness to 1000. We guess they are equal then. */
5894 if (recursive >= 1000)
5895 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005896
5897 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005899 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005900 ++recursive;
5901 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5902 --recursive;
5903 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005904
5905 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005906 ++recursive;
5907 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5908 --recursive;
5909 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005910
5911 case VAR_FUNC:
5912 return (tv1->vval.v_string != NULL
5913 && tv2->vval.v_string != NULL
5914 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5915
5916 case VAR_NUMBER:
5917 return tv1->vval.v_number == tv2->vval.v_number;
5918
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005919#ifdef FEAT_FLOAT
5920 case VAR_FLOAT:
5921 return tv1->vval.v_float == tv2->vval.v_float;
5922#endif
5923
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005924 case VAR_STRING:
5925 s1 = get_tv_string_buf(tv1, buf1);
5926 s2 = get_tv_string_buf(tv2, buf2);
5927 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005929
5930 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 return TRUE;
5932}
5933
5934/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 * Locate item with index "n" in list "l" and return it.
5936 * A negative index is counted from the end; -1 is the last item.
5937 * Returns NULL when "n" is out of range.
5938 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 long n;
5943{
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 long idx;
5946
5947 if (l == NULL)
5948 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005949
5950 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005952 n = l->lv_len + n;
5953
5954 /* Check for index out of range. */
5955 if (n < 0 || n >= l->lv_len)
5956 return NULL;
5957
5958 /* When there is a cached index may start search from there. */
5959 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005961 if (n < l->lv_idx / 2)
5962 {
5963 /* closest to the start of the list */
5964 item = l->lv_first;
5965 idx = 0;
5966 }
5967 else if (n > (l->lv_idx + l->lv_len) / 2)
5968 {
5969 /* closest to the end of the list */
5970 item = l->lv_last;
5971 idx = l->lv_len - 1;
5972 }
5973 else
5974 {
5975 /* closest to the cached index */
5976 item = l->lv_idx_item;
5977 idx = l->lv_idx;
5978 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 }
5980 else
5981 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005982 if (n < l->lv_len / 2)
5983 {
5984 /* closest to the start of the list */
5985 item = l->lv_first;
5986 idx = 0;
5987 }
5988 else
5989 {
5990 /* closest to the end of the list */
5991 item = l->lv_last;
5992 idx = l->lv_len - 1;
5993 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005995
5996 while (n > idx)
5997 {
5998 /* search forward */
5999 item = item->li_next;
6000 ++idx;
6001 }
6002 while (n < idx)
6003 {
6004 /* search backward */
6005 item = item->li_prev;
6006 --idx;
6007 }
6008
6009 /* cache the used index */
6010 l->lv_idx = idx;
6011 l->lv_idx_item = item;
6012
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 return item;
6014}
6015
6016/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006017 * Get list item "l[idx]" as a number.
6018 */
6019 static long
6020list_find_nr(l, idx, errorp)
6021 list_T *l;
6022 long idx;
6023 int *errorp; /* set to TRUE when something wrong */
6024{
6025 listitem_T *li;
6026
6027 li = list_find(l, idx);
6028 if (li == NULL)
6029 {
6030 if (errorp != NULL)
6031 *errorp = TRUE;
6032 return -1L;
6033 }
6034 return get_tv_number_chk(&li->li_tv, errorp);
6035}
6036
6037/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006038 * Locate "item" list "l" and return its index.
6039 * Returns -1 when "item" is not in the list.
6040 */
6041 static long
6042list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 list_T *l;
6044 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006045{
6046 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006048
6049 if (l == NULL)
6050 return -1;
6051 idx = 0;
6052 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6053 ++idx;
6054 if (li == NULL)
6055 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006056 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006057}
6058
6059/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 * Append item "item" to the end of list "l".
6061 */
6062 static void
6063list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 list_T *l;
6065 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066{
6067 if (l->lv_last == NULL)
6068 {
6069 /* empty list */
6070 l->lv_first = item;
6071 l->lv_last = item;
6072 item->li_prev = NULL;
6073 }
6074 else
6075 {
6076 l->lv_last->li_next = item;
6077 item->li_prev = l->lv_last;
6078 l->lv_last = item;
6079 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006080 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 item->li_next = NULL;
6082}
6083
6084/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 */
6088 static int
6089list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 list_T *l;
6091 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006093 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094
Bram Moolenaar05159a02005-02-26 23:04:13 +00006095 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006097 copy_tv(tv, &li->li_tv);
6098 list_append(l, li);
6099 return OK;
6100}
6101
6102/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006103 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006104 * Return FAIL when out of memory.
6105 */
6106 int
6107list_append_dict(list, dict)
6108 list_T *list;
6109 dict_T *dict;
6110{
6111 listitem_T *li = listitem_alloc();
6112
6113 if (li == NULL)
6114 return FAIL;
6115 li->li_tv.v_type = VAR_DICT;
6116 li->li_tv.v_lock = 0;
6117 li->li_tv.vval.v_dict = dict;
6118 list_append(list, li);
6119 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 return OK;
6121}
6122
6123/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006124 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006125 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006126 * Returns FAIL when out of memory.
6127 */
6128 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006129list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006130 list_T *l;
6131 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006132 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006133{
6134 listitem_T *li = listitem_alloc();
6135
6136 if (li == NULL)
6137 return FAIL;
6138 list_append(l, li);
6139 li->li_tv.v_type = VAR_STRING;
6140 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006141 if (str == NULL)
6142 li->li_tv.vval.v_string = NULL;
6143 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006144 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006145 return FAIL;
6146 return OK;
6147}
6148
6149/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006150 * Append "n" to list "l".
6151 * Returns FAIL when out of memory.
6152 */
6153 static int
6154list_append_number(l, n)
6155 list_T *l;
6156 varnumber_T n;
6157{
6158 listitem_T *li;
6159
6160 li = listitem_alloc();
6161 if (li == NULL)
6162 return FAIL;
6163 li->li_tv.v_type = VAR_NUMBER;
6164 li->li_tv.v_lock = 0;
6165 li->li_tv.vval.v_number = n;
6166 list_append(l, li);
6167 return OK;
6168}
6169
6170/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006172 * If "item" is NULL append at the end.
6173 * Return FAIL when out of memory.
6174 */
6175 static int
6176list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 list_T *l;
6178 typval_T *tv;
6179 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180{
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182
6183 if (ni == NULL)
6184 return FAIL;
6185 copy_tv(tv, &ni->li_tv);
6186 if (item == NULL)
6187 /* Append new item at end of list. */
6188 list_append(l, ni);
6189 else
6190 {
6191 /* Insert new item before existing item. */
6192 ni->li_prev = item->li_prev;
6193 ni->li_next = item;
6194 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006195 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197 ++l->lv_idx;
6198 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006202 l->lv_idx_item = NULL;
6203 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 }
6207 return OK;
6208}
6209
6210/*
6211 * Extend "l1" with "l2".
6212 * If "bef" is NULL append at the end, otherwise insert before this item.
6213 * Returns FAIL when out of memory.
6214 */
6215 static int
6216list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 list_T *l1;
6218 list_T *l2;
6219 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220{
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006222
6223 for (item = l2->lv_first; item != NULL; item = item->li_next)
6224 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6225 return FAIL;
6226 return OK;
6227}
6228
6229/*
6230 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6231 * Return FAIL when out of memory.
6232 */
6233 static int
6234list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 list_T *l1;
6236 list_T *l2;
6237 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006238{
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240
6241 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006242 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243 if (l == NULL)
6244 return FAIL;
6245 tv->v_type = VAR_LIST;
6246 tv->vval.v_list = l;
6247
6248 /* append all items from the second list */
6249 return list_extend(l, l2, NULL);
6250}
6251
6252/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006255 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 * Returns NULL when out of memory.
6257 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006259list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006262 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263{
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *copy;
6265 listitem_T *item;
6266 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267
6268 if (orig == NULL)
6269 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270
6271 copy = list_alloc();
6272 if (copy != NULL)
6273 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006274 if (copyID != 0)
6275 {
6276 /* Do this before adding the items, because one of the items may
6277 * refer back to this list. */
6278 orig->lv_copyID = copyID;
6279 orig->lv_copylist = copy;
6280 }
6281 for (item = orig->lv_first; item != NULL && !got_int;
6282 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 {
6284 ni = listitem_alloc();
6285 if (ni == NULL)
6286 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006288 {
6289 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6290 {
6291 vim_free(ni);
6292 break;
6293 }
6294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006296 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 list_append(copy, ni);
6298 }
6299 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006300 if (item != NULL)
6301 {
6302 list_unref(copy);
6303 copy = NULL;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
6306
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 return copy;
6308}
6309
6310/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006312 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006315list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 listitem_T *item;
6318 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006319{
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006322 /* notify watchers */
6323 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006325 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326 list_fix_watch(l, ip);
6327 if (ip == item2)
6328 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330
6331 if (item2->li_next == NULL)
6332 l->lv_last = item->li_prev;
6333 else
6334 item2->li_next->li_prev = item->li_prev;
6335 if (item->li_prev == NULL)
6336 l->lv_first = item2->li_next;
6337 else
6338 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006339 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340}
6341
6342/*
6343 * Return an allocated string with the string representation of a list.
6344 * May return NULL.
6345 */
6346 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006347list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006348 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006349 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350{
6351 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352
6353 if (tv->vval.v_list == NULL)
6354 return NULL;
6355 ga_init2(&ga, (int)sizeof(char), 80);
6356 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006357 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006358 {
6359 vim_free(ga.ga_data);
6360 return NULL;
6361 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 ga_append(&ga, ']');
6363 ga_append(&ga, NUL);
6364 return (char_u *)ga.ga_data;
6365}
6366
6367/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006368 * Join list "l" into a string in "*gap", using separator "sep".
6369 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006370 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006371 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006372 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006373list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006374 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006376 char_u *sep;
6377 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006378 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006379{
6380 int first = TRUE;
6381 char_u *tofree;
6382 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006384 char_u *s;
6385
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006386 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006387 {
6388 if (first)
6389 first = FALSE;
6390 else
6391 ga_concat(gap, sep);
6392
6393 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006394 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006395 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006396 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006397 if (s != NULL)
6398 ga_concat(gap, s);
6399 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 if (s == NULL)
6401 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006402 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006403 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006404}
6405
6406/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006407 * Garbage collection for lists and dictionaries.
6408 *
6409 * We use reference counts to be able to free most items right away when they
6410 * are no longer used. But for composite items it's possible that it becomes
6411 * unused while the reference count is > 0: When there is a recursive
6412 * reference. Example:
6413 * :let l = [1, 2, 3]
6414 * :let d = {9: l}
6415 * :let l[1] = d
6416 *
6417 * Since this is quite unusual we handle this with garbage collection: every
6418 * once in a while find out which lists and dicts are not referenced from any
6419 * variable.
6420 *
6421 * Here is a good reference text about garbage collection (refers to Python
6422 * but it applies to all reference-counting mechanisms):
6423 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006424 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006425
6426/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006427 * Do garbage collection for lists and dicts.
6428 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006429 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006430 int
6431garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006432{
6433 dict_T *dd;
6434 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006435 int copyID = ++current_copyID;
6436 buf_T *buf;
6437 win_T *wp;
6438 int i;
6439 funccall_T *fc;
6440 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006441#ifdef FEAT_WINDOWS
6442 tabpage_T *tp;
6443#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006444
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006445 /* Only do this once. */
6446 want_garbage_collect = FALSE;
6447 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006448 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006449
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006450 /*
6451 * 1. Go through all accessible variables and mark all lists and dicts
6452 * with copyID.
6453 */
6454 /* script-local variables */
6455 for (i = 1; i <= ga_scripts.ga_len; ++i)
6456 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6457
6458 /* buffer-local variables */
6459 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6460 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6461
6462 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006463 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006464 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6465
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466#ifdef FEAT_WINDOWS
6467 /* tabpage-local variables */
6468 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6469 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6470#endif
6471
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006472 /* global variables */
6473 set_ref_in_ht(&globvarht, copyID);
6474
6475 /* function-local variables */
6476 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6477 {
6478 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6479 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6480 }
6481
6482 /*
6483 * 2. Go through the list of dicts and free items without the copyID.
6484 */
6485 for (dd = first_dict; dd != NULL; )
6486 if (dd->dv_copyID != copyID)
6487 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006488 /* Free the Dictionary and ordinary items it contains, but don't
6489 * recurse into Lists and Dictionaries, they will be in the list
6490 * of dicts or list of lists. */
6491 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006492 did_free = TRUE;
6493
6494 /* restart, next dict may also have been freed */
6495 dd = first_dict;
6496 }
6497 else
6498 dd = dd->dv_used_next;
6499
6500 /*
6501 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006502 * But don't free a list that has a watcher (used in a for loop), these
6503 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006504 */
6505 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006506 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006507 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006508 /* Free the List and ordinary items it contains, but don't recurse
6509 * into Lists and Dictionaries, they will be in the list of dicts
6510 * or list of lists. */
6511 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006512 did_free = TRUE;
6513
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006514 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006515 ll = first_list;
6516 }
6517 else
6518 ll = ll->lv_used_next;
6519
6520 return did_free;
6521}
6522
6523/*
6524 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6525 */
6526 static void
6527set_ref_in_ht(ht, copyID)
6528 hashtab_T *ht;
6529 int copyID;
6530{
6531 int todo;
6532 hashitem_T *hi;
6533
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006534 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006535 for (hi = ht->ht_array; todo > 0; ++hi)
6536 if (!HASHITEM_EMPTY(hi))
6537 {
6538 --todo;
6539 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6540 }
6541}
6542
6543/*
6544 * Mark all lists and dicts referenced through list "l" with "copyID".
6545 */
6546 static void
6547set_ref_in_list(l, copyID)
6548 list_T *l;
6549 int copyID;
6550{
6551 listitem_T *li;
6552
6553 for (li = l->lv_first; li != NULL; li = li->li_next)
6554 set_ref_in_item(&li->li_tv, copyID);
6555}
6556
6557/*
6558 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6559 */
6560 static void
6561set_ref_in_item(tv, copyID)
6562 typval_T *tv;
6563 int copyID;
6564{
6565 dict_T *dd;
6566 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006567
6568 switch (tv->v_type)
6569 {
6570 case VAR_DICT:
6571 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006572 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006573 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006574 /* Didn't see this dict yet. */
6575 dd->dv_copyID = copyID;
6576 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006577 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006578 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006579
6580 case VAR_LIST:
6581 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006583 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006584 /* Didn't see this list yet. */
6585 ll->lv_copyID = copyID;
6586 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006587 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006588 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006589 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006590 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006591}
6592
6593/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 * Allocate an empty header for a dictionary.
6595 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006596 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597dict_alloc()
6598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006600
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006602 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006603 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006604 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006605 if (first_dict != NULL)
6606 first_dict->dv_used_prev = d;
6607 d->dv_used_next = first_dict;
6608 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006609 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006610
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006612 d->dv_lock = 0;
6613 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006615 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006616 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617}
6618
6619/*
6620 * Unreference a Dictionary: decrement the reference count and free it when it
6621 * becomes zero.
6622 */
6623 static void
6624dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006626{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006627 if (d != NULL && --d->dv_refcount <= 0)
6628 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629}
6630
6631/*
6632 * Free a Dictionary, including all items it contains.
6633 * Ignores the reference count.
6634 */
6635 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006636dict_free(d, recurse)
6637 dict_T *d;
6638 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006640 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006642 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006644 /* Remove the dict from the list of dicts for garbage collection. */
6645 if (d->dv_used_prev == NULL)
6646 first_dict = d->dv_used_next;
6647 else
6648 d->dv_used_prev->dv_used_next = d->dv_used_next;
6649 if (d->dv_used_next != NULL)
6650 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6651
6652 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006653 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006654 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006657 if (!HASHITEM_EMPTY(hi))
6658 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006659 /* Remove the item before deleting it, just in case there is
6660 * something recursive causing trouble. */
6661 di = HI2DI(hi);
6662 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006663 if (recurse || (di->di_tv.v_type != VAR_LIST
6664 && di->di_tv.v_type != VAR_DICT))
6665 clear_tv(&di->di_tv);
6666 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006667 --todo;
6668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 vim_free(d);
6672}
6673
6674/*
6675 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006676 * The "key" is copied to the new item.
6677 * Note that the value of the item "di_tv" still needs to be initialized!
6678 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006681dictitem_alloc(key)
6682 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683{
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006685
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006686 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006687 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006689 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006690 di->di_flags = 0;
6691 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006692 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006693}
6694
6695/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 * Make a copy of a Dictionary item.
6697 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006698 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006700 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006701{
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006704 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6705 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006706 if (di != NULL)
6707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006708 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006709 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 copy_tv(&org->di_tv, &di->di_tv);
6711 }
6712 return di;
6713}
6714
6715/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006716 * Remove item "item" from Dictionary "dict" and free it.
6717 */
6718 static void
6719dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 dict_T *dict;
6721 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006722{
Bram Moolenaar33570922005-01-25 22:26:29 +00006723 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006724
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006726 if (HASHITEM_EMPTY(hi))
6727 EMSG2(_(e_intern2), "dictitem_remove()");
6728 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006730 dictitem_free(item);
6731}
6732
6733/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006734 * Free a dict item. Also clears the value.
6735 */
6736 static void
6737dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006738 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740 clear_tv(&item->di_tv);
6741 vim_free(item);
6742}
6743
6744/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6746 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006747 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006748 * Returns NULL when out of memory.
6749 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006750 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006751dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006752 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006753 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006754 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755{
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 dict_T *copy;
6757 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006758 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006759 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760
6761 if (orig == NULL)
6762 return NULL;
6763
6764 copy = dict_alloc();
6765 if (copy != NULL)
6766 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 if (copyID != 0)
6768 {
6769 orig->dv_copyID = copyID;
6770 orig->dv_copydict = copy;
6771 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006772 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006773 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006776 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777 --todo;
6778
6779 di = dictitem_alloc(hi->hi_key);
6780 if (di == NULL)
6781 break;
6782 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 {
6784 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6785 copyID) == FAIL)
6786 {
6787 vim_free(di);
6788 break;
6789 }
6790 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006791 else
6792 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6793 if (dict_add(copy, di) == FAIL)
6794 {
6795 dictitem_free(di);
6796 break;
6797 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006799 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006800
Bram Moolenaare9a41262005-01-15 22:18:47 +00006801 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006802 if (todo > 0)
6803 {
6804 dict_unref(copy);
6805 copy = NULL;
6806 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006807 }
6808
6809 return copy;
6810}
6811
6812/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006814 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006816 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006818 dict_T *d;
6819 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820{
Bram Moolenaar33570922005-01-25 22:26:29 +00006821 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822}
6823
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006825 * Add a number or string entry to dictionary "d".
6826 * When "str" is NULL use number "nr", otherwise use "str".
6827 * Returns FAIL when out of memory and when key already exists.
6828 */
6829 int
6830dict_add_nr_str(d, key, nr, str)
6831 dict_T *d;
6832 char *key;
6833 long nr;
6834 char_u *str;
6835{
6836 dictitem_T *item;
6837
6838 item = dictitem_alloc((char_u *)key);
6839 if (item == NULL)
6840 return FAIL;
6841 item->di_tv.v_lock = 0;
6842 if (str == NULL)
6843 {
6844 item->di_tv.v_type = VAR_NUMBER;
6845 item->di_tv.vval.v_number = nr;
6846 }
6847 else
6848 {
6849 item->di_tv.v_type = VAR_STRING;
6850 item->di_tv.vval.v_string = vim_strsave(str);
6851 }
6852 if (dict_add(d, item) == FAIL)
6853 {
6854 dictitem_free(item);
6855 return FAIL;
6856 }
6857 return OK;
6858}
6859
6860/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006861 * Get the number of items in a Dictionary.
6862 */
6863 static long
6864dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006865 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006866{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006867 if (d == NULL)
6868 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006869 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870}
6871
6872/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873 * Find item "key[len]" in Dictionary "d".
6874 * If "len" is negative use strlen(key).
6875 * Returns NULL when not found.
6876 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880 char_u *key;
6881 int len;
6882{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006883#define AKEYLEN 200
6884 char_u buf[AKEYLEN];
6885 char_u *akey;
6886 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006888
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006889 if (len < 0)
6890 akey = key;
6891 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006892 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893 tofree = akey = vim_strnsave(key, len);
6894 if (akey == NULL)
6895 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006896 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006897 else
6898 {
6899 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006900 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006901 akey = buf;
6902 }
6903
Bram Moolenaar33570922005-01-25 22:26:29 +00006904 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 vim_free(tofree);
6906 if (HASHITEM_EMPTY(hi))
6907 return NULL;
6908 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909}
6910
6911/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006912 * Get a string item from a dictionary.
6913 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006914 * Returns NULL if the entry doesn't exist or out of memory.
6915 */
6916 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006917get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006918 dict_T *d;
6919 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006920 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006921{
6922 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006923 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006924
6925 di = dict_find(d, key, -1);
6926 if (di == NULL)
6927 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006928 s = get_tv_string(&di->di_tv);
6929 if (save && s != NULL)
6930 s = vim_strsave(s);
6931 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006932}
6933
6934/*
6935 * Get a number item from a dictionary.
6936 * Returns 0 if the entry doesn't exist or out of memory.
6937 */
6938 long
6939get_dict_number(d, key)
6940 dict_T *d;
6941 char_u *key;
6942{
6943 dictitem_T *di;
6944
6945 di = dict_find(d, key, -1);
6946 if (di == NULL)
6947 return 0;
6948 return get_tv_number(&di->di_tv);
6949}
6950
6951/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006952 * Return an allocated string with the string representation of a Dictionary.
6953 * May return NULL.
6954 */
6955 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006956dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006957 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006958 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006959{
6960 garray_T ga;
6961 int first = TRUE;
6962 char_u *tofree;
6963 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006965 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006969 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 return NULL;
6971 ga_init2(&ga, (int)sizeof(char), 80);
6972 ga_append(&ga, '{');
6973
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006974 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006975 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006977 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006979 --todo;
6980
6981 if (first)
6982 first = FALSE;
6983 else
6984 ga_concat(&ga, (char_u *)", ");
6985
6986 tofree = string_quote(hi->hi_key, FALSE);
6987 if (tofree != NULL)
6988 {
6989 ga_concat(&ga, tofree);
6990 vim_free(tofree);
6991 }
6992 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006993 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006994 if (s != NULL)
6995 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006996 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006997 if (s == NULL)
6998 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007000 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007001 if (todo > 0)
7002 {
7003 vim_free(ga.ga_data);
7004 return NULL;
7005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006
7007 ga_append(&ga, '}');
7008 ga_append(&ga, NUL);
7009 return (char_u *)ga.ga_data;
7010}
7011
7012/*
7013 * Allocate a variable for a Dictionary and fill it from "*arg".
7014 * Return OK or FAIL. Returns NOTDONE for {expr}.
7015 */
7016 static int
7017get_dict_tv(arg, rettv, evaluate)
7018 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007019 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020 int evaluate;
7021{
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 dict_T *d = NULL;
7023 typval_T tvkey;
7024 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007025 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007028 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007029
7030 /*
7031 * First check if it's not a curly-braces thing: {expr}.
7032 * Must do this without evaluating, otherwise a function may be called
7033 * twice. Unfortunately this means we need to call eval1() twice for the
7034 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007037 if (*start != '}')
7038 {
7039 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7040 return FAIL;
7041 if (*start == '}')
7042 return NOTDONE;
7043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007044
7045 if (evaluate)
7046 {
7047 d = dict_alloc();
7048 if (d == NULL)
7049 return FAIL;
7050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 tvkey.v_type = VAR_UNKNOWN;
7052 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053
7054 *arg = skipwhite(*arg + 1);
7055 while (**arg != '}' && **arg != NUL)
7056 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007057 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058 goto failret;
7059 if (**arg != ':')
7060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007061 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063 goto failret;
7064 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007065 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007067 key = get_tv_string_buf_chk(&tvkey, buf);
7068 if (key == NULL || *key == NUL)
7069 {
7070 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7071 if (key != NULL)
7072 EMSG(_(e_emptykey));
7073 clear_tv(&tvkey);
7074 goto failret;
7075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007077
7078 *arg = skipwhite(*arg + 1);
7079 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7080 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007081 if (evaluate)
7082 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 goto failret;
7084 }
7085 if (evaluate)
7086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007087 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 if (item != NULL)
7089 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007090 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 clear_tv(&tv);
7093 goto failret;
7094 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007095 item = dictitem_alloc(key);
7096 clear_tv(&tvkey);
7097 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007100 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 if (dict_add(d, item) == FAIL)
7102 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 }
7104 }
7105
7106 if (**arg == '}')
7107 break;
7108 if (**arg != ',')
7109 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007110 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111 goto failret;
7112 }
7113 *arg = skipwhite(*arg + 1);
7114 }
7115
7116 if (**arg != '}')
7117 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007118 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119failret:
7120 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007121 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122 return FAIL;
7123 }
7124
7125 *arg = skipwhite(*arg + 1);
7126 if (evaluate)
7127 {
7128 rettv->v_type = VAR_DICT;
7129 rettv->vval.v_dict = d;
7130 ++d->dv_refcount;
7131 }
7132
7133 return OK;
7134}
7135
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007137 * Return a string with the string representation of a variable.
7138 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007139 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007140 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007141 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007142 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007143 */
7144 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007145echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007147 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007148 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007149 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007150{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151 static int recurse = 0;
7152 char_u *r = NULL;
7153
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007156 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 *tofree = NULL;
7158 return NULL;
7159 }
7160 ++recurse;
7161
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007162 switch (tv->v_type)
7163 {
7164 case VAR_FUNC:
7165 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166 r = tv->vval.v_string;
7167 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007168
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007170 if (tv->vval.v_list == NULL)
7171 {
7172 *tofree = NULL;
7173 r = NULL;
7174 }
7175 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7176 {
7177 *tofree = NULL;
7178 r = (char_u *)"[...]";
7179 }
7180 else
7181 {
7182 tv->vval.v_list->lv_copyID = copyID;
7183 *tofree = list2string(tv, copyID);
7184 r = *tofree;
7185 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007187
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007189 if (tv->vval.v_dict == NULL)
7190 {
7191 *tofree = NULL;
7192 r = NULL;
7193 }
7194 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7195 {
7196 *tofree = NULL;
7197 r = (char_u *)"{...}";
7198 }
7199 else
7200 {
7201 tv->vval.v_dict->dv_copyID = copyID;
7202 *tofree = dict2string(tv, copyID);
7203 r = *tofree;
7204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007207 case VAR_STRING:
7208 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 *tofree = NULL;
7210 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007211 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007213#ifdef FEAT_FLOAT
7214 case VAR_FLOAT:
7215 *tofree = NULL;
7216 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7217 r = numbuf;
7218 break;
7219#endif
7220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007222 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007224 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225
7226 --recurse;
7227 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007228}
7229
7230/*
7231 * Return a string with the string representation of a variable.
7232 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7233 * "numbuf" is used for a number.
7234 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007235 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007236 */
7237 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007238tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007240 char_u **tofree;
7241 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007242 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007243{
7244 switch (tv->v_type)
7245 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007246 case VAR_FUNC:
7247 *tofree = string_quote(tv->vval.v_string, TRUE);
7248 return *tofree;
7249 case VAR_STRING:
7250 *tofree = string_quote(tv->vval.v_string, FALSE);
7251 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007252#ifdef FEAT_FLOAT
7253 case VAR_FLOAT:
7254 *tofree = NULL;
7255 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7256 return numbuf;
7257#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007259 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007261 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007262 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007263 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007264 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007265 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007266}
7267
7268/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 * Return string "str" in ' quotes, doubling ' characters.
7270 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007272 */
7273 static char_u *
7274string_quote(str, function)
7275 char_u *str;
7276 int function;
7277{
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007279 char_u *p, *r, *s;
7280
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 len = (function ? 13 : 3);
7282 if (str != NULL)
7283 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007284 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 for (p = str; *p != NUL; mb_ptr_adv(p))
7286 if (*p == '\'')
7287 ++len;
7288 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007289 s = r = alloc(len);
7290 if (r != NULL)
7291 {
7292 if (function)
7293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007295 r += 10;
7296 }
7297 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 if (str != NULL)
7300 for (p = str; *p != NUL; )
7301 {
7302 if (*p == '\'')
7303 *r++ = '\'';
7304 MB_COPY_CHAR(p, r);
7305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007307 if (function)
7308 *r++ = ')';
7309 *r++ = NUL;
7310 }
7311 return s;
7312}
7313
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007314#ifdef FEAT_FLOAT
7315/*
7316 * Convert the string "text" to a floating point number.
7317 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7318 * this always uses a decimal point.
7319 * Returns the length of the text that was consumed.
7320 */
7321 static int
7322string2float(text, value)
7323 char_u *text;
7324 float_T *value; /* result stored here */
7325{
7326 char *s = (char *)text;
7327 float_T f;
7328
7329 f = strtod(s, &s);
7330 *value = f;
7331 return (int)((char_u *)s - text);
7332}
7333#endif
7334
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 * Get the value of an environment variable.
7337 * "arg" is pointing to the '$'. It is advanced to after the name.
7338 * If the environment variable was not set, silently assume it is empty.
7339 * Always return OK.
7340 */
7341 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007342get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 int evaluate;
7346{
7347 char_u *string = NULL;
7348 int len;
7349 int cc;
7350 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007351 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
7353 ++*arg;
7354 name = *arg;
7355 len = get_env_len(arg);
7356 if (evaluate)
7357 {
7358 if (len != 0)
7359 {
7360 cc = name[len];
7361 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007362 /* first try vim_getenv(), fast for normal environment vars */
7363 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007365 {
7366 if (!mustfree)
7367 string = vim_strsave(string);
7368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 else
7370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007371 if (mustfree)
7372 vim_free(string);
7373
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 /* next try expanding things like $VIM and ${HOME} */
7375 string = expand_env_save(name - 1);
7376 if (string != NULL && *string == '$')
7377 {
7378 vim_free(string);
7379 string = NULL;
7380 }
7381 }
7382 name[len] = cc;
7383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007384 rettv->v_type = VAR_STRING;
7385 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 }
7387
7388 return OK;
7389}
7390
7391/*
7392 * Array with names and number of arguments of all internal functions
7393 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7394 */
7395static struct fst
7396{
7397 char *f_name; /* function name */
7398 char f_min_argc; /* minimal number of arguments */
7399 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007401 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402} functions[] =
7403{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007404#ifdef FEAT_FLOAT
7405 {"abs", 1, 1, f_abs},
7406#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007407 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {"append", 2, 2, f_append},
7409 {"argc", 0, 0, f_argc},
7410 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007411 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007412#ifdef FEAT_FLOAT
7413 {"atan", 1, 1, f_atan},
7414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007416 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {"bufexists", 1, 1, f_bufexists},
7418 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7419 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7420 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7421 {"buflisted", 1, 1, f_buflisted},
7422 {"bufloaded", 1, 1, f_bufloaded},
7423 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007424 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 {"bufwinnr", 1, 1, f_bufwinnr},
7426 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007427 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007429#ifdef FEAT_FLOAT
7430 {"ceil", 1, 1, f_ceil},
7431#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007432 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {"char2nr", 1, 1, f_char2nr},
7434 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007435 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007437#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007438 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007439 {"complete_add", 1, 1, f_complete_add},
7440 {"complete_check", 0, 0, f_complete_check},
7441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007443 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007444#ifdef FEAT_FLOAT
7445 {"cos", 1, 1, f_cos},
7446#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007449 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007450 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {"delete", 1, 1, f_delete},
7452 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007453 {"diff_filler", 1, 1, f_diff_filler},
7454 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007455 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007457 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 {"eventhandler", 0, 0, f_eventhandler},
7459 {"executable", 1, 1, f_executable},
7460 {"exists", 1, 1, f_exists},
7461 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007462 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007463 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7465 {"filereadable", 1, 1, f_filereadable},
7466 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007467 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007468 {"finddir", 1, 3, f_finddir},
7469 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007470#ifdef FEAT_FLOAT
7471 {"float2nr", 1, 1, f_float2nr},
7472 {"floor", 1, 1, f_floor},
7473#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007474 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {"fnamemodify", 2, 2, f_fnamemodify},
7476 {"foldclosed", 1, 1, f_foldclosed},
7477 {"foldclosedend", 1, 1, f_foldclosedend},
7478 {"foldlevel", 1, 1, f_foldlevel},
7479 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007480 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007483 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007484 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007485 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {"getbufvar", 2, 2, f_getbufvar},
7487 {"getchar", 0, 1, f_getchar},
7488 {"getcharmod", 0, 0, f_getcharmod},
7489 {"getcmdline", 0, 0, f_getcmdline},
7490 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007491 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007493 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007494 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {"getfsize", 1, 1, f_getfsize},
7496 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007497 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007498 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007499 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007500 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007501 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007502 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007503 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007504 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007506 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {"getwinposx", 0, 0, f_getwinposx},
7508 {"getwinposy", 0, 0, f_getwinposy},
7509 {"getwinvar", 2, 2, f_getwinvar},
7510 {"glob", 1, 1, f_glob},
7511 {"globpath", 2, 2, f_globpath},
7512 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007513 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007514 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007515 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7517 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7518 {"histadd", 2, 2, f_histadd},
7519 {"histdel", 1, 2, f_histdel},
7520 {"histget", 1, 2, f_histget},
7521 {"histnr", 1, 1, f_histnr},
7522 {"hlID", 1, 1, f_hlID},
7523 {"hlexists", 1, 1, f_hlexists},
7524 {"hostname", 0, 0, f_hostname},
7525 {"iconv", 3, 3, f_iconv},
7526 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007527 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007528 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007530 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {"inputrestore", 0, 0, f_inputrestore},
7532 {"inputsave", 0, 0, f_inputsave},
7533 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007534 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007536 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007541 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {"libcall", 3, 3, f_libcall},
7543 {"libcallnr", 3, 3, f_libcallnr},
7544 {"line", 1, 1, f_line},
7545 {"line2byte", 1, 1, f_line2byte},
7546 {"lispindent", 1, 1, f_lispindent},
7547 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007548#ifdef FEAT_FLOAT
7549 {"log10", 1, 1, f_log10},
7550#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007551 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007552 {"maparg", 1, 3, f_maparg},
7553 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007554 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007555 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007556 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007557 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007558 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007559 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007560 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007561 {"max", 1, 1, f_max},
7562 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007563#ifdef vim_mkdir
7564 {"mkdir", 1, 3, f_mkdir},
7565#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007566 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {"nextnonblank", 1, 1, f_nextnonblank},
7568 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007569 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570#ifdef FEAT_FLOAT
7571 {"pow", 2, 2, f_pow},
7572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007574 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007575 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007577 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007578 {"reltime", 0, 2, f_reltime},
7579 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 {"remote_expr", 2, 3, f_remote_expr},
7581 {"remote_foreground", 1, 1, f_remote_foreground},
7582 {"remote_peek", 1, 2, f_remote_peek},
7583 {"remote_read", 1, 1, f_remote_read},
7584 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007585 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007587 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007589 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007590#ifdef FEAT_FLOAT
7591 {"round", 1, 1, f_round},
7592#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007593 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007594 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007595 {"searchpair", 3, 7, f_searchpair},
7596 {"searchpairpos", 3, 7, f_searchpairpos},
7597 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {"server2client", 2, 2, f_server2client},
7599 {"serverlist", 0, 0, f_serverlist},
7600 {"setbufvar", 3, 3, f_setbufvar},
7601 {"setcmdpos", 1, 1, f_setcmdpos},
7602 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007603 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007604 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007605 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007606 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007608 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007610 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007612#ifdef FEAT_FLOAT
7613 {"sin", 1, 1, f_sin},
7614#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007615 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007616 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007617 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007618 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007619 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007620#ifdef FEAT_FLOAT
7621 {"sqrt", 1, 1, f_sqrt},
7622 {"str2float", 1, 1, f_str2float},
7623#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007624 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625#ifdef HAVE_STRFTIME
7626 {"strftime", 1, 2, f_strftime},
7627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007629 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {"strlen", 1, 1, f_strlen},
7631 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007632 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {"strtrans", 1, 1, f_strtrans},
7634 {"submatch", 1, 1, f_submatch},
7635 {"substitute", 4, 4, f_substitute},
7636 {"synID", 3, 3, f_synID},
7637 {"synIDattr", 2, 3, f_synIDattr},
7638 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007639 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007640 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007641 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007642 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007643 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007644 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007645 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007647 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {"tolower", 1, 1, f_tolower},
7649 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007650 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651#ifdef FEAT_FLOAT
7652 {"trunc", 1, 1, f_trunc},
7653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {"virtcol", 1, 1, f_virtcol},
7657 {"visualmode", 0, 1, f_visualmode},
7658 {"winbufnr", 1, 1, f_winbufnr},
7659 {"wincol", 0, 0, f_wincol},
7660 {"winheight", 1, 1, f_winheight},
7661 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007662 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007664 {"winrestview", 1, 1, f_winrestview},
7665 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007667 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668};
7669
7670#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7671
7672/*
7673 * Function given to ExpandGeneric() to obtain the list of internal
7674 * or user defined function names.
7675 */
7676 char_u *
7677get_function_name(xp, idx)
7678 expand_T *xp;
7679 int idx;
7680{
7681 static int intidx = -1;
7682 char_u *name;
7683
7684 if (idx == 0)
7685 intidx = -1;
7686 if (intidx < 0)
7687 {
7688 name = get_user_func_name(xp, idx);
7689 if (name != NULL)
7690 return name;
7691 }
7692 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7693 {
7694 STRCPY(IObuff, functions[intidx].f_name);
7695 STRCAT(IObuff, "(");
7696 if (functions[intidx].f_max_argc == 0)
7697 STRCAT(IObuff, ")");
7698 return IObuff;
7699 }
7700
7701 return NULL;
7702}
7703
7704/*
7705 * Function given to ExpandGeneric() to obtain the list of internal or
7706 * user defined variable or function names.
7707 */
7708/*ARGSUSED*/
7709 char_u *
7710get_expr_name(xp, idx)
7711 expand_T *xp;
7712 int idx;
7713{
7714 static int intidx = -1;
7715 char_u *name;
7716
7717 if (idx == 0)
7718 intidx = -1;
7719 if (intidx < 0)
7720 {
7721 name = get_function_name(xp, idx);
7722 if (name != NULL)
7723 return name;
7724 }
7725 return get_user_var_name(xp, ++intidx);
7726}
7727
7728#endif /* FEAT_CMDL_COMPL */
7729
7730/*
7731 * Find internal function in table above.
7732 * Return index, or -1 if not found
7733 */
7734 static int
7735find_internal_func(name)
7736 char_u *name; /* name of the function */
7737{
7738 int first = 0;
7739 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7740 int cmp;
7741 int x;
7742
7743 /*
7744 * Find the function name in the table. Binary search.
7745 */
7746 while (first <= last)
7747 {
7748 x = first + ((unsigned)(last - first) >> 1);
7749 cmp = STRCMP(name, functions[x].f_name);
7750 if (cmp < 0)
7751 last = x - 1;
7752 else if (cmp > 0)
7753 first = x + 1;
7754 else
7755 return x;
7756 }
7757 return -1;
7758}
7759
7760/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7762 * name it contains, otherwise return "name".
7763 */
7764 static char_u *
7765deref_func_name(name, lenp)
7766 char_u *name;
7767 int *lenp;
7768{
Bram Moolenaar33570922005-01-25 22:26:29 +00007769 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007770 int cc;
7771
7772 cc = name[*lenp];
7773 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007774 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 {
7780 *lenp = 0;
7781 return (char_u *)""; /* just in case */
7782 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007783 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007785 }
7786
7787 return name;
7788}
7789
7790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 * Allocate a variable for the result of a function.
7792 * Return OK or FAIL.
7793 */
7794 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007795get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7796 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 char_u *name; /* name of the function */
7798 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 char_u **arg; /* argument, pointing to the '(' */
7801 linenr_T firstline; /* first line of range */
7802 linenr_T lastline; /* last line of range */
7803 int *doesrange; /* return: function handled range */
7804 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807 char_u *argp;
7808 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007809 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 int argcount = 0; /* number of arguments found */
7811
7812 /*
7813 * Get the arguments.
7814 */
7815 argp = *arg;
7816 while (argcount < MAX_FUNC_ARGS)
7817 {
7818 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7819 if (*argp == ')' || *argp == ',' || *argp == NUL)
7820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7822 {
7823 ret = FAIL;
7824 break;
7825 }
7826 ++argcount;
7827 if (*argp != ',')
7828 break;
7829 }
7830 if (*argp == ')')
7831 ++argp;
7832 else
7833 ret = FAIL;
7834
7835 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 {
7840 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007841 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007843 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845
7846 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
7849 *arg = skipwhite(argp);
7850 return ret;
7851}
7852
7853
7854/*
7855 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007856 * Return OK when the function can't be called, FAIL otherwise.
7857 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 */
7859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 char_u *name; /* name of the function */
7863 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007866 typval_T *argvars; /* vars for arguments, must have "argcount"
7867 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 linenr_T firstline; /* first line of range */
7869 linenr_T lastline; /* last line of range */
7870 int *doesrange; /* return: function handled range */
7871 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875#define ERROR_UNKNOWN 0
7876#define ERROR_TOOMANY 1
7877#define ERROR_TOOFEW 2
7878#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879#define ERROR_DICT 4
7880#define ERROR_NONE 5
7881#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 int error = ERROR_NONE;
7883 int i;
7884 int llen;
7885 ufunc_T *fp;
7886 int cc;
7887#define FLEN_FIXED 40
7888 char_u fname_buf[FLEN_FIXED + 1];
7889 char_u *fname;
7890
7891 /*
7892 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7893 * Change <SNR>123_name() to K_SNR 123_name().
7894 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7895 */
7896 cc = name[len];
7897 name[len] = NUL;
7898 llen = eval_fname_script(name);
7899 if (llen > 0)
7900 {
7901 fname_buf[0] = K_SPECIAL;
7902 fname_buf[1] = KS_EXTRA;
7903 fname_buf[2] = (int)KE_SNR;
7904 i = 3;
7905 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7906 {
7907 if (current_SID <= 0)
7908 error = ERROR_SCRIPT;
7909 else
7910 {
7911 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7912 i = (int)STRLEN(fname_buf);
7913 }
7914 }
7915 if (i + STRLEN(name + llen) < FLEN_FIXED)
7916 {
7917 STRCPY(fname_buf + i, name + llen);
7918 fname = fname_buf;
7919 }
7920 else
7921 {
7922 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7923 if (fname == NULL)
7924 error = ERROR_OTHER;
7925 else
7926 {
7927 mch_memmove(fname, fname_buf, (size_t)i);
7928 STRCPY(fname + i, name + llen);
7929 }
7930 }
7931 }
7932 else
7933 fname = name;
7934
7935 *doesrange = FALSE;
7936
7937
7938 /* execute the function if no errors detected and executing */
7939 if (evaluate && error == ERROR_NONE)
7940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 error = ERROR_UNKNOWN;
7943
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007944 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
7946 /*
7947 * User defined function.
7948 */
7949 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007950
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007952 /* Trigger FuncUndefined event, may load the function. */
7953 if (fp == NULL
7954 && apply_autocmds(EVENT_FUNCUNDEFINED,
7955 fname, fname, TRUE, NULL)
7956 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007958 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 fp = find_func(fname);
7960 }
7961#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007962 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007963 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007964 {
7965 /* loaded a package, search for the function again */
7966 fp = find_func(fname);
7967 }
7968
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (fp != NULL)
7970 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007971 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007973 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007975 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007977 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 else
7980 {
7981 /*
7982 * Call the user function.
7983 * Save and restore search patterns, script variables and
7984 * redo buffer.
7985 */
7986 save_search_patterns();
7987 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007988 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007989 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007991 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7992 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7993 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007994 /* Function was unreferenced while being used, free it
7995 * now. */
7996 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 restoreRedobuff();
7998 restore_search_patterns();
7999 error = ERROR_NONE;
8000 }
8001 }
8002 }
8003 else
8004 {
8005 /*
8006 * Find the function name in the table, call its implementation.
8007 */
8008 i = find_internal_func(fname);
8009 if (i >= 0)
8010 {
8011 if (argcount < functions[i].f_min_argc)
8012 error = ERROR_TOOFEW;
8013 else if (argcount > functions[i].f_max_argc)
8014 error = ERROR_TOOMANY;
8015 else
8016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 error = ERROR_NONE;
8020 }
8021 }
8022 }
8023 /*
8024 * The function call (or "FuncUndefined" autocommand sequence) might
8025 * have been aborted by an error, an interrupt, or an explicitly thrown
8026 * exception that has not been caught so far. This situation can be
8027 * tested for by calling aborting(). For an error in an internal
8028 * function or for the "E132" error in call_user_func(), however, the
8029 * throw point at which the "force_abort" flag (temporarily reset by
8030 * emsg()) is normally updated has not been reached yet. We need to
8031 * update that flag first to make aborting() reliable.
8032 */
8033 update_force_abort();
8034 }
8035 if (error == ERROR_NONE)
8036 ret = OK;
8037
8038 /*
8039 * Report an error unless the argument evaluation or function call has been
8040 * cancelled due to an aborting error, an interrupt, or an exception.
8041 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042 if (!aborting())
8043 {
8044 switch (error)
8045 {
8046 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008047 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008048 break;
8049 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008050 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008051 break;
8052 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008053 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 name);
8055 break;
8056 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008057 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008058 name);
8059 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008060 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008061 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 name);
8063 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008064 }
8065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 name[len] = cc;
8068 if (fname != name && fname != fname_buf)
8069 vim_free(fname);
8070
8071 return ret;
8072}
8073
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008074/*
8075 * Give an error message with a function name. Handle <SNR> things.
8076 */
8077 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008078emsg_funcname(ermsg, name)
8079 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008080 char_u *name;
8081{
8082 char_u *p;
8083
8084 if (*name == K_SPECIAL)
8085 p = concat_str((char_u *)"<SNR>", name + 3);
8086 else
8087 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008088 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008089 if (p != name)
8090 vim_free(p);
8091}
8092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093/*********************************************
8094 * Implementation of the built-in functions
8095 */
8096
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097#ifdef FEAT_FLOAT
8098/*
8099 * "abs(expr)" function
8100 */
8101 static void
8102f_abs(argvars, rettv)
8103 typval_T *argvars;
8104 typval_T *rettv;
8105{
8106 if (argvars[0].v_type == VAR_FLOAT)
8107 {
8108 rettv->v_type = VAR_FLOAT;
8109 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8110 }
8111 else
8112 {
8113 varnumber_T n;
8114 int error = FALSE;
8115
8116 n = get_tv_number_chk(&argvars[0], &error);
8117 if (error)
8118 rettv->vval.v_number = -1;
8119 else if (n > 0)
8120 rettv->vval.v_number = n;
8121 else
8122 rettv->vval.v_number = -n;
8123 }
8124}
8125#endif
8126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008128 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 */
8130 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008131f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 typval_T *argvars;
8133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008140 if ((l = argvars[0].vval.v_list) != NULL
8141 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8142 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008143 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144 }
8145 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008146 EMSG(_(e_listreq));
8147}
8148
8149/*
8150 * "append(lnum, string/list)" function
8151 */
8152 static void
8153f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008154 typval_T *argvars;
8155 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008156{
8157 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008158 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 list_T *l = NULL;
8160 listitem_T *li = NULL;
8161 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008162 long added = 0;
8163
Bram Moolenaar0d660222005-01-07 21:51:51 +00008164 lnum = get_tv_lnum(argvars);
8165 if (lnum >= 0
8166 && lnum <= curbuf->b_ml.ml_line_count
8167 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008168 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008171 l = argvars[1].vval.v_list;
8172 if (l == NULL)
8173 return;
8174 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008177 for (;;)
8178 {
8179 if (l == NULL)
8180 tv = &argvars[1]; /* append a string */
8181 else if (li == NULL)
8182 break; /* end of list */
8183 else
8184 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008185 line = get_tv_string_chk(tv);
8186 if (line == NULL) /* type error */
8187 {
8188 rettv->vval.v_number = 1; /* Failed */
8189 break;
8190 }
8191 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008192 ++added;
8193 if (l == NULL)
8194 break;
8195 li = li->li_next;
8196 }
8197
8198 appended_lines_mark(lnum, added);
8199 if (curwin->w_cursor.lnum > lnum)
8200 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008202 else
8203 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204}
8205
8206/*
8207 * "argc()" function
8208 */
8209/* ARGSUSED */
8210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008211f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008212 typval_T *argvars;
8213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216}
8217
8218/*
8219 * "argidx()" function
8220 */
8221/* ARGSUSED */
8222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008223f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 typval_T *argvars;
8225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230/*
8231 * "argv(nr)" function
8232 */
8233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *argvars;
8236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238 int idx;
8239
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008240 if (argvars[0].v_type != VAR_UNKNOWN)
8241 {
8242 idx = get_tv_number_chk(&argvars[0], NULL);
8243 if (idx >= 0 && idx < ARGCOUNT)
8244 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8245 else
8246 rettv->vval.v_string = NULL;
8247 rettv->v_type = VAR_STRING;
8248 }
8249 else if (rettv_list_alloc(rettv) == OK)
8250 for (idx = 0; idx < ARGCOUNT; ++idx)
8251 list_append_string(rettv->vval.v_list,
8252 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253}
8254
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255#ifdef FEAT_FLOAT
8256static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8257
8258/*
8259 * Get the float value of "argvars[0]" into "f".
8260 * Returns FAIL when the argument is not a Number or Float.
8261 */
8262 static int
8263get_float_arg(argvars, f)
8264 typval_T *argvars;
8265 float_T *f;
8266{
8267 if (argvars[0].v_type == VAR_FLOAT)
8268 {
8269 *f = argvars[0].vval.v_float;
8270 return OK;
8271 }
8272 if (argvars[0].v_type == VAR_NUMBER)
8273 {
8274 *f = (float_T)argvars[0].vval.v_number;
8275 return OK;
8276 }
8277 EMSG(_("E808: Number or Float required"));
8278 return FAIL;
8279}
8280
8281/*
8282 * "atan()" function
8283 */
8284 static void
8285f_atan(argvars, rettv)
8286 typval_T *argvars;
8287 typval_T *rettv;
8288{
8289 float_T f;
8290
8291 rettv->v_type = VAR_FLOAT;
8292 if (get_float_arg(argvars, &f) == OK)
8293 rettv->vval.v_float = atan(f);
8294 else
8295 rettv->vval.v_float = 0.0;
8296}
8297#endif
8298
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299/*
8300 * "browse(save, title, initdir, default)" function
8301 */
8302/* ARGSUSED */
8303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 typval_T *argvars;
8306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307{
8308#ifdef FEAT_BROWSE
8309 int save;
8310 char_u *title;
8311 char_u *initdir;
8312 char_u *defname;
8313 char_u buf[NUMBUFLEN];
8314 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008315 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008317 save = get_tv_number_chk(&argvars[0], &error);
8318 title = get_tv_string_chk(&argvars[1]);
8319 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8320 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 if (error || title == NULL || initdir == NULL || defname == NULL)
8323 rettv->vval.v_string = NULL;
8324 else
8325 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008326 do_browse(save ? BROWSE_SAVE : 0,
8327 title, defname, NULL, initdir, NULL, curbuf);
8328#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008329 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008331 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008332}
8333
8334/*
8335 * "browsedir(title, initdir)" function
8336 */
8337/* ARGSUSED */
8338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 typval_T *argvars;
8341 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008342{
8343#ifdef FEAT_BROWSE
8344 char_u *title;
8345 char_u *initdir;
8346 char_u buf[NUMBUFLEN];
8347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008348 title = get_tv_string_chk(&argvars[0]);
8349 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008351 if (title == NULL || initdir == NULL)
8352 rettv->vval.v_string = NULL;
8353 else
8354 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008355 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360}
8361
Bram Moolenaar33570922005-01-25 22:26:29 +00008362static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008363
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364/*
8365 * Find a buffer by number or exact name.
8366 */
8367 static buf_T *
8368find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008373 if (avar->v_type == VAR_NUMBER)
8374 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008375 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008377 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008378 if (buf == NULL)
8379 {
8380 /* No full path name match, try a match with a URL or a "nofile"
8381 * buffer, these don't use the full path. */
8382 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8383 if (buf->b_fname != NULL
8384 && (path_with_url(buf->b_fname)
8385#ifdef FEAT_QUICKFIX
8386 || bt_nofile(buf)
8387#endif
8388 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008389 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008390 break;
8391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 }
8393 return buf;
8394}
8395
8396/*
8397 * "bufexists(expr)" function
8398 */
8399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008401 typval_T *argvars;
8402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405}
8406
8407/*
8408 * "buflisted(expr)" function
8409 */
8410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *argvars;
8413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
8415 buf_T *buf;
8416
8417 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419}
8420
8421/*
8422 * "bufloaded(expr)" function
8423 */
8424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 typval_T *argvars;
8427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 buf_T *buf;
8430
8431 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008432 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433}
8434
Bram Moolenaar33570922005-01-25 22:26:29 +00008435static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008436
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437/*
8438 * Get buffer by number or pattern.
8439 */
8440 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008441get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008444 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 int save_magic;
8446 char_u *save_cpo;
8447 buf_T *buf;
8448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449 if (tv->v_type == VAR_NUMBER)
8450 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008451 if (tv->v_type != VAR_STRING)
8452 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (name == NULL || *name == NUL)
8454 return curbuf;
8455 if (name[0] == '$' && name[1] == NUL)
8456 return lastbuf;
8457
8458 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8459 save_magic = p_magic;
8460 p_magic = TRUE;
8461 save_cpo = p_cpo;
8462 p_cpo = (char_u *)"";
8463
8464 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8465 TRUE, FALSE));
8466
8467 p_magic = save_magic;
8468 p_cpo = save_cpo;
8469
8470 /* If not found, try expanding the name, like done for bufexists(). */
8471 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008472 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473
8474 return buf;
8475}
8476
8477/*
8478 * "bufname(expr)" function
8479 */
8480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 buf_T *buf;
8486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008489 buf = get_buf_tv(&argvars[0]);
8490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 --emsg_off;
8496}
8497
8498/*
8499 * "bufnr(expr)" function
8500 */
8501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 typval_T *argvars;
8504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008507 int error = FALSE;
8508 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 --emsg_off;
8514
8515 /* If the buffer isn't found and the second argument is not zero create a
8516 * new buffer. */
8517 if (buf == NULL
8518 && argvars[1].v_type != VAR_UNKNOWN
8519 && get_tv_number_chk(&argvars[1], &error) != 0
8520 && !error
8521 && (name = get_tv_string_chk(&argvars[0])) != NULL
8522 && !error)
8523 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008528 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531/*
8532 * "bufwinnr(nr)" function
8533 */
8534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *argvars;
8537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539#ifdef FEAT_WINDOWS
8540 win_T *wp;
8541 int winnr = 0;
8542#endif
8543 buf_T *buf;
8544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008545 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008547 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548#ifdef FEAT_WINDOWS
8549 for (wp = firstwin; wp; wp = wp->w_next)
8550 {
8551 ++winnr;
8552 if (wp->w_buffer == buf)
8553 break;
8554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558#endif
8559 --emsg_off;
8560}
8561
8562/*
8563 * "byte2line(byte)" function
8564 */
8565/*ARGSUSED*/
8566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567f_byte2line(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#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#else
8574 long boff = 0;
8575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 (linenr_T)0, &boff);
8582#endif
8583}
8584
8585/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008586 * "byteidx()" function
8587 */
8588/*ARGSUSED*/
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008593{
8594#ifdef FEAT_MBYTE
8595 char_u *t;
8596#endif
8597 char_u *str;
8598 long idx;
8599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008600 str = get_tv_string_chk(&argvars[0]);
8601 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008604 return;
8605
8606#ifdef FEAT_MBYTE
8607 t = str;
8608 for ( ; idx > 0; idx--)
8609 {
8610 if (*t == NUL) /* EOL reached */
8611 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008612 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008613 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008614 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008615#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008616 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008618#endif
8619}
8620
8621/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008622 * "call(func, arglist)" function
8623 */
8624 static void
8625f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 typval_T *argvars;
8627 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008628{
8629 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008630 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008631 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008633 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008635
8636 rettv->vval.v_number = 0;
8637 if (argvars[1].v_type != VAR_LIST)
8638 {
8639 EMSG(_(e_listreq));
8640 return;
8641 }
8642 if (argvars[1].vval.v_list == NULL)
8643 return;
8644
8645 if (argvars[0].v_type == VAR_FUNC)
8646 func = argvars[0].vval.v_string;
8647 else
8648 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 if (*func == NUL)
8650 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008651
Bram Moolenaare9a41262005-01-15 22:18:47 +00008652 if (argvars[2].v_type != VAR_UNKNOWN)
8653 {
8654 if (argvars[2].v_type != VAR_DICT)
8655 {
8656 EMSG(_(e_dictreq));
8657 return;
8658 }
8659 selfdict = argvars[2].vval.v_dict;
8660 }
8661
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008662 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8663 item = item->li_next)
8664 {
8665 if (argc == MAX_FUNC_ARGS)
8666 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008667 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008668 break;
8669 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008670 /* Make a copy of each argument. This is needed to be able to set
8671 * v_lock to VAR_FIXED in the copy without changing the original list.
8672 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008673 copy_tv(&item->li_tv, &argv[argc++]);
8674 }
8675
8676 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008677 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008678 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8679 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008680
8681 /* Free the arguments. */
8682 while (argc > 0)
8683 clear_tv(&argv[--argc]);
8684}
8685
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686#ifdef FEAT_FLOAT
8687/*
8688 * "ceil({float})" function
8689 */
8690 static void
8691f_ceil(argvars, rettv)
8692 typval_T *argvars;
8693 typval_T *rettv;
8694{
8695 float_T f;
8696
8697 rettv->v_type = VAR_FLOAT;
8698 if (get_float_arg(argvars, &f) == OK)
8699 rettv->vval.v_float = ceil(f);
8700 else
8701 rettv->vval.v_float = 0.0;
8702}
8703#endif
8704
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008705/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008706 * "changenr()" function
8707 */
8708/*ARGSUSED*/
8709 static void
8710f_changenr(argvars, rettv)
8711 typval_T *argvars;
8712 typval_T *rettv;
8713{
8714 rettv->vval.v_number = curbuf->b_u_seq_cur;
8715}
8716
8717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 * "char2nr(string)" function
8719 */
8720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008722 typval_T *argvars;
8723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725#ifdef FEAT_MBYTE
8726 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008727 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else
8729#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731}
8732
8733/*
8734 * "cindent(lnum)" function
8735 */
8736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 typval_T *argvars;
8739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
8741#ifdef FEAT_CINDENT
8742 pos_T pos;
8743 linenr_T lnum;
8744
8745 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8748 {
8749 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 curwin->w_cursor = pos;
8752 }
8753 else
8754#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756}
8757
8758/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008759 * "clearmatches()" function
8760 */
8761/*ARGSUSED*/
8762 static void
8763f_clearmatches(argvars, rettv)
8764 typval_T *argvars;
8765 typval_T *rettv;
8766{
8767#ifdef FEAT_SEARCH_EXTRA
8768 clear_matches(curwin);
8769#endif
8770}
8771
8772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 * "col(string)" function
8774 */
8775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 typval_T *argvars;
8778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 colnr_T col = 0;
8781 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008782 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008784 fp = var2fpos(&argvars[0], FALSE, &fnum);
8785 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 {
8787 if (fp->col == MAXCOL)
8788 {
8789 /* '> can be MAXCOL, get the length of the line then */
8790 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008791 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 else
8793 col = MAXCOL;
8794 }
8795 else
8796 {
8797 col = fp->col + 1;
8798#ifdef FEAT_VIRTUALEDIT
8799 /* col(".") when the cursor is on the NUL at the end of the line
8800 * because of "coladd" can be seen as an extra column. */
8801 if (virtual_active() && fp == &curwin->w_cursor)
8802 {
8803 char_u *p = ml_get_cursor();
8804
8805 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8806 curwin->w_virtcol - curwin->w_cursor.coladd))
8807 {
8808# ifdef FEAT_MBYTE
8809 int l;
8810
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008811 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 col += l;
8813# else
8814 if (*p != NUL && p[1] == NUL)
8815 ++col;
8816# endif
8817 }
8818 }
8819#endif
8820 }
8821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
Bram Moolenaar572cb562005-08-05 21:35:02 +00008825#if defined(FEAT_INS_EXPAND)
8826/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008827 * "complete()" function
8828 */
8829/*ARGSUSED*/
8830 static void
8831f_complete(argvars, rettv)
8832 typval_T *argvars;
8833 typval_T *rettv;
8834{
8835 int startcol;
8836
8837 if ((State & INSERT) == 0)
8838 {
8839 EMSG(_("E785: complete() can only be used in Insert mode"));
8840 return;
8841 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008842
8843 /* Check for undo allowed here, because if something was already inserted
8844 * the line was already saved for undo and this check isn't done. */
8845 if (!undo_allowed())
8846 return;
8847
Bram Moolenaarade00832006-03-10 21:46:58 +00008848 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8849 {
8850 EMSG(_(e_invarg));
8851 return;
8852 }
8853
8854 startcol = get_tv_number_chk(&argvars[0], NULL);
8855 if (startcol <= 0)
8856 return;
8857
8858 set_completion(startcol - 1, argvars[1].vval.v_list);
8859}
8860
8861/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008862 * "complete_add()" function
8863 */
8864/*ARGSUSED*/
8865 static void
8866f_complete_add(argvars, rettv)
8867 typval_T *argvars;
8868 typval_T *rettv;
8869{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008870 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008871}
8872
8873/*
8874 * "complete_check()" function
8875 */
8876/*ARGSUSED*/
8877 static void
8878f_complete_check(argvars, rettv)
8879 typval_T *argvars;
8880 typval_T *rettv;
8881{
8882 int saved = RedrawingDisabled;
8883
8884 RedrawingDisabled = 0;
8885 ins_compl_check_keys(0);
8886 rettv->vval.v_number = compl_interrupted;
8887 RedrawingDisabled = saved;
8888}
8889#endif
8890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891/*
8892 * "confirm(message, buttons[, default [, type]])" function
8893 */
8894/*ARGSUSED*/
8895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008897 typval_T *argvars;
8898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8901 char_u *message;
8902 char_u *buttons = NULL;
8903 char_u buf[NUMBUFLEN];
8904 char_u buf2[NUMBUFLEN];
8905 int def = 1;
8906 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 char_u *typestr;
8908 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 message = get_tv_string_chk(&argvars[0]);
8911 if (message == NULL)
8912 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008913 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008915 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8916 if (buttons == NULL)
8917 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008918 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008921 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8924 if (typestr == NULL)
8925 error = TRUE;
8926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008928 switch (TOUPPER_ASC(*typestr))
8929 {
8930 case 'E': type = VIM_ERROR; break;
8931 case 'Q': type = VIM_QUESTION; break;
8932 case 'I': type = VIM_INFO; break;
8933 case 'W': type = VIM_WARNING; break;
8934 case 'G': type = VIM_GENERIC; break;
8935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
8937 }
8938 }
8939 }
8940
8941 if (buttons == NULL || *buttons == NUL)
8942 buttons = (char_u *)_("&Ok");
8943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008944 if (error)
8945 rettv->vval.v_number = 0;
8946 else
8947 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 def, NULL);
8949#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#endif
8952}
8953
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954/*
8955 * "copy()" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008961{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008962 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008965#ifdef FEAT_FLOAT
8966/*
8967 * "cos()" function
8968 */
8969 static void
8970f_cos(argvars, rettv)
8971 typval_T *argvars;
8972 typval_T *rettv;
8973{
8974 float_T f;
8975
8976 rettv->v_type = VAR_FLOAT;
8977 if (get_float_arg(argvars, &f) == OK)
8978 rettv->vval.v_float = cos(f);
8979 else
8980 rettv->vval.v_float = 0.0;
8981}
8982#endif
8983
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008985 * "count()" function
8986 */
8987 static void
8988f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008991{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008992 long n = 0;
8993 int ic = FALSE;
8994
Bram Moolenaare9a41262005-01-15 22:18:47 +00008995 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008996 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008997 listitem_T *li;
8998 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009000
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001 if ((l = argvars[0].vval.v_list) != NULL)
9002 {
9003 li = l->lv_first;
9004 if (argvars[2].v_type != VAR_UNKNOWN)
9005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 int error = FALSE;
9007
9008 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009009 if (argvars[3].v_type != VAR_UNKNOWN)
9010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 idx = get_tv_number_chk(&argvars[3], &error);
9012 if (!error)
9013 {
9014 li = list_find(l, idx);
9015 if (li == NULL)
9016 EMSGN(_(e_listidx), idx);
9017 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 if (error)
9020 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009021 }
9022
9023 for ( ; li != NULL; li = li->li_next)
9024 if (tv_equal(&li->li_tv, &argvars[1], ic))
9025 ++n;
9026 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009027 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009028 else if (argvars[0].v_type == VAR_DICT)
9029 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 int todo;
9031 dict_T *d;
9032 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009033
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009034 if ((d = argvars[0].vval.v_dict) != NULL)
9035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009036 int error = FALSE;
9037
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038 if (argvars[2].v_type != VAR_UNKNOWN)
9039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009040 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 if (argvars[3].v_type != VAR_UNKNOWN)
9042 EMSG(_(e_invarg));
9043 }
9044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009045 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009047 {
9048 if (!HASHITEM_EMPTY(hi))
9049 {
9050 --todo;
9051 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9052 ++n;
9053 }
9054 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009055 }
9056 }
9057 else
9058 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009059 rettv->vval.v_number = n;
9060}
9061
9062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9064 *
9065 * Checks the existence of a cscope connection.
9066 */
9067/*ARGSUSED*/
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 typval_T *argvars;
9071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073#ifdef FEAT_CSCOPE
9074 int num = 0;
9075 char_u *dbpath = NULL;
9076 char_u *prepend = NULL;
9077 char_u buf[NUMBUFLEN];
9078
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009079 if (argvars[0].v_type != VAR_UNKNOWN
9080 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 num = (int)get_tv_number(&argvars[0]);
9083 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009084 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
9087
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091#endif
9092}
9093
9094/*
9095 * "cursor(lnum, col)" function
9096 *
9097 * Moves the cursor to the specified line and column
9098 */
9099/*ARGSUSED*/
9100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101f_cursor(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 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009106#ifdef FEAT_VIRTUALEDIT
9107 long coladd = 0;
9108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
Bram Moolenaara5525202006-03-02 22:52:09 +00009110 if (argvars[1].v_type == VAR_UNKNOWN)
9111 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009112 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009113
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009114 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009115 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009116 line = pos.lnum;
9117 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009118#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009119 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009120#endif
9121 }
9122 else
9123 {
9124 line = get_tv_lnum(argvars);
9125 col = get_tv_number_chk(&argvars[1], NULL);
9126#ifdef FEAT_VIRTUALEDIT
9127 if (argvars[2].v_type != VAR_UNKNOWN)
9128 coladd = get_tv_number_chk(&argvars[2], NULL);
9129#endif
9130 }
9131 if (line < 0 || col < 0
9132#ifdef FEAT_VIRTUALEDIT
9133 || coladd < 0
9134#endif
9135 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 if (line > 0)
9138 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 if (col > 0)
9140 curwin->w_cursor.col = col - 1;
9141#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009142 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#endif
9144
9145 /* Make sure the cursor is in a valid position. */
9146 check_cursor();
9147#ifdef FEAT_MBYTE
9148 /* Correct cursor for multi-byte character. */
9149 if (has_mbyte)
9150 mb_adjust_cursor();
9151#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009152
9153 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
9156/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009157 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 */
9159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009161 typval_T *argvars;
9162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009164 int noref = 0;
9165
9166 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009168 if (noref < 0 || noref > 1)
9169 EMSG(_(e_invarg));
9170 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009171 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172}
9173
9174/*
9175 * "delete()" function
9176 */
9177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009179 typval_T *argvars;
9180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186}
9187
9188/*
9189 * "did_filetype()" function
9190 */
9191/*ARGSUSED*/
9192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009194 typval_T *argvars;
9195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196{
9197#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201#endif
9202}
9203
9204/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009205 * "diff_filler()" function
9206 */
9207/*ARGSUSED*/
9208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *argvars;
9211 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009212{
9213#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009215#endif
9216}
9217
9218/*
9219 * "diff_hlID()" function
9220 */
9221/*ARGSUSED*/
9222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009223f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009224 typval_T *argvars;
9225 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009226{
9227#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009228 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009229 static linenr_T prev_lnum = 0;
9230 static int changedtick = 0;
9231 static int fnum = 0;
9232 static int change_start = 0;
9233 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009234 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009235 int filler_lines;
9236 int col;
9237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 if (lnum < 0) /* ignore type error in {lnum} arg */
9239 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009240 if (lnum != prev_lnum
9241 || changedtick != curbuf->b_changedtick
9242 || fnum != curbuf->b_fnum)
9243 {
9244 /* New line, buffer, change: need to get the values. */
9245 filler_lines = diff_check(curwin, lnum);
9246 if (filler_lines < 0)
9247 {
9248 if (filler_lines == -1)
9249 {
9250 change_start = MAXCOL;
9251 change_end = -1;
9252 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9253 hlID = HLF_ADD; /* added line */
9254 else
9255 hlID = HLF_CHD; /* changed line */
9256 }
9257 else
9258 hlID = HLF_ADD; /* added line */
9259 }
9260 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009261 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009262 prev_lnum = lnum;
9263 changedtick = curbuf->b_changedtick;
9264 fnum = curbuf->b_fnum;
9265 }
9266
9267 if (hlID == HLF_CHD || hlID == HLF_TXD)
9268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009270 if (col >= change_start && col <= change_end)
9271 hlID = HLF_TXD; /* changed text */
9272 else
9273 hlID = HLF_CHD; /* changed line */
9274 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009275 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009276#endif
9277}
9278
9279/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009280 * "empty({expr})" function
9281 */
9282 static void
9283f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 typval_T *argvars;
9285 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009286{
9287 int n;
9288
9289 switch (argvars[0].v_type)
9290 {
9291 case VAR_STRING:
9292 case VAR_FUNC:
9293 n = argvars[0].vval.v_string == NULL
9294 || *argvars[0].vval.v_string == NUL;
9295 break;
9296 case VAR_NUMBER:
9297 n = argvars[0].vval.v_number == 0;
9298 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009299#ifdef FEAT_FLOAT
9300 case VAR_FLOAT:
9301 n = argvars[0].vval.v_float == 0.0;
9302 break;
9303#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009304 case VAR_LIST:
9305 n = argvars[0].vval.v_list == NULL
9306 || argvars[0].vval.v_list->lv_first == NULL;
9307 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 case VAR_DICT:
9309 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009312 default:
9313 EMSG2(_(e_intern2), "f_empty()");
9314 n = 0;
9315 }
9316
9317 rettv->vval.v_number = n;
9318}
9319
9320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 * "escape({string}, {chars})" function
9322 */
9323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009325 typval_T *argvars;
9326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 char_u buf[NUMBUFLEN];
9329
Bram Moolenaar758711c2005-02-02 23:11:38 +00009330 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9331 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333}
9334
9335/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009336 * "eval()" function
9337 */
9338/*ARGSUSED*/
9339 static void
9340f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009341 typval_T *argvars;
9342 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009343{
9344 char_u *s;
9345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 s = get_tv_string_chk(&argvars[0]);
9347 if (s != NULL)
9348 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009350 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9351 {
9352 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009353 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009355 else if (*s != NUL)
9356 EMSG(_(e_trailing));
9357}
9358
9359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 * "eventhandler()" function
9361 */
9362/*ARGSUSED*/
9363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 typval_T *argvars;
9366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369}
9370
9371/*
9372 * "executable()" function
9373 */
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380}
9381
9382/*
9383 * "exists()" function
9384 */
9385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 typval_T *argvars;
9388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 char_u *p;
9391 char_u *name;
9392 int n = FALSE;
9393 int len = 0;
9394
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 if (*p == '$') /* environment variable */
9397 {
9398 /* first try "normal" environment variables (fast) */
9399 if (mch_getenv(p + 1) != NULL)
9400 n = TRUE;
9401 else
9402 {
9403 /* try expanding things like $VIM and ${HOME} */
9404 p = expand_env_save(p);
9405 if (p != NULL && *p != '$')
9406 n = TRUE;
9407 vim_free(p);
9408 }
9409 }
9410 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009413 if (*skipwhite(p) != NUL)
9414 n = FALSE; /* trailing garbage */
9415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 else if (*p == '*') /* internal or user defined function */
9417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009418 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 }
9420 else if (*p == ':')
9421 {
9422 n = cmd_exists(p + 1);
9423 }
9424 else if (*p == '#')
9425 {
9426#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009427 if (p[1] == '#')
9428 n = autocmd_supported(p + 2);
9429 else
9430 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431#endif
9432 }
9433 else /* internal variable */
9434 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009435 char_u *tofree;
9436 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009438 /* get_name_len() takes care of expanding curly braces */
9439 name = p;
9440 len = get_name_len(&p, &tofree, TRUE, FALSE);
9441 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009443 if (tofree != NULL)
9444 name = tofree;
9445 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9446 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009448 /* handle d.key, l[idx], f(expr) */
9449 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9450 if (n)
9451 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 }
9453 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009454 if (*p != NUL)
9455 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009457 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 }
9459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
9463/*
9464 * "expand()" function
9465 */
9466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009468 typval_T *argvars;
9469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 char_u *s;
9472 int len;
9473 char_u *errormsg;
9474 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9475 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 rettv->v_type = VAR_STRING;
9479 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 if (*s == '%' || *s == '#' || *s == '<')
9481 {
9482 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009483 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 --emsg_off;
9485 }
9486 else
9487 {
9488 /* When the optional second argument is non-zero, don't remove matches
9489 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009490 if (argvars[1].v_type != VAR_UNKNOWN
9491 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 if (!error)
9494 {
9495 ExpandInit(&xpc);
9496 xpc.xp_context = EXPAND_FILES;
9497 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 }
9499 else
9500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 }
9502}
9503
9504/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009505 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009506 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009507 */
9508 static void
9509f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009510 typval_T *argvars;
9511 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009512{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009513 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009514 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009515 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 list_T *l1, *l2;
9517 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009518 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009520
Bram Moolenaare9a41262005-01-15 22:18:47 +00009521 l1 = argvars[0].vval.v_list;
9522 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009523 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9524 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009525 {
9526 if (argvars[2].v_type != VAR_UNKNOWN)
9527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009528 before = get_tv_number_chk(&argvars[2], &error);
9529 if (error)
9530 return; /* type error; errmsg already given */
9531
Bram Moolenaar758711c2005-02-02 23:11:38 +00009532 if (before == l1->lv_len)
9533 item = NULL;
9534 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009535 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009536 item = list_find(l1, before);
9537 if (item == NULL)
9538 {
9539 EMSGN(_(e_listidx), before);
9540 return;
9541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009542 }
9543 }
9544 else
9545 item = NULL;
9546 list_extend(l1, l2, item);
9547
Bram Moolenaare9a41262005-01-15 22:18:47 +00009548 copy_tv(&argvars[0], rettv);
9549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009550 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009551 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9552 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 dict_T *d1, *d2;
9554 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 char_u *action;
9556 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009558 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009559
9560 d1 = argvars[0].vval.v_dict;
9561 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009562 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9563 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009564 {
9565 /* Check the third argument. */
9566 if (argvars[2].v_type != VAR_UNKNOWN)
9567 {
9568 static char *(av[]) = {"keep", "force", "error"};
9569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 action = get_tv_string_chk(&argvars[2]);
9571 if (action == NULL)
9572 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009573 for (i = 0; i < 3; ++i)
9574 if (STRCMP(action, av[i]) == 0)
9575 break;
9576 if (i == 3)
9577 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009578 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009579 return;
9580 }
9581 }
9582 else
9583 action = (char_u *)"force";
9584
9585 /* Go over all entries in the second dict and add them to the
9586 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009587 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009588 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009590 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009592 --todo;
9593 di1 = dict_find(d1, hi2->hi_key, -1);
9594 if (di1 == NULL)
9595 {
9596 di1 = dictitem_copy(HI2DI(hi2));
9597 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9598 dictitem_free(di1);
9599 }
9600 else if (*action == 'e')
9601 {
9602 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9603 break;
9604 }
9605 else if (*action == 'f')
9606 {
9607 clear_tv(&di1->di_tv);
9608 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009610 }
9611 }
9612
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 copy_tv(&argvars[0], rettv);
9614 }
9615 }
9616 else
9617 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009618}
9619
9620/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009621 * "feedkeys()" function
9622 */
9623/*ARGSUSED*/
9624 static void
9625f_feedkeys(argvars, rettv)
9626 typval_T *argvars;
9627 typval_T *rettv;
9628{
9629 int remap = TRUE;
9630 char_u *keys, *flags;
9631 char_u nbuf[NUMBUFLEN];
9632 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009633 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009634
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009635 /* This is not allowed in the sandbox. If the commands would still be
9636 * executed in the sandbox it would be OK, but it probably happens later,
9637 * when "sandbox" is no longer set. */
9638 if (check_secure())
9639 return;
9640
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009641 rettv->vval.v_number = 0;
9642 keys = get_tv_string(&argvars[0]);
9643 if (*keys != NUL)
9644 {
9645 if (argvars[1].v_type != VAR_UNKNOWN)
9646 {
9647 flags = get_tv_string_buf(&argvars[1], nbuf);
9648 for ( ; *flags != NUL; ++flags)
9649 {
9650 switch (*flags)
9651 {
9652 case 'n': remap = FALSE; break;
9653 case 'm': remap = TRUE; break;
9654 case 't': typed = TRUE; break;
9655 }
9656 }
9657 }
9658
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009659 /* Need to escape K_SPECIAL and CSI before putting the string in the
9660 * typeahead buffer. */
9661 keys_esc = vim_strsave_escape_csi(keys);
9662 if (keys_esc != NULL)
9663 {
9664 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009665 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009666 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009667 if (vgetc_busy)
9668 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009669 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009670 }
9671}
9672
9673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 * "filereadable()" function
9675 */
9676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009678 typval_T *argvars;
9679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 FILE *fd;
9682 char_u *p;
9683 int n;
9684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9687 {
9688 n = TRUE;
9689 fclose(fd);
9690 }
9691 else
9692 n = FALSE;
9693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
9697/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009698 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 * rights to write into.
9700 */
9701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009703 typval_T *argvars;
9704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009706 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707}
9708
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009709static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009710
9711 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009712findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009713 typval_T *argvars;
9714 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009715 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009716{
9717#ifdef FEAT_SEARCHPATH
9718 char_u *fname;
9719 char_u *fresult = NULL;
9720 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9721 char_u *p;
9722 char_u pathbuf[NUMBUFLEN];
9723 int count = 1;
9724 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009725 int error = FALSE;
9726#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009727
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009728 rettv->vval.v_string = NULL;
9729 rettv->v_type = VAR_STRING;
9730
9731#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009733
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009734 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9737 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009738 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 else
9740 {
9741 if (*p != NUL)
9742 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009745 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009746 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009747 }
9748
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009749 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9750 error = TRUE;
9751
9752 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009754 do
9755 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009756 if (rettv->v_type == VAR_STRING)
9757 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 fresult = find_file_in_path_option(first ? fname : NULL,
9759 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009760 0, first, path,
9761 find_what,
9762 curbuf->b_ffname,
9763 find_what == FINDFILE_DIR
9764 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009765 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009766
9767 if (fresult != NULL && rettv->v_type == VAR_LIST)
9768 list_append_string(rettv->vval.v_list, fresult, -1);
9769
9770 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009772
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009773 if (rettv->v_type == VAR_STRING)
9774 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009775#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009776}
9777
Bram Moolenaar33570922005-01-25 22:26:29 +00009778static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9779static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009780
9781/*
9782 * Implementation of map() and filter().
9783 */
9784 static void
9785filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 typval_T *argvars;
9787 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009788 int map;
9789{
9790 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009791 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009792 listitem_T *li, *nli;
9793 list_T *l = NULL;
9794 dictitem_T *di;
9795 hashtab_T *ht;
9796 hashitem_T *hi;
9797 dict_T *d = NULL;
9798 typval_T save_val;
9799 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009800 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009801 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009802 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009803 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009804
9805 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009806 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009807 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009808 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009809 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009810 return;
9811 }
9812 else if (argvars[0].v_type == VAR_DICT)
9813 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009814 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009815 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009816 return;
9817 }
9818 else
9819 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009820 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009821 return;
9822 }
9823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 expr = get_tv_string_buf_chk(&argvars[1], buf);
9825 /* On type errors, the preceding call has already displayed an error
9826 * message. Avoid a misleading error message for an empty string that
9827 * was not passed as argument. */
9828 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009830 prepare_vimvar(VV_VAL, &save_val);
9831 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009832
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009833 /* We reset "did_emsg" to be able to detect whether an error
9834 * occurred during evaluation of the expression. */
9835 save_did_emsg = did_emsg;
9836 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009838 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 prepare_vimvar(VV_KEY, &save_key);
9841 vimvars[VV_KEY].vv_type = VAR_STRING;
9842
9843 ht = &d->dv_hashtab;
9844 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009845 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009846 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 if (!HASHITEM_EMPTY(hi))
9849 {
9850 --todo;
9851 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009852 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 break;
9854 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009855 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009856 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 break;
9858 if (!map && rem)
9859 dictitem_remove(d, di);
9860 clear_tv(&vimvars[VV_KEY].vv_tv);
9861 }
9862 }
9863 hash_unlock(ht);
9864
9865 restore_vimvar(VV_KEY, &save_key);
9866 }
9867 else
9868 {
9869 for (li = l->lv_first; li != NULL; li = nli)
9870 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009871 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009872 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009873 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009874 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009875 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009876 break;
9877 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009879 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009880 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009883
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009884 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009885 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886
9887 copy_tv(&argvars[0], rettv);
9888}
9889
9890 static int
9891filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009893 char_u *expr;
9894 int map;
9895 int *remp;
9896{
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009898 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009899 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009900
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 s = expr;
9903 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009904 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 if (*s != NUL) /* check for trailing chars after expr */
9906 {
9907 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009908 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 }
9910 if (map)
9911 {
9912 /* map(): replace the list item value */
9913 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009914 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009915 *tv = rettv;
9916 }
9917 else
9918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009919 int error = FALSE;
9920
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 /* On type error, nothing has been removed; return FAIL to stop the
9925 * loop. The error message was given by get_tv_number_chk(). */
9926 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009927 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009929 retval = OK;
9930theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009932 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009933}
9934
9935/*
9936 * "filter()" function
9937 */
9938 static void
9939f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009942{
9943 filter_map(argvars, rettv, FALSE);
9944}
9945
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009947 * "finddir({fname}[, {path}[, {count}]])" function
9948 */
9949 static void
9950f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009951 typval_T *argvars;
9952 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009953{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009954 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009955}
9956
9957/*
9958 * "findfile({fname}[, {path}[, {count}]])" function
9959 */
9960 static void
9961f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 typval_T *argvars;
9963 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009964{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009965 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009966}
9967
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009968#ifdef FEAT_FLOAT
9969/*
9970 * "float2nr({float})" function
9971 */
9972 static void
9973f_float2nr(argvars, rettv)
9974 typval_T *argvars;
9975 typval_T *rettv;
9976{
9977 float_T f;
9978
9979 if (get_float_arg(argvars, &f) == OK)
9980 {
9981 if (f < -0x7fffffff)
9982 rettv->vval.v_number = -0x7fffffff;
9983 else if (f > 0x7fffffff)
9984 rettv->vval.v_number = 0x7fffffff;
9985 else
9986 rettv->vval.v_number = (varnumber_T)f;
9987 }
9988 else
9989 rettv->vval.v_number = 0;
9990}
9991
9992/*
9993 * "floor({float})" function
9994 */
9995 static void
9996f_floor(argvars, rettv)
9997 typval_T *argvars;
9998 typval_T *rettv;
9999{
10000 float_T f;
10001
10002 rettv->v_type = VAR_FLOAT;
10003 if (get_float_arg(argvars, &f) == OK)
10004 rettv->vval.v_float = floor(f);
10005 else
10006 rettv->vval.v_float = 0.0;
10007}
10008#endif
10009
Bram Moolenaar0d660222005-01-07 21:51:51 +000010010/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010011 * "fnameescape({string})" function
10012 */
10013 static void
10014f_fnameescape(argvars, rettv)
10015 typval_T *argvars;
10016 typval_T *rettv;
10017{
10018 rettv->vval.v_string = vim_strsave_fnameescape(
10019 get_tv_string(&argvars[0]), FALSE);
10020 rettv->v_type = VAR_STRING;
10021}
10022
10023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 * "fnamemodify({fname}, {mods})" function
10025 */
10026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 char_u *fname;
10032 char_u *mods;
10033 int usedlen = 0;
10034 int len;
10035 char_u *fbuf = NULL;
10036 char_u buf[NUMBUFLEN];
10037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010038 fname = get_tv_string_chk(&argvars[0]);
10039 mods = get_tv_string_buf_chk(&argvars[1], buf);
10040 if (fname == NULL || mods == NULL)
10041 fname = NULL;
10042 else
10043 {
10044 len = (int)STRLEN(fname);
10045 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 vim_free(fbuf);
10054}
10055
Bram Moolenaar33570922005-01-25 22:26:29 +000010056static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
10058/*
10059 * "foldclosed()" function
10060 */
10061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010063 typval_T *argvars;
10064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 int end;
10066{
10067#ifdef FEAT_FOLDING
10068 linenr_T lnum;
10069 linenr_T first, last;
10070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10073 {
10074 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10075 {
10076 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 return;
10081 }
10082 }
10083#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085}
10086
10087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010088 * "foldclosed()" function
10089 */
10090 static void
10091f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010094{
10095 foldclosed_both(argvars, rettv, FALSE);
10096}
10097
10098/*
10099 * "foldclosedend()" function
10100 */
10101 static void
10102f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010103 typval_T *argvars;
10104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010105{
10106 foldclosed_both(argvars, rettv, TRUE);
10107}
10108
10109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 * "foldlevel()" function
10111 */
10112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010114 typval_T *argvars;
10115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117#ifdef FEAT_FOLDING
10118 linenr_T lnum;
10119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 else
10124#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126}
10127
10128/*
10129 * "foldtext()" function
10130 */
10131/*ARGSUSED*/
10132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010134 typval_T *argvars;
10135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136{
10137#ifdef FEAT_FOLDING
10138 linenr_T lnum;
10139 char_u *s;
10140 char_u *r;
10141 int len;
10142 char *txt;
10143#endif
10144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->v_type = VAR_STRING;
10146 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010148 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10149 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10150 <= curbuf->b_ml.ml_line_count
10151 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 {
10153 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010154 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10155 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 {
10157 if (!linewhite(lnum))
10158 break;
10159 ++lnum;
10160 }
10161
10162 /* Find interesting text in this line. */
10163 s = skipwhite(ml_get(lnum));
10164 /* skip C comment-start */
10165 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010168 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010170 {
10171 s = skipwhite(ml_get(lnum + 1));
10172 if (*s == '*')
10173 s = skipwhite(s + 1);
10174 }
10175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 txt = _("+-%s%3ld lines: ");
10177 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 + 20 /* for %3ld */
10180 + STRLEN(s))); /* concatenated */
10181 if (r != NULL)
10182 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10184 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10185 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 len = (int)STRLEN(r);
10187 STRCAT(r, s);
10188 /* remove 'foldmarker' and 'commentstring' */
10189 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 }
10192 }
10193#endif
10194}
10195
10196/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010197 * "foldtextresult(lnum)" function
10198 */
10199/*ARGSUSED*/
10200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010201f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010202 typval_T *argvars;
10203 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010204{
10205#ifdef FEAT_FOLDING
10206 linenr_T lnum;
10207 char_u *text;
10208 char_u buf[51];
10209 foldinfo_T foldinfo;
10210 int fold_count;
10211#endif
10212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->v_type = VAR_STRING;
10214 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010215#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 /* treat illegal types and illegal string values for {lnum} the same */
10218 if (lnum < 0)
10219 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010220 fold_count = foldedCount(curwin, lnum, &foldinfo);
10221 if (fold_count > 0)
10222 {
10223 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10224 &foldinfo, buf);
10225 if (text == buf)
10226 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010227 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010228 }
10229#endif
10230}
10231
10232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 * "foreground()" function
10234 */
10235/*ARGSUSED*/
10236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 typval_T *argvars;
10239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#ifdef FEAT_GUI
10243 if (gui.in_use)
10244 gui_mch_set_foreground();
10245#else
10246# ifdef WIN32
10247 win32_set_foreground();
10248# endif
10249#endif
10250}
10251
10252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010253 * "function()" function
10254 */
10255/*ARGSUSED*/
10256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010258 typval_T *argvars;
10259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010260{
10261 char_u *s;
10262
Bram Moolenaara7043832005-01-21 11:56:39 +000010263 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010266 EMSG2(_(e_invarg2), s);
10267 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010268 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010269 else
10270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010271 rettv->vval.v_string = vim_strsave(s);
10272 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010273 }
10274}
10275
10276/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010277 * "garbagecollect()" function
10278 */
10279/*ARGSUSED*/
10280 static void
10281f_garbagecollect(argvars, rettv)
10282 typval_T *argvars;
10283 typval_T *rettv;
10284{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010285 /* This is postponed until we are back at the toplevel, because we may be
10286 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10287 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010288
10289 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10290 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010291}
10292
10293/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010294 * "get()" function
10295 */
10296 static void
10297f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010298 typval_T *argvars;
10299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010300{
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 listitem_T *li;
10302 list_T *l;
10303 dictitem_T *di;
10304 dict_T *d;
10305 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010306
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010308 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 int error = FALSE;
10312
10313 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10314 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010316 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 else if (argvars[0].v_type == VAR_DICT)
10319 {
10320 if ((d = argvars[0].vval.v_dict) != NULL)
10321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010322 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 if (di != NULL)
10324 tv = &di->di_tv;
10325 }
10326 }
10327 else
10328 EMSG2(_(e_listdictarg), "get()");
10329
10330 if (tv == NULL)
10331 {
10332 if (argvars[2].v_type == VAR_UNKNOWN)
10333 rettv->vval.v_number = 0;
10334 else
10335 copy_tv(&argvars[2], rettv);
10336 }
10337 else
10338 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010339}
10340
Bram Moolenaar342337a2005-07-21 21:11:17 +000010341static 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 +000010342
10343/*
10344 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010345 * Return a range (from start to end) of lines in rettv from the specified
10346 * buffer.
10347 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010348 */
10349 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010350get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010351 buf_T *buf;
10352 linenr_T start;
10353 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010354 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010355 typval_T *rettv;
10356{
10357 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010358
Bram Moolenaar342337a2005-07-21 21:11:17 +000010359 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010360 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010361 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010362 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010363 }
10364 else
10365 rettv->vval.v_number = 0;
10366
10367 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10368 return;
10369
10370 if (!retlist)
10371 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010372 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10373 p = ml_get_buf(buf, start, FALSE);
10374 else
10375 p = (char_u *)"";
10376
10377 rettv->v_type = VAR_STRING;
10378 rettv->vval.v_string = vim_strsave(p);
10379 }
10380 else
10381 {
10382 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010383 return;
10384
10385 if (start < 1)
10386 start = 1;
10387 if (end > buf->b_ml.ml_line_count)
10388 end = buf->b_ml.ml_line_count;
10389 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010390 if (list_append_string(rettv->vval.v_list,
10391 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010392 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010393 }
10394}
10395
10396/*
10397 * "getbufline()" function
10398 */
10399 static void
10400f_getbufline(argvars, rettv)
10401 typval_T *argvars;
10402 typval_T *rettv;
10403{
10404 linenr_T lnum;
10405 linenr_T end;
10406 buf_T *buf;
10407
10408 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10409 ++emsg_off;
10410 buf = get_buf_tv(&argvars[0]);
10411 --emsg_off;
10412
Bram Moolenaar661b1822005-07-28 22:36:45 +000010413 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010414 if (argvars[2].v_type == VAR_UNKNOWN)
10415 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010416 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010417 end = get_tv_lnum_buf(&argvars[2], buf);
10418
Bram Moolenaar342337a2005-07-21 21:11:17 +000010419 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010420}
10421
Bram Moolenaar0d660222005-01-07 21:51:51 +000010422/*
10423 * "getbufvar()" function
10424 */
10425 static void
10426f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010427 typval_T *argvars;
10428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010429{
10430 buf_T *buf;
10431 buf_T *save_curbuf;
10432 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10436 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010437 ++emsg_off;
10438 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010439
10440 rettv->v_type = VAR_STRING;
10441 rettv->vval.v_string = NULL;
10442
10443 if (buf != NULL && varname != NULL)
10444 {
10445 if (*varname == '&') /* buffer-local-option */
10446 {
10447 /* set curbuf to be our buf, temporarily */
10448 save_curbuf = curbuf;
10449 curbuf = buf;
10450
10451 get_option_tv(&varname, rettv, TRUE);
10452
10453 /* restore previous notion of curbuf */
10454 curbuf = save_curbuf;
10455 }
10456 else
10457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 if (*varname == NUL)
10459 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10460 * scope prefix before the NUL byte is required by
10461 * find_var_in_ht(). */
10462 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010463 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010464 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010465 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010466 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010467 }
10468 }
10469
10470 --emsg_off;
10471}
10472
10473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 * "getchar()" function
10475 */
10476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *argvars;
10479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480{
10481 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010484 /* Position the cursor. Needed after a message that ends in a space. */
10485 windgoto(msg_row, msg_col);
10486
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 ++no_mapping;
10488 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010489 for (;;)
10490 {
10491 if (argvars[0].v_type == VAR_UNKNOWN)
10492 /* getchar(): blocking wait. */
10493 n = safe_vgetc();
10494 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10495 /* getchar(1): only check if char avail */
10496 n = vpeekc();
10497 else if (error || vpeekc() == NUL)
10498 /* illegal argument or getchar(0) and no char avail: return zero */
10499 n = 0;
10500 else
10501 /* getchar(0) and char avail: return char */
10502 n = safe_vgetc();
10503 if (n == K_IGNORE)
10504 continue;
10505 break;
10506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 --no_mapping;
10508 --allow_keys;
10509
Bram Moolenaar219b8702006-11-01 14:32:36 +000010510 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10511 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10512 vimvars[VV_MOUSE_COL].vv_nr = 0;
10513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 if (IS_SPECIAL(n) || mod_mask != 0)
10516 {
10517 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10518 int i = 0;
10519
10520 /* Turn a special key into three bytes, plus modifier. */
10521 if (mod_mask != 0)
10522 {
10523 temp[i++] = K_SPECIAL;
10524 temp[i++] = KS_MODIFIER;
10525 temp[i++] = mod_mask;
10526 }
10527 if (IS_SPECIAL(n))
10528 {
10529 temp[i++] = K_SPECIAL;
10530 temp[i++] = K_SECOND(n);
10531 temp[i++] = K_THIRD(n);
10532 }
10533#ifdef FEAT_MBYTE
10534 else if (has_mbyte)
10535 i += (*mb_char2bytes)(n, temp + i);
10536#endif
10537 else
10538 temp[i++] = n;
10539 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540 rettv->v_type = VAR_STRING;
10541 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010542
10543#ifdef FEAT_MOUSE
10544 if (n == K_LEFTMOUSE
10545 || n == K_LEFTMOUSE_NM
10546 || n == K_LEFTDRAG
10547 || n == K_LEFTRELEASE
10548 || n == K_LEFTRELEASE_NM
10549 || n == K_MIDDLEMOUSE
10550 || n == K_MIDDLEDRAG
10551 || n == K_MIDDLERELEASE
10552 || n == K_RIGHTMOUSE
10553 || n == K_RIGHTDRAG
10554 || n == K_RIGHTRELEASE
10555 || n == K_X1MOUSE
10556 || n == K_X1DRAG
10557 || n == K_X1RELEASE
10558 || n == K_X2MOUSE
10559 || n == K_X2DRAG
10560 || n == K_X2RELEASE
10561 || n == K_MOUSEDOWN
10562 || n == K_MOUSEUP)
10563 {
10564 int row = mouse_row;
10565 int col = mouse_col;
10566 win_T *win;
10567 linenr_T lnum;
10568# ifdef FEAT_WINDOWS
10569 win_T *wp;
10570# endif
10571 int n = 1;
10572
10573 if (row >= 0 && col >= 0)
10574 {
10575 /* Find the window at the mouse coordinates and compute the
10576 * text position. */
10577 win = mouse_find_win(&row, &col);
10578 (void)mouse_comp_pos(win, &row, &col, &lnum);
10579# ifdef FEAT_WINDOWS
10580 for (wp = firstwin; wp != win; wp = wp->w_next)
10581 ++n;
10582# endif
10583 vimvars[VV_MOUSE_WIN].vv_nr = n;
10584 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10585 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10586 }
10587 }
10588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 }
10590}
10591
10592/*
10593 * "getcharmod()" function
10594 */
10595/*ARGSUSED*/
10596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010597f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010598 typval_T *argvars;
10599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602}
10603
10604/*
10605 * "getcmdline()" function
10606 */
10607/*ARGSUSED*/
10608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 typval_T *argvars;
10611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->v_type = VAR_STRING;
10614 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615}
10616
10617/*
10618 * "getcmdpos()" function
10619 */
10620/*ARGSUSED*/
10621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010623 typval_T *argvars;
10624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010626 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627}
10628
10629/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010630 * "getcmdtype()" function
10631 */
10632/*ARGSUSED*/
10633 static void
10634f_getcmdtype(argvars, rettv)
10635 typval_T *argvars;
10636 typval_T *rettv;
10637{
10638 rettv->v_type = VAR_STRING;
10639 rettv->vval.v_string = alloc(2);
10640 if (rettv->vval.v_string != NULL)
10641 {
10642 rettv->vval.v_string[0] = get_cmdline_type();
10643 rettv->vval.v_string[1] = NUL;
10644 }
10645}
10646
10647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 * "getcwd()" function
10649 */
10650/*ARGSUSED*/
10651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 typval_T *argvars;
10654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655{
10656 char_u cwd[MAXPATHL];
10657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 else
10662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010665 if (rettv->vval.v_string != NULL)
10666 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667#endif
10668 }
10669}
10670
10671/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010672 * "getfontname()" function
10673 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010674/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 typval_T *argvars;
10678 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010679{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010680 rettv->v_type = VAR_STRING;
10681 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010682#ifdef FEAT_GUI
10683 if (gui.in_use)
10684 {
10685 GuiFont font;
10686 char_u *name = NULL;
10687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010688 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010689 {
10690 /* Get the "Normal" font. Either the name saved by
10691 * hl_set_font_name() or from the font ID. */
10692 font = gui.norm_font;
10693 name = hl_get_font_name();
10694 }
10695 else
10696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010698 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10699 return;
10700 font = gui_mch_get_font(name, FALSE);
10701 if (font == NOFONT)
10702 return; /* Invalid font name, return empty string. */
10703 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010705 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010706 gui_mch_free_font(font);
10707 }
10708#endif
10709}
10710
10711/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010712 * "getfperm({fname})" function
10713 */
10714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010716 typval_T *argvars;
10717 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010718{
10719 char_u *fname;
10720 struct stat st;
10721 char_u *perm = NULL;
10722 char_u flags[] = "rwx";
10723 int i;
10724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010728 if (mch_stat((char *)fname, &st) >= 0)
10729 {
10730 perm = vim_strsave((char_u *)"---------");
10731 if (perm != NULL)
10732 {
10733 for (i = 0; i < 9; i++)
10734 {
10735 if (st.st_mode & (1 << (8 - i)))
10736 perm[i] = flags[i % 3];
10737 }
10738 }
10739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010740 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010741}
10742
10743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 * "getfsize({fname})" function
10745 */
10746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 typval_T *argvars;
10749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750{
10751 char_u *fname;
10752 struct stat st;
10753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757
10758 if (mch_stat((char *)fname, &st) >= 0)
10759 {
10760 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010764 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010765
10766 /* non-perfect check for overflow */
10767 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10768 rettv->vval.v_number = -2;
10769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 }
10771 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773}
10774
10775/*
10776 * "getftime({fname})" function
10777 */
10778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010780 typval_T *argvars;
10781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782{
10783 char_u *fname;
10784 struct stat st;
10785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787
10788 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792}
10793
10794/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010795 * "getftype({fname})" function
10796 */
10797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 typval_T *argvars;
10800 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010801{
10802 char_u *fname;
10803 struct stat st;
10804 char_u *type = NULL;
10805 char *t;
10806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010810 if (mch_lstat((char *)fname, &st) >= 0)
10811 {
10812#ifdef S_ISREG
10813 if (S_ISREG(st.st_mode))
10814 t = "file";
10815 else if (S_ISDIR(st.st_mode))
10816 t = "dir";
10817# ifdef S_ISLNK
10818 else if (S_ISLNK(st.st_mode))
10819 t = "link";
10820# endif
10821# ifdef S_ISBLK
10822 else if (S_ISBLK(st.st_mode))
10823 t = "bdev";
10824# endif
10825# ifdef S_ISCHR
10826 else if (S_ISCHR(st.st_mode))
10827 t = "cdev";
10828# endif
10829# ifdef S_ISFIFO
10830 else if (S_ISFIFO(st.st_mode))
10831 t = "fifo";
10832# endif
10833# ifdef S_ISSOCK
10834 else if (S_ISSOCK(st.st_mode))
10835 t = "fifo";
10836# endif
10837 else
10838 t = "other";
10839#else
10840# ifdef S_IFMT
10841 switch (st.st_mode & S_IFMT)
10842 {
10843 case S_IFREG: t = "file"; break;
10844 case S_IFDIR: t = "dir"; break;
10845# ifdef S_IFLNK
10846 case S_IFLNK: t = "link"; break;
10847# endif
10848# ifdef S_IFBLK
10849 case S_IFBLK: t = "bdev"; break;
10850# endif
10851# ifdef S_IFCHR
10852 case S_IFCHR: t = "cdev"; break;
10853# endif
10854# ifdef S_IFIFO
10855 case S_IFIFO: t = "fifo"; break;
10856# endif
10857# ifdef S_IFSOCK
10858 case S_IFSOCK: t = "socket"; break;
10859# endif
10860 default: t = "other";
10861 }
10862# else
10863 if (mch_isdir(fname))
10864 t = "dir";
10865 else
10866 t = "file";
10867# endif
10868#endif
10869 type = vim_strsave((char_u *)t);
10870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010872}
10873
10874/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010875 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876 */
10877 static void
10878f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010879 typval_T *argvars;
10880 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010881{
10882 linenr_T lnum;
10883 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010884 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010885
10886 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010887 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010888 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010889 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010890 retlist = FALSE;
10891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010892 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010893 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010895 retlist = TRUE;
10896 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010897
Bram Moolenaar342337a2005-07-21 21:11:17 +000010898 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010899}
10900
10901/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010902 * "getmatches()" function
10903 */
10904/*ARGSUSED*/
10905 static void
10906f_getmatches(argvars, rettv)
10907 typval_T *argvars;
10908 typval_T *rettv;
10909{
10910#ifdef FEAT_SEARCH_EXTRA
10911 dict_T *dict;
10912 matchitem_T *cur = curwin->w_match_head;
10913
10914 rettv->vval.v_number = 0;
10915
10916 if (rettv_list_alloc(rettv) == OK)
10917 {
10918 while (cur != NULL)
10919 {
10920 dict = dict_alloc();
10921 if (dict == NULL)
10922 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010923 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10924 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10925 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10926 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10927 list_append_dict(rettv->vval.v_list, dict);
10928 cur = cur->next;
10929 }
10930 }
10931#endif
10932}
10933
10934/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010935 * "getpid()" function
10936 */
10937/*ARGSUSED*/
10938 static void
10939f_getpid(argvars, rettv)
10940 typval_T *argvars;
10941 typval_T *rettv;
10942{
10943 rettv->vval.v_number = mch_get_pid();
10944}
10945
10946/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010947 * "getpos(string)" function
10948 */
10949 static void
10950f_getpos(argvars, rettv)
10951 typval_T *argvars;
10952 typval_T *rettv;
10953{
10954 pos_T *fp;
10955 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010956 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010957
10958 if (rettv_list_alloc(rettv) == OK)
10959 {
10960 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010961 fp = var2fpos(&argvars[0], TRUE, &fnum);
10962 if (fnum != -1)
10963 list_append_number(l, (varnumber_T)fnum);
10964 else
10965 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010966 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10967 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010968 list_append_number(l, (fp != NULL)
10969 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010970 : (varnumber_T)0);
10971 list_append_number(l,
10972#ifdef FEAT_VIRTUALEDIT
10973 (fp != NULL) ? (varnumber_T)fp->coladd :
10974#endif
10975 (varnumber_T)0);
10976 }
10977 else
10978 rettv->vval.v_number = FALSE;
10979}
10980
10981/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010982 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010983 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010984/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010985 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010986f_getqflist(argvars, rettv)
10987 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010988 typval_T *rettv;
10989{
10990#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010991 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010992#endif
10993
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010994 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010995#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010996 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010997 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010998 wp = NULL;
10999 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11000 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011001 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011002 if (wp == NULL)
11003 return;
11004 }
11005
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011006 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011007 }
11008#endif
11009}
11010
11011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 * "getreg()" function
11013 */
11014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011016 typval_T *argvars;
11017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018{
11019 char_u *strregname;
11020 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011021 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011022 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011024 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011026 strregname = get_tv_string_chk(&argvars[0]);
11027 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011028 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011032 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 regname = (strregname == NULL ? '"' : *strregname);
11034 if (regname == 0)
11035 regname = '"';
11036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011037 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 rettv->vval.v_string = error ? NULL :
11039 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040}
11041
11042/*
11043 * "getregtype()" function
11044 */
11045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011047 typval_T *argvars;
11048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 char_u *strregname;
11051 int regname;
11052 char_u buf[NUMBUFLEN + 2];
11053 long reglen = 0;
11054
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011055 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 {
11057 strregname = get_tv_string_chk(&argvars[0]);
11058 if (strregname == NULL) /* type error; errmsg already given */
11059 {
11060 rettv->v_type = VAR_STRING;
11061 rettv->vval.v_string = NULL;
11062 return;
11063 }
11064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 else
11066 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011067 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068
11069 regname = (strregname == NULL ? '"' : *strregname);
11070 if (regname == 0)
11071 regname = '"';
11072
11073 buf[0] = NUL;
11074 buf[1] = NUL;
11075 switch (get_reg_type(regname, &reglen))
11076 {
11077 case MLINE: buf[0] = 'V'; break;
11078 case MCHAR: buf[0] = 'v'; break;
11079#ifdef FEAT_VISUAL
11080 case MBLOCK:
11081 buf[0] = Ctrl_V;
11082 sprintf((char *)buf + 1, "%ld", reglen + 1);
11083 break;
11084#endif
11085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->v_type = VAR_STRING;
11087 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088}
11089
11090/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011091 * "gettabwinvar()" function
11092 */
11093 static void
11094f_gettabwinvar(argvars, rettv)
11095 typval_T *argvars;
11096 typval_T *rettv;
11097{
11098 getwinvar(argvars, rettv, 1);
11099}
11100
11101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 * "getwinposx()" function
11103 */
11104/*ARGSUSED*/
11105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011107 typval_T *argvars;
11108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111#ifdef FEAT_GUI
11112 if (gui.in_use)
11113 {
11114 int x, y;
11115
11116 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 }
11119#endif
11120}
11121
11122/*
11123 * "getwinposy()" function
11124 */
11125/*ARGSUSED*/
11126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132#ifdef FEAT_GUI
11133 if (gui.in_use)
11134 {
11135 int x, y;
11136
11137 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 }
11140#endif
11141}
11142
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011143/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011144 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011145 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011146 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011147find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011148 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011149 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011150{
11151#ifdef FEAT_WINDOWS
11152 win_T *wp;
11153#endif
11154 int nr;
11155
11156 nr = get_tv_number_chk(vp, NULL);
11157
11158#ifdef FEAT_WINDOWS
11159 if (nr < 0)
11160 return NULL;
11161 if (nr == 0)
11162 return curwin;
11163
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011164 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11165 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011166 if (--nr <= 0)
11167 break;
11168 return wp;
11169#else
11170 if (nr == 0 || nr == 1)
11171 return curwin;
11172 return NULL;
11173#endif
11174}
11175
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176/*
11177 * "getwinvar()" function
11178 */
11179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 typval_T *argvars;
11182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011184 getwinvar(argvars, rettv, 0);
11185}
11186
11187/*
11188 * getwinvar() and gettabwinvar()
11189 */
11190 static void
11191getwinvar(argvars, rettv, off)
11192 typval_T *argvars;
11193 typval_T *rettv;
11194 int off; /* 1 for gettabwinvar() */
11195{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 win_T *win, *oldcurwin;
11197 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011198 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011199 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011201#ifdef FEAT_WINDOWS
11202 if (off == 1)
11203 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11204 else
11205 tp = curtab;
11206#endif
11207 win = find_win_by_nr(&argvars[off], tp);
11208 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->v_type = VAR_STRING;
11212 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213
11214 if (win != NULL && varname != NULL)
11215 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011216 /* Set curwin to be our win, temporarily. Also set curbuf, so
11217 * that we can get buffer-local options. */
11218 oldcurwin = curwin;
11219 curwin = win;
11220 curbuf = win->w_buffer;
11221
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 else
11225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 if (*varname == NUL)
11227 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11228 * scope prefix before the NUL byte is required by
11229 * find_var_in_ht(). */
11230 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011232 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011234 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011236
11237 /* restore previous notion of curwin */
11238 curwin = oldcurwin;
11239 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 }
11241
11242 --emsg_off;
11243}
11244
11245/*
11246 * "glob()" function
11247 */
11248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011250 typval_T *argvars;
11251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252{
11253 expand_T xpc;
11254
11255 ExpandInit(&xpc);
11256 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 rettv->v_type = VAR_STRING;
11258 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260}
11261
11262/*
11263 * "globpath()" function
11264 */
11265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *argvars;
11268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
11270 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 if (file == NULL)
11275 rettv->vval.v_string = NULL;
11276 else
11277 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
11280/*
11281 * "has()" function
11282 */
11283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *argvars;
11286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
11288 int i;
11289 char_u *name;
11290 int n = FALSE;
11291 static char *(has_list[]) =
11292 {
11293#ifdef AMIGA
11294 "amiga",
11295# ifdef FEAT_ARP
11296 "arp",
11297# endif
11298#endif
11299#ifdef __BEOS__
11300 "beos",
11301#endif
11302#ifdef MSDOS
11303# ifdef DJGPP
11304 "dos32",
11305# else
11306 "dos16",
11307# endif
11308#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011309#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 "mac",
11311#endif
11312#if defined(MACOS_X_UNIX)
11313 "macunix",
11314#endif
11315#ifdef OS2
11316 "os2",
11317#endif
11318#ifdef __QNX__
11319 "qnx",
11320#endif
11321#ifdef RISCOS
11322 "riscos",
11323#endif
11324#ifdef UNIX
11325 "unix",
11326#endif
11327#ifdef VMS
11328 "vms",
11329#endif
11330#ifdef WIN16
11331 "win16",
11332#endif
11333#ifdef WIN32
11334 "win32",
11335#endif
11336#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11337 "win32unix",
11338#endif
11339#ifdef WIN64
11340 "win64",
11341#endif
11342#ifdef EBCDIC
11343 "ebcdic",
11344#endif
11345#ifndef CASE_INSENSITIVE_FILENAME
11346 "fname_case",
11347#endif
11348#ifdef FEAT_ARABIC
11349 "arabic",
11350#endif
11351#ifdef FEAT_AUTOCMD
11352 "autocmd",
11353#endif
11354#ifdef FEAT_BEVAL
11355 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011356# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11357 "balloon_multiline",
11358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359#endif
11360#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11361 "builtin_terms",
11362# ifdef ALL_BUILTIN_TCAPS
11363 "all_builtin_terms",
11364# endif
11365#endif
11366#ifdef FEAT_BYTEOFF
11367 "byte_offset",
11368#endif
11369#ifdef FEAT_CINDENT
11370 "cindent",
11371#endif
11372#ifdef FEAT_CLIENTSERVER
11373 "clientserver",
11374#endif
11375#ifdef FEAT_CLIPBOARD
11376 "clipboard",
11377#endif
11378#ifdef FEAT_CMDL_COMPL
11379 "cmdline_compl",
11380#endif
11381#ifdef FEAT_CMDHIST
11382 "cmdline_hist",
11383#endif
11384#ifdef FEAT_COMMENTS
11385 "comments",
11386#endif
11387#ifdef FEAT_CRYPT
11388 "cryptv",
11389#endif
11390#ifdef FEAT_CSCOPE
11391 "cscope",
11392#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011393#ifdef CURSOR_SHAPE
11394 "cursorshape",
11395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396#ifdef DEBUG
11397 "debug",
11398#endif
11399#ifdef FEAT_CON_DIALOG
11400 "dialog_con",
11401#endif
11402#ifdef FEAT_GUI_DIALOG
11403 "dialog_gui",
11404#endif
11405#ifdef FEAT_DIFF
11406 "diff",
11407#endif
11408#ifdef FEAT_DIGRAPHS
11409 "digraphs",
11410#endif
11411#ifdef FEAT_DND
11412 "dnd",
11413#endif
11414#ifdef FEAT_EMACS_TAGS
11415 "emacs_tags",
11416#endif
11417 "eval", /* always present, of course! */
11418#ifdef FEAT_EX_EXTRA
11419 "ex_extra",
11420#endif
11421#ifdef FEAT_SEARCH_EXTRA
11422 "extra_search",
11423#endif
11424#ifdef FEAT_FKMAP
11425 "farsi",
11426#endif
11427#ifdef FEAT_SEARCHPATH
11428 "file_in_path",
11429#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011430#if defined(UNIX) && !defined(USE_SYSTEM)
11431 "filterpipe",
11432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433#ifdef FEAT_FIND_ID
11434 "find_in_path",
11435#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011436#ifdef FEAT_FLOAT
11437 "float",
11438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439#ifdef FEAT_FOLDING
11440 "folding",
11441#endif
11442#ifdef FEAT_FOOTER
11443 "footer",
11444#endif
11445#if !defined(USE_SYSTEM) && defined(UNIX)
11446 "fork",
11447#endif
11448#ifdef FEAT_GETTEXT
11449 "gettext",
11450#endif
11451#ifdef FEAT_GUI
11452 "gui",
11453#endif
11454#ifdef FEAT_GUI_ATHENA
11455# ifdef FEAT_GUI_NEXTAW
11456 "gui_neXtaw",
11457# else
11458 "gui_athena",
11459# endif
11460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461#ifdef FEAT_GUI_GTK
11462 "gui_gtk",
11463# ifdef HAVE_GTK2
11464 "gui_gtk2",
11465# endif
11466#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011467#ifdef FEAT_GUI_GNOME
11468 "gui_gnome",
11469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470#ifdef FEAT_GUI_MAC
11471 "gui_mac",
11472#endif
11473#ifdef FEAT_GUI_MOTIF
11474 "gui_motif",
11475#endif
11476#ifdef FEAT_GUI_PHOTON
11477 "gui_photon",
11478#endif
11479#ifdef FEAT_GUI_W16
11480 "gui_win16",
11481#endif
11482#ifdef FEAT_GUI_W32
11483 "gui_win32",
11484#endif
11485#ifdef FEAT_HANGULIN
11486 "hangul_input",
11487#endif
11488#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11489 "iconv",
11490#endif
11491#ifdef FEAT_INS_EXPAND
11492 "insert_expand",
11493#endif
11494#ifdef FEAT_JUMPLIST
11495 "jumplist",
11496#endif
11497#ifdef FEAT_KEYMAP
11498 "keymap",
11499#endif
11500#ifdef FEAT_LANGMAP
11501 "langmap",
11502#endif
11503#ifdef FEAT_LIBCALL
11504 "libcall",
11505#endif
11506#ifdef FEAT_LINEBREAK
11507 "linebreak",
11508#endif
11509#ifdef FEAT_LISP
11510 "lispindent",
11511#endif
11512#ifdef FEAT_LISTCMDS
11513 "listcmds",
11514#endif
11515#ifdef FEAT_LOCALMAP
11516 "localmap",
11517#endif
11518#ifdef FEAT_MENU
11519 "menu",
11520#endif
11521#ifdef FEAT_SESSION
11522 "mksession",
11523#endif
11524#ifdef FEAT_MODIFY_FNAME
11525 "modify_fname",
11526#endif
11527#ifdef FEAT_MOUSE
11528 "mouse",
11529#endif
11530#ifdef FEAT_MOUSESHAPE
11531 "mouseshape",
11532#endif
11533#if defined(UNIX) || defined(VMS)
11534# ifdef FEAT_MOUSE_DEC
11535 "mouse_dec",
11536# endif
11537# ifdef FEAT_MOUSE_GPM
11538 "mouse_gpm",
11539# endif
11540# ifdef FEAT_MOUSE_JSB
11541 "mouse_jsbterm",
11542# endif
11543# ifdef FEAT_MOUSE_NET
11544 "mouse_netterm",
11545# endif
11546# ifdef FEAT_MOUSE_PTERM
11547 "mouse_pterm",
11548# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011549# ifdef FEAT_SYSMOUSE
11550 "mouse_sysmouse",
11551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552# ifdef FEAT_MOUSE_XTERM
11553 "mouse_xterm",
11554# endif
11555#endif
11556#ifdef FEAT_MBYTE
11557 "multi_byte",
11558#endif
11559#ifdef FEAT_MBYTE_IME
11560 "multi_byte_ime",
11561#endif
11562#ifdef FEAT_MULTI_LANG
11563 "multi_lang",
11564#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011565#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011566#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011567 "mzscheme",
11568#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570#ifdef FEAT_OLE
11571 "ole",
11572#endif
11573#ifdef FEAT_OSFILETYPE
11574 "osfiletype",
11575#endif
11576#ifdef FEAT_PATH_EXTRA
11577 "path_extra",
11578#endif
11579#ifdef FEAT_PERL
11580#ifndef DYNAMIC_PERL
11581 "perl",
11582#endif
11583#endif
11584#ifdef FEAT_PYTHON
11585#ifndef DYNAMIC_PYTHON
11586 "python",
11587#endif
11588#endif
11589#ifdef FEAT_POSTSCRIPT
11590 "postscript",
11591#endif
11592#ifdef FEAT_PRINTER
11593 "printer",
11594#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011595#ifdef FEAT_PROFILE
11596 "profile",
11597#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011598#ifdef FEAT_RELTIME
11599 "reltime",
11600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601#ifdef FEAT_QUICKFIX
11602 "quickfix",
11603#endif
11604#ifdef FEAT_RIGHTLEFT
11605 "rightleft",
11606#endif
11607#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11608 "ruby",
11609#endif
11610#ifdef FEAT_SCROLLBIND
11611 "scrollbind",
11612#endif
11613#ifdef FEAT_CMDL_INFO
11614 "showcmd",
11615 "cmdline_info",
11616#endif
11617#ifdef FEAT_SIGNS
11618 "signs",
11619#endif
11620#ifdef FEAT_SMARTINDENT
11621 "smartindent",
11622#endif
11623#ifdef FEAT_SNIFF
11624 "sniff",
11625#endif
11626#ifdef FEAT_STL_OPT
11627 "statusline",
11628#endif
11629#ifdef FEAT_SUN_WORKSHOP
11630 "sun_workshop",
11631#endif
11632#ifdef FEAT_NETBEANS_INTG
11633 "netbeans_intg",
11634#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011635#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011636 "spell",
11637#endif
11638#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 "syntax",
11640#endif
11641#if defined(USE_SYSTEM) || !defined(UNIX)
11642 "system",
11643#endif
11644#ifdef FEAT_TAG_BINS
11645 "tag_binary",
11646#endif
11647#ifdef FEAT_TAG_OLDSTATIC
11648 "tag_old_static",
11649#endif
11650#ifdef FEAT_TAG_ANYWHITE
11651 "tag_any_white",
11652#endif
11653#ifdef FEAT_TCL
11654# ifndef DYNAMIC_TCL
11655 "tcl",
11656# endif
11657#endif
11658#ifdef TERMINFO
11659 "terminfo",
11660#endif
11661#ifdef FEAT_TERMRESPONSE
11662 "termresponse",
11663#endif
11664#ifdef FEAT_TEXTOBJ
11665 "textobjects",
11666#endif
11667#ifdef HAVE_TGETENT
11668 "tgetent",
11669#endif
11670#ifdef FEAT_TITLE
11671 "title",
11672#endif
11673#ifdef FEAT_TOOLBAR
11674 "toolbar",
11675#endif
11676#ifdef FEAT_USR_CMDS
11677 "user-commands", /* was accidentally included in 5.4 */
11678 "user_commands",
11679#endif
11680#ifdef FEAT_VIMINFO
11681 "viminfo",
11682#endif
11683#ifdef FEAT_VERTSPLIT
11684 "vertsplit",
11685#endif
11686#ifdef FEAT_VIRTUALEDIT
11687 "virtualedit",
11688#endif
11689#ifdef FEAT_VISUAL
11690 "visual",
11691#endif
11692#ifdef FEAT_VISUALEXTRA
11693 "visualextra",
11694#endif
11695#ifdef FEAT_VREPLACE
11696 "vreplace",
11697#endif
11698#ifdef FEAT_WILDIGN
11699 "wildignore",
11700#endif
11701#ifdef FEAT_WILDMENU
11702 "wildmenu",
11703#endif
11704#ifdef FEAT_WINDOWS
11705 "windows",
11706#endif
11707#ifdef FEAT_WAK
11708 "winaltkeys",
11709#endif
11710#ifdef FEAT_WRITEBACKUP
11711 "writebackup",
11712#endif
11713#ifdef FEAT_XIM
11714 "xim",
11715#endif
11716#ifdef FEAT_XFONTSET
11717 "xfontset",
11718#endif
11719#ifdef USE_XSMP
11720 "xsmp",
11721#endif
11722#ifdef USE_XSMP_INTERACT
11723 "xsmp_interact",
11724#endif
11725#ifdef FEAT_XCLIPBOARD
11726 "xterm_clipboard",
11727#endif
11728#ifdef FEAT_XTERM_SAVE
11729 "xterm_save",
11730#endif
11731#if defined(UNIX) && defined(FEAT_X11)
11732 "X11",
11733#endif
11734 NULL
11735 };
11736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 for (i = 0; has_list[i] != NULL; ++i)
11739 if (STRICMP(name, has_list[i]) == 0)
11740 {
11741 n = TRUE;
11742 break;
11743 }
11744
11745 if (n == FALSE)
11746 {
11747 if (STRNICMP(name, "patch", 5) == 0)
11748 n = has_patch(atoi((char *)name + 5));
11749 else if (STRICMP(name, "vim_starting") == 0)
11750 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011751#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11752 else if (STRICMP(name, "balloon_multiline") == 0)
11753 n = multiline_balloon_available();
11754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755#ifdef DYNAMIC_TCL
11756 else if (STRICMP(name, "tcl") == 0)
11757 n = tcl_enabled(FALSE);
11758#endif
11759#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11760 else if (STRICMP(name, "iconv") == 0)
11761 n = iconv_enabled(FALSE);
11762#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011763#ifdef DYNAMIC_MZSCHEME
11764 else if (STRICMP(name, "mzscheme") == 0)
11765 n = mzscheme_enabled(FALSE);
11766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767#ifdef DYNAMIC_RUBY
11768 else if (STRICMP(name, "ruby") == 0)
11769 n = ruby_enabled(FALSE);
11770#endif
11771#ifdef DYNAMIC_PYTHON
11772 else if (STRICMP(name, "python") == 0)
11773 n = python_enabled(FALSE);
11774#endif
11775#ifdef DYNAMIC_PERL
11776 else if (STRICMP(name, "perl") == 0)
11777 n = perl_enabled(FALSE);
11778#endif
11779#ifdef FEAT_GUI
11780 else if (STRICMP(name, "gui_running") == 0)
11781 n = (gui.in_use || gui.starting);
11782# ifdef FEAT_GUI_W32
11783 else if (STRICMP(name, "gui_win32s") == 0)
11784 n = gui_is_win32s();
11785# endif
11786# ifdef FEAT_BROWSE
11787 else if (STRICMP(name, "browse") == 0)
11788 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11789# endif
11790#endif
11791#ifdef FEAT_SYN_HL
11792 else if (STRICMP(name, "syntax_items") == 0)
11793 n = syntax_present(curbuf);
11794#endif
11795#if defined(WIN3264)
11796 else if (STRICMP(name, "win95") == 0)
11797 n = mch_windows95();
11798#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011799#ifdef FEAT_NETBEANS_INTG
11800 else if (STRICMP(name, "netbeans_enabled") == 0)
11801 n = usingNetbeans;
11802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 }
11804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806}
11807
11808/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011809 * "has_key()" function
11810 */
11811 static void
11812f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011813 typval_T *argvars;
11814 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011815{
11816 rettv->vval.v_number = 0;
11817 if (argvars[0].v_type != VAR_DICT)
11818 {
11819 EMSG(_(e_dictreq));
11820 return;
11821 }
11822 if (argvars[0].vval.v_dict == NULL)
11823 return;
11824
11825 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011826 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011827}
11828
11829/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011830 * "haslocaldir()" function
11831 */
11832/*ARGSUSED*/
11833 static void
11834f_haslocaldir(argvars, rettv)
11835 typval_T *argvars;
11836 typval_T *rettv;
11837{
11838 rettv->vval.v_number = (curwin->w_localdir != NULL);
11839}
11840
11841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 * "hasmapto()" function
11843 */
11844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 typval_T *argvars;
11847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
11849 char_u *name;
11850 char_u *mode;
11851 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011852 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 mode = (char_u *)"nvo";
11857 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011860 if (argvars[2].v_type != VAR_UNKNOWN)
11861 abbr = get_tv_number(&argvars[2]);
11862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863
Bram Moolenaar2c932302006-03-18 21:42:09 +000011864 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868}
11869
11870/*
11871 * "histadd()" function
11872 */
11873/*ARGSUSED*/
11874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 typval_T *argvars;
11877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878{
11879#ifdef FEAT_CMDHIST
11880 int histype;
11881 char_u *str;
11882 char_u buf[NUMBUFLEN];
11883#endif
11884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 if (check_restricted() || check_secure())
11887 return;
11888#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11890 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 if (histype >= 0)
11892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 if (*str != NUL)
11895 {
11896 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 return;
11899 }
11900 }
11901#endif
11902}
11903
11904/*
11905 * "histdel()" function
11906 */
11907/*ARGSUSED*/
11908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909f_histdel(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 n;
11915 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11919 if (str == NULL)
11920 n = 0;
11921 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011923 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011924 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011926 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 else
11929 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 get_tv_string_buf(&argvars[1], buf));
11932 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935#endif
11936}
11937
11938/*
11939 * "histget()" function
11940 */
11941/*ARGSUSED*/
11942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943f_histget(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 type;
11949 int idx;
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 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956 {
11957 type = get_histtype(str);
11958 if (argvars[1].v_type == VAR_UNKNOWN)
11959 idx = get_history_idx(type);
11960 else
11961 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11962 /* -1 on type error */
11963 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969}
11970
11971/*
11972 * "histnr()" function
11973 */
11974/*ARGSUSED*/
11975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011977 typval_T *argvars;
11978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979{
11980 int i;
11981
11982#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011983 char_u *history = get_tv_string_chk(&argvars[0]);
11984
11985 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986 if (i >= HIST_CMD && i < HIST_COUNT)
11987 i = get_history_idx(i);
11988 else
11989#endif
11990 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992}
11993
11994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 * "highlightID(name)" function
11996 */
11997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *argvars;
12000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003}
12004
12005/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012006 * "highlight_exists()" function
12007 */
12008 static void
12009f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012010 typval_T *argvars;
12011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012012{
12013 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12014}
12015
12016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 * "hostname()" function
12018 */
12019/*ARGSUSED*/
12020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 typval_T *argvars;
12023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024{
12025 char_u hostname[256];
12026
12027 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->v_type = VAR_STRING;
12029 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030}
12031
12032/*
12033 * iconv() function
12034 */
12035/*ARGSUSED*/
12036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T *argvars;
12039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040{
12041#ifdef FEAT_MBYTE
12042 char_u buf1[NUMBUFLEN];
12043 char_u buf2[NUMBUFLEN];
12044 char_u *from, *to, *str;
12045 vimconv_T vimconv;
12046#endif
12047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 rettv->v_type = VAR_STRING;
12049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050
12051#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 str = get_tv_string(&argvars[0]);
12053 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12054 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 vimconv.vc_type = CONV_NONE;
12056 convert_setup(&vimconv, from, to);
12057
12058 /* If the encodings are equal, no conversion needed. */
12059 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063
12064 convert_setup(&vimconv, NULL, NULL);
12065 vim_free(from);
12066 vim_free(to);
12067#endif
12068}
12069
12070/*
12071 * "indent()" function
12072 */
12073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012075 typval_T *argvars;
12076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077{
12078 linenr_T lnum;
12079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085}
12086
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012087/*
12088 * "index()" function
12089 */
12090 static void
12091f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012092 typval_T *argvars;
12093 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012094{
Bram Moolenaar33570922005-01-25 22:26:29 +000012095 list_T *l;
12096 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012097 long idx = 0;
12098 int ic = FALSE;
12099
12100 rettv->vval.v_number = -1;
12101 if (argvars[0].v_type != VAR_LIST)
12102 {
12103 EMSG(_(e_listreq));
12104 return;
12105 }
12106 l = argvars[0].vval.v_list;
12107 if (l != NULL)
12108 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012109 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012110 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012112 int error = FALSE;
12113
Bram Moolenaar758711c2005-02-02 23:11:38 +000012114 /* Start at specified item. Use the cached index that list_find()
12115 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012116 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012117 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012118 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012119 ic = get_tv_number_chk(&argvars[3], &error);
12120 if (error)
12121 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012122 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012123
Bram Moolenaar758711c2005-02-02 23:11:38 +000012124 for ( ; item != NULL; item = item->li_next, ++idx)
12125 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012126 {
12127 rettv->vval.v_number = idx;
12128 break;
12129 }
12130 }
12131}
12132
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133static int inputsecret_flag = 0;
12134
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012135static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12136
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012138 * This function is used by f_input() and f_inputdialog() functions. The third
12139 * argument to f_input() specifies the type of completion to use at the
12140 * prompt. The third argument to f_inputdialog() specifies the value to return
12141 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 */
12143 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012144get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012145 typval_T *argvars;
12146 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012147 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012149 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 char_u *p = NULL;
12151 int c;
12152 char_u buf[NUMBUFLEN];
12153 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012154 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012155 int xp_type = EXPAND_NOTHING;
12156 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012159 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160
12161#ifdef NO_CONSOLE_INPUT
12162 /* While starting up, there is no place to enter text. */
12163 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165#endif
12166
12167 cmd_silent = FALSE; /* Want to see the prompt. */
12168 if (prompt != NULL)
12169 {
12170 /* Only the part of the message after the last NL is considered as
12171 * prompt for the command line */
12172 p = vim_strrchr(prompt, '\n');
12173 if (p == NULL)
12174 p = prompt;
12175 else
12176 {
12177 ++p;
12178 c = *p;
12179 *p = NUL;
12180 msg_start();
12181 msg_clr_eos();
12182 msg_puts_attr(prompt, echo_attr);
12183 msg_didout = FALSE;
12184 msg_starthere();
12185 *p = c;
12186 }
12187 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189 if (argvars[1].v_type != VAR_UNKNOWN)
12190 {
12191 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12192 if (defstr != NULL)
12193 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012195 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012196 {
12197 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012198 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012199 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012200
Bram Moolenaar4463f292005-09-25 22:20:24 +000012201 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012202
Bram Moolenaar4463f292005-09-25 22:20:24 +000012203 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12204 if (xp_name == NULL)
12205 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012206
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012207 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012208
Bram Moolenaar4463f292005-09-25 22:20:24 +000012209 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12210 &xp_arg) == FAIL)
12211 return;
12212 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012213 }
12214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012215 if (defstr != NULL)
12216 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012217 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12218 xp_type, xp_arg);
12219
12220 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012222 /* since the user typed this, no need to wait for return */
12223 need_wait_return = FALSE;
12224 msg_didout = FALSE;
12225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 cmd_silent = cmd_silent_save;
12227}
12228
12229/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012230 * "input()" function
12231 * Also handles inputsecret() when inputsecret is set.
12232 */
12233 static void
12234f_input(argvars, rettv)
12235 typval_T *argvars;
12236 typval_T *rettv;
12237{
12238 get_user_input(argvars, rettv, FALSE);
12239}
12240
12241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 * "inputdialog()" function
12243 */
12244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012246 typval_T *argvars;
12247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248{
12249#if defined(FEAT_GUI_TEXTDIALOG)
12250 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12251 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12252 {
12253 char_u *message;
12254 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012257 message = get_tv_string_chk(&argvars[0]);
12258 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012259 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012260 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 else
12262 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012263 if (message != NULL && defstr != NULL
12264 && do_dialog(VIM_QUESTION, NULL, message,
12265 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 else
12268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 if (message != NULL && defstr != NULL
12270 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012271 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->vval.v_string = vim_strsave(
12273 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012277 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 }
12279 else
12280#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012281 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282}
12283
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012284/*
12285 * "inputlist()" function
12286 */
12287 static void
12288f_inputlist(argvars, rettv)
12289 typval_T *argvars;
12290 typval_T *rettv;
12291{
12292 listitem_T *li;
12293 int selected;
12294 int mouse_used;
12295
12296 rettv->vval.v_number = 0;
12297#ifdef NO_CONSOLE_INPUT
12298 /* While starting up, there is no place to enter text. */
12299 if (no_console_input())
12300 return;
12301#endif
12302 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12303 {
12304 EMSG2(_(e_listarg), "inputlist()");
12305 return;
12306 }
12307
12308 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012309 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012310 lines_left = Rows; /* avoid more prompt */
12311 msg_scroll = TRUE;
12312 msg_clr_eos();
12313
12314 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12315 {
12316 msg_puts(get_tv_string(&li->li_tv));
12317 msg_putchar('\n');
12318 }
12319
12320 /* Ask for choice. */
12321 selected = prompt_for_number(&mouse_used);
12322 if (mouse_used)
12323 selected -= lines_left;
12324
12325 rettv->vval.v_number = selected;
12326}
12327
12328
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12330
12331/*
12332 * "inputrestore()" function
12333 */
12334/*ARGSUSED*/
12335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012337 typval_T *argvars;
12338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339{
12340 if (ga_userinput.ga_len > 0)
12341 {
12342 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12344 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 }
12347 else if (p_verbose > 1)
12348 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012349 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 }
12352}
12353
12354/*
12355 * "inputsave()" function
12356 */
12357/*ARGSUSED*/
12358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012360 typval_T *argvars;
12361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012363 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 if (ga_grow(&ga_userinput, 1) == OK)
12365 {
12366 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12367 + ga_userinput.ga_len);
12368 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 }
12371 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373}
12374
12375/*
12376 * "inputsecret()" function
12377 */
12378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012380 typval_T *argvars;
12381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382{
12383 ++cmdline_star;
12384 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 --cmdline_star;
12387 --inputsecret_flag;
12388}
12389
12390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012391 * "insert()" function
12392 */
12393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012395 typval_T *argvars;
12396 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012397{
12398 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 listitem_T *item;
12400 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012402
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012403 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012404 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012406 else if ((l = argvars[0].vval.v_list) != NULL
12407 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012408 {
12409 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 before = get_tv_number_chk(&argvars[2], &error);
12411 if (error)
12412 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012413
Bram Moolenaar758711c2005-02-02 23:11:38 +000012414 if (before == l->lv_len)
12415 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012416 else
12417 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012418 item = list_find(l, before);
12419 if (item == NULL)
12420 {
12421 EMSGN(_(e_listidx), before);
12422 l = NULL;
12423 }
12424 }
12425 if (l != NULL)
12426 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012427 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012428 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012429 }
12430 }
12431}
12432
12433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 * "isdirectory()" function
12435 */
12436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012438 typval_T *argvars;
12439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442}
12443
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012444/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012445 * "islocked()" function
12446 */
12447 static void
12448f_islocked(argvars, rettv)
12449 typval_T *argvars;
12450 typval_T *rettv;
12451{
12452 lval_T lv;
12453 char_u *end;
12454 dictitem_T *di;
12455
12456 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012457 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12458 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012459 if (end != NULL && lv.ll_name != NULL)
12460 {
12461 if (*end != NUL)
12462 EMSG(_(e_trailing));
12463 else
12464 {
12465 if (lv.ll_tv == NULL)
12466 {
12467 if (check_changedtick(lv.ll_name))
12468 rettv->vval.v_number = 1; /* always locked */
12469 else
12470 {
12471 di = find_var(lv.ll_name, NULL);
12472 if (di != NULL)
12473 {
12474 /* Consider a variable locked when:
12475 * 1. the variable itself is locked
12476 * 2. the value of the variable is locked.
12477 * 3. the List or Dict value is locked.
12478 */
12479 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12480 || tv_islocked(&di->di_tv));
12481 }
12482 }
12483 }
12484 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012485 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012486 else if (lv.ll_newkey != NULL)
12487 EMSG2(_(e_dictkey), lv.ll_newkey);
12488 else if (lv.ll_list != NULL)
12489 /* List item. */
12490 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12491 else
12492 /* Dictionary item. */
12493 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12494 }
12495 }
12496
12497 clear_lval(&lv);
12498}
12499
Bram Moolenaar33570922005-01-25 22:26:29 +000012500static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012501
12502/*
12503 * Turn a dict into a list:
12504 * "what" == 0: list of keys
12505 * "what" == 1: list of values
12506 * "what" == 2: list of items
12507 */
12508 static void
12509dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 typval_T *argvars;
12511 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012512 int what;
12513{
Bram Moolenaar33570922005-01-25 22:26:29 +000012514 list_T *l2;
12515 dictitem_T *di;
12516 hashitem_T *hi;
12517 listitem_T *li;
12518 listitem_T *li2;
12519 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012520 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012521
12522 rettv->vval.v_number = 0;
12523 if (argvars[0].v_type != VAR_DICT)
12524 {
12525 EMSG(_(e_dictreq));
12526 return;
12527 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012528 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012529 return;
12530
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012531 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012532 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012533
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012534 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012536 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012537 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012539 --todo;
12540 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012541
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012542 li = listitem_alloc();
12543 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012544 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012545 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012546
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012547 if (what == 0)
12548 {
12549 /* keys() */
12550 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012551 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012552 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12553 }
12554 else if (what == 1)
12555 {
12556 /* values() */
12557 copy_tv(&di->di_tv, &li->li_tv);
12558 }
12559 else
12560 {
12561 /* items() */
12562 l2 = list_alloc();
12563 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012564 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012565 li->li_tv.vval.v_list = l2;
12566 if (l2 == NULL)
12567 break;
12568 ++l2->lv_refcount;
12569
12570 li2 = listitem_alloc();
12571 if (li2 == NULL)
12572 break;
12573 list_append(l2, li2);
12574 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012575 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012576 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12577
12578 li2 = listitem_alloc();
12579 if (li2 == NULL)
12580 break;
12581 list_append(l2, li2);
12582 copy_tv(&di->di_tv, &li2->li_tv);
12583 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012584 }
12585 }
12586}
12587
12588/*
12589 * "items(dict)" function
12590 */
12591 static void
12592f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 typval_T *argvars;
12594 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012595{
12596 dict_list(argvars, rettv, 2);
12597}
12598
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012600 * "join()" function
12601 */
12602 static void
12603f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012604 typval_T *argvars;
12605 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012606{
12607 garray_T ga;
12608 char_u *sep;
12609
12610 rettv->vval.v_number = 0;
12611 if (argvars[0].v_type != VAR_LIST)
12612 {
12613 EMSG(_(e_listreq));
12614 return;
12615 }
12616 if (argvars[0].vval.v_list == NULL)
12617 return;
12618 if (argvars[1].v_type == VAR_UNKNOWN)
12619 sep = (char_u *)" ";
12620 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012621 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012622
12623 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012624
12625 if (sep != NULL)
12626 {
12627 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012628 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012629 ga_append(&ga, NUL);
12630 rettv->vval.v_string = (char_u *)ga.ga_data;
12631 }
12632 else
12633 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012634}
12635
12636/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012637 * "keys()" function
12638 */
12639 static void
12640f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012641 typval_T *argvars;
12642 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012643{
12644 dict_list(argvars, rettv, 0);
12645}
12646
12647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 * "last_buffer_nr()" function.
12649 */
12650/*ARGSUSED*/
12651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012653 typval_T *argvars;
12654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655{
12656 int n = 0;
12657 buf_T *buf;
12658
12659 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12660 if (n < buf->b_fnum)
12661 n = buf->b_fnum;
12662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012664}
12665
12666/*
12667 * "len()" function
12668 */
12669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012671 typval_T *argvars;
12672 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012673{
12674 switch (argvars[0].v_type)
12675 {
12676 case VAR_STRING:
12677 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->vval.v_number = (varnumber_T)STRLEN(
12679 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012680 break;
12681 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012683 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012684 case VAR_DICT:
12685 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12686 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012687 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012688 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012689 break;
12690 }
12691}
12692
Bram Moolenaar33570922005-01-25 22:26:29 +000012693static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012694
12695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 typval_T *argvars;
12698 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012699 int type;
12700{
12701#ifdef FEAT_LIBCALL
12702 char_u *string_in;
12703 char_u **string_result;
12704 int nr_result;
12705#endif
12706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012708 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012712
12713 if (check_restricted() || check_secure())
12714 return;
12715
12716#ifdef FEAT_LIBCALL
12717 /* The first two args must be strings, otherwise its meaningless */
12718 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12719 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012720 string_in = NULL;
12721 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012722 string_in = argvars[2].vval.v_string;
12723 if (type == VAR_NUMBER)
12724 string_result = NULL;
12725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012727 if (mch_libcall(argvars[0].vval.v_string,
12728 argvars[1].vval.v_string,
12729 string_in,
12730 argvars[2].vval.v_number,
12731 string_result,
12732 &nr_result) == OK
12733 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012735 }
12736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737}
12738
12739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012740 * "libcall()" function
12741 */
12742 static void
12743f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012744 typval_T *argvars;
12745 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012746{
12747 libcall_common(argvars, rettv, VAR_STRING);
12748}
12749
12750/*
12751 * "libcallnr()" function
12752 */
12753 static void
12754f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012755 typval_T *argvars;
12756 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012757{
12758 libcall_common(argvars, rettv, VAR_NUMBER);
12759}
12760
12761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 * "line(string)" function
12763 */
12764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 typval_T *argvars;
12767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768{
12769 linenr_T lnum = 0;
12770 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012771 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012773 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 if (fp != NULL)
12775 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777}
12778
12779/*
12780 * "line2byte(lnum)" function
12781 */
12782/*ARGSUSED*/
12783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012785 typval_T *argvars;
12786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787{
12788#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790#else
12791 linenr_T lnum;
12792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12798 if (rettv->vval.v_number >= 0)
12799 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800#endif
12801}
12802
12803/*
12804 * "lispindent(lnum)" function
12805 */
12806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012808 typval_T *argvars;
12809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
12811#ifdef FEAT_LISP
12812 pos_T pos;
12813 linenr_T lnum;
12814
12815 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12818 {
12819 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 curwin->w_cursor = pos;
12822 }
12823 else
12824#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826}
12827
12828/*
12829 * "localtime()" function
12830 */
12831/*ARGSUSED*/
12832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012834 typval_T *argvars;
12835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838}
12839
Bram Moolenaar33570922005-01-25 22:26:29 +000012840static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841
12842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012844 typval_T *argvars;
12845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 int exact;
12847{
12848 char_u *keys;
12849 char_u *which;
12850 char_u buf[NUMBUFLEN];
12851 char_u *keys_buf = NULL;
12852 char_u *rhs;
12853 int mode;
12854 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012855 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856
12857 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858 rettv->v_type = VAR_STRING;
12859 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 if (*keys == NUL)
12863 return;
12864
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012865 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012867 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012868 if (argvars[2].v_type != VAR_UNKNOWN)
12869 abbr = get_tv_number(&argvars[2]);
12870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 else
12872 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012873 if (which == NULL)
12874 return;
12875
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 mode = get_map_mode(&which, 0);
12877
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012878 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012879 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 vim_free(keys_buf);
12881 if (rhs != NULL)
12882 {
12883 ga_init(&ga);
12884 ga.ga_itemsize = 1;
12885 ga.ga_growsize = 40;
12886
12887 while (*rhs != NUL)
12888 ga_concat(&ga, str2special(&rhs, FALSE));
12889
12890 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 }
12893}
12894
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012895#ifdef FEAT_FLOAT
12896/*
12897 * "log10()" function
12898 */
12899 static void
12900f_log10(argvars, rettv)
12901 typval_T *argvars;
12902 typval_T *rettv;
12903{
12904 float_T f;
12905
12906 rettv->v_type = VAR_FLOAT;
12907 if (get_float_arg(argvars, &f) == OK)
12908 rettv->vval.v_float = log10(f);
12909 else
12910 rettv->vval.v_float = 0.0;
12911}
12912#endif
12913
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012915 * "map()" function
12916 */
12917 static void
12918f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012919 typval_T *argvars;
12920 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012921{
12922 filter_map(argvars, rettv, TRUE);
12923}
12924
12925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012926 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 */
12928 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012929f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012933 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934}
12935
12936/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012937 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 */
12939 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012940f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012941 typval_T *argvars;
12942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012944 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945}
12946
Bram Moolenaar33570922005-01-25 22:26:29 +000012947static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948
12949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012950find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012951 typval_T *argvars;
12952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 int type;
12954{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012955 char_u *str = NULL;
12956 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 char_u *pat;
12958 regmatch_T regmatch;
12959 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012960 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 char_u *save_cpo;
12962 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012963 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012964 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012965 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012966 list_T *l = NULL;
12967 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012968 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012969 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970
12971 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12972 save_cpo = p_cpo;
12973 p_cpo = (char_u *)"";
12974
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012975 rettv->vval.v_number = -1;
12976 if (type == 3)
12977 {
12978 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012979 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012980 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012981 }
12982 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984 rettv->v_type = VAR_STRING;
12985 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012988 if (argvars[0].v_type == VAR_LIST)
12989 {
12990 if ((l = argvars[0].vval.v_list) == NULL)
12991 goto theend;
12992 li = l->lv_first;
12993 }
12994 else
12995 expr = str = get_tv_string(&argvars[0]);
12996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012997 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12998 if (pat == NULL)
12999 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013000
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013003 int error = FALSE;
13004
13005 start = get_tv_number_chk(&argvars[2], &error);
13006 if (error)
13007 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013008 if (l != NULL)
13009 {
13010 li = list_find(l, start);
13011 if (li == NULL)
13012 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013013 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013014 }
13015 else
13016 {
13017 if (start < 0)
13018 start = 0;
13019 if (start > (long)STRLEN(str))
13020 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013021 /* When "count" argument is there ignore matches before "start",
13022 * otherwise skip part of the string. Differs when pattern is "^"
13023 * or "\<". */
13024 if (argvars[3].v_type != VAR_UNKNOWN)
13025 startcol = start;
13026 else
13027 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013028 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013029
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013030 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 nth = get_tv_number_chk(&argvars[3], &error);
13032 if (error)
13033 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 }
13035
13036 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13037 if (regmatch.regprog != NULL)
13038 {
13039 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013040
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013041 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013042 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013043 if (l != NULL)
13044 {
13045 if (li == NULL)
13046 {
13047 match = FALSE;
13048 break;
13049 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013050 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013051 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013052 if (str == NULL)
13053 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013054 }
13055
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013056 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013057
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013058 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013059 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 if (l == NULL && !match)
13061 break;
13062
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013063 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013064 if (l != NULL)
13065 {
13066 li = li->li_next;
13067 ++idx;
13068 }
13069 else
13070 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013071#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013072 startcol = (colnr_T)(regmatch.startp[0]
13073 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013074#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013075 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013076#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013077 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013078 }
13079
13080 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013082 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013084 int i;
13085
13086 /* return list with matched string and submatches */
13087 for (i = 0; i < NSUBEXP; ++i)
13088 {
13089 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013090 {
13091 if (list_append_string(rettv->vval.v_list,
13092 (char_u *)"", 0) == FAIL)
13093 break;
13094 }
13095 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013096 regmatch.startp[i],
13097 (int)(regmatch.endp[i] - regmatch.startp[i]))
13098 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013099 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013100 }
13101 }
13102 else if (type == 2)
13103 {
13104 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013105 if (l != NULL)
13106 copy_tv(&li->li_tv, rettv);
13107 else
13108 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013110 }
13111 else if (l != NULL)
13112 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 else
13114 {
13115 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013116 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117 (varnumber_T)(regmatch.startp[0] - str);
13118 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013119 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013121 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 }
13123 }
13124 vim_free(regmatch.regprog);
13125 }
13126
13127theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013128 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 p_cpo = save_cpo;
13130}
13131
13132/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013133 * "match()" function
13134 */
13135 static void
13136f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013137 typval_T *argvars;
13138 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139{
13140 find_some_match(argvars, rettv, 1);
13141}
13142
13143/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013144 * "matchadd()" function
13145 */
13146 static void
13147f_matchadd(argvars, rettv)
13148 typval_T *argvars;
13149 typval_T *rettv;
13150{
13151#ifdef FEAT_SEARCH_EXTRA
13152 char_u buf[NUMBUFLEN];
13153 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13154 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13155 int prio = 10; /* default priority */
13156 int id = -1;
13157 int error = FALSE;
13158
13159 rettv->vval.v_number = -1;
13160
13161 if (grp == NULL || pat == NULL)
13162 return;
13163 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013164 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013165 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013166 if (argvars[3].v_type != VAR_UNKNOWN)
13167 id = get_tv_number_chk(&argvars[3], &error);
13168 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013169 if (error == TRUE)
13170 return;
13171 if (id >= 1 && id <= 3)
13172 {
13173 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13174 return;
13175 }
13176
13177 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13178#endif
13179}
13180
13181/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013182 * "matcharg()" function
13183 */
13184 static void
13185f_matcharg(argvars, rettv)
13186 typval_T *argvars;
13187 typval_T *rettv;
13188{
13189 if (rettv_list_alloc(rettv) == OK)
13190 {
13191#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013192 int id = get_tv_number(&argvars[0]);
13193 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013194
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013195 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013196 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013197 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13198 {
13199 list_append_string(rettv->vval.v_list,
13200 syn_id2name(m->hlg_id), -1);
13201 list_append_string(rettv->vval.v_list, m->pattern, -1);
13202 }
13203 else
13204 {
13205 list_append_string(rettv->vval.v_list, NUL, -1);
13206 list_append_string(rettv->vval.v_list, NUL, -1);
13207 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013208 }
13209#endif
13210 }
13211}
13212
13213/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013214 * "matchdelete()" function
13215 */
13216 static void
13217f_matchdelete(argvars, rettv)
13218 typval_T *argvars;
13219 typval_T *rettv;
13220{
13221#ifdef FEAT_SEARCH_EXTRA
13222 rettv->vval.v_number = match_delete(curwin,
13223 (int)get_tv_number(&argvars[0]), TRUE);
13224#endif
13225}
13226
13227/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013228 * "matchend()" function
13229 */
13230 static void
13231f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 typval_T *argvars;
13233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013234{
13235 find_some_match(argvars, rettv, 0);
13236}
13237
13238/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013239 * "matchlist()" function
13240 */
13241 static void
13242f_matchlist(argvars, rettv)
13243 typval_T *argvars;
13244 typval_T *rettv;
13245{
13246 find_some_match(argvars, rettv, 3);
13247}
13248
13249/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013250 * "matchstr()" function
13251 */
13252 static void
13253f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013254 typval_T *argvars;
13255 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256{
13257 find_some_match(argvars, rettv, 2);
13258}
13259
Bram Moolenaar33570922005-01-25 22:26:29 +000013260static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013261
13262 static void
13263max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013264 typval_T *argvars;
13265 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013266 int domax;
13267{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013268 long n = 0;
13269 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013270 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013271
13272 if (argvars[0].v_type == VAR_LIST)
13273 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 list_T *l;
13275 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013276
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013277 l = argvars[0].vval.v_list;
13278 if (l != NULL)
13279 {
13280 li = l->lv_first;
13281 if (li != NULL)
13282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013284 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013285 {
13286 li = li->li_next;
13287 if (li == NULL)
13288 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013289 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013290 if (domax ? i > n : i < n)
13291 n = i;
13292 }
13293 }
13294 }
13295 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013296 else if (argvars[0].v_type == VAR_DICT)
13297 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013299 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013301 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013302
13303 d = argvars[0].vval.v_dict;
13304 if (d != NULL)
13305 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013306 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013307 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013309 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013311 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013312 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013313 if (first)
13314 {
13315 n = i;
13316 first = FALSE;
13317 }
13318 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013319 n = i;
13320 }
13321 }
13322 }
13323 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013324 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013325 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013327}
13328
13329/*
13330 * "max()" function
13331 */
13332 static void
13333f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 typval_T *argvars;
13335 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013336{
13337 max_min(argvars, rettv, TRUE);
13338}
13339
13340/*
13341 * "min()" function
13342 */
13343 static void
13344f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013347{
13348 max_min(argvars, rettv, FALSE);
13349}
13350
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013351static int mkdir_recurse __ARGS((char_u *dir, int prot));
13352
13353/*
13354 * Create the directory in which "dir" is located, and higher levels when
13355 * needed.
13356 */
13357 static int
13358mkdir_recurse(dir, prot)
13359 char_u *dir;
13360 int prot;
13361{
13362 char_u *p;
13363 char_u *updir;
13364 int r = FAIL;
13365
13366 /* Get end of directory name in "dir".
13367 * We're done when it's "/" or "c:/". */
13368 p = gettail_sep(dir);
13369 if (p <= get_past_head(dir))
13370 return OK;
13371
13372 /* If the directory exists we're done. Otherwise: create it.*/
13373 updir = vim_strnsave(dir, (int)(p - dir));
13374 if (updir == NULL)
13375 return FAIL;
13376 if (mch_isdir(updir))
13377 r = OK;
13378 else if (mkdir_recurse(updir, prot) == OK)
13379 r = vim_mkdir_emsg(updir, prot);
13380 vim_free(updir);
13381 return r;
13382}
13383
13384#ifdef vim_mkdir
13385/*
13386 * "mkdir()" function
13387 */
13388 static void
13389f_mkdir(argvars, rettv)
13390 typval_T *argvars;
13391 typval_T *rettv;
13392{
13393 char_u *dir;
13394 char_u buf[NUMBUFLEN];
13395 int prot = 0755;
13396
13397 rettv->vval.v_number = FAIL;
13398 if (check_restricted() || check_secure())
13399 return;
13400
13401 dir = get_tv_string_buf(&argvars[0], buf);
13402 if (argvars[1].v_type != VAR_UNKNOWN)
13403 {
13404 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 prot = get_tv_number_chk(&argvars[2], NULL);
13406 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013407 mkdir_recurse(dir, prot);
13408 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013410}
13411#endif
13412
Bram Moolenaar0d660222005-01-07 21:51:51 +000013413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 * "mode()" function
13415 */
13416/*ARGSUSED*/
13417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 typval_T *argvars;
13420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013422 char_u buf[3];
13423
13424 buf[1] = NUL;
13425 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426
13427#ifdef FEAT_VISUAL
13428 if (VIsual_active)
13429 {
13430 if (VIsual_select)
13431 buf[0] = VIsual_mode + 's' - 'v';
13432 else
13433 buf[0] = VIsual_mode;
13434 }
13435 else
13436#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013437 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13438 || State == CONFIRM)
13439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013441 if (State == ASKMORE)
13442 buf[1] = 'm';
13443 else if (State == CONFIRM)
13444 buf[1] = '?';
13445 }
13446 else if (State == EXTERNCMD)
13447 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 else if (State & INSERT)
13449 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013450#ifdef FEAT_VREPLACE
13451 if (State & VREPLACE_FLAG)
13452 {
13453 buf[0] = 'R';
13454 buf[1] = 'v';
13455 }
13456 else
13457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 if (State & REPLACE_FLAG)
13459 buf[0] = 'R';
13460 else
13461 buf[0] = 'i';
13462 }
13463 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013466 if (exmode_active)
13467 buf[1] = 'v';
13468 }
13469 else if (exmode_active)
13470 {
13471 buf[0] = 'c';
13472 buf[1] = 'e';
13473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013477 if (finish_op)
13478 buf[1] = 'o';
13479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013481 /* A zero number or empty string argument: return only major mode. */
13482 if (!(argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number != 0)
13483 && !(argvars[0].v_type == VAR_STRING
13484 && *get_tv_string(&argvars[0]) != NUL))
13485 buf[1] = NUL;
13486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487 rettv->vval.v_string = vim_strsave(buf);
13488 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489}
13490
13491/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492 * "nextnonblank()" function
13493 */
13494 static void
13495f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013496 typval_T *argvars;
13497 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013498{
13499 linenr_T lnum;
13500
13501 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013503 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013504 {
13505 lnum = 0;
13506 break;
13507 }
13508 if (*skipwhite(ml_get(lnum)) != NUL)
13509 break;
13510 }
13511 rettv->vval.v_number = lnum;
13512}
13513
13514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 * "nr2char()" function
13516 */
13517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013519 typval_T *argvars;
13520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521{
13522 char_u buf[NUMBUFLEN];
13523
13524#ifdef FEAT_MBYTE
13525 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 else
13528#endif
13529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 buf[1] = NUL;
13532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->v_type = VAR_STRING;
13534 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013535}
13536
13537/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013538 * "pathshorten()" function
13539 */
13540 static void
13541f_pathshorten(argvars, rettv)
13542 typval_T *argvars;
13543 typval_T *rettv;
13544{
13545 char_u *p;
13546
13547 rettv->v_type = VAR_STRING;
13548 p = get_tv_string_chk(&argvars[0]);
13549 if (p == NULL)
13550 rettv->vval.v_string = NULL;
13551 else
13552 {
13553 p = vim_strsave(p);
13554 rettv->vval.v_string = p;
13555 if (p != NULL)
13556 shorten_dir(p);
13557 }
13558}
13559
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013560#ifdef FEAT_FLOAT
13561/*
13562 * "pow()" function
13563 */
13564 static void
13565f_pow(argvars, rettv)
13566 typval_T *argvars;
13567 typval_T *rettv;
13568{
13569 float_T fx, fy;
13570
13571 rettv->v_type = VAR_FLOAT;
13572 if (get_float_arg(argvars, &fx) == OK
13573 && get_float_arg(&argvars[1], &fy) == OK)
13574 rettv->vval.v_float = pow(fx, fy);
13575 else
13576 rettv->vval.v_float = 0.0;
13577}
13578#endif
13579
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013580/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013581 * "prevnonblank()" function
13582 */
13583 static void
13584f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013585 typval_T *argvars;
13586 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013587{
13588 linenr_T lnum;
13589
13590 lnum = get_tv_lnum(argvars);
13591 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13592 lnum = 0;
13593 else
13594 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13595 --lnum;
13596 rettv->vval.v_number = lnum;
13597}
13598
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013599#ifdef HAVE_STDARG_H
13600/* This dummy va_list is here because:
13601 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13602 * - locally in the function results in a "used before set" warning
13603 * - using va_start() to initialize it gives "function with fixed args" error */
13604static va_list ap;
13605#endif
13606
Bram Moolenaar8c711452005-01-14 21:53:12 +000013607/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013608 * "printf()" function
13609 */
13610 static void
13611f_printf(argvars, rettv)
13612 typval_T *argvars;
13613 typval_T *rettv;
13614{
13615 rettv->v_type = VAR_STRING;
13616 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013617#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013618 {
13619 char_u buf[NUMBUFLEN];
13620 int len;
13621 char_u *s;
13622 int saved_did_emsg = did_emsg;
13623 char *fmt;
13624
13625 /* Get the required length, allocate the buffer and do it for real. */
13626 did_emsg = FALSE;
13627 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013628 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013629 if (!did_emsg)
13630 {
13631 s = alloc(len + 1);
13632 if (s != NULL)
13633 {
13634 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013635 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013636 }
13637 }
13638 did_emsg |= saved_did_emsg;
13639 }
13640#endif
13641}
13642
13643/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013644 * "pumvisible()" function
13645 */
13646/*ARGSUSED*/
13647 static void
13648f_pumvisible(argvars, rettv)
13649 typval_T *argvars;
13650 typval_T *rettv;
13651{
13652 rettv->vval.v_number = 0;
13653#ifdef FEAT_INS_EXPAND
13654 if (pum_visible())
13655 rettv->vval.v_number = 1;
13656#endif
13657}
13658
13659/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013660 * "range()" function
13661 */
13662 static void
13663f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013666{
13667 long start;
13668 long end;
13669 long stride = 1;
13670 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013673 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013674 if (argvars[1].v_type == VAR_UNKNOWN)
13675 {
13676 end = start - 1;
13677 start = 0;
13678 }
13679 else
13680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013681 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013682 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013684 }
13685
13686 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 if (error)
13688 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013689 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013690 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013691 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013692 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013693 else
13694 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013695 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013696 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013697 if (list_append_number(rettv->vval.v_list,
13698 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013699 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013700 }
13701}
13702
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013703/*
13704 * "readfile()" function
13705 */
13706 static void
13707f_readfile(argvars, rettv)
13708 typval_T *argvars;
13709 typval_T *rettv;
13710{
13711 int binary = FALSE;
13712 char_u *fname;
13713 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013714 listitem_T *li;
13715#define FREAD_SIZE 200 /* optimized for text lines */
13716 char_u buf[FREAD_SIZE];
13717 int readlen; /* size of last fread() */
13718 int buflen; /* nr of valid chars in buf[] */
13719 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13720 int tolist; /* first byte in buf[] still to be put in list */
13721 int chop; /* how many CR to chop off */
13722 char_u *prev = NULL; /* previously read bytes, if any */
13723 int prevlen = 0; /* length of "prev" if not NULL */
13724 char_u *s;
13725 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013726 long maxline = MAXLNUM;
13727 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013728
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013729 if (argvars[1].v_type != VAR_UNKNOWN)
13730 {
13731 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13732 binary = TRUE;
13733 if (argvars[2].v_type != VAR_UNKNOWN)
13734 maxline = get_tv_number(&argvars[2]);
13735 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013736
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013737 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013738 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013739
13740 /* Always open the file in binary mode, library functions have a mind of
13741 * their own about CR-LF conversion. */
13742 fname = get_tv_string(&argvars[0]);
13743 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13744 {
13745 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13746 return;
13747 }
13748
13749 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013750 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013751 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013752 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013753 buflen = filtd + readlen;
13754 tolist = 0;
13755 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13756 {
13757 if (buf[filtd] == '\n' || readlen <= 0)
13758 {
13759 /* Only when in binary mode add an empty list item when the
13760 * last line ends in a '\n'. */
13761 if (!binary && readlen == 0 && filtd == 0)
13762 break;
13763
13764 /* Found end-of-line or end-of-file: add a text line to the
13765 * list. */
13766 chop = 0;
13767 if (!binary)
13768 while (filtd - chop - 1 >= tolist
13769 && buf[filtd - chop - 1] == '\r')
13770 ++chop;
13771 len = filtd - tolist - chop;
13772 if (prev == NULL)
13773 s = vim_strnsave(buf + tolist, len);
13774 else
13775 {
13776 s = alloc((unsigned)(prevlen + len + 1));
13777 if (s != NULL)
13778 {
13779 mch_memmove(s, prev, prevlen);
13780 vim_free(prev);
13781 prev = NULL;
13782 mch_memmove(s + prevlen, buf + tolist, len);
13783 s[prevlen + len] = NUL;
13784 }
13785 }
13786 tolist = filtd + 1;
13787
13788 li = listitem_alloc();
13789 if (li == NULL)
13790 {
13791 vim_free(s);
13792 break;
13793 }
13794 li->li_tv.v_type = VAR_STRING;
13795 li->li_tv.v_lock = 0;
13796 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013797 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013798
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013799 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013800 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013801 if (readlen <= 0)
13802 break;
13803 }
13804 else if (buf[filtd] == NUL)
13805 buf[filtd] = '\n';
13806 }
13807 if (readlen <= 0)
13808 break;
13809
13810 if (tolist == 0)
13811 {
13812 /* "buf" is full, need to move text to an allocated buffer */
13813 if (prev == NULL)
13814 {
13815 prev = vim_strnsave(buf, buflen);
13816 prevlen = buflen;
13817 }
13818 else
13819 {
13820 s = alloc((unsigned)(prevlen + buflen));
13821 if (s != NULL)
13822 {
13823 mch_memmove(s, prev, prevlen);
13824 mch_memmove(s + prevlen, buf, buflen);
13825 vim_free(prev);
13826 prev = s;
13827 prevlen += buflen;
13828 }
13829 }
13830 filtd = 0;
13831 }
13832 else
13833 {
13834 mch_memmove(buf, buf + tolist, buflen - tolist);
13835 filtd -= tolist;
13836 }
13837 }
13838
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013839 /*
13840 * For a negative line count use only the lines at the end of the file,
13841 * free the rest.
13842 */
13843 if (maxline < 0)
13844 while (cnt > -maxline)
13845 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013846 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013847 --cnt;
13848 }
13849
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013850 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013851 fclose(fd);
13852}
13853
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013854#if defined(FEAT_RELTIME)
13855static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13856
13857/*
13858 * Convert a List to proftime_T.
13859 * Return FAIL when there is something wrong.
13860 */
13861 static int
13862list2proftime(arg, tm)
13863 typval_T *arg;
13864 proftime_T *tm;
13865{
13866 long n1, n2;
13867 int error = FALSE;
13868
13869 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13870 || arg->vval.v_list->lv_len != 2)
13871 return FAIL;
13872 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13873 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13874# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013875 tm->HighPart = n1;
13876 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013877# else
13878 tm->tv_sec = n1;
13879 tm->tv_usec = n2;
13880# endif
13881 return error ? FAIL : OK;
13882}
13883#endif /* FEAT_RELTIME */
13884
13885/*
13886 * "reltime()" function
13887 */
13888 static void
13889f_reltime(argvars, rettv)
13890 typval_T *argvars;
13891 typval_T *rettv;
13892{
13893#ifdef FEAT_RELTIME
13894 proftime_T res;
13895 proftime_T start;
13896
13897 if (argvars[0].v_type == VAR_UNKNOWN)
13898 {
13899 /* No arguments: get current time. */
13900 profile_start(&res);
13901 }
13902 else if (argvars[1].v_type == VAR_UNKNOWN)
13903 {
13904 if (list2proftime(&argvars[0], &res) == FAIL)
13905 return;
13906 profile_end(&res);
13907 }
13908 else
13909 {
13910 /* Two arguments: compute the difference. */
13911 if (list2proftime(&argvars[0], &start) == FAIL
13912 || list2proftime(&argvars[1], &res) == FAIL)
13913 return;
13914 profile_sub(&res, &start);
13915 }
13916
13917 if (rettv_list_alloc(rettv) == OK)
13918 {
13919 long n1, n2;
13920
13921# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013922 n1 = res.HighPart;
13923 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013924# else
13925 n1 = res.tv_sec;
13926 n2 = res.tv_usec;
13927# endif
13928 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13929 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13930 }
13931#endif
13932}
13933
13934/*
13935 * "reltimestr()" function
13936 */
13937 static void
13938f_reltimestr(argvars, rettv)
13939 typval_T *argvars;
13940 typval_T *rettv;
13941{
13942#ifdef FEAT_RELTIME
13943 proftime_T tm;
13944#endif
13945
13946 rettv->v_type = VAR_STRING;
13947 rettv->vval.v_string = NULL;
13948#ifdef FEAT_RELTIME
13949 if (list2proftime(&argvars[0], &tm) == OK)
13950 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13951#endif
13952}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013953
Bram Moolenaar0d660222005-01-07 21:51:51 +000013954#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13955static void make_connection __ARGS((void));
13956static int check_connection __ARGS((void));
13957
13958 static void
13959make_connection()
13960{
13961 if (X_DISPLAY == NULL
13962# ifdef FEAT_GUI
13963 && !gui.in_use
13964# endif
13965 )
13966 {
13967 x_force_connect = TRUE;
13968 setup_term_clip();
13969 x_force_connect = FALSE;
13970 }
13971}
13972
13973 static int
13974check_connection()
13975{
13976 make_connection();
13977 if (X_DISPLAY == NULL)
13978 {
13979 EMSG(_("E240: No connection to Vim server"));
13980 return FAIL;
13981 }
13982 return OK;
13983}
13984#endif
13985
13986#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013987static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013988
13989 static void
13990remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013991 typval_T *argvars;
13992 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993 int expr;
13994{
13995 char_u *server_name;
13996 char_u *keys;
13997 char_u *r = NULL;
13998 char_u buf[NUMBUFLEN];
13999# ifdef WIN32
14000 HWND w;
14001# else
14002 Window w;
14003# endif
14004
14005 if (check_restricted() || check_secure())
14006 return;
14007
14008# ifdef FEAT_X11
14009 if (check_connection() == FAIL)
14010 return;
14011# endif
14012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014013 server_name = get_tv_string_chk(&argvars[0]);
14014 if (server_name == NULL)
14015 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014016 keys = get_tv_string_buf(&argvars[1], buf);
14017# ifdef WIN32
14018 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14019# else
14020 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14021 < 0)
14022# endif
14023 {
14024 if (r != NULL)
14025 EMSG(r); /* sending worked but evaluation failed */
14026 else
14027 EMSG2(_("E241: Unable to send to %s"), server_name);
14028 return;
14029 }
14030
14031 rettv->vval.v_string = r;
14032
14033 if (argvars[2].v_type != VAR_UNKNOWN)
14034 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014036 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014038
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014039 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014040 v.di_tv.v_type = VAR_STRING;
14041 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 idvar = get_tv_string_chk(&argvars[2]);
14043 if (idvar != NULL)
14044 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014045 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014046 }
14047}
14048#endif
14049
14050/*
14051 * "remote_expr()" function
14052 */
14053/*ARGSUSED*/
14054 static void
14055f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014056 typval_T *argvars;
14057 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058{
14059 rettv->v_type = VAR_STRING;
14060 rettv->vval.v_string = NULL;
14061#ifdef FEAT_CLIENTSERVER
14062 remote_common(argvars, rettv, TRUE);
14063#endif
14064}
14065
14066/*
14067 * "remote_foreground()" function
14068 */
14069/*ARGSUSED*/
14070 static void
14071f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014072 typval_T *argvars;
14073 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074{
14075 rettv->vval.v_number = 0;
14076#ifdef FEAT_CLIENTSERVER
14077# ifdef WIN32
14078 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 {
14080 char_u *server_name = get_tv_string_chk(&argvars[0]);
14081
14082 if (server_name != NULL)
14083 serverForeground(server_name);
14084 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014085# else
14086 /* Send a foreground() expression to the server. */
14087 argvars[1].v_type = VAR_STRING;
14088 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14089 argvars[2].v_type = VAR_UNKNOWN;
14090 remote_common(argvars, rettv, TRUE);
14091 vim_free(argvars[1].vval.v_string);
14092# endif
14093#endif
14094}
14095
14096/*ARGSUSED*/
14097 static void
14098f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 typval_T *argvars;
14100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101{
14102#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104 char_u *s = NULL;
14105# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014106 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014109
14110 if (check_restricted() || check_secure())
14111 {
14112 rettv->vval.v_number = -1;
14113 return;
14114 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 serverid = get_tv_string_chk(&argvars[0]);
14116 if (serverid == NULL)
14117 {
14118 rettv->vval.v_number = -1;
14119 return; /* type error; errmsg already given */
14120 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014122 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123 if (n == 0)
14124 rettv->vval.v_number = -1;
14125 else
14126 {
14127 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14128 rettv->vval.v_number = (s != NULL);
14129 }
14130# else
14131 rettv->vval.v_number = 0;
14132 if (check_connection() == FAIL)
14133 return;
14134
14135 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014137# endif
14138
14139 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 char_u *retvar;
14142
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 v.di_tv.v_type = VAR_STRING;
14144 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 retvar = get_tv_string_chk(&argvars[1]);
14146 if (retvar != NULL)
14147 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014148 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 }
14150#else
14151 rettv->vval.v_number = -1;
14152#endif
14153}
14154
14155/*ARGSUSED*/
14156 static void
14157f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 typval_T *argvars;
14159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014160{
14161 char_u *r = NULL;
14162
14163#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 char_u *serverid = get_tv_string_chk(&argvars[0]);
14165
14166 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014167 {
14168# ifdef WIN32
14169 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014170 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014171
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014172 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173 if (n != 0)
14174 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14175 if (r == NULL)
14176# else
14177 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179# endif
14180 EMSG(_("E277: Unable to read a server reply"));
14181 }
14182#endif
14183 rettv->v_type = VAR_STRING;
14184 rettv->vval.v_string = r;
14185}
14186
14187/*
14188 * "remote_send()" function
14189 */
14190/*ARGSUSED*/
14191 static void
14192f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *argvars;
14194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195{
14196 rettv->v_type = VAR_STRING;
14197 rettv->vval.v_string = NULL;
14198#ifdef FEAT_CLIENTSERVER
14199 remote_common(argvars, rettv, FALSE);
14200#endif
14201}
14202
14203/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014204 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014205 */
14206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014207f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014208 typval_T *argvars;
14209 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014210{
Bram Moolenaar33570922005-01-25 22:26:29 +000014211 list_T *l;
14212 listitem_T *item, *item2;
14213 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014214 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014215 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014216 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014217 dict_T *d;
14218 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014219
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014220 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014221 if (argvars[0].v_type == VAR_DICT)
14222 {
14223 if (argvars[2].v_type != VAR_UNKNOWN)
14224 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014225 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014226 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 key = get_tv_string_chk(&argvars[1]);
14229 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 di = dict_find(d, key, -1);
14232 if (di == NULL)
14233 EMSG2(_(e_dictkey), key);
14234 else
14235 {
14236 *rettv = di->di_tv;
14237 init_tv(&di->di_tv);
14238 dictitem_remove(d, di);
14239 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014241 }
14242 }
14243 else if (argvars[0].v_type != VAR_LIST)
14244 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014245 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014246 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 int error = FALSE;
14249
14250 idx = get_tv_number_chk(&argvars[1], &error);
14251 if (error)
14252 ; /* type error: do nothing, errmsg already given */
14253 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014254 EMSGN(_(e_listidx), idx);
14255 else
14256 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014257 if (argvars[2].v_type == VAR_UNKNOWN)
14258 {
14259 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014260 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014261 *rettv = item->li_tv;
14262 vim_free(item);
14263 }
14264 else
14265 {
14266 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 end = get_tv_number_chk(&argvars[2], &error);
14268 if (error)
14269 ; /* type error: do nothing */
14270 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014271 EMSGN(_(e_listidx), end);
14272 else
14273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014274 int cnt = 0;
14275
14276 for (li = item; li != NULL; li = li->li_next)
14277 {
14278 ++cnt;
14279 if (li == item2)
14280 break;
14281 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014282 if (li == NULL) /* didn't find "item2" after "item" */
14283 EMSG(_(e_invrange));
14284 else
14285 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014286 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014287 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014288 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014289 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014290 l->lv_first = item;
14291 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014292 item->li_prev = NULL;
14293 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014294 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014295 }
14296 }
14297 }
14298 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014299 }
14300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301}
14302
14303/*
14304 * "rename({from}, {to})" function
14305 */
14306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014308 typval_T *argvars;
14309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310{
14311 char_u buf[NUMBUFLEN];
14312
14313 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14317 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318}
14319
14320/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014321 * "repeat()" function
14322 */
14323/*ARGSUSED*/
14324 static void
14325f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014326 typval_T *argvars;
14327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014328{
14329 char_u *p;
14330 int n;
14331 int slen;
14332 int len;
14333 char_u *r;
14334 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014335
14336 n = get_tv_number(&argvars[1]);
14337 if (argvars[0].v_type == VAR_LIST)
14338 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014339 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014340 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014341 if (list_extend(rettv->vval.v_list,
14342 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014343 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014344 }
14345 else
14346 {
14347 p = get_tv_string(&argvars[0]);
14348 rettv->v_type = VAR_STRING;
14349 rettv->vval.v_string = NULL;
14350
14351 slen = (int)STRLEN(p);
14352 len = slen * n;
14353 if (len <= 0)
14354 return;
14355
14356 r = alloc(len + 1);
14357 if (r != NULL)
14358 {
14359 for (i = 0; i < n; i++)
14360 mch_memmove(r + i * slen, p, (size_t)slen);
14361 r[len] = NUL;
14362 }
14363
14364 rettv->vval.v_string = r;
14365 }
14366}
14367
14368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 * "resolve()" function
14370 */
14371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014373 typval_T *argvars;
14374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375{
14376 char_u *p;
14377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379#ifdef FEAT_SHORTCUT
14380 {
14381 char_u *v = NULL;
14382
14383 v = mch_resolve_shortcut(p);
14384 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 }
14389#else
14390# ifdef HAVE_READLINK
14391 {
14392 char_u buf[MAXPATHL + 1];
14393 char_u *cpy;
14394 int len;
14395 char_u *remain = NULL;
14396 char_u *q;
14397 int is_relative_to_current = FALSE;
14398 int has_trailing_pathsep = FALSE;
14399 int limit = 100;
14400
14401 p = vim_strsave(p);
14402
14403 if (p[0] == '.' && (vim_ispathsep(p[1])
14404 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14405 is_relative_to_current = TRUE;
14406
14407 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014408 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 has_trailing_pathsep = TRUE;
14410
14411 q = getnextcomp(p);
14412 if (*q != NUL)
14413 {
14414 /* Separate the first path component in "p", and keep the
14415 * remainder (beginning with the path separator). */
14416 remain = vim_strsave(q - 1);
14417 q[-1] = NUL;
14418 }
14419
14420 for (;;)
14421 {
14422 for (;;)
14423 {
14424 len = readlink((char *)p, (char *)buf, MAXPATHL);
14425 if (len <= 0)
14426 break;
14427 buf[len] = NUL;
14428
14429 if (limit-- == 0)
14430 {
14431 vim_free(p);
14432 vim_free(remain);
14433 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 goto fail;
14436 }
14437
14438 /* Ensure that the result will have a trailing path separator
14439 * if the argument has one. */
14440 if (remain == NULL && has_trailing_pathsep)
14441 add_pathsep(buf);
14442
14443 /* Separate the first path component in the link value and
14444 * concatenate the remainders. */
14445 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14446 if (*q != NUL)
14447 {
14448 if (remain == NULL)
14449 remain = vim_strsave(q - 1);
14450 else
14451 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014452 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 if (cpy != NULL)
14454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 vim_free(remain);
14456 remain = cpy;
14457 }
14458 }
14459 q[-1] = NUL;
14460 }
14461
14462 q = gettail(p);
14463 if (q > p && *q == NUL)
14464 {
14465 /* Ignore trailing path separator. */
14466 q[-1] = NUL;
14467 q = gettail(p);
14468 }
14469 if (q > p && !mch_isFullName(buf))
14470 {
14471 /* symlink is relative to directory of argument */
14472 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14473 if (cpy != NULL)
14474 {
14475 STRCPY(cpy, p);
14476 STRCPY(gettail(cpy), buf);
14477 vim_free(p);
14478 p = cpy;
14479 }
14480 }
14481 else
14482 {
14483 vim_free(p);
14484 p = vim_strsave(buf);
14485 }
14486 }
14487
14488 if (remain == NULL)
14489 break;
14490
14491 /* Append the first path component of "remain" to "p". */
14492 q = getnextcomp(remain + 1);
14493 len = q - remain - (*q != NUL);
14494 cpy = vim_strnsave(p, STRLEN(p) + len);
14495 if (cpy != NULL)
14496 {
14497 STRNCAT(cpy, remain, len);
14498 vim_free(p);
14499 p = cpy;
14500 }
14501 /* Shorten "remain". */
14502 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014503 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 else
14505 {
14506 vim_free(remain);
14507 remain = NULL;
14508 }
14509 }
14510
14511 /* If the result is a relative path name, make it explicitly relative to
14512 * the current directory if and only if the argument had this form. */
14513 if (!vim_ispathsep(*p))
14514 {
14515 if (is_relative_to_current
14516 && *p != NUL
14517 && !(p[0] == '.'
14518 && (p[1] == NUL
14519 || vim_ispathsep(p[1])
14520 || (p[1] == '.'
14521 && (p[2] == NUL
14522 || vim_ispathsep(p[2]))))))
14523 {
14524 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014525 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 if (cpy != NULL)
14527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 vim_free(p);
14529 p = cpy;
14530 }
14531 }
14532 else if (!is_relative_to_current)
14533 {
14534 /* Strip leading "./". */
14535 q = p;
14536 while (q[0] == '.' && vim_ispathsep(q[1]))
14537 q += 2;
14538 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014539 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 }
14541 }
14542
14543 /* Ensure that the result will have no trailing path separator
14544 * if the argument had none. But keep "/" or "//". */
14545 if (!has_trailing_pathsep)
14546 {
14547 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014548 if (after_pathsep(p, q))
14549 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 }
14551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014552 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 }
14554# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556# endif
14557#endif
14558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560
14561#ifdef HAVE_READLINK
14562fail:
14563#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565}
14566
14567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 */
14570 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 typval_T *argvars;
14573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574{
Bram Moolenaar33570922005-01-25 22:26:29 +000014575 list_T *l;
14576 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577
Bram Moolenaar0d660222005-01-07 21:51:51 +000014578 rettv->vval.v_number = 0;
14579 if (argvars[0].v_type != VAR_LIST)
14580 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014581 else if ((l = argvars[0].vval.v_list) != NULL
14582 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583 {
14584 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014585 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014586 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014587 while (li != NULL)
14588 {
14589 ni = li->li_prev;
14590 list_append(l, li);
14591 li = ni;
14592 }
14593 rettv->vval.v_list = l;
14594 rettv->v_type = VAR_LIST;
14595 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014596 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598}
14599
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014600#define SP_NOMOVE 0x01 /* don't move cursor */
14601#define SP_REPEAT 0x02 /* repeat to find outer pair */
14602#define SP_RETCOUNT 0x04 /* return matchcount */
14603#define SP_SETPCMARK 0x08 /* set previous context mark */
14604#define SP_START 0x10 /* accept match at start position */
14605#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14606#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014607
Bram Moolenaar33570922005-01-25 22:26:29 +000014608static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014609
14610/*
14611 * Get flags for a search function.
14612 * Possibly sets "p_ws".
14613 * Returns BACKWARD, FORWARD or zero (for an error).
14614 */
14615 static int
14616get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014617 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014618 int *flagsp;
14619{
14620 int dir = FORWARD;
14621 char_u *flags;
14622 char_u nbuf[NUMBUFLEN];
14623 int mask;
14624
14625 if (varp->v_type != VAR_UNKNOWN)
14626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 flags = get_tv_string_buf_chk(varp, nbuf);
14628 if (flags == NULL)
14629 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014630 while (*flags != NUL)
14631 {
14632 switch (*flags)
14633 {
14634 case 'b': dir = BACKWARD; break;
14635 case 'w': p_ws = TRUE; break;
14636 case 'W': p_ws = FALSE; break;
14637 default: mask = 0;
14638 if (flagsp != NULL)
14639 switch (*flags)
14640 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014641 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014642 case 'e': mask = SP_END; break;
14643 case 'm': mask = SP_RETCOUNT; break;
14644 case 'n': mask = SP_NOMOVE; break;
14645 case 'p': mask = SP_SUBPAT; break;
14646 case 'r': mask = SP_REPEAT; break;
14647 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 }
14649 if (mask == 0)
14650 {
14651 EMSG2(_(e_invarg2), flags);
14652 dir = 0;
14653 }
14654 else
14655 *flagsp |= mask;
14656 }
14657 if (dir == 0)
14658 break;
14659 ++flags;
14660 }
14661 }
14662 return dir;
14663}
14664
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014666 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014668 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014669search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014671 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014672 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014674 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 char_u *pat;
14676 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014677 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 int save_p_ws = p_ws;
14679 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014680 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014681 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014682 proftime_T tm;
14683#ifdef FEAT_RELTIME
14684 long time_limit = 0;
14685#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014686 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014687 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014690 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014691 if (dir == 0)
14692 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014693 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014694 if (flags & SP_START)
14695 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014696 if (flags & SP_END)
14697 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014698
Bram Moolenaar76929292008-01-06 19:07:36 +000014699 /* Optional arguments: line number to stop searching and timeout. */
14700 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014701 {
14702 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14703 if (lnum_stop < 0)
14704 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014705#ifdef FEAT_RELTIME
14706 if (argvars[3].v_type != VAR_UNKNOWN)
14707 {
14708 time_limit = get_tv_number_chk(&argvars[3], NULL);
14709 if (time_limit < 0)
14710 goto theend;
14711 }
14712#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014713 }
14714
Bram Moolenaar76929292008-01-06 19:07:36 +000014715#ifdef FEAT_RELTIME
14716 /* Set the time limit, if there is one. */
14717 profile_setlimit(time_limit, &tm);
14718#endif
14719
Bram Moolenaar231334e2005-07-25 20:46:57 +000014720 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014721 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014722 * Check to make sure only those flags are set.
14723 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14724 * flags cannot be set. Check for that condition also.
14725 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014726 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014727 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014729 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014730 goto theend;
14731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014733 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014734 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014735 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014736 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014738 if (flags & SP_SUBPAT)
14739 retval = subpatnum;
14740 else
14741 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014742 if (flags & SP_SETPCMARK)
14743 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014745 if (match_pos != NULL)
14746 {
14747 /* Store the match cursor position */
14748 match_pos->lnum = pos.lnum;
14749 match_pos->col = pos.col + 1;
14750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 /* "/$" will put the cursor after the end of the line, may need to
14752 * correct that here */
14753 check_cursor();
14754 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014755
14756 /* If 'n' flag is used: restore cursor position. */
14757 if (flags & SP_NOMOVE)
14758 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014759 else
14760 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014761theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014763
14764 return retval;
14765}
14766
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014767#ifdef FEAT_FLOAT
14768/*
14769 * "round({float})" function
14770 */
14771 static void
14772f_round(argvars, rettv)
14773 typval_T *argvars;
14774 typval_T *rettv;
14775{
14776 float_T f;
14777
14778 rettv->v_type = VAR_FLOAT;
14779 if (get_float_arg(argvars, &f) == OK)
14780 /* round() is not in C90, use ceil() or floor() instead. */
14781 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14782 else
14783 rettv->vval.v_float = 0.0;
14784}
14785#endif
14786
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014787/*
14788 * "search()" function
14789 */
14790 static void
14791f_search(argvars, rettv)
14792 typval_T *argvars;
14793 typval_T *rettv;
14794{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014795 int flags = 0;
14796
14797 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798}
14799
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014801 * "searchdecl()" function
14802 */
14803 static void
14804f_searchdecl(argvars, rettv)
14805 typval_T *argvars;
14806 typval_T *rettv;
14807{
14808 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014809 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014810 int error = FALSE;
14811 char_u *name;
14812
14813 rettv->vval.v_number = 1; /* default: FAIL */
14814
14815 name = get_tv_string_chk(&argvars[0]);
14816 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014817 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014818 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014819 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14820 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14821 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014822 if (!error && name != NULL)
14823 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014824 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014825}
14826
14827/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014828 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014830 static int
14831searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014832 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014833 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834{
14835 char_u *spat, *mpat, *epat;
14836 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 int dir;
14839 int flags = 0;
14840 char_u nbuf1[NUMBUFLEN];
14841 char_u nbuf2[NUMBUFLEN];
14842 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014843 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014844 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014845 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014848 spat = get_tv_string_chk(&argvars[0]);
14849 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14850 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14851 if (spat == NULL || mpat == NULL || epat == NULL)
14852 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 /* Handle the optional fourth argument: flags */
14855 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014856 if (dir == 0)
14857 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014858
14859 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014860 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14861 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014862 if ((flags & (SP_END | SP_SUBPAT)) != 0
14863 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014864 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014865 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014866 goto theend;
14867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014869 /* Using 'r' implies 'W', otherwise it doesn't work. */
14870 if (flags & SP_REPEAT)
14871 p_ws = FALSE;
14872
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014873 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014874 if (argvars[3].v_type == VAR_UNKNOWN
14875 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 skip = (char_u *)"";
14877 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014879 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014880 if (argvars[5].v_type != VAR_UNKNOWN)
14881 {
14882 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14883 if (lnum_stop < 0)
14884 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014885#ifdef FEAT_RELTIME
14886 if (argvars[6].v_type != VAR_UNKNOWN)
14887 {
14888 time_limit = get_tv_number_chk(&argvars[6], NULL);
14889 if (time_limit < 0)
14890 goto theend;
14891 }
14892#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014893 }
14894 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014895 if (skip == NULL)
14896 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014898 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014899 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014900
14901theend:
14902 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014903
14904 return retval;
14905}
14906
14907/*
14908 * "searchpair()" function
14909 */
14910 static void
14911f_searchpair(argvars, rettv)
14912 typval_T *argvars;
14913 typval_T *rettv;
14914{
14915 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14916}
14917
14918/*
14919 * "searchpairpos()" function
14920 */
14921 static void
14922f_searchpairpos(argvars, rettv)
14923 typval_T *argvars;
14924 typval_T *rettv;
14925{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014926 pos_T match_pos;
14927 int lnum = 0;
14928 int col = 0;
14929
14930 rettv->vval.v_number = 0;
14931
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014932 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014933 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014934
14935 if (searchpair_cmn(argvars, &match_pos) > 0)
14936 {
14937 lnum = match_pos.lnum;
14938 col = match_pos.col;
14939 }
14940
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014941 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14942 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014943}
14944
14945/*
14946 * Search for a start/middle/end thing.
14947 * Used by searchpair(), see its documentation for the details.
14948 * Returns 0 or -1 for no match,
14949 */
14950 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014951do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14952 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014953 char_u *spat; /* start pattern */
14954 char_u *mpat; /* middle pattern */
14955 char_u *epat; /* end pattern */
14956 int dir; /* BACKWARD or FORWARD */
14957 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014958 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014959 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014960 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014961 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014962{
14963 char_u *save_cpo;
14964 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14965 long retval = 0;
14966 pos_T pos;
14967 pos_T firstpos;
14968 pos_T foundpos;
14969 pos_T save_cursor;
14970 pos_T save_pos;
14971 int n;
14972 int r;
14973 int nest = 1;
14974 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014975 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014976 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014977
14978 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14979 save_cpo = p_cpo;
14980 p_cpo = (char_u *)"";
14981
Bram Moolenaar76929292008-01-06 19:07:36 +000014982#ifdef FEAT_RELTIME
14983 /* Set the time limit, if there is one. */
14984 profile_setlimit(time_limit, &tm);
14985#endif
14986
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014987 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14988 * start/middle/end (pat3, for the top pair). */
14989 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14990 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14991 if (pat2 == NULL || pat3 == NULL)
14992 goto theend;
14993 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14994 if (*mpat == NUL)
14995 STRCPY(pat3, pat2);
14996 else
14997 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14998 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014999 if (flags & SP_START)
15000 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015001
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 save_cursor = curwin->w_cursor;
15003 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015004 clearpos(&firstpos);
15005 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 pat = pat3;
15007 for (;;)
15008 {
15009 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015010 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15012 /* didn't find it or found the first match again: FAIL */
15013 break;
15014
15015 if (firstpos.lnum == 0)
15016 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015017 if (equalpos(pos, foundpos))
15018 {
15019 /* Found the same position again. Can happen with a pattern that
15020 * has "\zs" at the end and searching backwards. Advance one
15021 * character and try again. */
15022 if (dir == BACKWARD)
15023 decl(&pos);
15024 else
15025 incl(&pos);
15026 }
15027 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015029 /* clear the start flag to avoid getting stuck here */
15030 options &= ~SEARCH_START;
15031
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 /* If the skip pattern matches, ignore this match. */
15033 if (*skip != NUL)
15034 {
15035 save_pos = curwin->w_cursor;
15036 curwin->w_cursor = pos;
15037 r = eval_to_bool(skip, &err, NULL, FALSE);
15038 curwin->w_cursor = save_pos;
15039 if (err)
15040 {
15041 /* Evaluating {skip} caused an error, break here. */
15042 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015043 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 break;
15045 }
15046 if (r)
15047 continue;
15048 }
15049
15050 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15051 {
15052 /* Found end when searching backwards or start when searching
15053 * forward: nested pair. */
15054 ++nest;
15055 pat = pat2; /* nested, don't search for middle */
15056 }
15057 else
15058 {
15059 /* Found end when searching forward or start when searching
15060 * backward: end of (nested) pair; or found middle in outer pair. */
15061 if (--nest == 1)
15062 pat = pat3; /* outer level, search for middle */
15063 }
15064
15065 if (nest == 0)
15066 {
15067 /* Found the match: return matchcount or line number. */
15068 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015069 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015071 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015072 if (flags & SP_SETPCMARK)
15073 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 curwin->w_cursor = pos;
15075 if (!(flags & SP_REPEAT))
15076 break;
15077 nest = 1; /* search for next unmatched */
15078 }
15079 }
15080
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015081 if (match_pos != NULL)
15082 {
15083 /* Store the match cursor position */
15084 match_pos->lnum = curwin->w_cursor.lnum;
15085 match_pos->col = curwin->w_cursor.col + 1;
15086 }
15087
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015089 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 curwin->w_cursor = save_cursor;
15091
15092theend:
15093 vim_free(pat2);
15094 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015096
15097 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098}
15099
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015100/*
15101 * "searchpos()" function
15102 */
15103 static void
15104f_searchpos(argvars, rettv)
15105 typval_T *argvars;
15106 typval_T *rettv;
15107{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015108 pos_T match_pos;
15109 int lnum = 0;
15110 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015111 int n;
15112 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015113
15114 rettv->vval.v_number = 0;
15115
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015116 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015117 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015118
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015119 n = search_cmn(argvars, &match_pos, &flags);
15120 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015121 {
15122 lnum = match_pos.lnum;
15123 col = match_pos.col;
15124 }
15125
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015126 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15127 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015128 if (flags & SP_SUBPAT)
15129 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015130}
15131
15132
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133/*ARGSUSED*/
15134 static void
15135f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015136 typval_T *argvars;
15137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139#ifdef FEAT_CLIENTSERVER
15140 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015141 char_u *server = get_tv_string_chk(&argvars[0]);
15142 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015145 if (server == NULL || reply == NULL)
15146 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015147 if (check_restricted() || check_secure())
15148 return;
15149# ifdef FEAT_X11
15150 if (check_connection() == FAIL)
15151 return;
15152# endif
15153
15154 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 EMSG(_("E258: Unable to send to client"));
15157 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 rettv->vval.v_number = 0;
15160#else
15161 rettv->vval.v_number = -1;
15162#endif
15163}
15164
15165/*ARGSUSED*/
15166 static void
15167f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015168 typval_T *argvars;
15169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170{
15171 char_u *r = NULL;
15172
15173#ifdef FEAT_CLIENTSERVER
15174# ifdef WIN32
15175 r = serverGetVimNames();
15176# else
15177 make_connection();
15178 if (X_DISPLAY != NULL)
15179 r = serverGetVimNames(X_DISPLAY);
15180# endif
15181#endif
15182 rettv->v_type = VAR_STRING;
15183 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184}
15185
15186/*
15187 * "setbufvar()" function
15188 */
15189/*ARGSUSED*/
15190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015191f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *argvars;
15193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194{
15195 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015198 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 char_u nbuf[NUMBUFLEN];
15200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015201 rettv->vval.v_number = 0;
15202
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 if (check_restricted() || check_secure())
15204 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15206 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 varp = &argvars[2];
15209
15210 if (buf != NULL && varname != NULL && varp != NULL)
15211 {
15212 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214
15215 if (*varname == '&')
15216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015217 long numval;
15218 char_u *strval;
15219 int error = FALSE;
15220
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015222 numval = get_tv_number_chk(varp, &error);
15223 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 if (!error && strval != NULL)
15225 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 }
15227 else
15228 {
15229 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15230 if (bufvarname != NULL)
15231 {
15232 STRCPY(bufvarname, "b:");
15233 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015234 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 vim_free(bufvarname);
15236 }
15237 }
15238
15239 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242}
15243
15244/*
15245 * "setcmdpos()" function
15246 */
15247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015248f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015249 typval_T *argvars;
15250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 int pos = (int)get_tv_number(&argvars[0]) - 1;
15253
15254 if (pos >= 0)
15255 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256}
15257
15258/*
15259 * "setline()" function
15260 */
15261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015263 typval_T *argvars;
15264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265{
15266 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015267 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015268 list_T *l = NULL;
15269 listitem_T *li = NULL;
15270 long added = 0;
15271 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015273 lnum = get_tv_lnum(&argvars[0]);
15274 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015276 l = argvars[1].vval.v_list;
15277 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015279 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015281
15282 rettv->vval.v_number = 0; /* OK */
15283 for (;;)
15284 {
15285 if (l != NULL)
15286 {
15287 /* list argument, get next string */
15288 if (li == NULL)
15289 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015291 li = li->li_next;
15292 }
15293
15294 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015295 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015296 break;
15297 if (lnum <= curbuf->b_ml.ml_line_count)
15298 {
15299 /* existing line, replace it */
15300 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15301 {
15302 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015303 if (lnum == curwin->w_cursor.lnum)
15304 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015305 rettv->vval.v_number = 0; /* OK */
15306 }
15307 }
15308 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15309 {
15310 /* lnum is one past the last line, append the line */
15311 ++added;
15312 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15313 rettv->vval.v_number = 0; /* OK */
15314 }
15315
15316 if (l == NULL) /* only one string argument */
15317 break;
15318 ++lnum;
15319 }
15320
15321 if (added > 0)
15322 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323}
15324
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015325static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15326
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015328 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015329 */
15330/*ARGSUSED*/
15331 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015332set_qf_ll_list(wp, list_arg, action_arg, rettv)
15333 win_T *wp;
15334 typval_T *list_arg;
15335 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015336 typval_T *rettv;
15337{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015338#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015339 char_u *act;
15340 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015341#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015342
Bram Moolenaar2641f772005-03-25 21:58:17 +000015343 rettv->vval.v_number = -1;
15344
15345#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015346 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015347 EMSG(_(e_listreq));
15348 else
15349 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015350 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015351
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015352 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015353 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015354 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015355 if (act == NULL)
15356 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015357 if (*act == 'a' || *act == 'r')
15358 action = *act;
15359 }
15360
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015361 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015362 rettv->vval.v_number = 0;
15363 }
15364#endif
15365}
15366
15367/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015368 * "setloclist()" function
15369 */
15370/*ARGSUSED*/
15371 static void
15372f_setloclist(argvars, rettv)
15373 typval_T *argvars;
15374 typval_T *rettv;
15375{
15376 win_T *win;
15377
15378 rettv->vval.v_number = -1;
15379
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015380 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015381 if (win != NULL)
15382 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15383}
15384
15385/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015386 * "setmatches()" function
15387 */
15388 static void
15389f_setmatches(argvars, rettv)
15390 typval_T *argvars;
15391 typval_T *rettv;
15392{
15393#ifdef FEAT_SEARCH_EXTRA
15394 list_T *l;
15395 listitem_T *li;
15396 dict_T *d;
15397
15398 rettv->vval.v_number = -1;
15399 if (argvars[0].v_type != VAR_LIST)
15400 {
15401 EMSG(_(e_listreq));
15402 return;
15403 }
15404 if ((l = argvars[0].vval.v_list) != NULL)
15405 {
15406
15407 /* To some extent make sure that we are dealing with a list from
15408 * "getmatches()". */
15409 li = l->lv_first;
15410 while (li != NULL)
15411 {
15412 if (li->li_tv.v_type != VAR_DICT
15413 || (d = li->li_tv.vval.v_dict) == NULL)
15414 {
15415 EMSG(_(e_invarg));
15416 return;
15417 }
15418 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15419 && dict_find(d, (char_u *)"pattern", -1) != NULL
15420 && dict_find(d, (char_u *)"priority", -1) != NULL
15421 && dict_find(d, (char_u *)"id", -1) != NULL))
15422 {
15423 EMSG(_(e_invarg));
15424 return;
15425 }
15426 li = li->li_next;
15427 }
15428
15429 clear_matches(curwin);
15430 li = l->lv_first;
15431 while (li != NULL)
15432 {
15433 d = li->li_tv.vval.v_dict;
15434 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15435 get_dict_string(d, (char_u *)"pattern", FALSE),
15436 (int)get_dict_number(d, (char_u *)"priority"),
15437 (int)get_dict_number(d, (char_u *)"id"));
15438 li = li->li_next;
15439 }
15440 rettv->vval.v_number = 0;
15441 }
15442#endif
15443}
15444
15445/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015446 * "setpos()" function
15447 */
15448/*ARGSUSED*/
15449 static void
15450f_setpos(argvars, rettv)
15451 typval_T *argvars;
15452 typval_T *rettv;
15453{
15454 pos_T pos;
15455 int fnum;
15456 char_u *name;
15457
Bram Moolenaar08250432008-02-13 11:42:46 +000015458 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015459 name = get_tv_string_chk(argvars);
15460 if (name != NULL)
15461 {
15462 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15463 {
15464 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015465 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015466 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015467 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015468 if (fnum == curbuf->b_fnum)
15469 {
15470 curwin->w_cursor = pos;
15471 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015472 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015473 }
15474 else
15475 EMSG(_(e_invarg));
15476 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015477 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15478 {
15479 /* set mark */
15480 if (setmark_pos(name[1], &pos, fnum) == OK)
15481 rettv->vval.v_number = 0;
15482 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015483 else
15484 EMSG(_(e_invarg));
15485 }
15486 }
15487}
15488
15489/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015490 * "setqflist()" function
15491 */
15492/*ARGSUSED*/
15493 static void
15494f_setqflist(argvars, rettv)
15495 typval_T *argvars;
15496 typval_T *rettv;
15497{
15498 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15499}
15500
15501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 * "setreg()" function
15503 */
15504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015505f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015506 typval_T *argvars;
15507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508{
15509 int regname;
15510 char_u *strregname;
15511 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015512 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 int append;
15514 char_u yank_type;
15515 long block_len;
15516
15517 block_len = -1;
15518 yank_type = MAUTO;
15519 append = FALSE;
15520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015521 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015524 if (strregname == NULL)
15525 return; /* type error; errmsg already given */
15526 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 if (regname == 0 || regname == '@')
15528 regname = '"';
15529 else if (regname == '=')
15530 return;
15531
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015532 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015534 stropt = get_tv_string_chk(&argvars[2]);
15535 if (stropt == NULL)
15536 return; /* type error */
15537 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 switch (*stropt)
15539 {
15540 case 'a': case 'A': /* append */
15541 append = TRUE;
15542 break;
15543 case 'v': case 'c': /* character-wise selection */
15544 yank_type = MCHAR;
15545 break;
15546 case 'V': case 'l': /* line-wise selection */
15547 yank_type = MLINE;
15548 break;
15549#ifdef FEAT_VISUAL
15550 case 'b': case Ctrl_V: /* block-wise selection */
15551 yank_type = MBLOCK;
15552 if (VIM_ISDIGIT(stropt[1]))
15553 {
15554 ++stropt;
15555 block_len = getdigits(&stropt) - 1;
15556 --stropt;
15557 }
15558 break;
15559#endif
15560 }
15561 }
15562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015563 strval = get_tv_string_chk(&argvars[1]);
15564 if (strval != NULL)
15565 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568}
15569
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015570/*
15571 * "settabwinvar()" function
15572 */
15573 static void
15574f_settabwinvar(argvars, rettv)
15575 typval_T *argvars;
15576 typval_T *rettv;
15577{
15578 setwinvar(argvars, rettv, 1);
15579}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580
15581/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015582 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015585f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015586 typval_T *argvars;
15587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015589 setwinvar(argvars, rettv, 0);
15590}
15591
15592/*
15593 * "setwinvar()" and "settabwinvar()" functions
15594 */
15595 static void
15596setwinvar(argvars, rettv, off)
15597 typval_T *argvars;
15598 typval_T *rettv;
15599 int off;
15600{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 win_T *win;
15602#ifdef FEAT_WINDOWS
15603 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015604 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605#endif
15606 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015607 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015609 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015611 rettv->vval.v_number = 0;
15612
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 if (check_restricted() || check_secure())
15614 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015615
15616#ifdef FEAT_WINDOWS
15617 if (off == 1)
15618 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15619 else
15620 tp = curtab;
15621#endif
15622 win = find_win_by_nr(&argvars[off], tp);
15623 varname = get_tv_string_chk(&argvars[off + 1]);
15624 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625
15626 if (win != NULL && varname != NULL && varp != NULL)
15627 {
15628#ifdef FEAT_WINDOWS
15629 /* set curwin to be our win, temporarily */
15630 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015631 save_curtab = curtab;
15632 goto_tabpage_tp(tp);
15633 if (!win_valid(win))
15634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 curwin = win;
15636 curbuf = curwin->w_buffer;
15637#endif
15638
15639 if (*varname == '&')
15640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 long numval;
15642 char_u *strval;
15643 int error = FALSE;
15644
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 numval = get_tv_number_chk(varp, &error);
15647 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 if (!error && strval != NULL)
15649 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 }
15651 else
15652 {
15653 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15654 if (winvarname != NULL)
15655 {
15656 STRCPY(winvarname, "w:");
15657 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015658 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 vim_free(winvarname);
15660 }
15661 }
15662
15663#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015664 /* Restore current tabpage and window, if still valid (autocomands can
15665 * make them invalid). */
15666 if (valid_tabpage(save_curtab))
15667 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 if (win_valid(save_curwin))
15669 {
15670 curwin = save_curwin;
15671 curbuf = curwin->w_buffer;
15672 }
15673#endif
15674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675}
15676
15677/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015678 * "shellescape({string})" function
15679 */
15680 static void
15681f_shellescape(argvars, rettv)
15682 typval_T *argvars;
15683 typval_T *rettv;
15684{
15685 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15686 rettv->v_type = VAR_STRING;
15687}
15688
15689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 */
15692 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015694 typval_T *argvars;
15695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 p = get_tv_string(&argvars[0]);
15700 rettv->vval.v_string = vim_strsave(p);
15701 simplify_filename(rettv->vval.v_string); /* simplify in place */
15702 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703}
15704
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015705#ifdef FEAT_FLOAT
15706/*
15707 * "sin()" function
15708 */
15709 static void
15710f_sin(argvars, rettv)
15711 typval_T *argvars;
15712 typval_T *rettv;
15713{
15714 float_T f;
15715
15716 rettv->v_type = VAR_FLOAT;
15717 if (get_float_arg(argvars, &f) == OK)
15718 rettv->vval.v_float = sin(f);
15719 else
15720 rettv->vval.v_float = 0.0;
15721}
15722#endif
15723
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724static int
15725#ifdef __BORLANDC__
15726 _RTLENTRYF
15727#endif
15728 item_compare __ARGS((const void *s1, const void *s2));
15729static int
15730#ifdef __BORLANDC__
15731 _RTLENTRYF
15732#endif
15733 item_compare2 __ARGS((const void *s1, const void *s2));
15734
15735static int item_compare_ic;
15736static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015737static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738#define ITEM_COMPARE_FAIL 999
15739
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015741 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 static int
15744#ifdef __BORLANDC__
15745_RTLENTRYF
15746#endif
15747item_compare(s1, s2)
15748 const void *s1;
15749 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751 char_u *p1, *p2;
15752 char_u *tofree1, *tofree2;
15753 int res;
15754 char_u numbuf1[NUMBUFLEN];
15755 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015757 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15758 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015759 if (p1 == NULL)
15760 p1 = (char_u *)"";
15761 if (p2 == NULL)
15762 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763 if (item_compare_ic)
15764 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015766 res = STRCMP(p1, p2);
15767 vim_free(tofree1);
15768 vim_free(tofree2);
15769 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770}
15771
15772 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773#ifdef __BORLANDC__
15774_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776item_compare2(s1, s2)
15777 const void *s1;
15778 const void *s2;
15779{
15780 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015782 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015785 /* shortcut after failure in previous call; compare all items equal */
15786 if (item_compare_func_err)
15787 return 0;
15788
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015789 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15790 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015791 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15792 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793
15794 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015795 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015796 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015797 clear_tv(&argv[0]);
15798 clear_tv(&argv[1]);
15799
15800 if (res == FAIL)
15801 res = ITEM_COMPARE_FAIL;
15802 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 /* return value has wrong type */
15804 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15805 if (item_compare_func_err)
15806 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807 clear_tv(&rettv);
15808 return res;
15809}
15810
15811/*
15812 * "sort({list})" function
15813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015816 typval_T *argvars;
15817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818{
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 list_T *l;
15820 listitem_T *li;
15821 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 long len;
15823 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824
Bram Moolenaar0d660222005-01-07 21:51:51 +000015825 rettv->vval.v_number = 0;
15826 if (argvars[0].v_type != VAR_LIST)
15827 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 else
15829 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015830 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015831 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832 return;
15833 rettv->vval.v_list = l;
15834 rettv->v_type = VAR_LIST;
15835 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836
Bram Moolenaar0d660222005-01-07 21:51:51 +000015837 len = list_len(l);
15838 if (len <= 1)
15839 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840
Bram Moolenaar0d660222005-01-07 21:51:51 +000015841 item_compare_ic = FALSE;
15842 item_compare_func = NULL;
15843 if (argvars[1].v_type != VAR_UNKNOWN)
15844 {
15845 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015846 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847 else
15848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015849 int error = FALSE;
15850
15851 i = get_tv_number_chk(&argvars[1], &error);
15852 if (error)
15853 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854 if (i == 1)
15855 item_compare_ic = TRUE;
15856 else
15857 item_compare_func = get_tv_string(&argvars[1]);
15858 }
15859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860
Bram Moolenaar0d660222005-01-07 21:51:51 +000015861 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015862 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 if (ptrs == NULL)
15864 return;
15865 i = 0;
15866 for (li = l->lv_first; li != NULL; li = li->li_next)
15867 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015869 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870 /* test the compare function */
15871 if (item_compare_func != NULL
15872 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15873 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015874 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015876 {
15877 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015879 item_compare_func == NULL ? item_compare : item_compare2);
15880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015881 if (!item_compare_func_err)
15882 {
15883 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015884 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015885 l->lv_len = 0;
15886 for (i = 0; i < len; ++i)
15887 list_append(l, ptrs[i]);
15888 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889 }
15890
15891 vim_free(ptrs);
15892 }
15893}
15894
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015895/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015896 * "soundfold({word})" function
15897 */
15898 static void
15899f_soundfold(argvars, rettv)
15900 typval_T *argvars;
15901 typval_T *rettv;
15902{
15903 char_u *s;
15904
15905 rettv->v_type = VAR_STRING;
15906 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015907#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015908 rettv->vval.v_string = eval_soundfold(s);
15909#else
15910 rettv->vval.v_string = vim_strsave(s);
15911#endif
15912}
15913
15914/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015915 * "spellbadword()" function
15916 */
15917/* ARGSUSED */
15918 static void
15919f_spellbadword(argvars, rettv)
15920 typval_T *argvars;
15921 typval_T *rettv;
15922{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015923 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015924 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015925 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015928 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015929
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015930#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015931 if (argvars[0].v_type == VAR_UNKNOWN)
15932 {
15933 /* Find the start and length of the badly spelled word. */
15934 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15935 if (len != 0)
15936 word = ml_get_cursor();
15937 }
15938 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15939 {
15940 char_u *str = get_tv_string_chk(&argvars[0]);
15941 int capcol = -1;
15942
15943 if (str != NULL)
15944 {
15945 /* Check the argument for spelling. */
15946 while (*str != NUL)
15947 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015948 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015949 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015950 {
15951 word = str;
15952 break;
15953 }
15954 str += len;
15955 }
15956 }
15957 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015958#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015959
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015960 list_append_string(rettv->vval.v_list, word, len);
15961 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015962 attr == HLF_SPB ? "bad" :
15963 attr == HLF_SPR ? "rare" :
15964 attr == HLF_SPL ? "local" :
15965 attr == HLF_SPC ? "caps" :
15966 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015967}
15968
15969/*
15970 * "spellsuggest()" function
15971 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015972/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015973 static void
15974f_spellsuggest(argvars, rettv)
15975 typval_T *argvars;
15976 typval_T *rettv;
15977{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015978#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015979 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015980 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015981 int maxcount;
15982 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015983 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015984 listitem_T *li;
15985 int need_capital = FALSE;
15986#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015988 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015989 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015990
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015991#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015992 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15993 {
15994 str = get_tv_string(&argvars[0]);
15995 if (argvars[1].v_type != VAR_UNKNOWN)
15996 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015997 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015998 if (maxcount <= 0)
15999 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016000 if (argvars[2].v_type != VAR_UNKNOWN)
16001 {
16002 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16003 if (typeerr)
16004 return;
16005 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016006 }
16007 else
16008 maxcount = 25;
16009
Bram Moolenaar4770d092006-01-12 23:22:24 +000016010 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016011
16012 for (i = 0; i < ga.ga_len; ++i)
16013 {
16014 str = ((char_u **)ga.ga_data)[i];
16015
16016 li = listitem_alloc();
16017 if (li == NULL)
16018 vim_free(str);
16019 else
16020 {
16021 li->li_tv.v_type = VAR_STRING;
16022 li->li_tv.v_lock = 0;
16023 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016024 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016025 }
16026 }
16027 ga_clear(&ga);
16028 }
16029#endif
16030}
16031
Bram Moolenaar0d660222005-01-07 21:51:51 +000016032 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016033f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016034 typval_T *argvars;
16035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016036{
16037 char_u *str;
16038 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016039 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016040 regmatch_T regmatch;
16041 char_u patbuf[NUMBUFLEN];
16042 char_u *save_cpo;
16043 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016044 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016045 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016046 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047
16048 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16049 save_cpo = p_cpo;
16050 p_cpo = (char_u *)"";
16051
16052 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016053 if (argvars[1].v_type != VAR_UNKNOWN)
16054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016055 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16056 if (pat == NULL)
16057 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016058 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016059 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016060 }
16061 if (pat == NULL || *pat == NUL)
16062 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016064 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016066 if (typeerr)
16067 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068
Bram Moolenaar0d660222005-01-07 21:51:51 +000016069 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16070 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016072 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016073 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016074 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016075 if (*str == NUL)
16076 match = FALSE; /* empty item at the end */
16077 else
16078 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016079 if (match)
16080 end = regmatch.startp[0];
16081 else
16082 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016083 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16084 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016086 if (list_append_string(rettv->vval.v_list, str,
16087 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016089 }
16090 if (!match)
16091 break;
16092 /* Advance to just after the match. */
16093 if (regmatch.endp[0] > str)
16094 col = 0;
16095 else
16096 {
16097 /* Don't get stuck at the same match. */
16098#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016099 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016100#else
16101 col = 1;
16102#endif
16103 }
16104 str = regmatch.endp[0];
16105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111}
16112
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016113#ifdef FEAT_FLOAT
16114/*
16115 * "sqrt()" function
16116 */
16117 static void
16118f_sqrt(argvars, rettv)
16119 typval_T *argvars;
16120 typval_T *rettv;
16121{
16122 float_T f;
16123
16124 rettv->v_type = VAR_FLOAT;
16125 if (get_float_arg(argvars, &f) == OK)
16126 rettv->vval.v_float = sqrt(f);
16127 else
16128 rettv->vval.v_float = 0.0;
16129}
16130
16131/*
16132 * "str2float()" function
16133 */
16134 static void
16135f_str2float(argvars, rettv)
16136 typval_T *argvars;
16137 typval_T *rettv;
16138{
16139 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16140
16141 if (*p == '+')
16142 p = skipwhite(p + 1);
16143 (void)string2float(p, &rettv->vval.v_float);
16144 rettv->v_type = VAR_FLOAT;
16145}
16146#endif
16147
Bram Moolenaar2c932302006-03-18 21:42:09 +000016148/*
16149 * "str2nr()" function
16150 */
16151 static void
16152f_str2nr(argvars, rettv)
16153 typval_T *argvars;
16154 typval_T *rettv;
16155{
16156 int base = 10;
16157 char_u *p;
16158 long n;
16159
16160 if (argvars[1].v_type != VAR_UNKNOWN)
16161 {
16162 base = get_tv_number(&argvars[1]);
16163 if (base != 8 && base != 10 && base != 16)
16164 {
16165 EMSG(_(e_invarg));
16166 return;
16167 }
16168 }
16169
16170 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016171 if (*p == '+')
16172 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016173 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16174 rettv->vval.v_number = n;
16175}
16176
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177#ifdef HAVE_STRFTIME
16178/*
16179 * "strftime({format}[, {time}])" function
16180 */
16181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 typval_T *argvars;
16184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185{
16186 char_u result_buf[256];
16187 struct tm *curtime;
16188 time_t seconds;
16189 char_u *p;
16190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016191 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016193 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016194 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 seconds = time(NULL);
16196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016197 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 curtime = localtime(&seconds);
16199 /* MSVC returns NULL for an invalid value of seconds. */
16200 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016201 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 else
16203 {
16204# ifdef FEAT_MBYTE
16205 vimconv_T conv;
16206 char_u *enc;
16207
16208 conv.vc_type = CONV_NONE;
16209 enc = enc_locale();
16210 convert_setup(&conv, p_enc, enc);
16211 if (conv.vc_type != CONV_NONE)
16212 p = string_convert(&conv, p, NULL);
16213# endif
16214 if (p != NULL)
16215 (void)strftime((char *)result_buf, sizeof(result_buf),
16216 (char *)p, curtime);
16217 else
16218 result_buf[0] = NUL;
16219
16220# ifdef FEAT_MBYTE
16221 if (conv.vc_type != CONV_NONE)
16222 vim_free(p);
16223 convert_setup(&conv, enc, p_enc);
16224 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 else
16227# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229
16230# ifdef FEAT_MBYTE
16231 /* Release conversion descriptors */
16232 convert_setup(&conv, NULL, NULL);
16233 vim_free(enc);
16234# endif
16235 }
16236}
16237#endif
16238
16239/*
16240 * "stridx()" function
16241 */
16242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016244 typval_T *argvars;
16245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246{
16247 char_u buf[NUMBUFLEN];
16248 char_u *needle;
16249 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016250 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016254 needle = get_tv_string_chk(&argvars[1]);
16255 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016256 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016257 if (needle == NULL || haystack == NULL)
16258 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259
Bram Moolenaar33570922005-01-25 22:26:29 +000016260 if (argvars[2].v_type != VAR_UNKNOWN)
16261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 int error = FALSE;
16263
16264 start_idx = get_tv_number_chk(&argvars[2], &error);
16265 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016266 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016267 if (start_idx >= 0)
16268 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 }
16270
16271 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16272 if (pos != NULL)
16273 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274}
16275
16276/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016277 * "string()" function
16278 */
16279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016280f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 typval_T *argvars;
16282 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016283{
16284 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016285 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016287 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016288 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016289 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016290 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292}
16293
16294/*
16295 * "strlen()" function
16296 */
16297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016298f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 typval_T *argvars;
16300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016302 rettv->vval.v_number = (varnumber_T)(STRLEN(
16303 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304}
16305
16306/*
16307 * "strpart()" function
16308 */
16309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 typval_T *argvars;
16312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313{
16314 char_u *p;
16315 int n;
16316 int len;
16317 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016320 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 slen = (int)STRLEN(p);
16322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016323 n = get_tv_number_chk(&argvars[1], &error);
16324 if (error)
16325 len = 0;
16326 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016327 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328 else
16329 len = slen - n; /* default len: all bytes that are available. */
16330
16331 /*
16332 * Only return the overlap between the specified part and the actual
16333 * string.
16334 */
16335 if (n < 0)
16336 {
16337 len += n;
16338 n = 0;
16339 }
16340 else if (n > slen)
16341 n = slen;
16342 if (len < 0)
16343 len = 0;
16344 else if (n + len > slen)
16345 len = slen - n;
16346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016347 rettv->v_type = VAR_STRING;
16348 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349}
16350
16351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 * "strridx()" function
16353 */
16354 static void
16355f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 typval_T *argvars;
16357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358{
16359 char_u buf[NUMBUFLEN];
16360 char_u *needle;
16361 char_u *haystack;
16362 char_u *rest;
16363 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016364 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366 needle = get_tv_string_chk(&argvars[1]);
16367 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368
16369 rettv->vval.v_number = -1;
16370 if (needle == NULL || haystack == NULL)
16371 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016372
16373 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016374 if (argvars[2].v_type != VAR_UNKNOWN)
16375 {
16376 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016377 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016378 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016379 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016380 }
16381 else
16382 end_idx = haystack_len;
16383
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016385 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016387 lastmatch = haystack + end_idx;
16388 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016390 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391 for (rest = haystack; *rest != '\0'; ++rest)
16392 {
16393 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016394 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 break;
16396 lastmatch = rest;
16397 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016398 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399
16400 if (lastmatch == NULL)
16401 rettv->vval.v_number = -1;
16402 else
16403 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16404}
16405
16406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 * "strtrans()" function
16408 */
16409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016410f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016411 typval_T *argvars;
16412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016414 rettv->v_type = VAR_STRING;
16415 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416}
16417
16418/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 * "submatch()" function
16420 */
16421 static void
16422f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016423 typval_T *argvars;
16424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425{
16426 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 rettv->vval.v_string =
16428 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429}
16430
16431/*
16432 * "substitute()" function
16433 */
16434 static void
16435f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 typval_T *argvars;
16437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438{
16439 char_u patbuf[NUMBUFLEN];
16440 char_u subbuf[NUMBUFLEN];
16441 char_u flagsbuf[NUMBUFLEN];
16442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 char_u *str = get_tv_string_chk(&argvars[0]);
16444 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16445 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16446 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16447
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16450 rettv->vval.v_string = NULL;
16451 else
16452 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016453}
16454
16455/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016456 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 */
16458/*ARGSUSED*/
16459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016460f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *argvars;
16462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463{
16464 int id = 0;
16465#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016466 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 long col;
16468 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016469 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016471 lnum = get_tv_lnum(argvars); /* -1 on type error */
16472 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16473 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016475 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016476 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016477 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478#endif
16479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016480 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481}
16482
16483/*
16484 * "synIDattr(id, what [, mode])" function
16485 */
16486/*ARGSUSED*/
16487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016488f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 typval_T *argvars;
16490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491{
16492 char_u *p = NULL;
16493#ifdef FEAT_SYN_HL
16494 int id;
16495 char_u *what;
16496 char_u *mode;
16497 char_u modebuf[NUMBUFLEN];
16498 int modec;
16499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500 id = get_tv_number(&argvars[0]);
16501 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016504 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 modec = TOLOWER_ASC(mode[0]);
16506 if (modec != 't' && modec != 'c'
16507#ifdef FEAT_GUI
16508 && modec != 'g'
16509#endif
16510 )
16511 modec = 0; /* replace invalid with current */
16512 }
16513 else
16514 {
16515#ifdef FEAT_GUI
16516 if (gui.in_use)
16517 modec = 'g';
16518 else
16519#endif
16520 if (t_colors > 1)
16521 modec = 'c';
16522 else
16523 modec = 't';
16524 }
16525
16526
16527 switch (TOLOWER_ASC(what[0]))
16528 {
16529 case 'b':
16530 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16531 p = highlight_color(id, what, modec);
16532 else /* bold */
16533 p = highlight_has_attr(id, HL_BOLD, modec);
16534 break;
16535
16536 case 'f': /* fg[#] */
16537 p = highlight_color(id, what, modec);
16538 break;
16539
16540 case 'i':
16541 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16542 p = highlight_has_attr(id, HL_INVERSE, modec);
16543 else /* italic */
16544 p = highlight_has_attr(id, HL_ITALIC, modec);
16545 break;
16546
16547 case 'n': /* name */
16548 p = get_highlight_name(NULL, id - 1);
16549 break;
16550
16551 case 'r': /* reverse */
16552 p = highlight_has_attr(id, HL_INVERSE, modec);
16553 break;
16554
16555 case 's': /* standout */
16556 p = highlight_has_attr(id, HL_STANDOUT, modec);
16557 break;
16558
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016559 case 'u':
16560 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16561 /* underline */
16562 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16563 else
16564 /* undercurl */
16565 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 break;
16567 }
16568
16569 if (p != NULL)
16570 p = vim_strsave(p);
16571#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016572 rettv->v_type = VAR_STRING;
16573 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574}
16575
16576/*
16577 * "synIDtrans(id)" function
16578 */
16579/*ARGSUSED*/
16580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016581f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016582 typval_T *argvars;
16583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584{
16585 int id;
16586
16587#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589
16590 if (id > 0)
16591 id = syn_get_final_id(id);
16592 else
16593#endif
16594 id = 0;
16595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016596 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597}
16598
16599/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016600 * "synstack(lnum, col)" function
16601 */
16602/*ARGSUSED*/
16603 static void
16604f_synstack(argvars, rettv)
16605 typval_T *argvars;
16606 typval_T *rettv;
16607{
16608#ifdef FEAT_SYN_HL
16609 long lnum;
16610 long col;
16611 int i;
16612 int id;
16613#endif
16614
16615 rettv->v_type = VAR_LIST;
16616 rettv->vval.v_list = NULL;
16617
16618#ifdef FEAT_SYN_HL
16619 lnum = get_tv_lnum(argvars); /* -1 on type error */
16620 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16621
16622 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
16623 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
16624 && rettv_list_alloc(rettv) != FAIL)
16625 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016626 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016627 for (i = 0; ; ++i)
16628 {
16629 id = syn_get_stack_item(i);
16630 if (id < 0)
16631 break;
16632 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16633 break;
16634 }
16635 }
16636#endif
16637}
16638
16639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 * "system()" function
16641 */
16642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016644 typval_T *argvars;
16645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016647 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016649 char_u *infile = NULL;
16650 char_u buf[NUMBUFLEN];
16651 int err = FALSE;
16652 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016654 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016655 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016656
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016657 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016658 {
16659 /*
16660 * Write the string to a temp file, to be used for input of the shell
16661 * command.
16662 */
16663 if ((infile = vim_tempname('i')) == NULL)
16664 {
16665 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016666 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016667 }
16668
16669 fd = mch_fopen((char *)infile, WRITEBIN);
16670 if (fd == NULL)
16671 {
16672 EMSG2(_(e_notopen), infile);
16673 goto done;
16674 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016675 p = get_tv_string_buf_chk(&argvars[1], buf);
16676 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016677 {
16678 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016679 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016680 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016681 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16682 err = TRUE;
16683 if (fclose(fd) != 0)
16684 err = TRUE;
16685 if (err)
16686 {
16687 EMSG(_("E677: Error writing temp file"));
16688 goto done;
16689 }
16690 }
16691
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016692 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16693 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016694
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695#ifdef USE_CR
16696 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016697 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 {
16699 char_u *s;
16700
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016701 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 {
16703 if (*s == CAR)
16704 *s = NL;
16705 }
16706 }
16707#else
16708# ifdef USE_CRNL
16709 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016710 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 {
16712 char_u *s, *d;
16713
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016714 d = res;
16715 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 {
16717 if (s[0] == CAR && s[1] == NL)
16718 ++s;
16719 *d++ = *s;
16720 }
16721 *d = NUL;
16722 }
16723# endif
16724#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016725
16726done:
16727 if (infile != NULL)
16728 {
16729 mch_remove(infile);
16730 vim_free(infile);
16731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016732 rettv->v_type = VAR_STRING;
16733 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734}
16735
16736/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016737 * "tabpagebuflist()" function
16738 */
16739/* ARGSUSED */
16740 static void
16741f_tabpagebuflist(argvars, rettv)
16742 typval_T *argvars;
16743 typval_T *rettv;
16744{
16745#ifndef FEAT_WINDOWS
16746 rettv->vval.v_number = 0;
16747#else
16748 tabpage_T *tp;
16749 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016750
16751 if (argvars[0].v_type == VAR_UNKNOWN)
16752 wp = firstwin;
16753 else
16754 {
16755 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16756 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016757 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016758 }
16759 if (wp == NULL)
16760 rettv->vval.v_number = 0;
16761 else
16762 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016763 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016764 rettv->vval.v_number = 0;
16765 else
16766 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016767 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016768 if (list_append_number(rettv->vval.v_list,
16769 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016770 break;
16771 }
16772 }
16773#endif
16774}
16775
16776
16777/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016778 * "tabpagenr()" function
16779 */
16780/* ARGSUSED */
16781 static void
16782f_tabpagenr(argvars, rettv)
16783 typval_T *argvars;
16784 typval_T *rettv;
16785{
16786 int nr = 1;
16787#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016788 char_u *arg;
16789
16790 if (argvars[0].v_type != VAR_UNKNOWN)
16791 {
16792 arg = get_tv_string_chk(&argvars[0]);
16793 nr = 0;
16794 if (arg != NULL)
16795 {
16796 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016797 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016798 else
16799 EMSG2(_(e_invexpr2), arg);
16800 }
16801 }
16802 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016803 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016804#endif
16805 rettv->vval.v_number = nr;
16806}
16807
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016808
16809#ifdef FEAT_WINDOWS
16810static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16811
16812/*
16813 * Common code for tabpagewinnr() and winnr().
16814 */
16815 static int
16816get_winnr(tp, argvar)
16817 tabpage_T *tp;
16818 typval_T *argvar;
16819{
16820 win_T *twin;
16821 int nr = 1;
16822 win_T *wp;
16823 char_u *arg;
16824
16825 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16826 if (argvar->v_type != VAR_UNKNOWN)
16827 {
16828 arg = get_tv_string_chk(argvar);
16829 if (arg == NULL)
16830 nr = 0; /* type error; errmsg already given */
16831 else if (STRCMP(arg, "$") == 0)
16832 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16833 else if (STRCMP(arg, "#") == 0)
16834 {
16835 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16836 if (twin == NULL)
16837 nr = 0;
16838 }
16839 else
16840 {
16841 EMSG2(_(e_invexpr2), arg);
16842 nr = 0;
16843 }
16844 }
16845
16846 if (nr > 0)
16847 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16848 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016849 {
16850 if (wp == NULL)
16851 {
16852 /* didn't find it in this tabpage */
16853 nr = 0;
16854 break;
16855 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016856 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016857 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016858 return nr;
16859}
16860#endif
16861
16862/*
16863 * "tabpagewinnr()" function
16864 */
16865/* ARGSUSED */
16866 static void
16867f_tabpagewinnr(argvars, rettv)
16868 typval_T *argvars;
16869 typval_T *rettv;
16870{
16871 int nr = 1;
16872#ifdef FEAT_WINDOWS
16873 tabpage_T *tp;
16874
16875 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16876 if (tp == NULL)
16877 nr = 0;
16878 else
16879 nr = get_winnr(tp, &argvars[1]);
16880#endif
16881 rettv->vval.v_number = nr;
16882}
16883
16884
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016885/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016886 * "tagfiles()" function
16887 */
16888/*ARGSUSED*/
16889 static void
16890f_tagfiles(argvars, rettv)
16891 typval_T *argvars;
16892 typval_T *rettv;
16893{
16894 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016895 tagname_T tn;
16896 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016898 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016899 {
16900 rettv->vval.v_number = 0;
16901 return;
16902 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016903
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016904 for (first = TRUE; ; first = FALSE)
16905 if (get_tagfname(&tn, first, fname) == FAIL
16906 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016907 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016908 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016909}
16910
16911/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016912 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016913 */
16914 static void
16915f_taglist(argvars, rettv)
16916 typval_T *argvars;
16917 typval_T *rettv;
16918{
16919 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016920
16921 tag_pattern = get_tv_string(&argvars[0]);
16922
16923 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016924 if (*tag_pattern == NUL)
16925 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016927 if (rettv_list_alloc(rettv) == OK)
16928 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016929}
16930
16931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 * "tempname()" function
16933 */
16934/*ARGSUSED*/
16935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016936f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016937 typval_T *argvars;
16938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939{
16940 static int x = 'A';
16941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016942 rettv->v_type = VAR_STRING;
16943 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944
16945 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16946 * names. Skip 'I' and 'O', they are used for shell redirection. */
16947 do
16948 {
16949 if (x == 'Z')
16950 x = '0';
16951 else if (x == '9')
16952 x = 'A';
16953 else
16954 {
16955#ifdef EBCDIC
16956 if (x == 'I')
16957 x = 'J';
16958 else if (x == 'R')
16959 x = 'S';
16960 else
16961#endif
16962 ++x;
16963 }
16964 } while (x == 'I' || x == 'O');
16965}
16966
16967/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016968 * "test(list)" function: Just checking the walls...
16969 */
16970/*ARGSUSED*/
16971 static void
16972f_test(argvars, rettv)
16973 typval_T *argvars;
16974 typval_T *rettv;
16975{
16976 /* Used for unit testing. Change the code below to your liking. */
16977#if 0
16978 listitem_T *li;
16979 list_T *l;
16980 char_u *bad, *good;
16981
16982 if (argvars[0].v_type != VAR_LIST)
16983 return;
16984 l = argvars[0].vval.v_list;
16985 if (l == NULL)
16986 return;
16987 li = l->lv_first;
16988 if (li == NULL)
16989 return;
16990 bad = get_tv_string(&li->li_tv);
16991 li = li->li_next;
16992 if (li == NULL)
16993 return;
16994 good = get_tv_string(&li->li_tv);
16995 rettv->vval.v_number = test_edit_score(bad, good);
16996#endif
16997}
16998
16999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 * "tolower(string)" function
17001 */
17002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017003f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 typval_T *argvars;
17005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006{
17007 char_u *p;
17008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017009 p = vim_strsave(get_tv_string(&argvars[0]));
17010 rettv->v_type = VAR_STRING;
17011 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012
17013 if (p != NULL)
17014 while (*p != NUL)
17015 {
17016#ifdef FEAT_MBYTE
17017 int l;
17018
17019 if (enc_utf8)
17020 {
17021 int c, lc;
17022
17023 c = utf_ptr2char(p);
17024 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017025 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 /* TODO: reallocate string when byte count changes. */
17027 if (utf_char2len(lc) == l)
17028 utf_char2bytes(lc, p);
17029 p += l;
17030 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017031 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 p += l; /* skip multi-byte character */
17033 else
17034#endif
17035 {
17036 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17037 ++p;
17038 }
17039 }
17040}
17041
17042/*
17043 * "toupper(string)" function
17044 */
17045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017046f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017047 typval_T *argvars;
17048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052}
17053
17054/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017055 * "tr(string, fromstr, tostr)" function
17056 */
17057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017058f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017059 typval_T *argvars;
17060 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017061{
17062 char_u *instr;
17063 char_u *fromstr;
17064 char_u *tostr;
17065 char_u *p;
17066#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017067 int inlen;
17068 int fromlen;
17069 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017070 int idx;
17071 char_u *cpstr;
17072 int cplen;
17073 int first = TRUE;
17074#endif
17075 char_u buf[NUMBUFLEN];
17076 char_u buf2[NUMBUFLEN];
17077 garray_T ga;
17078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17081 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017082
17083 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017084 rettv->v_type = VAR_STRING;
17085 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017086 if (fromstr == NULL || tostr == NULL)
17087 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017088 ga_init2(&ga, (int)sizeof(char), 80);
17089
17090#ifdef FEAT_MBYTE
17091 if (!has_mbyte)
17092#endif
17093 /* not multi-byte: fromstr and tostr must be the same length */
17094 if (STRLEN(fromstr) != STRLEN(tostr))
17095 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017096#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017097error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017098#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017099 EMSG2(_(e_invarg2), fromstr);
17100 ga_clear(&ga);
17101 return;
17102 }
17103
17104 /* fromstr and tostr have to contain the same number of chars */
17105 while (*instr != NUL)
17106 {
17107#ifdef FEAT_MBYTE
17108 if (has_mbyte)
17109 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017110 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017111 cpstr = instr;
17112 cplen = inlen;
17113 idx = 0;
17114 for (p = fromstr; *p != NUL; p += fromlen)
17115 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017116 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017117 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17118 {
17119 for (p = tostr; *p != NUL; p += tolen)
17120 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017121 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017122 if (idx-- == 0)
17123 {
17124 cplen = tolen;
17125 cpstr = p;
17126 break;
17127 }
17128 }
17129 if (*p == NUL) /* tostr is shorter than fromstr */
17130 goto error;
17131 break;
17132 }
17133 ++idx;
17134 }
17135
17136 if (first && cpstr == instr)
17137 {
17138 /* Check that fromstr and tostr have the same number of
17139 * (multi-byte) characters. Done only once when a character
17140 * of instr doesn't appear in fromstr. */
17141 first = FALSE;
17142 for (p = tostr; *p != NUL; p += tolen)
17143 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017144 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017145 --idx;
17146 }
17147 if (idx != 0)
17148 goto error;
17149 }
17150
17151 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017152 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017153 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017154
17155 instr += inlen;
17156 }
17157 else
17158#endif
17159 {
17160 /* When not using multi-byte chars we can do it faster. */
17161 p = vim_strchr(fromstr, *instr);
17162 if (p != NULL)
17163 ga_append(&ga, tostr[p - fromstr]);
17164 else
17165 ga_append(&ga, *instr);
17166 ++instr;
17167 }
17168 }
17169
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017170 /* add a terminating NUL */
17171 ga_grow(&ga, 1);
17172 ga_append(&ga, NUL);
17173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017174 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017175}
17176
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017177#ifdef FEAT_FLOAT
17178/*
17179 * "trunc({float})" function
17180 */
17181 static void
17182f_trunc(argvars, rettv)
17183 typval_T *argvars;
17184 typval_T *rettv;
17185{
17186 float_T f;
17187
17188 rettv->v_type = VAR_FLOAT;
17189 if (get_float_arg(argvars, &f) == OK)
17190 /* trunc() is not in C90, use floor() or ceil() instead. */
17191 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17192 else
17193 rettv->vval.v_float = 0.0;
17194}
17195#endif
17196
Bram Moolenaar8299df92004-07-10 09:47:34 +000017197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 * "type(expr)" function
17199 */
17200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017202 typval_T *argvars;
17203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017205 int n;
17206
17207 switch (argvars[0].v_type)
17208 {
17209 case VAR_NUMBER: n = 0; break;
17210 case VAR_STRING: n = 1; break;
17211 case VAR_FUNC: n = 2; break;
17212 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017213 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017214#ifdef FEAT_FLOAT
17215 case VAR_FLOAT: n = 5; break;
17216#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017217 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17218 }
17219 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220}
17221
17222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017223 * "values(dict)" function
17224 */
17225 static void
17226f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 typval_T *argvars;
17228 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017229{
17230 dict_list(argvars, rettv, 1);
17231}
17232
17233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 * "virtcol(string)" function
17235 */
17236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017237f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017238 typval_T *argvars;
17239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240{
17241 colnr_T vcol = 0;
17242 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017243 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017245 fp = var2fpos(&argvars[0], FALSE, &fnum);
17246 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17247 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 {
17249 getvvcol(curwin, fp, NULL, NULL, &vcol);
17250 ++vcol;
17251 }
17252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017253 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254}
17255
17256/*
17257 * "visualmode()" function
17258 */
17259/*ARGSUSED*/
17260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017261f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017262 typval_T *argvars;
17263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264{
17265#ifdef FEAT_VISUAL
17266 char_u str[2];
17267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 str[0] = curbuf->b_visual_mode_eval;
17270 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017271 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272
17273 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017274 if ((argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number != 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017275 || (argvars[0].v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017276 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 curbuf->b_visual_mode_eval = NUL;
17278#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280#endif
17281}
17282
17283/*
17284 * "winbufnr(nr)" function
17285 */
17286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017287f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017288 typval_T *argvars;
17289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290{
17291 win_T *wp;
17292
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017293 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298}
17299
17300/*
17301 * "wincol()" function
17302 */
17303/*ARGSUSED*/
17304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017305f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017306 typval_T *argvars;
17307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308{
17309 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311}
17312
17313/*
17314 * "winheight(nr)" function
17315 */
17316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017317f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017318 typval_T *argvars;
17319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320{
17321 win_T *wp;
17322
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017323 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017327 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328}
17329
17330/*
17331 * "winline()" function
17332 */
17333/*ARGSUSED*/
17334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017336 typval_T *argvars;
17337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338{
17339 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341}
17342
17343/*
17344 * "winnr()" function
17345 */
17346/* ARGSUSED */
17347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 typval_T *argvars;
17350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351{
17352 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017353
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017355 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017357 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358}
17359
17360/*
17361 * "winrestcmd()" function
17362 */
17363/* ARGSUSED */
17364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017365f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017366 typval_T *argvars;
17367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368{
17369#ifdef FEAT_WINDOWS
17370 win_T *wp;
17371 int winnr = 1;
17372 garray_T ga;
17373 char_u buf[50];
17374
17375 ga_init2(&ga, (int)sizeof(char), 70);
17376 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17377 {
17378 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17379 ga_concat(&ga, buf);
17380# ifdef FEAT_VERTSPLIT
17381 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17382 ga_concat(&ga, buf);
17383# endif
17384 ++winnr;
17385 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017386 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017392 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393}
17394
17395/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017396 * "winrestview()" function
17397 */
17398/* ARGSUSED */
17399 static void
17400f_winrestview(argvars, rettv)
17401 typval_T *argvars;
17402 typval_T *rettv;
17403{
17404 dict_T *dict;
17405
17406 if (argvars[0].v_type != VAR_DICT
17407 || (dict = argvars[0].vval.v_dict) == NULL)
17408 EMSG(_(e_invarg));
17409 else
17410 {
17411 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17412 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17413#ifdef FEAT_VIRTUALEDIT
17414 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17415#endif
17416 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017417 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017418
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017419 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017420#ifdef FEAT_DIFF
17421 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17422#endif
17423 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17424 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17425
17426 check_cursor();
17427 changed_cline_bef_curs();
17428 invalidate_botline();
17429 redraw_later(VALID);
17430
17431 if (curwin->w_topline == 0)
17432 curwin->w_topline = 1;
17433 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17434 curwin->w_topline = curbuf->b_ml.ml_line_count;
17435#ifdef FEAT_DIFF
17436 check_topfill(curwin, TRUE);
17437#endif
17438 }
17439}
17440
17441/*
17442 * "winsaveview()" function
17443 */
17444/* ARGSUSED */
17445 static void
17446f_winsaveview(argvars, rettv)
17447 typval_T *argvars;
17448 typval_T *rettv;
17449{
17450 dict_T *dict;
17451
17452 dict = dict_alloc();
17453 if (dict == NULL)
17454 return;
17455 rettv->v_type = VAR_DICT;
17456 rettv->vval.v_dict = dict;
17457 ++dict->dv_refcount;
17458
17459 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17460 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17461#ifdef FEAT_VIRTUALEDIT
17462 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17463#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017464 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017465 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17466
17467 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17468#ifdef FEAT_DIFF
17469 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17470#endif
17471 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17472 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17473}
17474
17475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 * "winwidth(nr)" function
17477 */
17478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017479f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 typval_T *argvars;
17481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482{
17483 win_T *wp;
17484
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017485 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017487 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 else
17489#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493#endif
17494}
17495
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017497 * "writefile()" function
17498 */
17499 static void
17500f_writefile(argvars, rettv)
17501 typval_T *argvars;
17502 typval_T *rettv;
17503{
17504 int binary = FALSE;
17505 char_u *fname;
17506 FILE *fd;
17507 listitem_T *li;
17508 char_u *s;
17509 int ret = 0;
17510 int c;
17511
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017512 if (check_restricted() || check_secure())
17513 return;
17514
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017515 if (argvars[0].v_type != VAR_LIST)
17516 {
17517 EMSG2(_(e_listarg), "writefile()");
17518 return;
17519 }
17520 if (argvars[0].vval.v_list == NULL)
17521 return;
17522
17523 if (argvars[2].v_type != VAR_UNKNOWN
17524 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17525 binary = TRUE;
17526
17527 /* Always open the file in binary mode, library functions have a mind of
17528 * their own about CR-LF conversion. */
17529 fname = get_tv_string(&argvars[1]);
17530 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17531 {
17532 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17533 ret = -1;
17534 }
17535 else
17536 {
17537 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17538 li = li->li_next)
17539 {
17540 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17541 {
17542 if (*s == '\n')
17543 c = putc(NUL, fd);
17544 else
17545 c = putc(*s, fd);
17546 if (c == EOF)
17547 {
17548 ret = -1;
17549 break;
17550 }
17551 }
17552 if (!binary || li->li_next != NULL)
17553 if (putc('\n', fd) == EOF)
17554 {
17555 ret = -1;
17556 break;
17557 }
17558 if (ret < 0)
17559 {
17560 EMSG(_(e_write));
17561 break;
17562 }
17563 }
17564 fclose(fd);
17565 }
17566
17567 rettv->vval.v_number = ret;
17568}
17569
17570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017572 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 */
17574 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017575var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017577 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017578 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017580 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017582 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583
Bram Moolenaara5525202006-03-02 22:52:09 +000017584 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017585 if (varp->v_type == VAR_LIST)
17586 {
17587 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017588 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017589 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017590 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017591
17592 l = varp->vval.v_list;
17593 if (l == NULL)
17594 return NULL;
17595
17596 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017597 pos.lnum = list_find_nr(l, 0L, &error);
17598 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017599 return NULL; /* invalid line number */
17600
17601 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017602 pos.col = list_find_nr(l, 1L, &error);
17603 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017604 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017605 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017606
17607 /* We accept "$" for the column number: last column. */
17608 li = list_find(l, 1L);
17609 if (li != NULL && li->li_tv.v_type == VAR_STRING
17610 && li->li_tv.vval.v_string != NULL
17611 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17612 pos.col = len + 1;
17613
Bram Moolenaara5525202006-03-02 22:52:09 +000017614 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017615 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017616 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017617 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017618
Bram Moolenaara5525202006-03-02 22:52:09 +000017619#ifdef FEAT_VIRTUALEDIT
17620 /* Get the virtual offset. Defaults to zero. */
17621 pos.coladd = list_find_nr(l, 2L, &error);
17622 if (error)
17623 pos.coladd = 0;
17624#endif
17625
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017626 return &pos;
17627 }
17628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017629 name = get_tv_string_chk(varp);
17630 if (name == NULL)
17631 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017632 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017634#ifdef FEAT_VISUAL
17635 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17636 {
17637 if (VIsual_active)
17638 return &VIsual;
17639 return &curwin->w_cursor;
17640 }
17641#endif
17642 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017644 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17646 return NULL;
17647 return pp;
17648 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017649
17650#ifdef FEAT_VIRTUALEDIT
17651 pos.coladd = 0;
17652#endif
17653
Bram Moolenaar477933c2007-07-17 14:32:23 +000017654 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017655 {
17656 pos.col = 0;
17657 if (name[1] == '0') /* "w0": first visible line */
17658 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017659 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017660 pos.lnum = curwin->w_topline;
17661 return &pos;
17662 }
17663 else if (name[1] == '$') /* "w$": last visible line */
17664 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017665 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017666 pos.lnum = curwin->w_botline - 1;
17667 return &pos;
17668 }
17669 }
17670 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017672 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 {
17674 pos.lnum = curbuf->b_ml.ml_line_count;
17675 pos.col = 0;
17676 }
17677 else
17678 {
17679 pos.lnum = curwin->w_cursor.lnum;
17680 pos.col = (colnr_T)STRLEN(ml_get_curline());
17681 }
17682 return &pos;
17683 }
17684 return NULL;
17685}
17686
17687/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017688 * Convert list in "arg" into a position and optional file number.
17689 * When "fnump" is NULL there is no file number, only 3 items.
17690 * Note that the column is passed on as-is, the caller may want to decrement
17691 * it to use 1 for the first column.
17692 * Return FAIL when conversion is not possible, doesn't check the position for
17693 * validity.
17694 */
17695 static int
17696list2fpos(arg, posp, fnump)
17697 typval_T *arg;
17698 pos_T *posp;
17699 int *fnump;
17700{
17701 list_T *l = arg->vval.v_list;
17702 long i = 0;
17703 long n;
17704
Bram Moolenaarbde35262006-07-23 20:12:24 +000017705 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17706 * when "fnump" isn't NULL and "coladd" is optional. */
17707 if (arg->v_type != VAR_LIST
17708 || l == NULL
17709 || l->lv_len < (fnump == NULL ? 2 : 3)
17710 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017711 return FAIL;
17712
17713 if (fnump != NULL)
17714 {
17715 n = list_find_nr(l, i++, NULL); /* fnum */
17716 if (n < 0)
17717 return FAIL;
17718 if (n == 0)
17719 n = curbuf->b_fnum; /* current buffer */
17720 *fnump = n;
17721 }
17722
17723 n = list_find_nr(l, i++, NULL); /* lnum */
17724 if (n < 0)
17725 return FAIL;
17726 posp->lnum = n;
17727
17728 n = list_find_nr(l, i++, NULL); /* col */
17729 if (n < 0)
17730 return FAIL;
17731 posp->col = n;
17732
17733#ifdef FEAT_VIRTUALEDIT
17734 n = list_find_nr(l, i, NULL);
17735 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017736 posp->coladd = 0;
17737 else
17738 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017739#endif
17740
17741 return OK;
17742}
17743
17744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 * Get the length of an environment variable name.
17746 * Advance "arg" to the first character after the name.
17747 * Return 0 for error.
17748 */
17749 static int
17750get_env_len(arg)
17751 char_u **arg;
17752{
17753 char_u *p;
17754 int len;
17755
17756 for (p = *arg; vim_isIDc(*p); ++p)
17757 ;
17758 if (p == *arg) /* no name found */
17759 return 0;
17760
17761 len = (int)(p - *arg);
17762 *arg = p;
17763 return len;
17764}
17765
17766/*
17767 * Get the length of the name of a function or internal variable.
17768 * "arg" is advanced to the first non-white character after the name.
17769 * Return 0 if something is wrong.
17770 */
17771 static int
17772get_id_len(arg)
17773 char_u **arg;
17774{
17775 char_u *p;
17776 int len;
17777
17778 /* Find the end of the name. */
17779 for (p = *arg; eval_isnamec(*p); ++p)
17780 ;
17781 if (p == *arg) /* no name found */
17782 return 0;
17783
17784 len = (int)(p - *arg);
17785 *arg = skipwhite(p);
17786
17787 return len;
17788}
17789
17790/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017791 * Get the length of the name of a variable or function.
17792 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017794 * Return -1 if curly braces expansion failed.
17795 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 * If the name contains 'magic' {}'s, expand them and return the
17797 * expanded name in an allocated string via 'alias' - caller must free.
17798 */
17799 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017800get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 char_u **arg;
17802 char_u **alias;
17803 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017804 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805{
17806 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 char_u *p;
17808 char_u *expr_start;
17809 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
17811 *alias = NULL; /* default to no alias */
17812
17813 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17814 && (*arg)[2] == (int)KE_SNR)
17815 {
17816 /* hard coded <SNR>, already translated */
17817 *arg += 3;
17818 return get_id_len(arg) + 3;
17819 }
17820 len = eval_fname_script(*arg);
17821 if (len > 0)
17822 {
17823 /* literal "<SID>", "s:" or "<SNR>" */
17824 *arg += len;
17825 }
17826
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017828 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017830 p = find_name_end(*arg, &expr_start, &expr_end,
17831 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 if (expr_start != NULL)
17833 {
17834 char_u *temp_string;
17835
17836 if (!evaluate)
17837 {
17838 len += (int)(p - *arg);
17839 *arg = skipwhite(p);
17840 return len;
17841 }
17842
17843 /*
17844 * Include any <SID> etc in the expanded string:
17845 * Thus the -len here.
17846 */
17847 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17848 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017849 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 *alias = temp_string;
17851 *arg = skipwhite(p);
17852 return (int)STRLEN(temp_string);
17853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854
17855 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017856 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 EMSG2(_(e_invexpr2), *arg);
17858
17859 return len;
17860}
17861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017862/*
17863 * Find the end of a variable or function name, taking care of magic braces.
17864 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17865 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017866 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017867 * Return a pointer to just after the name. Equal to "arg" if there is no
17868 * valid name.
17869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017871find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 char_u *arg;
17873 char_u **expr_start;
17874 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017875 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017877 int mb_nest = 0;
17878 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 char_u *p;
17880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017883 *expr_start = NULL;
17884 *expr_end = NULL;
17885 }
17886
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017887 /* Quick check for valid starting character. */
17888 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17889 return arg;
17890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017891 for (p = arg; *p != NUL
17892 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017893 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017894 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017896 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017897 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017898 if (*p == '\'')
17899 {
17900 /* skip over 'string' to avoid counting [ and ] inside it. */
17901 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17902 ;
17903 if (*p == NUL)
17904 break;
17905 }
17906 else if (*p == '"')
17907 {
17908 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17909 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17910 if (*p == '\\' && p[1] != NUL)
17911 ++p;
17912 if (*p == NUL)
17913 break;
17914 }
17915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017916 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918 if (*p == '[')
17919 ++br_nest;
17920 else if (*p == ']')
17921 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017924 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017926 if (*p == '{')
17927 {
17928 mb_nest++;
17929 if (expr_start != NULL && *expr_start == NULL)
17930 *expr_start = p;
17931 }
17932 else if (*p == '}')
17933 {
17934 mb_nest--;
17935 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17936 *expr_end = p;
17937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 }
17940
17941 return p;
17942}
17943
17944/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017945 * Expands out the 'magic' {}'s in a variable/function name.
17946 * Note that this can call itself recursively, to deal with
17947 * constructs like foo{bar}{baz}{bam}
17948 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17949 * "in_start" ^
17950 * "expr_start" ^
17951 * "expr_end" ^
17952 * "in_end" ^
17953 *
17954 * Returns a new allocated string, which the caller must free.
17955 * Returns NULL for failure.
17956 */
17957 static char_u *
17958make_expanded_name(in_start, expr_start, expr_end, in_end)
17959 char_u *in_start;
17960 char_u *expr_start;
17961 char_u *expr_end;
17962 char_u *in_end;
17963{
17964 char_u c1;
17965 char_u *retval = NULL;
17966 char_u *temp_result;
17967 char_u *nextcmd = NULL;
17968
17969 if (expr_end == NULL || in_end == NULL)
17970 return NULL;
17971 *expr_start = NUL;
17972 *expr_end = NUL;
17973 c1 = *in_end;
17974 *in_end = NUL;
17975
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017976 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017977 if (temp_result != NULL && nextcmd == NULL)
17978 {
17979 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17980 + (in_end - expr_end) + 1));
17981 if (retval != NULL)
17982 {
17983 STRCPY(retval, in_start);
17984 STRCAT(retval, temp_result);
17985 STRCAT(retval, expr_end + 1);
17986 }
17987 }
17988 vim_free(temp_result);
17989
17990 *in_end = c1; /* put char back for error messages */
17991 *expr_start = '{';
17992 *expr_end = '}';
17993
17994 if (retval != NULL)
17995 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017996 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017997 if (expr_start != NULL)
17998 {
17999 /* Further expansion! */
18000 temp_result = make_expanded_name(retval, expr_start,
18001 expr_end, temp_result);
18002 vim_free(retval);
18003 retval = temp_result;
18004 }
18005 }
18006
18007 return retval;
18008}
18009
18010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018012 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 */
18014 static int
18015eval_isnamec(c)
18016 int c;
18017{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018018 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18019}
18020
18021/*
18022 * Return TRUE if character "c" can be used as the first character in a
18023 * variable or function name (excluding '{' and '}').
18024 */
18025 static int
18026eval_isnamec1(c)
18027 int c;
18028{
18029 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030}
18031
18032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 * Set number v: variable to "val".
18034 */
18035 void
18036set_vim_var_nr(idx, val)
18037 int idx;
18038 long val;
18039{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018040 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041}
18042
18043/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018044 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 */
18046 long
18047get_vim_var_nr(idx)
18048 int idx;
18049{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018050 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051}
18052
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018053#if defined(FEAT_AUTOCMD) || defined(PROTO)
18054/*
18055 * Get string v: variable value. Uses a static buffer, can only be used once.
18056 */
18057 char_u *
18058get_vim_var_str(idx)
18059 int idx;
18060{
18061 return get_tv_string(&vimvars[idx].vv_tv);
18062}
18063#endif
18064
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065/*
18066 * Set v:count, v:count1 and v:prevcount.
18067 */
18068 void
18069set_vcount(count, count1)
18070 long count;
18071 long count1;
18072{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018073 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
18074 vimvars[VV_COUNT].vv_nr = count;
18075 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076}
18077
18078/*
18079 * Set string v: variable to a copy of "val".
18080 */
18081 void
18082set_vim_var_string(idx, val, len)
18083 int idx;
18084 char_u *val;
18085 int len; /* length of "val" to use or -1 (whole string) */
18086{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018087 /* Need to do this (at least) once, since we can't initialize a union.
18088 * Will always be invoked when "v:progname" is set. */
18089 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18090
Bram Moolenaare9a41262005-01-15 22:18:47 +000018091 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018093 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018095 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018097 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098}
18099
18100/*
18101 * Set v:register if needed.
18102 */
18103 void
18104set_reg_var(c)
18105 int c;
18106{
18107 char_u regname;
18108
18109 if (c == 0 || c == ' ')
18110 regname = '"';
18111 else
18112 regname = c;
18113 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018114 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 set_vim_var_string(VV_REG, &regname, 1);
18116}
18117
18118/*
18119 * Get or set v:exception. If "oldval" == NULL, return the current value.
18120 * Otherwise, restore the value to "oldval" and return NULL.
18121 * Must always be called in pairs to save and restore v:exception! Does not
18122 * take care of memory allocations.
18123 */
18124 char_u *
18125v_exception(oldval)
18126 char_u *oldval;
18127{
18128 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018129 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130
Bram Moolenaare9a41262005-01-15 22:18:47 +000018131 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 return NULL;
18133}
18134
18135/*
18136 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18137 * Otherwise, restore the value to "oldval" and return NULL.
18138 * Must always be called in pairs to save and restore v:throwpoint! Does not
18139 * take care of memory allocations.
18140 */
18141 char_u *
18142v_throwpoint(oldval)
18143 char_u *oldval;
18144{
18145 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018146 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147
Bram Moolenaare9a41262005-01-15 22:18:47 +000018148 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 return NULL;
18150}
18151
18152#if defined(FEAT_AUTOCMD) || defined(PROTO)
18153/*
18154 * Set v:cmdarg.
18155 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18156 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18157 * Must always be called in pairs!
18158 */
18159 char_u *
18160set_cmdarg(eap, oldarg)
18161 exarg_T *eap;
18162 char_u *oldarg;
18163{
18164 char_u *oldval;
18165 char_u *newval;
18166 unsigned len;
18167
Bram Moolenaare9a41262005-01-15 22:18:47 +000018168 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018169 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018171 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018172 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018173 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 }
18175
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018176 if (eap->force_bin == FORCE_BIN)
18177 len = 6;
18178 else if (eap->force_bin == FORCE_NOBIN)
18179 len = 8;
18180 else
18181 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018182
18183 if (eap->read_edit)
18184 len += 7;
18185
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018186 if (eap->force_ff != 0)
18187 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18188# ifdef FEAT_MBYTE
18189 if (eap->force_enc != 0)
18190 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018191 if (eap->bad_char != 0)
18192 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018193# endif
18194
18195 newval = alloc(len + 1);
18196 if (newval == NULL)
18197 return NULL;
18198
18199 if (eap->force_bin == FORCE_BIN)
18200 sprintf((char *)newval, " ++bin");
18201 else if (eap->force_bin == FORCE_NOBIN)
18202 sprintf((char *)newval, " ++nobin");
18203 else
18204 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018205
18206 if (eap->read_edit)
18207 STRCAT(newval, " ++edit");
18208
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018209 if (eap->force_ff != 0)
18210 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18211 eap->cmd + eap->force_ff);
18212# ifdef FEAT_MBYTE
18213 if (eap->force_enc != 0)
18214 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18215 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018216 if (eap->bad_char != 0)
18217 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18218 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018219# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018220 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018221 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222}
18223#endif
18224
18225/*
18226 * Get the value of internal variable "name".
18227 * Return OK or FAIL.
18228 */
18229 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018230get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 char_u *name;
18232 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018233 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018234 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235{
18236 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018237 typval_T *tv = NULL;
18238 typval_T atv;
18239 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241
18242 /* truncate the name, so that we can use strcmp() */
18243 cc = name[len];
18244 name[len] = NUL;
18245
18246 /*
18247 * Check for "b:changedtick".
18248 */
18249 if (STRCMP(name, "b:changedtick") == 0)
18250 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018251 atv.v_type = VAR_NUMBER;
18252 atv.vval.v_number = curbuf->b_changedtick;
18253 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 }
18255
18256 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 * Check for user-defined variables.
18258 */
18259 else
18260 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018261 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018263 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 }
18265
Bram Moolenaare9a41262005-01-15 22:18:47 +000018266 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018268 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018269 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 ret = FAIL;
18271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018272 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018273 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274
18275 name[len] = cc;
18276
18277 return ret;
18278}
18279
18280/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018281 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18282 * Also handle function call with Funcref variable: func(expr)
18283 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18284 */
18285 static int
18286handle_subscript(arg, rettv, evaluate, verbose)
18287 char_u **arg;
18288 typval_T *rettv;
18289 int evaluate; /* do more than finding the end */
18290 int verbose; /* give error messages */
18291{
18292 int ret = OK;
18293 dict_T *selfdict = NULL;
18294 char_u *s;
18295 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018296 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018297
18298 while (ret == OK
18299 && (**arg == '['
18300 || (**arg == '.' && rettv->v_type == VAR_DICT)
18301 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18302 && !vim_iswhite(*(*arg - 1)))
18303 {
18304 if (**arg == '(')
18305 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018306 /* need to copy the funcref so that we can clear rettv */
18307 functv = *rettv;
18308 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018309
18310 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018311 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018312 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018313 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18314 &len, evaluate, selfdict);
18315
18316 /* Clear the funcref afterwards, so that deleting it while
18317 * evaluating the arguments is possible (see test55). */
18318 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018319
18320 /* Stop the expression evaluation when immediately aborting on
18321 * error, or when an interrupt occurred or an exception was thrown
18322 * but not caught. */
18323 if (aborting())
18324 {
18325 if (ret == OK)
18326 clear_tv(rettv);
18327 ret = FAIL;
18328 }
18329 dict_unref(selfdict);
18330 selfdict = NULL;
18331 }
18332 else /* **arg == '[' || **arg == '.' */
18333 {
18334 dict_unref(selfdict);
18335 if (rettv->v_type == VAR_DICT)
18336 {
18337 selfdict = rettv->vval.v_dict;
18338 if (selfdict != NULL)
18339 ++selfdict->dv_refcount;
18340 }
18341 else
18342 selfdict = NULL;
18343 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18344 {
18345 clear_tv(rettv);
18346 ret = FAIL;
18347 }
18348 }
18349 }
18350 dict_unref(selfdict);
18351 return ret;
18352}
18353
18354/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018355 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018356 * value).
18357 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018358 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018359alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018360{
Bram Moolenaar33570922005-01-25 22:26:29 +000018361 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018362}
18363
18364/*
18365 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 * The string "s" must have been allocated, it is consumed.
18367 * Return NULL for out of memory, the variable otherwise.
18368 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018369 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018370alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 char_u *s;
18372{
Bram Moolenaar33570922005-01-25 22:26:29 +000018373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018375 rettv = alloc_tv();
18376 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018378 rettv->v_type = VAR_STRING;
18379 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 }
18381 else
18382 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384}
18385
18386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018387 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018389 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018390free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018391 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392{
18393 if (varp != NULL)
18394 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018395 switch (varp->v_type)
18396 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018397 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018398 func_unref(varp->vval.v_string);
18399 /*FALLTHROUGH*/
18400 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018401 vim_free(varp->vval.v_string);
18402 break;
18403 case VAR_LIST:
18404 list_unref(varp->vval.v_list);
18405 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018406 case VAR_DICT:
18407 dict_unref(varp->vval.v_dict);
18408 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018409 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018410#ifdef FEAT_FLOAT
18411 case VAR_FLOAT:
18412#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018413 case VAR_UNKNOWN:
18414 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018415 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018416 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018417 break;
18418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 vim_free(varp);
18420 }
18421}
18422
18423/*
18424 * Free the memory for a variable value and set the value to NULL or 0.
18425 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018426 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018427clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018428 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429{
18430 if (varp != NULL)
18431 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018432 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018434 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018435 func_unref(varp->vval.v_string);
18436 /*FALLTHROUGH*/
18437 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018438 vim_free(varp->vval.v_string);
18439 varp->vval.v_string = NULL;
18440 break;
18441 case VAR_LIST:
18442 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018443 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018444 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018445 case VAR_DICT:
18446 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018447 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018448 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018449 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018450 varp->vval.v_number = 0;
18451 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018452#ifdef FEAT_FLOAT
18453 case VAR_FLOAT:
18454 varp->vval.v_float = 0.0;
18455 break;
18456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018457 case VAR_UNKNOWN:
18458 break;
18459 default:
18460 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018462 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 }
18464}
18465
18466/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018467 * Set the value of a variable to NULL without freeing items.
18468 */
18469 static void
18470init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018471 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018472{
18473 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018474 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018475}
18476
18477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 * Get the number value of a variable.
18479 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018480 * For incompatible types, return 0.
18481 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18482 * caller of incompatible types: it sets *denote to TRUE if "denote"
18483 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 */
18485 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018487 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018489 int error = FALSE;
18490
18491 return get_tv_number_chk(varp, &error); /* return 0L on error */
18492}
18493
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018494 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018495get_tv_number_chk(varp, denote)
18496 typval_T *varp;
18497 int *denote;
18498{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018499 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018501 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018503 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018504 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018505#ifdef FEAT_FLOAT
18506 case VAR_FLOAT:
18507 EMSG(_("E805: Using a Float as a number"));
18508 break;
18509#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018510 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018511 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018512 break;
18513 case VAR_STRING:
18514 if (varp->vval.v_string != NULL)
18515 vim_str2nr(varp->vval.v_string, NULL, NULL,
18516 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018517 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018518 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018519 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018520 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018521 case VAR_DICT:
18522 EMSG(_("E728: Using a Dictionary as a number"));
18523 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018524 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018525 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018528 if (denote == NULL) /* useful for values that must be unsigned */
18529 n = -1;
18530 else
18531 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018532 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533}
18534
18535/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018536 * Get the lnum from the first argument.
18537 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018538 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 */
18540 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018541get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018542 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543{
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 linenr_T lnum;
18546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018547 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 if (lnum == 0) /* no valid number, try using line() */
18549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018550 rettv.v_type = VAR_NUMBER;
18551 f_line(argvars, &rettv);
18552 lnum = rettv.vval.v_number;
18553 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 }
18555 return lnum;
18556}
18557
18558/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018559 * Get the lnum from the first argument.
18560 * Also accepts "$", then "buf" is used.
18561 * Returns 0 on error.
18562 */
18563 static linenr_T
18564get_tv_lnum_buf(argvars, buf)
18565 typval_T *argvars;
18566 buf_T *buf;
18567{
18568 if (argvars[0].v_type == VAR_STRING
18569 && argvars[0].vval.v_string != NULL
18570 && argvars[0].vval.v_string[0] == '$'
18571 && buf != NULL)
18572 return buf->b_ml.ml_line_count;
18573 return get_tv_number_chk(&argvars[0], NULL);
18574}
18575
18576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 * Get the string value of a variable.
18578 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018579 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18580 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 * If the String variable has never been set, return an empty string.
18582 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018583 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18584 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 */
18586 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018588 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589{
18590 static char_u mybuf[NUMBUFLEN];
18591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593}
18594
18595 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018596get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018597 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018598 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018600 char_u *res = get_tv_string_buf_chk(varp, buf);
18601
18602 return res != NULL ? res : (char_u *)"";
18603}
18604
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018605 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018606get_tv_string_chk(varp)
18607 typval_T *varp;
18608{
18609 static char_u mybuf[NUMBUFLEN];
18610
18611 return get_tv_string_buf_chk(varp, mybuf);
18612}
18613
18614 static char_u *
18615get_tv_string_buf_chk(varp, buf)
18616 typval_T *varp;
18617 char_u *buf;
18618{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018619 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018621 case VAR_NUMBER:
18622 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18623 return buf;
18624 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018625 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018626 break;
18627 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018628 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018629 break;
18630 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018631 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018632 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018633#ifdef FEAT_FLOAT
18634 case VAR_FLOAT:
18635 EMSG(_("E806: using Float as a String"));
18636 break;
18637#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018638 case VAR_STRING:
18639 if (varp->vval.v_string != NULL)
18640 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018641 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018642 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018643 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018646 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647}
18648
18649/*
18650 * Find variable "name" in the list of variables.
18651 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018652 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018653 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018654 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018656 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018657find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018659 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663
Bram Moolenaara7043832005-01-21 11:56:39 +000018664 ht = find_var_ht(name, &varname);
18665 if (htp != NULL)
18666 *htp = ht;
18667 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018669 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670}
18671
18672/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018673 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018674 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018676 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018677find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018678 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018679 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018680 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018681{
Bram Moolenaar33570922005-01-25 22:26:29 +000018682 hashitem_T *hi;
18683
18684 if (*varname == NUL)
18685 {
18686 /* Must be something like "s:", otherwise "ht" would be NULL. */
18687 switch (varname[-2])
18688 {
18689 case 's': return &SCRIPT_SV(current_SID).sv_var;
18690 case 'g': return &globvars_var;
18691 case 'v': return &vimvars_var;
18692 case 'b': return &curbuf->b_bufvar;
18693 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018694#ifdef FEAT_WINDOWS
18695 case 't': return &curtab->tp_winvar;
18696#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018697 case 'l': return current_funccal == NULL
18698 ? NULL : &current_funccal->l_vars_var;
18699 case 'a': return current_funccal == NULL
18700 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018701 }
18702 return NULL;
18703 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018704
18705 hi = hash_find(ht, varname);
18706 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018707 {
18708 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018709 * worked find the variable again. Don't auto-load a script if it was
18710 * loaded already, otherwise it would be loaded every time when
18711 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018712 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018713 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018714 hi = hash_find(ht, varname);
18715 if (HASHITEM_EMPTY(hi))
18716 return NULL;
18717 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018719}
18720
18721/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018722 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018723 * Set "varname" to the start of name without ':'.
18724 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018725 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018726find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 char_u *name;
18728 char_u **varname;
18729{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018730 hashitem_T *hi;
18731
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 if (name[1] != ':')
18733 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018734 /* The name must not start with a colon or #. */
18735 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 return NULL;
18737 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018738
18739 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018740 hi = hash_find(&compat_hashtab, name);
18741 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018742 return &compat_hashtab;
18743
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018745 return &globvarht; /* global variable */
18746 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 }
18748 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018749 if (*name == 'g') /* global variable */
18750 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018751 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18752 */
18753 if (vim_strchr(name + 2, ':') != NULL
18754 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018755 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018757 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018759 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018760#ifdef FEAT_WINDOWS
18761 if (*name == 't') /* tab page variable */
18762 return &curtab->tp_vars.dv_hashtab;
18763#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018764 if (*name == 'v') /* v: variable */
18765 return &vimvarht;
18766 if (*name == 'a' && current_funccal != NULL) /* function argument */
18767 return &current_funccal->l_avars.dv_hashtab;
18768 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18769 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 if (*name == 's' /* script variable */
18771 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18772 return &SCRIPT_VARS(current_SID);
18773 return NULL;
18774}
18775
18776/*
18777 * Get the string value of a (global/local) variable.
18778 * Returns NULL when it doesn't exist.
18779 */
18780 char_u *
18781get_var_value(name)
18782 char_u *name;
18783{
Bram Moolenaar33570922005-01-25 22:26:29 +000018784 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785
Bram Moolenaara7043832005-01-21 11:56:39 +000018786 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 if (v == NULL)
18788 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018789 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790}
18791
18792/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 * sourcing this script and when executing functions defined in the script.
18795 */
18796 void
18797new_script_vars(id)
18798 scid_T id;
18799{
Bram Moolenaara7043832005-01-21 11:56:39 +000018800 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018801 hashtab_T *ht;
18802 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018803
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18805 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018806 /* Re-allocating ga_data means that an ht_array pointing to
18807 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018808 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018809 for (i = 1; i <= ga_scripts.ga_len; ++i)
18810 {
18811 ht = &SCRIPT_VARS(i);
18812 if (ht->ht_mask == HT_INIT_SIZE - 1)
18813 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018814 sv = &SCRIPT_SV(i);
18815 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018816 }
18817
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 while (ga_scripts.ga_len < id)
18819 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018820 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18821 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 }
18824 }
18825}
18826
18827/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18829 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830 */
18831 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018832init_var_dict(dict, dict_var)
18833 dict_T *dict;
18834 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835{
Bram Moolenaar33570922005-01-25 22:26:29 +000018836 hash_init(&dict->dv_hashtab);
18837 dict->dv_refcount = 99999;
18838 dict_var->di_tv.vval.v_dict = dict;
18839 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018840 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18842 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843}
18844
18845/*
18846 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018847 * Frees all allocated variables and the value they contain.
18848 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 */
18850 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018851vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018852 hashtab_T *ht;
18853{
18854 vars_clear_ext(ht, TRUE);
18855}
18856
18857/*
18858 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18859 */
18860 static void
18861vars_clear_ext(ht, free_val)
18862 hashtab_T *ht;
18863 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864{
Bram Moolenaara7043832005-01-21 11:56:39 +000018865 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018866 hashitem_T *hi;
18867 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018870 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018871 for (hi = ht->ht_array; todo > 0; ++hi)
18872 {
18873 if (!HASHITEM_EMPTY(hi))
18874 {
18875 --todo;
18876
Bram Moolenaar33570922005-01-25 22:26:29 +000018877 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018878 * ht_array might change then. hash_clear() takes care of it
18879 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018880 v = HI2DI(hi);
18881 if (free_val)
18882 clear_tv(&v->di_tv);
18883 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18884 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018885 }
18886 }
18887 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018888 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889}
18890
Bram Moolenaara7043832005-01-21 11:56:39 +000018891/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018892 * Delete a variable from hashtab "ht" at item "hi".
18893 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018896delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018897 hashtab_T *ht;
18898 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899{
Bram Moolenaar33570922005-01-25 22:26:29 +000018900 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018901
18902 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018903 clear_tv(&di->di_tv);
18904 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905}
18906
18907/*
18908 * List the value of one internal variable.
18909 */
18910 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018911list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018912 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018914 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018916 char_u *tofree;
18917 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018918 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018919
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018920 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018921 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018922 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018923 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924}
18925
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018927list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 char_u *prefix;
18929 char_u *name;
18930 int type;
18931 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018932 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933{
Bram Moolenaar31859182007-08-14 20:41:13 +000018934 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18935 msg_start();
18936 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 if (name != NULL) /* "a:" vars don't have a name stored */
18938 msg_puts(name);
18939 msg_putchar(' ');
18940 msg_advance(22);
18941 if (type == VAR_NUMBER)
18942 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018943 else if (type == VAR_FUNC)
18944 msg_putchar('*');
18945 else if (type == VAR_LIST)
18946 {
18947 msg_putchar('[');
18948 if (*string == '[')
18949 ++string;
18950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018951 else if (type == VAR_DICT)
18952 {
18953 msg_putchar('{');
18954 if (*string == '{')
18955 ++string;
18956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 else
18958 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018959
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018961
18962 if (type == VAR_FUNC)
18963 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018964 if (*first)
18965 {
18966 msg_clr_eos();
18967 *first = FALSE;
18968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969}
18970
18971/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018972 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 * If the variable already exists, the value is updated.
18974 * Otherwise the variable is created.
18975 */
18976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018977set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018979 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981{
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018985 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018988 {
18989 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18990 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18991 ? name[2] : name[0]))
18992 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018993 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018994 return;
18995 }
18996 if (function_exists(name))
18997 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018998 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018999 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019000 return;
19001 }
19002 }
19003
Bram Moolenaara7043832005-01-21 11:56:39 +000019004 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019005 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019006 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019007 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019008 return;
19009 }
19010
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019011 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019015 if (var_check_ro(v->di_flags, name)
19016 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 return;
19018 if (v->di_tv.v_type != tv->v_type
19019 && !((v->di_tv.v_type == VAR_STRING
19020 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019022 || tv->v_type == VAR_NUMBER))
19023#ifdef FEAT_FLOAT
19024 && !((v->di_tv.v_type == VAR_NUMBER
19025 || v->di_tv.v_type == VAR_FLOAT)
19026 && (tv->v_type == VAR_NUMBER
19027 || tv->v_type == VAR_FLOAT))
19028#endif
19029 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019030 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019031 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019032 return;
19033 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019034
19035 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019036 * Handle setting internal v: variables separately: we don't change
19037 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019038 */
19039 if (ht == &vimvarht)
19040 {
19041 if (v->di_tv.v_type == VAR_STRING)
19042 {
19043 vim_free(v->di_tv.vval.v_string);
19044 if (copy || tv->v_type != VAR_STRING)
19045 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19046 else
19047 {
19048 /* Take over the string to avoid an extra alloc/free. */
19049 v->di_tv.vval.v_string = tv->vval.v_string;
19050 tv->vval.v_string = NULL;
19051 }
19052 }
19053 else if (v->di_tv.v_type != VAR_NUMBER)
19054 EMSG2(_(e_intern2), "set_var()");
19055 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019056 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019057 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019058 if (STRCMP(varname, "searchforward") == 0)
19059 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19060 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019061 return;
19062 }
19063
19064 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 }
19066 else /* add a new variable */
19067 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019068 /* Can't add "v:" variable. */
19069 if (ht == &vimvarht)
19070 {
19071 EMSG2(_(e_illvar), name);
19072 return;
19073 }
19074
Bram Moolenaar92124a32005-06-17 22:03:40 +000019075 /* Make sure the variable name is valid. */
19076 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019077 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19078 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019079 {
19080 EMSG2(_(e_illvar), varname);
19081 return;
19082 }
19083
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019084 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19085 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019086 if (v == NULL)
19087 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019089 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019091 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 return;
19093 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019094 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019096
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019097 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019098 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019099 else
19100 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019102 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019103 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105}
19106
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019107/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019108 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019109 * Also give an error message.
19110 */
19111 static int
19112var_check_ro(flags, name)
19113 int flags;
19114 char_u *name;
19115{
19116 if (flags & DI_FLAGS_RO)
19117 {
19118 EMSG2(_(e_readonlyvar), name);
19119 return TRUE;
19120 }
19121 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19122 {
19123 EMSG2(_(e_readonlysbx), name);
19124 return TRUE;
19125 }
19126 return FALSE;
19127}
19128
19129/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019130 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19131 * Also give an error message.
19132 */
19133 static int
19134var_check_fixed(flags, name)
19135 int flags;
19136 char_u *name;
19137{
19138 if (flags & DI_FLAGS_FIX)
19139 {
19140 EMSG2(_("E795: Cannot delete variable %s"), name);
19141 return TRUE;
19142 }
19143 return FALSE;
19144}
19145
19146/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019147 * Return TRUE if typeval "tv" is set to be locked (immutable).
19148 * Also give an error message, using "name".
19149 */
19150 static int
19151tv_check_lock(lock, name)
19152 int lock;
19153 char_u *name;
19154{
19155 if (lock & VAR_LOCKED)
19156 {
19157 EMSG2(_("E741: Value is locked: %s"),
19158 name == NULL ? (char_u *)_("Unknown") : name);
19159 return TRUE;
19160 }
19161 if (lock & VAR_FIXED)
19162 {
19163 EMSG2(_("E742: Cannot change value of %s"),
19164 name == NULL ? (char_u *)_("Unknown") : name);
19165 return TRUE;
19166 }
19167 return FALSE;
19168}
19169
19170/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019172 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019173 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019176copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019177 typval_T *from;
19178 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019180 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019181 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019182 switch (from->v_type)
19183 {
19184 case VAR_NUMBER:
19185 to->vval.v_number = from->vval.v_number;
19186 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019187#ifdef FEAT_FLOAT
19188 case VAR_FLOAT:
19189 to->vval.v_float = from->vval.v_float;
19190 break;
19191#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019192 case VAR_STRING:
19193 case VAR_FUNC:
19194 if (from->vval.v_string == NULL)
19195 to->vval.v_string = NULL;
19196 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019197 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019199 if (from->v_type == VAR_FUNC)
19200 func_ref(to->vval.v_string);
19201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019202 break;
19203 case VAR_LIST:
19204 if (from->vval.v_list == NULL)
19205 to->vval.v_list = NULL;
19206 else
19207 {
19208 to->vval.v_list = from->vval.v_list;
19209 ++to->vval.v_list->lv_refcount;
19210 }
19211 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019212 case VAR_DICT:
19213 if (from->vval.v_dict == NULL)
19214 to->vval.v_dict = NULL;
19215 else
19216 {
19217 to->vval.v_dict = from->vval.v_dict;
19218 ++to->vval.v_dict->dv_refcount;
19219 }
19220 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 break;
19224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225}
19226
19227/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019228 * Make a copy of an item.
19229 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019230 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19231 * reference to an already copied list/dict can be used.
19232 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019233 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019234 static int
19235item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019236 typval_T *from;
19237 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019238 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019239 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019240{
19241 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019242 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019243
Bram Moolenaar33570922005-01-25 22:26:29 +000019244 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019245 {
19246 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019247 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019248 }
19249 ++recurse;
19250
19251 switch (from->v_type)
19252 {
19253 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019254#ifdef FEAT_FLOAT
19255 case VAR_FLOAT:
19256#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019257 case VAR_STRING:
19258 case VAR_FUNC:
19259 copy_tv(from, to);
19260 break;
19261 case VAR_LIST:
19262 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019263 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019264 if (from->vval.v_list == NULL)
19265 to->vval.v_list = NULL;
19266 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19267 {
19268 /* use the copy made earlier */
19269 to->vval.v_list = from->vval.v_list->lv_copylist;
19270 ++to->vval.v_list->lv_refcount;
19271 }
19272 else
19273 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19274 if (to->vval.v_list == NULL)
19275 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019276 break;
19277 case VAR_DICT:
19278 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019279 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019280 if (from->vval.v_dict == NULL)
19281 to->vval.v_dict = NULL;
19282 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19283 {
19284 /* use the copy made earlier */
19285 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19286 ++to->vval.v_dict->dv_refcount;
19287 }
19288 else
19289 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19290 if (to->vval.v_dict == NULL)
19291 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019292 break;
19293 default:
19294 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019295 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019296 }
19297 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019298 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019299}
19300
19301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 * ":echo expr1 ..." print each argument separated with a space, add a
19303 * newline at the end.
19304 * ":echon expr1 ..." print each argument plain.
19305 */
19306 void
19307ex_echo(eap)
19308 exarg_T *eap;
19309{
19310 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019311 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019312 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 char_u *p;
19314 int needclr = TRUE;
19315 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019316 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317
19318 if (eap->skip)
19319 ++emsg_skip;
19320 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19321 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019322 /* If eval1() causes an error message the text from the command may
19323 * still need to be cleared. E.g., "echo 22,44". */
19324 need_clr_eos = needclr;
19325
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019327 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 {
19329 /*
19330 * Report the invalid expression unless the expression evaluation
19331 * has been cancelled due to an aborting error, an interrupt, or an
19332 * exception.
19333 */
19334 if (!aborting())
19335 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019336 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 break;
19338 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019339 need_clr_eos = FALSE;
19340
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 if (!eap->skip)
19342 {
19343 if (atstart)
19344 {
19345 atstart = FALSE;
19346 /* Call msg_start() after eval1(), evaluating the expression
19347 * may cause a message to appear. */
19348 if (eap->cmdidx == CMD_echo)
19349 msg_start();
19350 }
19351 else if (eap->cmdidx == CMD_echo)
19352 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019353 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019354 if (p != NULL)
19355 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019357 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019359 if (*p != TAB && needclr)
19360 {
19361 /* remove any text still there from the command */
19362 msg_clr_eos();
19363 needclr = FALSE;
19364 }
19365 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 }
19367 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019368 {
19369#ifdef FEAT_MBYTE
19370 if (has_mbyte)
19371 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019372 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019373
19374 (void)msg_outtrans_len_attr(p, i, echo_attr);
19375 p += i - 1;
19376 }
19377 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019379 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019382 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019384 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 arg = skipwhite(arg);
19386 }
19387 eap->nextcmd = check_nextcmd(arg);
19388
19389 if (eap->skip)
19390 --emsg_skip;
19391 else
19392 {
19393 /* remove text that may still be there from the command */
19394 if (needclr)
19395 msg_clr_eos();
19396 if (eap->cmdidx == CMD_echo)
19397 msg_end();
19398 }
19399}
19400
19401/*
19402 * ":echohl {name}".
19403 */
19404 void
19405ex_echohl(eap)
19406 exarg_T *eap;
19407{
19408 int id;
19409
19410 id = syn_name2id(eap->arg);
19411 if (id == 0)
19412 echo_attr = 0;
19413 else
19414 echo_attr = syn_id2attr(id);
19415}
19416
19417/*
19418 * ":execute expr1 ..." execute the result of an expression.
19419 * ":echomsg expr1 ..." Print a message
19420 * ":echoerr expr1 ..." Print an error
19421 * Each gets spaces around each argument and a newline at the end for
19422 * echo commands
19423 */
19424 void
19425ex_execute(eap)
19426 exarg_T *eap;
19427{
19428 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019429 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 int ret = OK;
19431 char_u *p;
19432 garray_T ga;
19433 int len;
19434 int save_did_emsg;
19435
19436 ga_init2(&ga, 1, 80);
19437
19438 if (eap->skip)
19439 ++emsg_skip;
19440 while (*arg != NUL && *arg != '|' && *arg != '\n')
19441 {
19442 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019443 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444 {
19445 /*
19446 * Report the invalid expression unless the expression evaluation
19447 * has been cancelled due to an aborting error, an interrupt, or an
19448 * exception.
19449 */
19450 if (!aborting())
19451 EMSG2(_(e_invexpr2), p);
19452 ret = FAIL;
19453 break;
19454 }
19455
19456 if (!eap->skip)
19457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019458 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 len = (int)STRLEN(p);
19460 if (ga_grow(&ga, len + 2) == FAIL)
19461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 ret = FAIL;
19464 break;
19465 }
19466 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 ga.ga_len += len;
19470 }
19471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 arg = skipwhite(arg);
19474 }
19475
19476 if (ret != FAIL && ga.ga_data != NULL)
19477 {
19478 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019481 out_flush();
19482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 else if (eap->cmdidx == CMD_echoerr)
19484 {
19485 /* We don't want to abort following commands, restore did_emsg. */
19486 save_did_emsg = did_emsg;
19487 EMSG((char_u *)ga.ga_data);
19488 if (!force_abort)
19489 did_emsg = save_did_emsg;
19490 }
19491 else if (eap->cmdidx == CMD_execute)
19492 do_cmdline((char_u *)ga.ga_data,
19493 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19494 }
19495
19496 ga_clear(&ga);
19497
19498 if (eap->skip)
19499 --emsg_skip;
19500
19501 eap->nextcmd = check_nextcmd(arg);
19502}
19503
19504/*
19505 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19506 * "arg" points to the "&" or '+' when called, to "option" when returning.
19507 * Returns NULL when no option name found. Otherwise pointer to the char
19508 * after the option name.
19509 */
19510 static char_u *
19511find_option_end(arg, opt_flags)
19512 char_u **arg;
19513 int *opt_flags;
19514{
19515 char_u *p = *arg;
19516
19517 ++p;
19518 if (*p == 'g' && p[1] == ':')
19519 {
19520 *opt_flags = OPT_GLOBAL;
19521 p += 2;
19522 }
19523 else if (*p == 'l' && p[1] == ':')
19524 {
19525 *opt_flags = OPT_LOCAL;
19526 p += 2;
19527 }
19528 else
19529 *opt_flags = 0;
19530
19531 if (!ASCII_ISALPHA(*p))
19532 return NULL;
19533 *arg = p;
19534
19535 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19536 p += 4; /* termcap option */
19537 else
19538 while (ASCII_ISALPHA(*p))
19539 ++p;
19540 return p;
19541}
19542
19543/*
19544 * ":function"
19545 */
19546 void
19547ex_function(eap)
19548 exarg_T *eap;
19549{
19550 char_u *theline;
19551 int j;
19552 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 char_u *name = NULL;
19555 char_u *p;
19556 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019557 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 garray_T newargs;
19559 garray_T newlines;
19560 int varargs = FALSE;
19561 int mustend = FALSE;
19562 int flags = 0;
19563 ufunc_T *fp;
19564 int indent;
19565 int nesting;
19566 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019567 dictitem_T *v;
19568 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019569 static int func_nr = 0; /* number for nameless function */
19570 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019571 hashtab_T *ht;
19572 int todo;
19573 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019574 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575
19576 /*
19577 * ":function" without argument: list functions.
19578 */
19579 if (ends_excmd(*eap->arg))
19580 {
19581 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019582 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019583 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019584 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019585 {
19586 if (!HASHITEM_EMPTY(hi))
19587 {
19588 --todo;
19589 fp = HI2UF(hi);
19590 if (!isdigit(*fp->uf_name))
19591 list_func_head(fp, FALSE);
19592 }
19593 }
19594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 eap->nextcmd = check_nextcmd(eap->arg);
19596 return;
19597 }
19598
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019599 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019600 * ":function /pat": list functions matching pattern.
19601 */
19602 if (*eap->arg == '/')
19603 {
19604 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19605 if (!eap->skip)
19606 {
19607 regmatch_T regmatch;
19608
19609 c = *p;
19610 *p = NUL;
19611 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19612 *p = c;
19613 if (regmatch.regprog != NULL)
19614 {
19615 regmatch.rm_ic = p_ic;
19616
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019617 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019618 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19619 {
19620 if (!HASHITEM_EMPTY(hi))
19621 {
19622 --todo;
19623 fp = HI2UF(hi);
19624 if (!isdigit(*fp->uf_name)
19625 && vim_regexec(&regmatch, fp->uf_name, 0))
19626 list_func_head(fp, FALSE);
19627 }
19628 }
19629 }
19630 }
19631 if (*p == '/')
19632 ++p;
19633 eap->nextcmd = check_nextcmd(p);
19634 return;
19635 }
19636
19637 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019638 * Get the function name. There are these situations:
19639 * func normal function name
19640 * "name" == func, "fudi.fd_dict" == NULL
19641 * dict.func new dictionary entry
19642 * "name" == NULL, "fudi.fd_dict" set,
19643 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19644 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019645 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019646 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19647 * dict.func existing dict entry that's not a Funcref
19648 * "name" == NULL, "fudi.fd_dict" set,
19649 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019652 name = trans_function_name(&p, eap->skip, 0, &fudi);
19653 paren = (vim_strchr(p, '(') != NULL);
19654 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 {
19656 /*
19657 * Return on an invalid expression in braces, unless the expression
19658 * evaluation has been cancelled due to an aborting error, an
19659 * interrupt, or an exception.
19660 */
19661 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019663 if (!eap->skip && fudi.fd_newkey != NULL)
19664 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019665 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 else
19669 eap->skip = TRUE;
19670 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019671
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 /* An error in a function call during evaluation of an expression in magic
19673 * braces should not cause the function not to be defined. */
19674 saved_did_emsg = did_emsg;
19675 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676
19677 /*
19678 * ":function func" with only function name: list function.
19679 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019680 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 {
19682 if (!ends_excmd(*skipwhite(p)))
19683 {
19684 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019685 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 }
19687 eap->nextcmd = check_nextcmd(p);
19688 if (eap->nextcmd != NULL)
19689 *p = NUL;
19690 if (!eap->skip && !got_int)
19691 {
19692 fp = find_func(name);
19693 if (fp != NULL)
19694 {
19695 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019696 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019698 if (FUNCLINE(fp, j) == NULL)
19699 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 msg_putchar('\n');
19701 msg_outnum((long)(j + 1));
19702 if (j < 9)
19703 msg_putchar(' ');
19704 if (j < 99)
19705 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019706 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 out_flush(); /* show a line at a time */
19708 ui_breakcheck();
19709 }
19710 if (!got_int)
19711 {
19712 msg_putchar('\n');
19713 msg_puts((char_u *)" endfunction");
19714 }
19715 }
19716 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019717 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019719 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 }
19721
19722 /*
19723 * ":function name(arg1, arg2)" Define function.
19724 */
19725 p = skipwhite(p);
19726 if (*p != '(')
19727 {
19728 if (!eap->skip)
19729 {
19730 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019731 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 }
19733 /* attempt to continue by skipping some text */
19734 if (vim_strchr(p, '(') != NULL)
19735 p = vim_strchr(p, '(');
19736 }
19737 p = skipwhite(p + 1);
19738
19739 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19740 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19741
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019742 if (!eap->skip)
19743 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019744 /* Check the name of the function. Unless it's a dictionary function
19745 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019746 if (name != NULL)
19747 arg = name;
19748 else
19749 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019750 if (arg != NULL && (fudi.fd_di == NULL
19751 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019752 {
19753 if (*arg == K_SPECIAL)
19754 j = 3;
19755 else
19756 j = 0;
19757 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19758 : eval_isnamec(arg[j])))
19759 ++j;
19760 if (arg[j] != NUL)
19761 emsg_funcname(_(e_invarg2), arg);
19762 }
19763 }
19764
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 /*
19766 * Isolate the arguments: "arg1, arg2, ...)"
19767 */
19768 while (*p != ')')
19769 {
19770 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19771 {
19772 varargs = TRUE;
19773 p += 3;
19774 mustend = TRUE;
19775 }
19776 else
19777 {
19778 arg = p;
19779 while (ASCII_ISALNUM(*p) || *p == '_')
19780 ++p;
19781 if (arg == p || isdigit(*arg)
19782 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19783 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19784 {
19785 if (!eap->skip)
19786 EMSG2(_("E125: Illegal argument: %s"), arg);
19787 break;
19788 }
19789 if (ga_grow(&newargs, 1) == FAIL)
19790 goto erret;
19791 c = *p;
19792 *p = NUL;
19793 arg = vim_strsave(arg);
19794 if (arg == NULL)
19795 goto erret;
19796 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19797 *p = c;
19798 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 if (*p == ',')
19800 ++p;
19801 else
19802 mustend = TRUE;
19803 }
19804 p = skipwhite(p);
19805 if (mustend && *p != ')')
19806 {
19807 if (!eap->skip)
19808 EMSG2(_(e_invarg2), eap->arg);
19809 break;
19810 }
19811 }
19812 ++p; /* skip the ')' */
19813
Bram Moolenaare9a41262005-01-15 22:18:47 +000019814 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 for (;;)
19816 {
19817 p = skipwhite(p);
19818 if (STRNCMP(p, "range", 5) == 0)
19819 {
19820 flags |= FC_RANGE;
19821 p += 5;
19822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019823 else if (STRNCMP(p, "dict", 4) == 0)
19824 {
19825 flags |= FC_DICT;
19826 p += 4;
19827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 else if (STRNCMP(p, "abort", 5) == 0)
19829 {
19830 flags |= FC_ABORT;
19831 p += 5;
19832 }
19833 else
19834 break;
19835 }
19836
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019837 /* When there is a line break use what follows for the function body.
19838 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19839 if (*p == '\n')
19840 line_arg = p + 1;
19841 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 EMSG(_(e_trailing));
19843
19844 /*
19845 * Read the body of the function, until ":endfunction" is found.
19846 */
19847 if (KeyTyped)
19848 {
19849 /* Check if the function already exists, don't let the user type the
19850 * whole function before telling him it doesn't work! For a script we
19851 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019852 if (!eap->skip && !eap->forceit)
19853 {
19854 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19855 EMSG(_(e_funcdict));
19856 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019857 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019860 if (!eap->skip && did_emsg)
19861 goto erret;
19862
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 msg_putchar('\n'); /* don't overwrite the function name */
19864 cmdline_row = msg_row;
19865 }
19866
19867 indent = 2;
19868 nesting = 0;
19869 for (;;)
19870 {
19871 msg_scroll = TRUE;
19872 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019873 sourcing_lnum_off = sourcing_lnum;
19874
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019875 if (line_arg != NULL)
19876 {
19877 /* Use eap->arg, split up in parts by line breaks. */
19878 theline = line_arg;
19879 p = vim_strchr(theline, '\n');
19880 if (p == NULL)
19881 line_arg += STRLEN(line_arg);
19882 else
19883 {
19884 *p = NUL;
19885 line_arg = p + 1;
19886 }
19887 }
19888 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 theline = getcmdline(':', 0L, indent);
19890 else
19891 theline = eap->getline(':', eap->cookie, indent);
19892 if (KeyTyped)
19893 lines_left = Rows - 1;
19894 if (theline == NULL)
19895 {
19896 EMSG(_("E126: Missing :endfunction"));
19897 goto erret;
19898 }
19899
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019900 /* Detect line continuation: sourcing_lnum increased more than one. */
19901 if (sourcing_lnum > sourcing_lnum_off + 1)
19902 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19903 else
19904 sourcing_lnum_off = 0;
19905
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 if (skip_until != NULL)
19907 {
19908 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19909 * don't check for ":endfunc". */
19910 if (STRCMP(theline, skip_until) == 0)
19911 {
19912 vim_free(skip_until);
19913 skip_until = NULL;
19914 }
19915 }
19916 else
19917 {
19918 /* skip ':' and blanks*/
19919 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19920 ;
19921
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019922 /* Check for "endfunction". */
19923 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019925 if (line_arg == NULL)
19926 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927 break;
19928 }
19929
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019930 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 * at "end". */
19932 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19933 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019934 else if (STRNCMP(p, "if", 2) == 0
19935 || STRNCMP(p, "wh", 2) == 0
19936 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 || STRNCMP(p, "try", 3) == 0)
19938 indent += 2;
19939
19940 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019941 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019943 if (*p == '!')
19944 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 p += eval_fname_script(p);
19946 if (ASCII_ISALPHA(*p))
19947 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019948 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 if (*skipwhite(p) == '(')
19950 {
19951 ++nesting;
19952 indent += 2;
19953 }
19954 }
19955 }
19956
19957 /* Check for ":append" or ":insert". */
19958 p = skip_range(p, NULL);
19959 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19960 || (p[0] == 'i'
19961 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19962 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19963 skip_until = vim_strsave((char_u *)".");
19964
19965 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19966 arg = skipwhite(skiptowhite(p));
19967 if (arg[0] == '<' && arg[1] =='<'
19968 && ((p[0] == 'p' && p[1] == 'y'
19969 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19970 || (p[0] == 'p' && p[1] == 'e'
19971 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19972 || (p[0] == 't' && p[1] == 'c'
19973 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19974 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19975 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019976 || (p[0] == 'm' && p[1] == 'z'
19977 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 ))
19979 {
19980 /* ":python <<" continues until a dot, like ":append" */
19981 p = skipwhite(arg + 2);
19982 if (*p == NUL)
19983 skip_until = vim_strsave((char_u *)".");
19984 else
19985 skip_until = vim_strsave(p);
19986 }
19987 }
19988
19989 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019990 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019991 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019992 if (line_arg == NULL)
19993 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019995 }
19996
19997 /* Copy the line to newly allocated memory. get_one_sourceline()
19998 * allocates 250 bytes per line, this saves 80% on average. The cost
19999 * is an extra alloc/free. */
20000 p = vim_strsave(theline);
20001 if (p != NULL)
20002 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020003 if (line_arg == NULL)
20004 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020005 theline = p;
20006 }
20007
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020008 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20009
20010 /* Add NULL lines for continuation lines, so that the line count is
20011 * equal to the index in the growarray. */
20012 while (sourcing_lnum_off-- > 0)
20013 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020014
20015 /* Check for end of eap->arg. */
20016 if (line_arg != NULL && *line_arg == NUL)
20017 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 }
20019
20020 /* Don't define the function when skipping commands or when an error was
20021 * detected. */
20022 if (eap->skip || did_emsg)
20023 goto erret;
20024
20025 /*
20026 * If there are no errors, add the function
20027 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020028 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020029 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020030 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020032 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020033 emsg_funcname("E707: Function name conflicts with variable: %s",
20034 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020035 goto erret;
20036 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020037
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020038 fp = find_func(name);
20039 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020041 if (!eap->forceit)
20042 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020043 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020044 goto erret;
20045 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020046 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020047 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020048 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020049 name);
20050 goto erret;
20051 }
20052 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020053 ga_clear_strings(&(fp->uf_args));
20054 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020055 vim_free(name);
20056 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 }
20059 else
20060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020061 char numbuf[20];
20062
20063 fp = NULL;
20064 if (fudi.fd_newkey == NULL && !eap->forceit)
20065 {
20066 EMSG(_(e_funcdict));
20067 goto erret;
20068 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020069 if (fudi.fd_di == NULL)
20070 {
20071 /* Can't add a function to a locked dictionary */
20072 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20073 goto erret;
20074 }
20075 /* Can't change an existing function if it is locked */
20076 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20077 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078
20079 /* Give the function a sequential number. Can only be used with a
20080 * Funcref! */
20081 vim_free(name);
20082 sprintf(numbuf, "%d", ++func_nr);
20083 name = vim_strsave((char_u *)numbuf);
20084 if (name == NULL)
20085 goto erret;
20086 }
20087
20088 if (fp == NULL)
20089 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020090 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020091 {
20092 int slen, plen;
20093 char_u *scriptname;
20094
20095 /* Check that the autoload name matches the script name. */
20096 j = FAIL;
20097 if (sourcing_name != NULL)
20098 {
20099 scriptname = autoload_name(name);
20100 if (scriptname != NULL)
20101 {
20102 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020103 plen = (int)STRLEN(p);
20104 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020105 if (slen > plen && fnamecmp(p,
20106 sourcing_name + slen - plen) == 0)
20107 j = OK;
20108 vim_free(scriptname);
20109 }
20110 }
20111 if (j == FAIL)
20112 {
20113 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20114 goto erret;
20115 }
20116 }
20117
20118 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 if (fp == NULL)
20120 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020121
20122 if (fudi.fd_dict != NULL)
20123 {
20124 if (fudi.fd_di == NULL)
20125 {
20126 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020127 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020128 if (fudi.fd_di == NULL)
20129 {
20130 vim_free(fp);
20131 goto erret;
20132 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020133 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20134 {
20135 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020136 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020137 goto erret;
20138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020139 }
20140 else
20141 /* overwrite existing dict entry */
20142 clear_tv(&fudi.fd_di->di_tv);
20143 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020144 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020145 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020146 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020147
20148 /* behave like "dict" was used */
20149 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020150 }
20151
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020153 STRCPY(fp->uf_name, name);
20154 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020156 fp->uf_args = newargs;
20157 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020158#ifdef FEAT_PROFILE
20159 fp->uf_tml_count = NULL;
20160 fp->uf_tml_total = NULL;
20161 fp->uf_tml_self = NULL;
20162 fp->uf_profiling = FALSE;
20163 if (prof_def_func())
20164 func_do_profile(fp);
20165#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020166 fp->uf_varargs = varargs;
20167 fp->uf_flags = flags;
20168 fp->uf_calls = 0;
20169 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020170 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171
20172erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 ga_clear_strings(&newargs);
20174 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020175ret_free:
20176 vim_free(skip_until);
20177 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180}
20181
20182/*
20183 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020184 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020186 * flags:
20187 * TFN_INT: internal function name OK
20188 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 * Advances "pp" to just after the function name (if no error).
20190 */
20191 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020192trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 char_u **pp;
20194 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020195 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020196 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020198 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 char_u *start;
20200 char_u *end;
20201 int lead;
20202 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020205
20206 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020207 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020209
20210 /* Check for hard coded <SNR>: already translated function ID (from a user
20211 * command). */
20212 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20213 && (*pp)[2] == (int)KE_SNR)
20214 {
20215 *pp += 3;
20216 len = get_id_len(pp) + 3;
20217 return vim_strnsave(start, len);
20218 }
20219
20220 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20221 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020223 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020225
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020226 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20227 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020228 if (end == start)
20229 {
20230 if (!skip)
20231 EMSG(_("E129: Function name required"));
20232 goto theend;
20233 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020234 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020235 {
20236 /*
20237 * Report an invalid expression in braces, unless the expression
20238 * evaluation has been cancelled due to an aborting error, an
20239 * interrupt, or an exception.
20240 */
20241 if (!aborting())
20242 {
20243 if (end != NULL)
20244 EMSG2(_(e_invarg2), start);
20245 }
20246 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020247 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020248 goto theend;
20249 }
20250
20251 if (lv.ll_tv != NULL)
20252 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020253 if (fdp != NULL)
20254 {
20255 fdp->fd_dict = lv.ll_dict;
20256 fdp->fd_newkey = lv.ll_newkey;
20257 lv.ll_newkey = NULL;
20258 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020259 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020260 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20261 {
20262 name = vim_strsave(lv.ll_tv->vval.v_string);
20263 *pp = end;
20264 }
20265 else
20266 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020267 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20268 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020269 EMSG(_(e_funcref));
20270 else
20271 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020272 name = NULL;
20273 }
20274 goto theend;
20275 }
20276
20277 if (lv.ll_name == NULL)
20278 {
20279 /* Error found, but continue after the function name. */
20280 *pp = end;
20281 goto theend;
20282 }
20283
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020284 /* Check if the name is a Funcref. If so, use the value. */
20285 if (lv.ll_exp_name != NULL)
20286 {
20287 len = (int)STRLEN(lv.ll_exp_name);
20288 name = deref_func_name(lv.ll_exp_name, &len);
20289 if (name == lv.ll_exp_name)
20290 name = NULL;
20291 }
20292 else
20293 {
20294 len = (int)(end - *pp);
20295 name = deref_func_name(*pp, &len);
20296 if (name == *pp)
20297 name = NULL;
20298 }
20299 if (name != NULL)
20300 {
20301 name = vim_strsave(name);
20302 *pp = end;
20303 goto theend;
20304 }
20305
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020306 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020307 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020308 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020309 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20310 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20311 {
20312 /* When there was "s:" already or the name expanded to get a
20313 * leading "s:" then remove it. */
20314 lv.ll_name += 2;
20315 len -= 2;
20316 lead = 2;
20317 }
20318 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020319 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020320 {
20321 if (lead == 2) /* skip over "s:" */
20322 lv.ll_name += 2;
20323 len = (int)(end - lv.ll_name);
20324 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020325
20326 /*
20327 * Copy the function name to allocated memory.
20328 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20329 * Accept <SNR>123_name() outside a script.
20330 */
20331 if (skip)
20332 lead = 0; /* do nothing */
20333 else if (lead > 0)
20334 {
20335 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020336 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20337 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020338 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020339 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020340 if (current_SID <= 0)
20341 {
20342 EMSG(_(e_usingsid));
20343 goto theend;
20344 }
20345 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20346 lead += (int)STRLEN(sid_buf);
20347 }
20348 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020349 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020350 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020351 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020352 goto theend;
20353 }
20354 name = alloc((unsigned)(len + lead + 1));
20355 if (name != NULL)
20356 {
20357 if (lead > 0)
20358 {
20359 name[0] = K_SPECIAL;
20360 name[1] = KS_EXTRA;
20361 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020362 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020363 STRCPY(name + 3, sid_buf);
20364 }
20365 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20366 name[len + lead] = NUL;
20367 }
20368 *pp = end;
20369
20370theend:
20371 clear_lval(&lv);
20372 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373}
20374
20375/*
20376 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20377 * Return 2 if "p" starts with "s:".
20378 * Return 0 otherwise.
20379 */
20380 static int
20381eval_fname_script(p)
20382 char_u *p;
20383{
20384 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20385 || STRNICMP(p + 1, "SNR>", 4) == 0))
20386 return 5;
20387 if (p[0] == 's' && p[1] == ':')
20388 return 2;
20389 return 0;
20390}
20391
20392/*
20393 * Return TRUE if "p" starts with "<SID>" or "s:".
20394 * Only works if eval_fname_script() returned non-zero for "p"!
20395 */
20396 static int
20397eval_fname_sid(p)
20398 char_u *p;
20399{
20400 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20401}
20402
20403/*
20404 * List the head of the function: "name(arg1, arg2)".
20405 */
20406 static void
20407list_func_head(fp, indent)
20408 ufunc_T *fp;
20409 int indent;
20410{
20411 int j;
20412
20413 msg_start();
20414 if (indent)
20415 MSG_PUTS(" ");
20416 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020417 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 {
20419 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020420 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 }
20422 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020423 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020425 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 {
20427 if (j)
20428 MSG_PUTS(", ");
20429 msg_puts(FUNCARG(fp, j));
20430 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020431 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 {
20433 if (j)
20434 MSG_PUTS(", ");
20435 MSG_PUTS("...");
20436 }
20437 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020438 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020439 if (p_verbose > 0)
20440 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441}
20442
20443/*
20444 * Find a function by name, return pointer to it in ufuncs.
20445 * Return NULL for unknown function.
20446 */
20447 static ufunc_T *
20448find_func(name)
20449 char_u *name;
20450{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020451 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020453 hi = hash_find(&func_hashtab, name);
20454 if (!HASHITEM_EMPTY(hi))
20455 return HI2UF(hi);
20456 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457}
20458
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020459#if defined(EXITFREE) || defined(PROTO)
20460 void
20461free_all_functions()
20462{
20463 hashitem_T *hi;
20464
20465 /* Need to start all over every time, because func_free() may change the
20466 * hash table. */
20467 while (func_hashtab.ht_used > 0)
20468 for (hi = func_hashtab.ht_array; ; ++hi)
20469 if (!HASHITEM_EMPTY(hi))
20470 {
20471 func_free(HI2UF(hi));
20472 break;
20473 }
20474}
20475#endif
20476
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020477/*
20478 * Return TRUE if a function "name" exists.
20479 */
20480 static int
20481function_exists(name)
20482 char_u *name;
20483{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020484 char_u *nm = name;
20485 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020486 int n = FALSE;
20487
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020488 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020489 nm = skipwhite(nm);
20490
20491 /* Only accept "funcname", "funcname ", "funcname (..." and
20492 * "funcname(...", not "funcname!...". */
20493 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020494 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020495 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020497 else
20498 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020499 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020500 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020501 return n;
20502}
20503
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020504/*
20505 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020506 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020507 */
20508 static int
20509builtin_function(name)
20510 char_u *name;
20511{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020512 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20513 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020514}
20515
Bram Moolenaar05159a02005-02-26 23:04:13 +000020516#if defined(FEAT_PROFILE) || defined(PROTO)
20517/*
20518 * Start profiling function "fp".
20519 */
20520 static void
20521func_do_profile(fp)
20522 ufunc_T *fp;
20523{
20524 fp->uf_tm_count = 0;
20525 profile_zero(&fp->uf_tm_self);
20526 profile_zero(&fp->uf_tm_total);
20527 if (fp->uf_tml_count == NULL)
20528 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20529 (sizeof(int) * fp->uf_lines.ga_len));
20530 if (fp->uf_tml_total == NULL)
20531 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20532 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20533 if (fp->uf_tml_self == NULL)
20534 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20535 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20536 fp->uf_tml_idx = -1;
20537 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20538 || fp->uf_tml_self == NULL)
20539 return; /* out of memory */
20540
20541 fp->uf_profiling = TRUE;
20542}
20543
20544/*
20545 * Dump the profiling results for all functions in file "fd".
20546 */
20547 void
20548func_dump_profile(fd)
20549 FILE *fd;
20550{
20551 hashitem_T *hi;
20552 int todo;
20553 ufunc_T *fp;
20554 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020555 ufunc_T **sorttab;
20556 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020557
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020558 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000020559 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20560
Bram Moolenaar05159a02005-02-26 23:04:13 +000020561 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20562 {
20563 if (!HASHITEM_EMPTY(hi))
20564 {
20565 --todo;
20566 fp = HI2UF(hi);
20567 if (fp->uf_profiling)
20568 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020569 if (sorttab != NULL)
20570 sorttab[st_len++] = fp;
20571
Bram Moolenaar05159a02005-02-26 23:04:13 +000020572 if (fp->uf_name[0] == K_SPECIAL)
20573 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20574 else
20575 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20576 if (fp->uf_tm_count == 1)
20577 fprintf(fd, "Called 1 time\n");
20578 else
20579 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20580 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20581 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20582 fprintf(fd, "\n");
20583 fprintf(fd, "count total (s) self (s)\n");
20584
20585 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20586 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020587 if (FUNCLINE(fp, i) == NULL)
20588 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020589 prof_func_line(fd, fp->uf_tml_count[i],
20590 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020591 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20592 }
20593 fprintf(fd, "\n");
20594 }
20595 }
20596 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020597
20598 if (sorttab != NULL && st_len > 0)
20599 {
20600 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20601 prof_total_cmp);
20602 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20603 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20604 prof_self_cmp);
20605 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20606 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020607}
Bram Moolenaar73830342005-02-28 22:48:19 +000020608
20609 static void
20610prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20611 FILE *fd;
20612 ufunc_T **sorttab;
20613 int st_len;
20614 char *title;
20615 int prefer_self; /* when equal print only self time */
20616{
20617 int i;
20618 ufunc_T *fp;
20619
20620 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20621 fprintf(fd, "count total (s) self (s) function\n");
20622 for (i = 0; i < 20 && i < st_len; ++i)
20623 {
20624 fp = sorttab[i];
20625 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20626 prefer_self);
20627 if (fp->uf_name[0] == K_SPECIAL)
20628 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20629 else
20630 fprintf(fd, " %s()\n", fp->uf_name);
20631 }
20632 fprintf(fd, "\n");
20633}
20634
20635/*
20636 * Print the count and times for one function or function line.
20637 */
20638 static void
20639prof_func_line(fd, count, total, self, prefer_self)
20640 FILE *fd;
20641 int count;
20642 proftime_T *total;
20643 proftime_T *self;
20644 int prefer_self; /* when equal print only self time */
20645{
20646 if (count > 0)
20647 {
20648 fprintf(fd, "%5d ", count);
20649 if (prefer_self && profile_equal(total, self))
20650 fprintf(fd, " ");
20651 else
20652 fprintf(fd, "%s ", profile_msg(total));
20653 if (!prefer_self && profile_equal(total, self))
20654 fprintf(fd, " ");
20655 else
20656 fprintf(fd, "%s ", profile_msg(self));
20657 }
20658 else
20659 fprintf(fd, " ");
20660}
20661
20662/*
20663 * Compare function for total time sorting.
20664 */
20665 static int
20666#ifdef __BORLANDC__
20667_RTLENTRYF
20668#endif
20669prof_total_cmp(s1, s2)
20670 const void *s1;
20671 const void *s2;
20672{
20673 ufunc_T *p1, *p2;
20674
20675 p1 = *(ufunc_T **)s1;
20676 p2 = *(ufunc_T **)s2;
20677 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20678}
20679
20680/*
20681 * Compare function for self time sorting.
20682 */
20683 static int
20684#ifdef __BORLANDC__
20685_RTLENTRYF
20686#endif
20687prof_self_cmp(s1, s2)
20688 const void *s1;
20689 const void *s2;
20690{
20691 ufunc_T *p1, *p2;
20692
20693 p1 = *(ufunc_T **)s1;
20694 p2 = *(ufunc_T **)s2;
20695 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20696}
20697
Bram Moolenaar05159a02005-02-26 23:04:13 +000020698#endif
20699
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020700/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020701 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020702 * Return TRUE if a package was loaded.
20703 */
20704 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020705script_autoload(name, reload)
20706 char_u *name;
20707 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020708{
20709 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020710 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020711 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020712 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020713
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020714 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020715 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020716 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020717 return FALSE;
20718
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020719 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020720
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020721 /* Find the name in the list of previously loaded package names. Skip
20722 * "autoload/", it's always the same. */
20723 for (i = 0; i < ga_loaded.ga_len; ++i)
20724 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20725 break;
20726 if (!reload && i < ga_loaded.ga_len)
20727 ret = FALSE; /* was loaded already */
20728 else
20729 {
20730 /* Remember the name if it wasn't loaded already. */
20731 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20732 {
20733 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20734 tofree = NULL;
20735 }
20736
20737 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020738 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020739 ret = TRUE;
20740 }
20741
20742 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020743 return ret;
20744}
20745
20746/*
20747 * Return the autoload script name for a function or variable name.
20748 * Returns NULL when out of memory.
20749 */
20750 static char_u *
20751autoload_name(name)
20752 char_u *name;
20753{
20754 char_u *p;
20755 char_u *scriptname;
20756
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020757 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020758 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20759 if (scriptname == NULL)
20760 return FALSE;
20761 STRCPY(scriptname, "autoload/");
20762 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020763 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020764 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020765 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020766 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020767 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020768}
20769
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20771
20772/*
20773 * Function given to ExpandGeneric() to obtain the list of user defined
20774 * function names.
20775 */
20776 char_u *
20777get_user_func_name(xp, idx)
20778 expand_T *xp;
20779 int idx;
20780{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020781 static long_u done;
20782 static hashitem_T *hi;
20783 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784
20785 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020787 done = 0;
20788 hi = func_hashtab.ht_array;
20789 }
20790 if (done < func_hashtab.ht_used)
20791 {
20792 if (done++ > 0)
20793 ++hi;
20794 while (HASHITEM_EMPTY(hi))
20795 ++hi;
20796 fp = HI2UF(hi);
20797
20798 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20799 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800
20801 cat_func_name(IObuff, fp);
20802 if (xp->xp_context != EXPAND_USER_FUNC)
20803 {
20804 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020805 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 STRCAT(IObuff, ")");
20807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 return IObuff;
20809 }
20810 return NULL;
20811}
20812
20813#endif /* FEAT_CMDL_COMPL */
20814
20815/*
20816 * Copy the function name of "fp" to buffer "buf".
20817 * "buf" must be able to hold the function name plus three bytes.
20818 * Takes care of script-local function names.
20819 */
20820 static void
20821cat_func_name(buf, fp)
20822 char_u *buf;
20823 ufunc_T *fp;
20824{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020825 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 {
20827 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020828 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 }
20830 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020831 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832}
20833
20834/*
20835 * ":delfunction {name}"
20836 */
20837 void
20838ex_delfunction(eap)
20839 exarg_T *eap;
20840{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020841 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 char_u *p;
20843 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020844 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845
20846 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020847 name = trans_function_name(&p, eap->skip, 0, &fudi);
20848 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020850 {
20851 if (fudi.fd_dict != NULL && !eap->skip)
20852 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 if (!ends_excmd(*skipwhite(p)))
20856 {
20857 vim_free(name);
20858 EMSG(_(e_trailing));
20859 return;
20860 }
20861 eap->nextcmd = check_nextcmd(p);
20862 if (eap->nextcmd != NULL)
20863 *p = NUL;
20864
20865 if (!eap->skip)
20866 fp = find_func(name);
20867 vim_free(name);
20868
20869 if (!eap->skip)
20870 {
20871 if (fp == NULL)
20872 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020873 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 return;
20875 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020876 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 {
20878 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20879 return;
20880 }
20881
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020882 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020884 /* Delete the dict item that refers to the function, it will
20885 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020886 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020888 else
20889 func_free(fp);
20890 }
20891}
20892
20893/*
20894 * Free a function and remove it from the list of functions.
20895 */
20896 static void
20897func_free(fp)
20898 ufunc_T *fp;
20899{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020900 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020901
20902 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020903 ga_clear_strings(&(fp->uf_args));
20904 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020905#ifdef FEAT_PROFILE
20906 vim_free(fp->uf_tml_count);
20907 vim_free(fp->uf_tml_total);
20908 vim_free(fp->uf_tml_self);
20909#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020910
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020911 /* remove the function from the function hashtable */
20912 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20913 if (HASHITEM_EMPTY(hi))
20914 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020915 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020916 hash_remove(&func_hashtab, hi);
20917
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020918 vim_free(fp);
20919}
20920
20921/*
20922 * Unreference a Function: decrement the reference count and free it when it
20923 * becomes zero. Only for numbered functions.
20924 */
20925 static void
20926func_unref(name)
20927 char_u *name;
20928{
20929 ufunc_T *fp;
20930
20931 if (name != NULL && isdigit(*name))
20932 {
20933 fp = find_func(name);
20934 if (fp == NULL)
20935 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020936 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020937 {
20938 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020939 * when "uf_calls" becomes zero. */
20940 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020941 func_free(fp);
20942 }
20943 }
20944}
20945
20946/*
20947 * Count a reference to a Function.
20948 */
20949 static void
20950func_ref(name)
20951 char_u *name;
20952{
20953 ufunc_T *fp;
20954
20955 if (name != NULL && isdigit(*name))
20956 {
20957 fp = find_func(name);
20958 if (fp == NULL)
20959 EMSG2(_(e_intern2), "func_ref()");
20960 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020961 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 }
20963}
20964
20965/*
20966 * Call a user function.
20967 */
20968 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020969call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 ufunc_T *fp; /* pointer to function */
20971 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020972 typval_T *argvars; /* arguments */
20973 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 linenr_T firstline; /* first line of range */
20975 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020976 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977{
Bram Moolenaar33570922005-01-25 22:26:29 +000020978 char_u *save_sourcing_name;
20979 linenr_T save_sourcing_lnum;
20980 scid_T save_current_SID;
20981 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020982 int save_did_emsg;
20983 static int depth = 0;
20984 dictitem_T *v;
20985 int fixvar_idx = 0; /* index in fixvar[] */
20986 int i;
20987 int ai;
20988 char_u numbuf[NUMBUFLEN];
20989 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020990#ifdef FEAT_PROFILE
20991 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020992 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994
20995 /* If depth of calling is getting too high, don't execute the function */
20996 if (depth >= p_mfd)
20997 {
20998 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020999 rettv->v_type = VAR_NUMBER;
21000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 return;
21002 }
21003 ++depth;
21004
21005 line_breakcheck(); /* check for CTRL-C hit */
21006
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021007 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000021008 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021010 fc.rettv = rettv;
21011 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 fc.linenr = 0;
21013 fc.returned = FALSE;
21014 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021016 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 fc.dbg_tick = debug_tick;
21018
Bram Moolenaar33570922005-01-25 22:26:29 +000021019 /*
21020 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21021 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21022 * each argument variable and saves a lot of time.
21023 */
21024 /*
21025 * Init l: variables.
21026 */
21027 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021028 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021029 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021030 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21031 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021032 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021033 name = v->di_key;
21034 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021035 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
21036 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
21037 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021038 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021039 v->di_tv.vval.v_dict = selfdict;
21040 ++selfdict->dv_refcount;
21041 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021042
Bram Moolenaar33570922005-01-25 22:26:29 +000021043 /*
21044 * Init a: variables.
21045 * Set a:0 to "argcount".
21046 * Set a:000 to a list with room for the "..." arguments.
21047 */
21048 init_var_dict(&fc.l_avars, &fc.l_avars_var);
21049 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021050 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000021051 v = &fc.fixvar[fixvar_idx++].var;
21052 STRCPY(v->di_key, "000");
21053 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21054 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21055 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021056 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021057 v->di_tv.vval.v_list = &fc.l_varlist;
21058 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
21059 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000021060 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021061
21062 /*
21063 * Set a:firstline to "firstline" and a:lastline to "lastline".
21064 * Set a:name to named arguments.
21065 * Set a:N to the "..." arguments.
21066 */
21067 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
21068 (varnumber_T)firstline);
21069 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
21070 (varnumber_T)lastline);
21071 for (i = 0; i < argcount; ++i)
21072 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021073 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021074 if (ai < 0)
21075 /* named argument a:name */
21076 name = FUNCARG(fp, i);
21077 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021078 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021079 /* "..." argument a:1, a:2, etc. */
21080 sprintf((char *)numbuf, "%d", ai + 1);
21081 name = numbuf;
21082 }
21083 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21084 {
21085 v = &fc.fixvar[fixvar_idx++].var;
21086 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21087 }
21088 else
21089 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021090 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21091 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021092 if (v == NULL)
21093 break;
21094 v->di_flags = DI_FLAGS_RO;
21095 }
21096 STRCPY(v->di_key, name);
21097 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21098
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021099 /* Note: the values are copied directly to avoid alloc/free.
21100 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021101 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021102 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021103
21104 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21105 {
21106 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
21107 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021108 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021109 }
21110 }
21111
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 /* Don't redraw while executing the function. */
21113 ++RedrawingDisabled;
21114 save_sourcing_name = sourcing_name;
21115 save_sourcing_lnum = sourcing_lnum;
21116 sourcing_lnum = 1;
21117 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021118 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 if (sourcing_name != NULL)
21120 {
21121 if (save_sourcing_name != NULL
21122 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21123 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21124 else
21125 STRCPY(sourcing_name, "function ");
21126 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21127
21128 if (p_verbose >= 12)
21129 {
21130 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021131 verbose_enter_scroll();
21132
Bram Moolenaar555b2802005-05-19 21:08:39 +000021133 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 if (p_verbose >= 14)
21135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021137 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021138 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021139 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140
21141 msg_puts((char_u *)"(");
21142 for (i = 0; i < argcount; ++i)
21143 {
21144 if (i > 0)
21145 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021146 if (argvars[i].v_type == VAR_NUMBER)
21147 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 else
21149 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021150 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21151 if (s != NULL)
21152 {
21153 trunc_string(s, buf, MSG_BUF_CLEN);
21154 msg_puts(buf);
21155 vim_free(tofree);
21156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 }
21158 }
21159 msg_puts((char_u *)")");
21160 }
21161 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021162
21163 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 --no_wait_return;
21165 }
21166 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021167#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021168 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021169 {
21170 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21171 func_do_profile(fp);
21172 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021173 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021174 {
21175 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021176 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021177 profile_zero(&fp->uf_tm_children);
21178 }
21179 script_prof_save(&wait_start);
21180 }
21181#endif
21182
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021184 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 save_did_emsg = did_emsg;
21186 did_emsg = FALSE;
21187
21188 /* call do_cmdline() to execute the lines */
21189 do_cmdline(NULL, get_func_line, (void *)&fc,
21190 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21191
21192 --RedrawingDisabled;
21193
21194 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021195 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021197 clear_tv(rettv);
21198 rettv->v_type = VAR_NUMBER;
21199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 }
21201
Bram Moolenaar05159a02005-02-26 23:04:13 +000021202#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021203 if (do_profiling == PROF_YES && (fp->uf_profiling
21204 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021205 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021206 profile_end(&call_start);
21207 profile_sub_wait(&wait_start, &call_start);
21208 profile_add(&fp->uf_tm_total, &call_start);
21209 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021210 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021211 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021212 profile_add(&fc.caller->func->uf_tm_children, &call_start);
21213 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021214 }
21215 }
21216#endif
21217
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 /* when being verbose, mention the return value */
21219 if (p_verbose >= 12)
21220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021222 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021225 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021226 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021227 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
21228 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021231 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021232 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021233 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021234 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021235
Bram Moolenaar555b2802005-05-19 21:08:39 +000021236 /* The value may be very long. Skip the middle part, so that we
21237 * have some idea how it starts and ends. smsg() would always
21238 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021239 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
21240 if (s != NULL)
21241 {
21242 trunc_string(s, buf, MSG_BUF_CLEN);
21243 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21244 vim_free(tofree);
21245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 }
21247 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021248
21249 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 --no_wait_return;
21251 }
21252
21253 vim_free(sourcing_name);
21254 sourcing_name = save_sourcing_name;
21255 sourcing_lnum = save_sourcing_lnum;
21256 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021257#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021258 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021259 script_prof_restore(&wait_start);
21260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261
21262 if (p_verbose >= 12 && sourcing_name != NULL)
21263 {
21264 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021265 verbose_enter_scroll();
21266
Bram Moolenaar555b2802005-05-19 21:08:39 +000021267 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021269
21270 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 --no_wait_return;
21272 }
21273
21274 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021275 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021277 /* The a: variables typevals were not allocated, only free the allocated
Bram Moolenaar33570922005-01-25 22:26:29 +000021278 * variables. */
21279 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
21280
21281 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 --depth;
21283}
21284
21285/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021286 * Add a number variable "name" to dict "dp" with value "nr".
21287 */
21288 static void
21289add_nr_var(dp, v, name, nr)
21290 dict_T *dp;
21291 dictitem_T *v;
21292 char *name;
21293 varnumber_T nr;
21294{
21295 STRCPY(v->di_key, name);
21296 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21297 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21298 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021299 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 v->di_tv.vval.v_number = nr;
21301}
21302
21303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 * ":return [expr]"
21305 */
21306 void
21307ex_return(eap)
21308 exarg_T *eap;
21309{
21310 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021311 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 int returning = FALSE;
21313
21314 if (current_funccal == NULL)
21315 {
21316 EMSG(_("E133: :return not inside a function"));
21317 return;
21318 }
21319
21320 if (eap->skip)
21321 ++emsg_skip;
21322
21323 eap->nextcmd = NULL;
21324 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021325 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 {
21327 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021328 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021330 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 }
21332 /* It's safer to return also on error. */
21333 else if (!eap->skip)
21334 {
21335 /*
21336 * Return unless the expression evaluation has been cancelled due to an
21337 * aborting error, an interrupt, or an exception.
21338 */
21339 if (!aborting())
21340 returning = do_return(eap, FALSE, TRUE, NULL);
21341 }
21342
21343 /* When skipping or the return gets pending, advance to the next command
21344 * in this line (!returning). Otherwise, ignore the rest of the line.
21345 * Following lines will be ignored by get_func_line(). */
21346 if (returning)
21347 eap->nextcmd = NULL;
21348 else if (eap->nextcmd == NULL) /* no argument */
21349 eap->nextcmd = check_nextcmd(arg);
21350
21351 if (eap->skip)
21352 --emsg_skip;
21353}
21354
21355/*
21356 * Return from a function. Possibly makes the return pending. Also called
21357 * for a pending return at the ":endtry" or after returning from an extra
21358 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021359 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021360 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 * FALSE when the return gets pending.
21362 */
21363 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021364do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 exarg_T *eap;
21366 int reanimate;
21367 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021368 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369{
21370 int idx;
21371 struct condstack *cstack = eap->cstack;
21372
21373 if (reanimate)
21374 /* Undo the return. */
21375 current_funccal->returned = FALSE;
21376
21377 /*
21378 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21379 * not in its finally clause (which then is to be executed next) is found.
21380 * In this case, make the ":return" pending for execution at the ":endtry".
21381 * Otherwise, return normally.
21382 */
21383 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21384 if (idx >= 0)
21385 {
21386 cstack->cs_pending[idx] = CSTP_RETURN;
21387
21388 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021389 /* A pending return again gets pending. "rettv" points to an
21390 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 else
21394 {
21395 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021396 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021398 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021400 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 {
21402 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021404 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 else
21406 EMSG(_(e_outofmem));
21407 }
21408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021409 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410
21411 if (reanimate)
21412 {
21413 /* The pending return value could be overwritten by a ":return"
21414 * without argument in a finally clause; reset the default
21415 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021416 current_funccal->rettv->v_type = VAR_NUMBER;
21417 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 }
21419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021420 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 }
21422 else
21423 {
21424 current_funccal->returned = TRUE;
21425
21426 /* If the return is carried out now, store the return value. For
21427 * a return immediately after reanimation, the value is already
21428 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021429 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021431 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021432 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 }
21436 }
21437
21438 return idx < 0;
21439}
21440
21441/*
21442 * Free the variable with a pending return value.
21443 */
21444 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021445discard_pending_return(rettv)
21446 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447{
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449}
21450
21451/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 * is an allocated string. Used by report_pending() for verbose messages.
21454 */
21455 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021456get_return_cmd(rettv)
21457 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021459 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021461 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021463 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021464 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021465 if (s == NULL)
21466 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021467
21468 STRCPY(IObuff, ":return ");
21469 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21470 if (STRLEN(s) + 8 >= IOSIZE)
21471 STRCPY(IObuff + IOSIZE - 4, "...");
21472 vim_free(tofree);
21473 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474}
21475
21476/*
21477 * Get next function line.
21478 * Called by do_cmdline() to get the next line.
21479 * Returns allocated string, or NULL for end of function.
21480 */
21481/* ARGSUSED */
21482 char_u *
21483get_func_line(c, cookie, indent)
21484 int c; /* not used */
21485 void *cookie;
21486 int indent; /* not used */
21487{
Bram Moolenaar33570922005-01-25 22:26:29 +000021488 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021489 ufunc_T *fp = fcp->func;
21490 char_u *retval;
21491 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492
21493 /* If breakpoints have been added/deleted need to check for it. */
21494 if (fcp->dbg_tick != debug_tick)
21495 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021496 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 sourcing_lnum);
21498 fcp->dbg_tick = debug_tick;
21499 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021500#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021501 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021502 func_line_end(cookie);
21503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504
Bram Moolenaar05159a02005-02-26 23:04:13 +000021505 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021506 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21507 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 retval = NULL;
21509 else
21510 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021511 /* Skip NULL lines (continuation lines). */
21512 while (fcp->linenr < gap->ga_len
21513 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21514 ++fcp->linenr;
21515 if (fcp->linenr >= gap->ga_len)
21516 retval = NULL;
21517 else
21518 {
21519 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21520 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021521#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021522 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021523 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021524#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 }
21527
21528 /* Did we encounter a breakpoint? */
21529 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21530 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021531 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021533 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 sourcing_lnum);
21535 fcp->dbg_tick = debug_tick;
21536 }
21537
21538 return retval;
21539}
21540
Bram Moolenaar05159a02005-02-26 23:04:13 +000021541#if defined(FEAT_PROFILE) || defined(PROTO)
21542/*
21543 * Called when starting to read a function line.
21544 * "sourcing_lnum" must be correct!
21545 * When skipping lines it may not actually be executed, but we won't find out
21546 * until later and we need to store the time now.
21547 */
21548 void
21549func_line_start(cookie)
21550 void *cookie;
21551{
21552 funccall_T *fcp = (funccall_T *)cookie;
21553 ufunc_T *fp = fcp->func;
21554
21555 if (fp->uf_profiling && sourcing_lnum >= 1
21556 && sourcing_lnum <= fp->uf_lines.ga_len)
21557 {
21558 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021559 /* Skip continuation lines. */
21560 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21561 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021562 fp->uf_tml_execed = FALSE;
21563 profile_start(&fp->uf_tml_start);
21564 profile_zero(&fp->uf_tml_children);
21565 profile_get_wait(&fp->uf_tml_wait);
21566 }
21567}
21568
21569/*
21570 * Called when actually executing a function line.
21571 */
21572 void
21573func_line_exec(cookie)
21574 void *cookie;
21575{
21576 funccall_T *fcp = (funccall_T *)cookie;
21577 ufunc_T *fp = fcp->func;
21578
21579 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21580 fp->uf_tml_execed = TRUE;
21581}
21582
21583/*
21584 * Called when done with a function line.
21585 */
21586 void
21587func_line_end(cookie)
21588 void *cookie;
21589{
21590 funccall_T *fcp = (funccall_T *)cookie;
21591 ufunc_T *fp = fcp->func;
21592
21593 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21594 {
21595 if (fp->uf_tml_execed)
21596 {
21597 ++fp->uf_tml_count[fp->uf_tml_idx];
21598 profile_end(&fp->uf_tml_start);
21599 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021600 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021601 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21602 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021603 }
21604 fp->uf_tml_idx = -1;
21605 }
21606}
21607#endif
21608
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609/*
21610 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021611 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 */
21613 int
21614func_has_ended(cookie)
21615 void *cookie;
21616{
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618
21619 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21620 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021621 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 || fcp->returned);
21623}
21624
21625/*
21626 * return TRUE if cookie indicates a function which "abort"s on errors.
21627 */
21628 int
21629func_has_abort(cookie)
21630 void *cookie;
21631{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021632 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633}
21634
21635#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21636typedef enum
21637{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021638 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21639 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21640 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641} var_flavour_T;
21642
21643static var_flavour_T var_flavour __ARGS((char_u *varname));
21644
21645 static var_flavour_T
21646var_flavour(varname)
21647 char_u *varname;
21648{
21649 char_u *p = varname;
21650
21651 if (ASCII_ISUPPER(*p))
21652 {
21653 while (*(++p))
21654 if (ASCII_ISLOWER(*p))
21655 return VAR_FLAVOUR_SESSION;
21656 return VAR_FLAVOUR_VIMINFO;
21657 }
21658 else
21659 return VAR_FLAVOUR_DEFAULT;
21660}
21661#endif
21662
21663#if defined(FEAT_VIMINFO) || defined(PROTO)
21664/*
21665 * Restore global vars that start with a capital from the viminfo file
21666 */
21667 int
21668read_viminfo_varlist(virp, writing)
21669 vir_T *virp;
21670 int writing;
21671{
21672 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021673 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675
21676 if (!writing && (find_viminfo_parameter('!') != NULL))
21677 {
21678 tab = vim_strchr(virp->vir_line + 1, '\t');
21679 if (tab != NULL)
21680 {
21681 *tab++ = '\0'; /* isolate the variable name */
21682 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021683 type = VAR_STRING;
21684#ifdef FEAT_FLOAT
21685 else if (*tab == 'F')
21686 type = VAR_FLOAT;
21687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688
21689 tab = vim_strchr(tab, '\t');
21690 if (tab != NULL)
21691 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021692 tv.v_type = type;
21693 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021694 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021696#ifdef FEAT_FLOAT
21697 else if (type == VAR_FLOAT)
21698 (void)string2float(tab + 1, &tv.vval.v_float);
21699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021701 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021702 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021703 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021704 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 }
21706 }
21707 }
21708
21709 return viminfo_readline(virp);
21710}
21711
21712/*
21713 * Write global vars that start with a capital to the viminfo file
21714 */
21715 void
21716write_viminfo_varlist(fp)
21717 FILE *fp;
21718{
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 hashitem_T *hi;
21720 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021721 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021722 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021723 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021724 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021725 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726
21727 if (find_viminfo_parameter('!') == NULL)
21728 return;
21729
21730 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021731
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021732 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021733 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021735 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021737 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 this_var = HI2DI(hi);
21739 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021741 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021742 {
21743 case VAR_STRING: s = "STR"; break;
21744 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021745#ifdef FEAT_FLOAT
21746 case VAR_FLOAT: s = "FLO"; break;
21747#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021748 default: continue;
21749 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021750 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021751 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021752 if (p != NULL)
21753 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021754 vim_free(tofree);
21755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 }
21757 }
21758}
21759#endif
21760
21761#if defined(FEAT_SESSION) || defined(PROTO)
21762 int
21763store_session_globals(fd)
21764 FILE *fd;
21765{
Bram Moolenaar33570922005-01-25 22:26:29 +000021766 hashitem_T *hi;
21767 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021768 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 char_u *p, *t;
21770
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021771 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021772 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021774 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021776 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 this_var = HI2DI(hi);
21778 if ((this_var->di_tv.v_type == VAR_NUMBER
21779 || this_var->di_tv.v_type == VAR_STRING)
21780 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021781 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021782 /* Escape special characters with a backslash. Turn a LF and
21783 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021784 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021785 (char_u *)"\\\"\n\r");
21786 if (p == NULL) /* out of memory */
21787 break;
21788 for (t = p; *t != NUL; ++t)
21789 if (*t == '\n')
21790 *t = 'n';
21791 else if (*t == '\r')
21792 *t = 'r';
21793 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021794 this_var->di_key,
21795 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21796 : ' ',
21797 p,
21798 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21799 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021800 || put_eol(fd) == FAIL)
21801 {
21802 vim_free(p);
21803 return FAIL;
21804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 vim_free(p);
21806 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021807#ifdef FEAT_FLOAT
21808 else if (this_var->di_tv.v_type == VAR_FLOAT
21809 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21810 {
21811 float_T f = this_var->di_tv.vval.v_float;
21812 int sign = ' ';
21813
21814 if (f < 0)
21815 {
21816 f = -f;
21817 sign = '-';
21818 }
21819 if ((fprintf(fd, "let %s = %c&%f",
21820 this_var->di_key, sign, f) < 0)
21821 || put_eol(fd) == FAIL)
21822 return FAIL;
21823 }
21824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 }
21826 }
21827 return OK;
21828}
21829#endif
21830
Bram Moolenaar661b1822005-07-28 22:36:45 +000021831/*
21832 * Display script name where an item was last set.
21833 * Should only be invoked when 'verbose' is non-zero.
21834 */
21835 void
21836last_set_msg(scriptID)
21837 scid_T scriptID;
21838{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021839 char_u *p;
21840
Bram Moolenaar661b1822005-07-28 22:36:45 +000021841 if (scriptID != 0)
21842 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021843 p = home_replace_save(NULL, get_scriptname(scriptID));
21844 if (p != NULL)
21845 {
21846 verbose_enter();
21847 MSG_PUTS(_("\n\tLast set from "));
21848 MSG_PUTS(p);
21849 vim_free(p);
21850 verbose_leave();
21851 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021852 }
21853}
21854
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855#endif /* FEAT_EVAL */
21856
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021858#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859
21860#ifdef WIN3264
21861/*
21862 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21863 */
21864static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21865static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21866static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21867
21868/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021869 * Get the short path (8.3) for the filename in "fnamep".
21870 * Only works for a valid file name.
21871 * When the path gets longer "fnamep" is changed and the allocated buffer
21872 * is put in "bufp".
21873 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21874 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875 */
21876 static int
21877get_short_pathname(fnamep, bufp, fnamelen)
21878 char_u **fnamep;
21879 char_u **bufp;
21880 int *fnamelen;
21881{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021882 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 char_u *newbuf;
21884
21885 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 l = GetShortPathName(*fnamep, *fnamep, len);
21887 if (l > len - 1)
21888 {
21889 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021890 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 newbuf = vim_strnsave(*fnamep, l);
21892 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021893 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894
21895 vim_free(*bufp);
21896 *fnamep = *bufp = newbuf;
21897
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021898 /* Really should always succeed, as the buffer is big enough. */
21899 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 }
21901
21902 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021903 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904}
21905
21906/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021907 * Get the short path (8.3) for the filename in "fname". The converted
21908 * path is returned in "bufp".
21909 *
21910 * Some of the directories specified in "fname" may not exist. This function
21911 * will shorten the existing directories at the beginning of the path and then
21912 * append the remaining non-existing path.
21913 *
21914 * fname - Pointer to the filename to shorten. On return, contains the
21915 * pointer to the shortened pathname
21916 * bufp - Pointer to an allocated buffer for the filename.
21917 * fnamelen - Length of the filename pointed to by fname
21918 *
21919 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 */
21921 static int
21922shortpath_for_invalid_fname(fname, bufp, fnamelen)
21923 char_u **fname;
21924 char_u **bufp;
21925 int *fnamelen;
21926{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021927 char_u *short_fname, *save_fname, *pbuf_unused;
21928 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021930 int old_len, len;
21931 int new_len, sfx_len;
21932 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933
21934 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021935 old_len = *fnamelen;
21936 save_fname = vim_strnsave(*fname, old_len);
21937 pbuf_unused = NULL;
21938 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021940 endp = save_fname + old_len - 1; /* Find the end of the copy */
21941 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021943 /*
21944 * Try shortening the supplied path till it succeeds by removing one
21945 * directory at a time from the tail of the path.
21946 */
21947 len = 0;
21948 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021950 /* go back one path-separator */
21951 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
21952 --endp;
21953 if (endp <= save_fname)
21954 break; /* processed the complete path */
21955
21956 /*
21957 * Replace the path separator with a NUL and try to shorten the
21958 * resulting path.
21959 */
21960 ch = *endp;
21961 *endp = 0;
21962 short_fname = save_fname;
21963 len = STRLEN(short_fname) + 1;
21964 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
21965 {
21966 retval = FAIL;
21967 goto theend;
21968 }
21969 *endp = ch; /* preserve the string */
21970
21971 if (len > 0)
21972 break; /* successfully shortened the path */
21973
21974 /* failed to shorten the path. Skip the path separator */
21975 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 }
21977
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021978 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021980 /*
21981 * Succeeded in shortening the path. Now concatenate the shortened
21982 * path with the remaining path at the tail.
21983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021985 /* Compute the length of the new path. */
21986 sfx_len = (int)(save_endp - endp) + 1;
21987 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021989 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021991 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021993 /* There is not enough space in the currently allocated string,
21994 * copy it to a buffer big enough. */
21995 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021997 {
21998 retval = FAIL;
21999 goto theend;
22000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 }
22002 else
22003 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022004 /* Transfer short_fname to the main buffer (it's big enough),
22005 * unless get_short_pathname() did its work in-place. */
22006 *fname = *bufp = save_fname;
22007 if (short_fname != save_fname)
22008 vim_strncpy(save_fname, short_fname, len);
22009 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022011
22012 /* concat the not-shortened part of the path */
22013 vim_strncpy(*fname + len, endp, sfx_len);
22014 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022016
22017theend:
22018 vim_free(pbuf_unused);
22019 vim_free(save_fname);
22020
22021 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022}
22023
22024/*
22025 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022026 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 */
22028 static int
22029shortpath_for_partial(fnamep, bufp, fnamelen)
22030 char_u **fnamep;
22031 char_u **bufp;
22032 int *fnamelen;
22033{
22034 int sepcount, len, tflen;
22035 char_u *p;
22036 char_u *pbuf, *tfname;
22037 int hasTilde;
22038
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022039 /* Count up the path separators from the RHS.. so we know which part
22040 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022042 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 if (vim_ispathsep(*p))
22044 ++sepcount;
22045
22046 /* Need full path first (use expand_env() to remove a "~/") */
22047 hasTilde = (**fnamep == '~');
22048 if (hasTilde)
22049 pbuf = tfname = expand_env_save(*fnamep);
22050 else
22051 pbuf = tfname = FullName_save(*fnamep, FALSE);
22052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022053 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022055 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22056 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057
22058 if (len == 0)
22059 {
22060 /* Don't have a valid filename, so shorten the rest of the
22061 * path if we can. This CAN give us invalid 8.3 filenames, but
22062 * there's not a lot of point in guessing what it might be.
22063 */
22064 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022065 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22066 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 }
22068
22069 /* Count the paths backward to find the beginning of the desired string. */
22070 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022071 {
22072#ifdef FEAT_MBYTE
22073 if (has_mbyte)
22074 p -= mb_head_off(tfname, p);
22075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 if (vim_ispathsep(*p))
22077 {
22078 if (sepcount == 0 || (hasTilde && sepcount == 1))
22079 break;
22080 else
22081 sepcount --;
22082 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 if (hasTilde)
22085 {
22086 --p;
22087 if (p >= tfname)
22088 *p = '~';
22089 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 }
22092 else
22093 ++p;
22094
22095 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22096 vim_free(*bufp);
22097 *fnamelen = (int)STRLEN(p);
22098 *bufp = pbuf;
22099 *fnamep = p;
22100
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022101 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102}
22103#endif /* WIN3264 */
22104
22105/*
22106 * Adjust a filename, according to a string of modifiers.
22107 * *fnamep must be NUL terminated when called. When returning, the length is
22108 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022109 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 * When there is an error, *fnamep is set to NULL.
22111 */
22112 int
22113modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22114 char_u *src; /* string with modifiers */
22115 int *usedlen; /* characters after src that are used */
22116 char_u **fnamep; /* file name so far */
22117 char_u **bufp; /* buffer for allocated file name or NULL */
22118 int *fnamelen; /* length of fnamep */
22119{
22120 int valid = 0;
22121 char_u *tail;
22122 char_u *s, *p, *pbuf;
22123 char_u dirname[MAXPATHL];
22124 int c;
22125 int has_fullname = 0;
22126#ifdef WIN3264
22127 int has_shortname = 0;
22128#endif
22129
22130repeat:
22131 /* ":p" - full path/file_name */
22132 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22133 {
22134 has_fullname = 1;
22135
22136 valid |= VALID_PATH;
22137 *usedlen += 2;
22138
22139 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22140 if ((*fnamep)[0] == '~'
22141#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22142 && ((*fnamep)[1] == '/'
22143# ifdef BACKSLASH_IN_FILENAME
22144 || (*fnamep)[1] == '\\'
22145# endif
22146 || (*fnamep)[1] == NUL)
22147
22148#endif
22149 )
22150 {
22151 *fnamep = expand_env_save(*fnamep);
22152 vim_free(*bufp); /* free any allocated file name */
22153 *bufp = *fnamep;
22154 if (*fnamep == NULL)
22155 return -1;
22156 }
22157
22158 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022159 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 {
22161 if (vim_ispathsep(*p)
22162 && p[1] == '.'
22163 && (p[2] == NUL
22164 || vim_ispathsep(p[2])
22165 || (p[2] == '.'
22166 && (p[3] == NUL || vim_ispathsep(p[3])))))
22167 break;
22168 }
22169
22170 /* FullName_save() is slow, don't use it when not needed. */
22171 if (*p != NUL || !vim_isAbsName(*fnamep))
22172 {
22173 *fnamep = FullName_save(*fnamep, *p != NUL);
22174 vim_free(*bufp); /* free any allocated file name */
22175 *bufp = *fnamep;
22176 if (*fnamep == NULL)
22177 return -1;
22178 }
22179
22180 /* Append a path separator to a directory. */
22181 if (mch_isdir(*fnamep))
22182 {
22183 /* Make room for one or two extra characters. */
22184 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22185 vim_free(*bufp); /* free any allocated file name */
22186 *bufp = *fnamep;
22187 if (*fnamep == NULL)
22188 return -1;
22189 add_pathsep(*fnamep);
22190 }
22191 }
22192
22193 /* ":." - path relative to the current directory */
22194 /* ":~" - path relative to the home directory */
22195 /* ":8" - shortname path - postponed till after */
22196 while (src[*usedlen] == ':'
22197 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22198 {
22199 *usedlen += 2;
22200 if (c == '8')
22201 {
22202#ifdef WIN3264
22203 has_shortname = 1; /* Postpone this. */
22204#endif
22205 continue;
22206 }
22207 pbuf = NULL;
22208 /* Need full path first (use expand_env() to remove a "~/") */
22209 if (!has_fullname)
22210 {
22211 if (c == '.' && **fnamep == '~')
22212 p = pbuf = expand_env_save(*fnamep);
22213 else
22214 p = pbuf = FullName_save(*fnamep, FALSE);
22215 }
22216 else
22217 p = *fnamep;
22218
22219 has_fullname = 0;
22220
22221 if (p != NULL)
22222 {
22223 if (c == '.')
22224 {
22225 mch_dirname(dirname, MAXPATHL);
22226 s = shorten_fname(p, dirname);
22227 if (s != NULL)
22228 {
22229 *fnamep = s;
22230 if (pbuf != NULL)
22231 {
22232 vim_free(*bufp); /* free any allocated file name */
22233 *bufp = pbuf;
22234 pbuf = NULL;
22235 }
22236 }
22237 }
22238 else
22239 {
22240 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22241 /* Only replace it when it starts with '~' */
22242 if (*dirname == '~')
22243 {
22244 s = vim_strsave(dirname);
22245 if (s != NULL)
22246 {
22247 *fnamep = s;
22248 vim_free(*bufp);
22249 *bufp = s;
22250 }
22251 }
22252 }
22253 vim_free(pbuf);
22254 }
22255 }
22256
22257 tail = gettail(*fnamep);
22258 *fnamelen = (int)STRLEN(*fnamep);
22259
22260 /* ":h" - head, remove "/file_name", can be repeated */
22261 /* Don't remove the first "/" or "c:\" */
22262 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22263 {
22264 valid |= VALID_HEAD;
22265 *usedlen += 2;
22266 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022267 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022268 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 *fnamelen = (int)(tail - *fnamep);
22270#ifdef VMS
22271 if (*fnamelen > 0)
22272 *fnamelen += 1; /* the path separator is part of the path */
22273#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022274 if (*fnamelen == 0)
22275 {
22276 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22277 p = vim_strsave((char_u *)".");
22278 if (p == NULL)
22279 return -1;
22280 vim_free(*bufp);
22281 *bufp = *fnamep = tail = p;
22282 *fnamelen = 1;
22283 }
22284 else
22285 {
22286 while (tail > s && !after_pathsep(s, tail))
22287 mb_ptr_back(*fnamep, tail);
22288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 }
22290
22291 /* ":8" - shortname */
22292 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22293 {
22294 *usedlen += 2;
22295#ifdef WIN3264
22296 has_shortname = 1;
22297#endif
22298 }
22299
22300#ifdef WIN3264
22301 /* Check shortname after we have done 'heads' and before we do 'tails'
22302 */
22303 if (has_shortname)
22304 {
22305 pbuf = NULL;
22306 /* Copy the string if it is shortened by :h */
22307 if (*fnamelen < (int)STRLEN(*fnamep))
22308 {
22309 p = vim_strnsave(*fnamep, *fnamelen);
22310 if (p == 0)
22311 return -1;
22312 vim_free(*bufp);
22313 *bufp = *fnamep = p;
22314 }
22315
22316 /* Split into two implementations - makes it easier. First is where
22317 * there isn't a full name already, second is where there is.
22318 */
22319 if (!has_fullname && !vim_isAbsName(*fnamep))
22320 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022321 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 return -1;
22323 }
22324 else
22325 {
22326 int l;
22327
22328 /* Simple case, already have the full-name
22329 * Nearly always shorter, so try first time. */
22330 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022331 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 return -1;
22333
22334 if (l == 0)
22335 {
22336 /* Couldn't find the filename.. search the paths.
22337 */
22338 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022339 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 return -1;
22341 }
22342 *fnamelen = l;
22343 }
22344 }
22345#endif /* WIN3264 */
22346
22347 /* ":t" - tail, just the basename */
22348 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22349 {
22350 *usedlen += 2;
22351 *fnamelen -= (int)(tail - *fnamep);
22352 *fnamep = tail;
22353 }
22354
22355 /* ":e" - extension, can be repeated */
22356 /* ":r" - root, without extension, can be repeated */
22357 while (src[*usedlen] == ':'
22358 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22359 {
22360 /* find a '.' in the tail:
22361 * - for second :e: before the current fname
22362 * - otherwise: The last '.'
22363 */
22364 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22365 s = *fnamep - 2;
22366 else
22367 s = *fnamep + *fnamelen - 1;
22368 for ( ; s > tail; --s)
22369 if (s[0] == '.')
22370 break;
22371 if (src[*usedlen + 1] == 'e') /* :e */
22372 {
22373 if (s > tail)
22374 {
22375 *fnamelen += (int)(*fnamep - (s + 1));
22376 *fnamep = s + 1;
22377#ifdef VMS
22378 /* cut version from the extension */
22379 s = *fnamep + *fnamelen - 1;
22380 for ( ; s > *fnamep; --s)
22381 if (s[0] == ';')
22382 break;
22383 if (s > *fnamep)
22384 *fnamelen = s - *fnamep;
22385#endif
22386 }
22387 else if (*fnamep <= tail)
22388 *fnamelen = 0;
22389 }
22390 else /* :r */
22391 {
22392 if (s > tail) /* remove one extension */
22393 *fnamelen = (int)(s - *fnamep);
22394 }
22395 *usedlen += 2;
22396 }
22397
22398 /* ":s?pat?foo?" - substitute */
22399 /* ":gs?pat?foo?" - global substitute */
22400 if (src[*usedlen] == ':'
22401 && (src[*usedlen + 1] == 's'
22402 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22403 {
22404 char_u *str;
22405 char_u *pat;
22406 char_u *sub;
22407 int sep;
22408 char_u *flags;
22409 int didit = FALSE;
22410
22411 flags = (char_u *)"";
22412 s = src + *usedlen + 2;
22413 if (src[*usedlen + 1] == 'g')
22414 {
22415 flags = (char_u *)"g";
22416 ++s;
22417 }
22418
22419 sep = *s++;
22420 if (sep)
22421 {
22422 /* find end of pattern */
22423 p = vim_strchr(s, sep);
22424 if (p != NULL)
22425 {
22426 pat = vim_strnsave(s, (int)(p - s));
22427 if (pat != NULL)
22428 {
22429 s = p + 1;
22430 /* find end of substitution */
22431 p = vim_strchr(s, sep);
22432 if (p != NULL)
22433 {
22434 sub = vim_strnsave(s, (int)(p - s));
22435 str = vim_strnsave(*fnamep, *fnamelen);
22436 if (sub != NULL && str != NULL)
22437 {
22438 *usedlen = (int)(p + 1 - src);
22439 s = do_string_sub(str, pat, sub, flags);
22440 if (s != NULL)
22441 {
22442 *fnamep = s;
22443 *fnamelen = (int)STRLEN(s);
22444 vim_free(*bufp);
22445 *bufp = s;
22446 didit = TRUE;
22447 }
22448 }
22449 vim_free(sub);
22450 vim_free(str);
22451 }
22452 vim_free(pat);
22453 }
22454 }
22455 /* after using ":s", repeat all the modifiers */
22456 if (didit)
22457 goto repeat;
22458 }
22459 }
22460
22461 return valid;
22462}
22463
22464/*
22465 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22466 * "flags" can be "g" to do a global substitute.
22467 * Returns an allocated string, NULL for error.
22468 */
22469 char_u *
22470do_string_sub(str, pat, sub, flags)
22471 char_u *str;
22472 char_u *pat;
22473 char_u *sub;
22474 char_u *flags;
22475{
22476 int sublen;
22477 regmatch_T regmatch;
22478 int i;
22479 int do_all;
22480 char_u *tail;
22481 garray_T ga;
22482 char_u *ret;
22483 char_u *save_cpo;
22484
22485 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22486 save_cpo = p_cpo;
22487 p_cpo = (char_u *)"";
22488
22489 ga_init2(&ga, 1, 200);
22490
22491 do_all = (flags[0] == 'g');
22492
22493 regmatch.rm_ic = p_ic;
22494 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22495 if (regmatch.regprog != NULL)
22496 {
22497 tail = str;
22498 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22499 {
22500 /*
22501 * Get some space for a temporary buffer to do the substitution
22502 * into. It will contain:
22503 * - The text up to where the match is.
22504 * - The substituted text.
22505 * - The text after the match.
22506 */
22507 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22508 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22509 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22510 {
22511 ga_clear(&ga);
22512 break;
22513 }
22514
22515 /* copy the text up to where the match is */
22516 i = (int)(regmatch.startp[0] - tail);
22517 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22518 /* add the substituted text */
22519 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22520 + ga.ga_len + i, TRUE, TRUE, FALSE);
22521 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 /* avoid getting stuck on a match with an empty string */
22523 if (tail == regmatch.endp[0])
22524 {
22525 if (*tail == NUL)
22526 break;
22527 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22528 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 }
22530 else
22531 {
22532 tail = regmatch.endp[0];
22533 if (*tail == NUL)
22534 break;
22535 }
22536 if (!do_all)
22537 break;
22538 }
22539
22540 if (ga.ga_data != NULL)
22541 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22542
22543 vim_free(regmatch.regprog);
22544 }
22545
22546 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22547 ga_clear(&ga);
22548 p_cpo = save_cpo;
22549
22550 return ret;
22551}
22552
22553#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */