blob: e4024986e27bf364524b8c8d3742fb3f7c74d8a9 [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));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000408static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
409static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000411static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000416static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static listitem_T *listitem_alloc __ARGS((void));
418static void listitem_free __ARGS((listitem_T *item));
419static void listitem_remove __ARGS((list_T *l, listitem_T *item));
420static long list_len __ARGS((list_T *l));
421static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
422static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
423static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void list_append __ARGS((list_T *l, listitem_T *item));
428static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000429static int list_append_string __ARGS((list_T *l, char_u *str, int len));
430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
432static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
433static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *list2string __ARGS((typval_T *tv, int copyID));
437static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000438static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
439static void set_ref_in_list __ARGS((list_T *l, int copyID));
440static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000442static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static dictitem_T *dictitem_alloc __ARGS((char_u *key));
444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
446static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int dict_add __ARGS((dict_T *d, dictitem_T *item));
449static long dict_len __ARGS((dict_T *d));
450static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000453static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
454static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static int string2float __ARGS((char_u *text, float_T *value));
458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
460static int find_internal_func __ARGS((char_u *name));
461static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
462static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
463static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000464static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram 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 Moolenaar8d00f9c2008-06-28 13:09:56 +00004461 if (eval6(arg, rettv, evaluate, FALSE) == 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);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004497 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
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 Moolenaar8d00f9c2008-06-28 13:09:56 +00004627eval6(arg, rettv, evaluate, want_string)
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;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004631 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
Bram Moolenaar33570922005-01-25 22:26:29 +00004633 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 int op;
4635 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004636#ifdef FEAT_FLOAT
4637 int use_float = FALSE;
4638 float_T f1 = 0, f2;
4639#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004640 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /*
4643 * Get the first variable.
4644 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004645 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return FAIL;
4647
4648 /*
4649 * Repeat computing, until no '*', '/' or '%' is following.
4650 */
4651 for (;;)
4652 {
4653 op = **arg;
4654 if (op != '*' && op != '/' && op != '%')
4655 break;
4656
4657 if (evaluate)
4658 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004659#ifdef FEAT_FLOAT
4660 if (rettv->v_type == VAR_FLOAT)
4661 {
4662 f1 = rettv->vval.v_float;
4663 use_float = TRUE;
4664 n1 = 0;
4665 }
4666 else
4667#endif
4668 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 if (error)
4671 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 else
4674 n1 = 0;
4675
4676 /*
4677 * Get the second variable.
4678 */
4679 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004680 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682
4683 if (evaluate)
4684 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004685#ifdef FEAT_FLOAT
4686 if (var2.v_type == VAR_FLOAT)
4687 {
4688 if (!use_float)
4689 {
4690 f1 = n1;
4691 use_float = TRUE;
4692 }
4693 f2 = var2.vval.v_float;
4694 n2 = 0;
4695 }
4696 else
4697#endif
4698 {
4699 n2 = get_tv_number_chk(&var2, &error);
4700 clear_tv(&var2);
4701 if (error)
4702 return FAIL;
4703#ifdef FEAT_FLOAT
4704 if (use_float)
4705 f2 = n2;
4706#endif
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 /*
4710 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 if (op == '*')
4717 f1 = f1 * f2;
4718 else if (op == '/')
4719 {
4720 /* We rely on the floating point library to handle divide
4721 * by zero to result in "inf" and not a crash. */
4722 f1 = f1 / f2;
4723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 {
4726 EMSG(_("E804: Cannot use % with float"));
4727 return FAIL;
4728 }
4729 rettv->v_type = VAR_FLOAT;
4730 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735 if (op == '*')
4736 n1 = n1 * n2;
4737 else if (op == '/')
4738 {
4739 if (n2 == 0) /* give an error message? */
4740 {
4741 if (n1 == 0)
4742 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4743 else if (n1 < 0)
4744 n1 = -0x7fffffffL;
4745 else
4746 n1 = 0x7fffffffL;
4747 }
4748 else
4749 n1 = n1 / n2;
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752 {
4753 if (n2 == 0) /* give an error message? */
4754 n1 = 0;
4755 else
4756 n1 = n1 % n2;
4757 }
4758 rettv->v_type = VAR_NUMBER;
4759 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 }
4763
4764 return OK;
4765}
4766
4767/*
4768 * Handle sixth level expression:
4769 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004770 * "string" string constant
4771 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 * &option-name option value
4773 * @r register contents
4774 * identifier variable value
4775 * function() function call
4776 * $VAR environment variable
4777 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004778 * [expr, expr] List
4779 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 *
4781 * Also handle:
4782 * ! in front logical NOT
4783 * - in front unary minus
4784 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785 * trailing [] subscript in String or List
4786 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 *
4788 * "arg" must point to the first non-white of the expression.
4789 * "arg" is advanced to the next non-white after the recognized expression.
4790 *
4791 * Return OK or FAIL.
4792 */
4793 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 long n;
4801 int len;
4802 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 char_u *start_leader, *end_leader;
4804 int ret = OK;
4805 char_u *alias;
4806
4807 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004808 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 /*
4814 * Skip '!' and '-' characters. They are handled later.
4815 */
4816 start_leader = *arg;
4817 while (**arg == '!' || **arg == '-' || **arg == '+')
4818 *arg = skipwhite(*arg + 1);
4819 end_leader = *arg;
4820
4821 switch (**arg)
4822 {
4823 /*
4824 * Number constant.
4825 */
4826 case '0':
4827 case '1':
4828 case '2':
4829 case '3':
4830 case '4':
4831 case '5':
4832 case '6':
4833 case '7':
4834 case '8':
4835 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 {
4837#ifdef FEAT_FLOAT
4838 char_u *p = skipdigits(*arg + 1);
4839 int get_float = FALSE;
4840
4841 /* We accept a float when the format matches
4842 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004843 * strict to avoid backwards compatibility problems.
4844 * Don't look for a float after the "." operator, so that
4845 * ":let vers = 1.2.3" doesn't fail. */
4846 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848 get_float = TRUE;
4849 p = skipdigits(p + 2);
4850 if (*p == 'e' || *p == 'E')
4851 {
4852 ++p;
4853 if (*p == '-' || *p == '+')
4854 ++p;
4855 if (!vim_isdigit(*p))
4856 get_float = FALSE;
4857 else
4858 p = skipdigits(p + 1);
4859 }
4860 if (ASCII_ISALPHA(*p) || *p == '.')
4861 get_float = FALSE;
4862 }
4863 if (get_float)
4864 {
4865 float_T f;
4866
4867 *arg += string2float(*arg, &f);
4868 if (evaluate)
4869 {
4870 rettv->v_type = VAR_FLOAT;
4871 rettv->vval.v_float = f;
4872 }
4873 }
4874 else
4875#endif
4876 {
4877 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4878 *arg += len;
4879 if (evaluate)
4880 {
4881 rettv->v_type = VAR_NUMBER;
4882 rettv->vval.v_number = n;
4883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 /*
4889 * String constant: "string".
4890 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 break;
4893
4894 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004895 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004898 break;
4899
4900 /*
4901 * List: [expr, expr]
4902 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 break;
4905
4906 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004907 * Dictionary: {key: val, key: val}
4908 */
4909 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4910 break;
4911
4912 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004913 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004915 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 break;
4917
4918 /*
4919 * Environment variable: $VAR.
4920 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 break;
4923
4924 /*
4925 * Register contents: @r.
4926 */
4927 case '@': ++*arg;
4928 if (evaluate)
4929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004931 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 if (**arg != NUL)
4934 ++*arg;
4935 break;
4936
4937 /*
4938 * nested expression: (expression).
4939 */
4940 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004941 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (**arg == ')')
4943 ++*arg;
4944 else if (ret == OK)
4945 {
4946 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 ret = FAIL;
4949 }
4950 break;
4951
Bram Moolenaar8c711452005-01-14 21:53:12 +00004952 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 break;
4954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955
4956 if (ret == NOTDONE)
4957 {
4958 /*
4959 * Must be a variable or function name.
4960 * Can also be a curly-braces kind of name: {expr}.
4961 */
4962 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004963 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 if (alias != NULL)
4965 s = alias;
4966
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004967 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 ret = FAIL;
4969 else
4970 {
4971 if (**arg == '(') /* recursive! */
4972 {
4973 /* If "s" is the name of a variable of type VAR_FUNC
4974 * use its contents. */
4975 s = deref_func_name(s, &len);
4976
4977 /* Invoke the function. */
4978 ret = get_func_tv(s, len, rettv, arg,
4979 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004980 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 /* Stop the expression evaluation when immediately
4982 * aborting on error, or when an interrupt occurred or
4983 * an exception was thrown but not caught. */
4984 if (aborting())
4985 {
4986 if (ret == OK)
4987 clear_tv(rettv);
4988 ret = FAIL;
4989 }
4990 }
4991 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004992 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004993 else
4994 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 }
4996
4997 if (alias != NULL)
4998 vim_free(alias);
4999 }
5000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 *arg = skipwhite(*arg);
5002
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005003 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5004 * expr(expr). */
5005 if (ret == OK)
5006 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 /*
5009 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5010 */
5011 if (ret == OK && evaluate && end_leader > start_leader)
5012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005013 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 int val = 0;
5015#ifdef FEAT_FLOAT
5016 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005017
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 if (rettv->v_type == VAR_FLOAT)
5019 f = rettv->vval.v_float;
5020 else
5021#endif
5022 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005023 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005025 clear_tv(rettv);
5026 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005028 else
5029 {
5030 while (end_leader > start_leader)
5031 {
5032 --end_leader;
5033 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 {
5035#ifdef FEAT_FLOAT
5036 if (rettv->v_type == VAR_FLOAT)
5037 f = !f;
5038 else
5039#endif
5040 val = !val;
5041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005042 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043 {
5044#ifdef FEAT_FLOAT
5045 if (rettv->v_type == VAR_FLOAT)
5046 f = -f;
5047 else
5048#endif
5049 val = -val;
5050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005051 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005052#ifdef FEAT_FLOAT
5053 if (rettv->v_type == VAR_FLOAT)
5054 {
5055 clear_tv(rettv);
5056 rettv->vval.v_float = f;
5057 }
5058 else
5059#endif
5060 {
5061 clear_tv(rettv);
5062 rettv->v_type = VAR_NUMBER;
5063 rettv->vval.v_number = val;
5064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067
5068 return ret;
5069}
5070
5071/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005072 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5073 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5075 */
5076 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005077eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005079 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005081 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005082{
5083 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005084 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 long len = -1;
5087 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005090
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 if (rettv->v_type == VAR_FUNC
5092#ifdef FEAT_FLOAT
5093 || rettv->v_type == VAR_FLOAT
5094#endif
5095 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005097 if (verbose)
5098 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 return FAIL;
5100 }
5101
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005103 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 /*
5105 * dict.name
5106 */
5107 key = *arg + 1;
5108 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5109 ;
5110 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 }
5114 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 /*
5117 * something[idx]
5118 *
5119 * Get the (first) variable from inside the [].
5120 */
5121 *arg = skipwhite(*arg + 1);
5122 if (**arg == ':')
5123 empty1 = TRUE;
5124 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5125 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5127 {
5128 /* not a number or string */
5129 clear_tv(&var1);
5130 return FAIL;
5131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132
5133 /*
5134 * Get the second variable from inside the [:].
5135 */
5136 if (**arg == ':')
5137 {
5138 range = TRUE;
5139 *arg = skipwhite(*arg + 1);
5140 if (**arg == ']')
5141 empty2 = TRUE;
5142 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005144 if (!empty1)
5145 clear_tv(&var1);
5146 return FAIL;
5147 }
5148 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5149 {
5150 /* not a number or string */
5151 if (!empty1)
5152 clear_tv(&var1);
5153 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 return FAIL;
5155 }
5156 }
5157
5158 /* Check for the ']'. */
5159 if (**arg != ']')
5160 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161 if (verbose)
5162 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 clear_tv(&var1);
5164 if (range)
5165 clear_tv(&var2);
5166 return FAIL;
5167 }
5168 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 }
5170
5171 if (evaluate)
5172 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 n1 = 0;
5174 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 n1 = get_tv_number(&var1);
5177 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 }
5179 if (range)
5180 {
5181 if (empty2)
5182 n2 = -1;
5183 else
5184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 n2 = get_tv_number(&var2);
5186 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 }
5188 }
5189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 {
5192 case VAR_NUMBER:
5193 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 len = (long)STRLEN(s);
5196 if (range)
5197 {
5198 /* The resulting variable is a substring. If the indexes
5199 * are out of range the result is empty. */
5200 if (n1 < 0)
5201 {
5202 n1 = len + n1;
5203 if (n1 < 0)
5204 n1 = 0;
5205 }
5206 if (n2 < 0)
5207 n2 = len + n2;
5208 else if (n2 >= len)
5209 n2 = len;
5210 if (n1 >= len || n2 < 0 || n1 > n2)
5211 s = NULL;
5212 else
5213 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5214 }
5215 else
5216 {
5217 /* The resulting variable is a string of a single
5218 * character. If the index is too big or negative the
5219 * result is empty. */
5220 if (n1 >= len || n1 < 0)
5221 s = NULL;
5222 else
5223 s = vim_strnsave(s + n1, 1);
5224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 clear_tv(rettv);
5226 rettv->v_type = VAR_STRING;
5227 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 break;
5229
5230 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 if (n1 < 0)
5233 n1 = len + n1;
5234 if (!empty1 && (n1 < 0 || n1 >= len))
5235 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005236 /* For a range we allow invalid values and return an empty
5237 * list. A list index out of range is an error. */
5238 if (!range)
5239 {
5240 if (verbose)
5241 EMSGN(_(e_listidx), n1);
5242 return FAIL;
5243 }
5244 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245 }
5246 if (range)
5247 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005248 list_T *l;
5249 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250
5251 if (n2 < 0)
5252 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005253 else if (n2 >= len)
5254 n2 = len - 1;
5255 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005256 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 l = list_alloc();
5258 if (l == NULL)
5259 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 n1 <= n2; ++n1)
5262 {
5263 if (list_append_tv(l, &item->li_tv) == FAIL)
5264 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005265 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 return FAIL;
5267 }
5268 item = item->li_next;
5269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 clear_tv(rettv);
5271 rettv->v_type = VAR_LIST;
5272 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005273 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 }
5275 else
5276 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005277 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 clear_tv(rettv);
5279 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 }
5281 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282
5283 case VAR_DICT:
5284 if (range)
5285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 if (verbose)
5287 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 if (len == -1)
5289 clear_tv(&var1);
5290 return FAIL;
5291 }
5292 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005293 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294
5295 if (len == -1)
5296 {
5297 key = get_tv_string(&var1);
5298 if (*key == NUL)
5299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005300 if (verbose)
5301 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 clear_tv(&var1);
5303 return FAIL;
5304 }
5305 }
5306
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005307 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005309 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005310 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 if (len == -1)
5312 clear_tv(&var1);
5313 if (item == NULL)
5314 return FAIL;
5315
5316 copy_tv(&item->di_tv, &var1);
5317 clear_tv(rettv);
5318 *rettv = var1;
5319 }
5320 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 }
5322 }
5323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 return OK;
5325}
5326
5327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 * Get an option value.
5329 * "arg" points to the '&' or '+' before the option name.
5330 * "arg" is advanced to character after the option name.
5331 * Return OK or FAIL.
5332 */
5333 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005334get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 int evaluate;
5338{
5339 char_u *option_end;
5340 long numval;
5341 char_u *stringval;
5342 int opt_type;
5343 int c;
5344 int working = (**arg == '+'); /* has("+option") */
5345 int ret = OK;
5346 int opt_flags;
5347
5348 /*
5349 * Isolate the option name and find its value.
5350 */
5351 option_end = find_option_end(arg, &opt_flags);
5352 if (option_end == NULL)
5353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 EMSG2(_("E112: Option name missing: %s"), *arg);
5356 return FAIL;
5357 }
5358
5359 if (!evaluate)
5360 {
5361 *arg = option_end;
5362 return OK;
5363 }
5364
5365 c = *option_end;
5366 *option_end = NUL;
5367 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 if (opt_type == -3) /* invalid name */
5371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 EMSG2(_("E113: Unknown option: %s"), *arg);
5374 ret = FAIL;
5375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
5378 if (opt_type == -2) /* hidden string option */
5379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005380 rettv->v_type = VAR_STRING;
5381 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 else if (opt_type == -1) /* hidden number option */
5384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 rettv->v_type = VAR_NUMBER;
5386 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 else if (opt_type == 1) /* number option */
5389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 rettv->v_type = VAR_NUMBER;
5391 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 else /* string option */
5394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395 rettv->v_type = VAR_STRING;
5396 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 }
5399 else if (working && (opt_type == -2 || opt_type == -1))
5400 ret = FAIL;
5401
5402 *option_end = c; /* put back for error messages */
5403 *arg = option_end;
5404
5405 return ret;
5406}
5407
5408/*
5409 * Allocate a variable for a string constant.
5410 * Return OK or FAIL.
5411 */
5412 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005413get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 int evaluate;
5417{
5418 char_u *p;
5419 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 int extra = 0;
5421
5422 /*
5423 * Find the end of the string, skipping backslashed characters.
5424 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005425 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
5427 if (*p == '\\' && p[1] != NUL)
5428 {
5429 ++p;
5430 /* A "\<x>" form occupies at least 4 characters, and produces up
5431 * to 6 characters: reserve space for 2 extra */
5432 if (*p == '<')
5433 extra += 2;
5434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 }
5436
5437 if (*p != '"')
5438 {
5439 EMSG2(_("E114: Missing quote: %s"), *arg);
5440 return FAIL;
5441 }
5442
5443 /* If only parsing, set *arg and return here */
5444 if (!evaluate)
5445 {
5446 *arg = p + 1;
5447 return OK;
5448 }
5449
5450 /*
5451 * Copy the string into allocated memory, handling backslashed
5452 * characters.
5453 */
5454 name = alloc((unsigned)(p - *arg + extra));
5455 if (name == NULL)
5456 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 rettv->v_type = VAR_STRING;
5458 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
5462 if (*p == '\\')
5463 {
5464 switch (*++p)
5465 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 case 'b': *name++ = BS; ++p; break;
5467 case 'e': *name++ = ESC; ++p; break;
5468 case 'f': *name++ = FF; ++p; break;
5469 case 'n': *name++ = NL; ++p; break;
5470 case 'r': *name++ = CAR; ++p; break;
5471 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473 case 'X': /* hex: "\x1", "\x12" */
5474 case 'x':
5475 case 'u': /* Unicode: "\u0023" */
5476 case 'U':
5477 if (vim_isxdigit(p[1]))
5478 {
5479 int n, nr;
5480 int c = toupper(*p);
5481
5482 if (c == 'X')
5483 n = 2;
5484 else
5485 n = 4;
5486 nr = 0;
5487 while (--n >= 0 && vim_isxdigit(p[1]))
5488 {
5489 ++p;
5490 nr = (nr << 4) + hex2nr(*p);
5491 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493#ifdef FEAT_MBYTE
5494 /* For "\u" store the number according to
5495 * 'encoding'. */
5496 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
5499#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 break;
5503
5504 /* octal: "\1", "\12", "\123" */
5505 case '0':
5506 case '1':
5507 case '2':
5508 case '3':
5509 case '4':
5510 case '5':
5511 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005512 case '7': *name = *p++ - '0';
5513 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515 *name = (*name << 3) + *p++ - '0';
5516 if (*p >= '0' && *p <= '7')
5517 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 break;
5521
5522 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005523 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 if (extra != 0)
5525 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 break;
5528 }
5529 /* FALLTHROUGH */
5530
Bram Moolenaar8c711452005-01-14 21:53:12 +00005531 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 break;
5533 }
5534 }
5535 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005539 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 *arg = p + 1;
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 return OK;
5543}
5544
5545/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 * Return OK or FAIL.
5548 */
5549 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 int evaluate;
5554{
5555 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005556 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005557 int reduce = 0;
5558
5559 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005560 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005561 */
5562 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5563 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005565 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005567 break;
5568 ++reduce;
5569 ++p;
5570 }
5571 }
5572
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005574 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 return FAIL;
5577 }
5578
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005580 if (!evaluate)
5581 {
5582 *arg = p + 1;
5583 return OK;
5584 }
5585
5586 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005588 */
5589 str = alloc((unsigned)((p - *arg) - reduce));
5590 if (str == NULL)
5591 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 rettv->v_type = VAR_STRING;
5593 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005594
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 break;
5601 ++p;
5602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005606 *arg = p + 1;
5607
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005608 return OK;
5609}
5610
5611/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 * Allocate a variable for a List and fill it from "*arg".
5613 * Return OK or FAIL.
5614 */
5615 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 int evaluate;
5620{
Bram Moolenaar33570922005-01-25 22:26:29 +00005621 list_T *l = NULL;
5622 typval_T tv;
5623 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624
5625 if (evaluate)
5626 {
5627 l = list_alloc();
5628 if (l == NULL)
5629 return FAIL;
5630 }
5631
5632 *arg = skipwhite(*arg + 1);
5633 while (**arg != ']' && **arg != NUL)
5634 {
5635 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5636 goto failret;
5637 if (evaluate)
5638 {
5639 item = listitem_alloc();
5640 if (item != NULL)
5641 {
5642 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005643 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 list_append(l, item);
5645 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005646 else
5647 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 }
5649
5650 if (**arg == ']')
5651 break;
5652 if (**arg != ',')
5653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 goto failret;
5656 }
5657 *arg = skipwhite(*arg + 1);
5658 }
5659
5660 if (**arg != ']')
5661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663failret:
5664 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005665 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 return FAIL;
5667 }
5668
5669 *arg = skipwhite(*arg + 1);
5670 if (evaluate)
5671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv->v_type = VAR_LIST;
5673 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 ++l->lv_refcount;
5675 }
5676
5677 return OK;
5678}
5679
5680/*
5681 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005682 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005684 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685list_alloc()
5686{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005687 list_T *l;
5688
5689 l = (list_T *)alloc_clear(sizeof(list_T));
5690 if (l != NULL)
5691 {
5692 /* Prepend the list to the list of lists for garbage collection. */
5693 if (first_list != NULL)
5694 first_list->lv_used_prev = l;
5695 l->lv_used_prev = NULL;
5696 l->lv_used_next = first_list;
5697 first_list = l;
5698 }
5699 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700}
5701
5702/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005703 * Allocate an empty list for a return value.
5704 * Returns OK or FAIL.
5705 */
5706 static int
5707rettv_list_alloc(rettv)
5708 typval_T *rettv;
5709{
5710 list_T *l = list_alloc();
5711
5712 if (l == NULL)
5713 return FAIL;
5714
5715 rettv->vval.v_list = l;
5716 rettv->v_type = VAR_LIST;
5717 ++l->lv_refcount;
5718 return OK;
5719}
5720
5721/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 * Unreference a list: decrement the reference count and free it when it
5723 * becomes zero.
5724 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005725 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005727 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005729 if (l != NULL && --l->lv_refcount <= 0)
5730 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731}
5732
5733/*
5734 * Free a list, including all items it points to.
5735 * Ignores the reference count.
5736 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005737 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005738list_free(l, recurse)
5739 list_T *l;
5740 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741{
Bram Moolenaar33570922005-01-25 22:26:29 +00005742 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005744 /* Remove the list from the list of lists for garbage collection. */
5745 if (l->lv_used_prev == NULL)
5746 first_list = l->lv_used_next;
5747 else
5748 l->lv_used_prev->lv_used_next = l->lv_used_next;
5749 if (l->lv_used_next != NULL)
5750 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5751
Bram Moolenaard9fba312005-06-26 22:34:35 +00005752 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005754 /* Remove the item before deleting it. */
5755 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005756 if (recurse || (item->li_tv.v_type != VAR_LIST
5757 && item->li_tv.v_type != VAR_DICT))
5758 clear_tv(&item->li_tv);
5759 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 }
5761 vim_free(l);
5762}
5763
5764/*
5765 * Allocate a list item.
5766 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005767 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768listitem_alloc()
5769{
Bram Moolenaar33570922005-01-25 22:26:29 +00005770 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771}
5772
5773/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005774 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 */
5776 static void
5777listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005778 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005780 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 vim_free(item);
5782}
5783
5784/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005785 * Remove a list item from a List and free it. Also clears the value.
5786 */
5787 static void
5788listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005789 list_T *l;
5790 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005791{
5792 list_remove(l, item, item);
5793 listitem_free(item);
5794}
5795
5796/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 * Get the number of items in a list.
5798 */
5799 static long
5800list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 if (l == NULL)
5804 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005805 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806}
5807
5808/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005809 * Return TRUE when two lists have exactly the same values.
5810 */
5811 static int
5812list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 list_T *l1;
5814 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005815 int ic; /* ignore case for strings */
5816{
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005818
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005819 if (l1 == l2)
5820 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005821 if (list_len(l1) != list_len(l2))
5822 return FALSE;
5823
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005824 for (item1 = l1->lv_first, item2 = l2->lv_first;
5825 item1 != NULL && item2 != NULL;
5826 item1 = item1->li_next, item2 = item2->li_next)
5827 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5828 return FALSE;
5829 return item1 == NULL && item2 == NULL;
5830}
5831
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005832#if defined(FEAT_PYTHON) || defined(PROTO)
5833/*
5834 * Return the dictitem that an entry in a hashtable points to.
5835 */
5836 dictitem_T *
5837dict_lookup(hi)
5838 hashitem_T *hi;
5839{
5840 return HI2DI(hi);
5841}
5842#endif
5843
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005844/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005845 * Return TRUE when two dictionaries have exactly the same key/values.
5846 */
5847 static int
5848dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 dict_T *d1;
5850 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005851 int ic; /* ignore case for strings */
5852{
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 hashitem_T *hi;
5854 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005856
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005857 if (d1 == d2)
5858 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005859 if (dict_len(d1) != dict_len(d2))
5860 return FALSE;
5861
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005862 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005864 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005865 if (!HASHITEM_EMPTY(hi))
5866 {
5867 item2 = dict_find(d2, hi->hi_key, -1);
5868 if (item2 == NULL)
5869 return FALSE;
5870 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5871 return FALSE;
5872 --todo;
5873 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005874 }
5875 return TRUE;
5876}
5877
5878/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005880 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005881 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882 */
5883 static int
5884tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 typval_T *tv1;
5886 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887 int ic; /* ignore case */
5888{
5889 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005890 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005891 static int recursive = 0; /* cach recursive loops */
5892 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005893
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005894 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005895 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005896 /* Catch lists and dicts that have an endless loop by limiting
5897 * recursiveness to 1000. We guess they are equal then. */
5898 if (recursive >= 1000)
5899 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005900
5901 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005902 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005903 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005904 ++recursive;
5905 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5906 --recursive;
5907 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005908
5909 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005910 ++recursive;
5911 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5912 --recursive;
5913 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005914
5915 case VAR_FUNC:
5916 return (tv1->vval.v_string != NULL
5917 && tv2->vval.v_string != NULL
5918 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5919
5920 case VAR_NUMBER:
5921 return tv1->vval.v_number == tv2->vval.v_number;
5922
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005923#ifdef FEAT_FLOAT
5924 case VAR_FLOAT:
5925 return tv1->vval.v_float == tv2->vval.v_float;
5926#endif
5927
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005928 case VAR_STRING:
5929 s1 = get_tv_string_buf(tv1, buf1);
5930 s2 = get_tv_string_buf(tv2, buf2);
5931 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005933
5934 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005935 return TRUE;
5936}
5937
5938/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 * Locate item with index "n" in list "l" and return it.
5940 * A negative index is counted from the end; -1 is the last item.
5941 * Returns NULL when "n" is out of range.
5942 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 long n;
5947{
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 long idx;
5950
5951 if (l == NULL)
5952 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005953
5954 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005956 n = l->lv_len + n;
5957
5958 /* Check for index out of range. */
5959 if (n < 0 || n >= l->lv_len)
5960 return NULL;
5961
5962 /* When there is a cached index may start search from there. */
5963 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005965 if (n < l->lv_idx / 2)
5966 {
5967 /* closest to the start of the list */
5968 item = l->lv_first;
5969 idx = 0;
5970 }
5971 else if (n > (l->lv_idx + l->lv_len) / 2)
5972 {
5973 /* closest to the end of the list */
5974 item = l->lv_last;
5975 idx = l->lv_len - 1;
5976 }
5977 else
5978 {
5979 /* closest to the cached index */
5980 item = l->lv_idx_item;
5981 idx = l->lv_idx;
5982 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 }
5984 else
5985 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005986 if (n < l->lv_len / 2)
5987 {
5988 /* closest to the start of the list */
5989 item = l->lv_first;
5990 idx = 0;
5991 }
5992 else
5993 {
5994 /* closest to the end of the list */
5995 item = l->lv_last;
5996 idx = l->lv_len - 1;
5997 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005999
6000 while (n > idx)
6001 {
6002 /* search forward */
6003 item = item->li_next;
6004 ++idx;
6005 }
6006 while (n < idx)
6007 {
6008 /* search backward */
6009 item = item->li_prev;
6010 --idx;
6011 }
6012
6013 /* cache the used index */
6014 l->lv_idx = idx;
6015 l->lv_idx_item = item;
6016
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 return item;
6018}
6019
6020/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006021 * Get list item "l[idx]" as a number.
6022 */
6023 static long
6024list_find_nr(l, idx, errorp)
6025 list_T *l;
6026 long idx;
6027 int *errorp; /* set to TRUE when something wrong */
6028{
6029 listitem_T *li;
6030
6031 li = list_find(l, idx);
6032 if (li == NULL)
6033 {
6034 if (errorp != NULL)
6035 *errorp = TRUE;
6036 return -1L;
6037 }
6038 return get_tv_number_chk(&li->li_tv, errorp);
6039}
6040
6041/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006042 * Locate "item" list "l" and return its index.
6043 * Returns -1 when "item" is not in the list.
6044 */
6045 static long
6046list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 list_T *l;
6048 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006049{
6050 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006052
6053 if (l == NULL)
6054 return -1;
6055 idx = 0;
6056 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6057 ++idx;
6058 if (li == NULL)
6059 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006060 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006061}
6062
6063/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 * Append item "item" to the end of list "l".
6065 */
6066 static void
6067list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 list_T *l;
6069 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070{
6071 if (l->lv_last == NULL)
6072 {
6073 /* empty list */
6074 l->lv_first = item;
6075 l->lv_last = item;
6076 item->li_prev = NULL;
6077 }
6078 else
6079 {
6080 l->lv_last->li_next = item;
6081 item->li_prev = l->lv_last;
6082 l->lv_last = item;
6083 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006084 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 item->li_next = NULL;
6086}
6087
6088/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 */
6092 static int
6093list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006094 list_T *l;
6095 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006097 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098
Bram Moolenaar05159a02005-02-26 23:04:13 +00006099 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006101 copy_tv(tv, &li->li_tv);
6102 list_append(l, li);
6103 return OK;
6104}
6105
6106/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006107 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006108 * Return FAIL when out of memory.
6109 */
6110 int
6111list_append_dict(list, dict)
6112 list_T *list;
6113 dict_T *dict;
6114{
6115 listitem_T *li = listitem_alloc();
6116
6117 if (li == NULL)
6118 return FAIL;
6119 li->li_tv.v_type = VAR_DICT;
6120 li->li_tv.v_lock = 0;
6121 li->li_tv.vval.v_dict = dict;
6122 list_append(list, li);
6123 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124 return OK;
6125}
6126
6127/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006128 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006129 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006130 * Returns FAIL when out of memory.
6131 */
6132 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006133list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006134 list_T *l;
6135 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006136 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006137{
6138 listitem_T *li = listitem_alloc();
6139
6140 if (li == NULL)
6141 return FAIL;
6142 list_append(l, li);
6143 li->li_tv.v_type = VAR_STRING;
6144 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006145 if (str == NULL)
6146 li->li_tv.vval.v_string = NULL;
6147 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006148 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006149 return FAIL;
6150 return OK;
6151}
6152
6153/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006154 * Append "n" to list "l".
6155 * Returns FAIL when out of memory.
6156 */
6157 static int
6158list_append_number(l, n)
6159 list_T *l;
6160 varnumber_T n;
6161{
6162 listitem_T *li;
6163
6164 li = listitem_alloc();
6165 if (li == NULL)
6166 return FAIL;
6167 li->li_tv.v_type = VAR_NUMBER;
6168 li->li_tv.v_lock = 0;
6169 li->li_tv.vval.v_number = n;
6170 list_append(l, li);
6171 return OK;
6172}
6173
6174/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176 * If "item" is NULL append at the end.
6177 * Return FAIL when out of memory.
6178 */
6179 static int
6180list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 list_T *l;
6182 typval_T *tv;
6183 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184{
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186
6187 if (ni == NULL)
6188 return FAIL;
6189 copy_tv(tv, &ni->li_tv);
6190 if (item == NULL)
6191 /* Append new item at end of list. */
6192 list_append(l, ni);
6193 else
6194 {
6195 /* Insert new item before existing item. */
6196 ni->li_prev = item->li_prev;
6197 ni->li_next = item;
6198 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006199 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006201 ++l->lv_idx;
6202 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006204 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006206 l->lv_idx_item = NULL;
6207 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006208 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006209 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210 }
6211 return OK;
6212}
6213
6214/*
6215 * Extend "l1" with "l2".
6216 * If "bef" is NULL append at the end, otherwise insert before this item.
6217 * Returns FAIL when out of memory.
6218 */
6219 static int
6220list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 list_T *l1;
6222 list_T *l2;
6223 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006224{
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006226
6227 for (item = l2->lv_first; item != NULL; item = item->li_next)
6228 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6229 return FAIL;
6230 return OK;
6231}
6232
6233/*
6234 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6235 * Return FAIL when out of memory.
6236 */
6237 static int
6238list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 list_T *l1;
6240 list_T *l2;
6241 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242{
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006244
6245 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006246 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247 if (l == NULL)
6248 return FAIL;
6249 tv->v_type = VAR_LIST;
6250 tv->vval.v_list = l;
6251
6252 /* append all items from the second list */
6253 return list_extend(l, l2, NULL);
6254}
6255
6256/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006257 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006259 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 * Returns NULL when out of memory.
6261 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006263list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006265 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006266 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267{
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 list_T *copy;
6269 listitem_T *item;
6270 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271
6272 if (orig == NULL)
6273 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274
6275 copy = list_alloc();
6276 if (copy != NULL)
6277 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006278 if (copyID != 0)
6279 {
6280 /* Do this before adding the items, because one of the items may
6281 * refer back to this list. */
6282 orig->lv_copyID = copyID;
6283 orig->lv_copylist = copy;
6284 }
6285 for (item = orig->lv_first; item != NULL && !got_int;
6286 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 {
6288 ni = listitem_alloc();
6289 if (ni == NULL)
6290 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006291 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006292 {
6293 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6294 {
6295 vim_free(ni);
6296 break;
6297 }
6298 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006300 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 list_append(copy, ni);
6302 }
6303 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006304 if (item != NULL)
6305 {
6306 list_unref(copy);
6307 copy = NULL;
6308 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 }
6310
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 return copy;
6312}
6313
6314/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006315 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006316 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006318 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006319list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 list_T *l;
6321 listitem_T *item;
6322 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006323{
Bram Moolenaar33570922005-01-25 22:26:29 +00006324 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326 /* notify watchers */
6327 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006329 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330 list_fix_watch(l, ip);
6331 if (ip == item2)
6332 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334
6335 if (item2->li_next == NULL)
6336 l->lv_last = item->li_prev;
6337 else
6338 item2->li_next->li_prev = item->li_prev;
6339 if (item->li_prev == NULL)
6340 l->lv_first = item2->li_next;
6341 else
6342 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006343 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344}
6345
6346/*
6347 * Return an allocated string with the string representation of a list.
6348 * May return NULL.
6349 */
6350 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006351list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006353 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354{
6355 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356
6357 if (tv->vval.v_list == NULL)
6358 return NULL;
6359 ga_init2(&ga, (int)sizeof(char), 80);
6360 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006361 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006362 {
6363 vim_free(ga.ga_data);
6364 return NULL;
6365 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366 ga_append(&ga, ']');
6367 ga_append(&ga, NUL);
6368 return (char_u *)ga.ga_data;
6369}
6370
6371/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006372 * Join list "l" into a string in "*gap", using separator "sep".
6373 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006374 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006375 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006377list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006378 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006380 char_u *sep;
6381 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006382 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006383{
6384 int first = TRUE;
6385 char_u *tofree;
6386 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006388 char_u *s;
6389
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006390 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006391 {
6392 if (first)
6393 first = FALSE;
6394 else
6395 ga_concat(gap, sep);
6396
6397 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006398 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006399 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006400 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006401 if (s != NULL)
6402 ga_concat(gap, s);
6403 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 if (s == NULL)
6405 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006406 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006407 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006408}
6409
6410/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006411 * Garbage collection for lists and dictionaries.
6412 *
6413 * We use reference counts to be able to free most items right away when they
6414 * are no longer used. But for composite items it's possible that it becomes
6415 * unused while the reference count is > 0: When there is a recursive
6416 * reference. Example:
6417 * :let l = [1, 2, 3]
6418 * :let d = {9: l}
6419 * :let l[1] = d
6420 *
6421 * Since this is quite unusual we handle this with garbage collection: every
6422 * once in a while find out which lists and dicts are not referenced from any
6423 * variable.
6424 *
6425 * Here is a good reference text about garbage collection (refers to Python
6426 * but it applies to all reference-counting mechanisms):
6427 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006428 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006429
6430/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006431 * Do garbage collection for lists and dicts.
6432 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006433 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006434 int
6435garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006436{
6437 dict_T *dd;
6438 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006439 int copyID = ++current_copyID;
6440 buf_T *buf;
6441 win_T *wp;
6442 int i;
6443 funccall_T *fc;
6444 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006445#ifdef FEAT_WINDOWS
6446 tabpage_T *tp;
6447#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006448
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006449 /* Only do this once. */
6450 want_garbage_collect = FALSE;
6451 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006452 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006453
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006454 /*
6455 * 1. Go through all accessible variables and mark all lists and dicts
6456 * with copyID.
6457 */
6458 /* script-local variables */
6459 for (i = 1; i <= ga_scripts.ga_len; ++i)
6460 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6461
6462 /* buffer-local variables */
6463 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6464 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6465
6466 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006467 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006468 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6469
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006470#ifdef FEAT_WINDOWS
6471 /* tabpage-local variables */
6472 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6473 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6474#endif
6475
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006476 /* global variables */
6477 set_ref_in_ht(&globvarht, copyID);
6478
6479 /* function-local variables */
6480 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6481 {
6482 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6483 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6484 }
6485
6486 /*
6487 * 2. Go through the list of dicts and free items without the copyID.
6488 */
6489 for (dd = first_dict; dd != NULL; )
6490 if (dd->dv_copyID != copyID)
6491 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006492 /* Free the Dictionary and ordinary items it contains, but don't
6493 * recurse into Lists and Dictionaries, they will be in the list
6494 * of dicts or list of lists. */
6495 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006496 did_free = TRUE;
6497
6498 /* restart, next dict may also have been freed */
6499 dd = first_dict;
6500 }
6501 else
6502 dd = dd->dv_used_next;
6503
6504 /*
6505 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006506 * But don't free a list that has a watcher (used in a for loop), these
6507 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006508 */
6509 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006510 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006511 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006512 /* Free the List and ordinary items it contains, but don't recurse
6513 * into Lists and Dictionaries, they will be in the list of dicts
6514 * or list of lists. */
6515 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006516 did_free = TRUE;
6517
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006518 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006519 ll = first_list;
6520 }
6521 else
6522 ll = ll->lv_used_next;
6523
6524 return did_free;
6525}
6526
6527/*
6528 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6529 */
6530 static void
6531set_ref_in_ht(ht, copyID)
6532 hashtab_T *ht;
6533 int copyID;
6534{
6535 int todo;
6536 hashitem_T *hi;
6537
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006538 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006539 for (hi = ht->ht_array; todo > 0; ++hi)
6540 if (!HASHITEM_EMPTY(hi))
6541 {
6542 --todo;
6543 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6544 }
6545}
6546
6547/*
6548 * Mark all lists and dicts referenced through list "l" with "copyID".
6549 */
6550 static void
6551set_ref_in_list(l, copyID)
6552 list_T *l;
6553 int copyID;
6554{
6555 listitem_T *li;
6556
6557 for (li = l->lv_first; li != NULL; li = li->li_next)
6558 set_ref_in_item(&li->li_tv, copyID);
6559}
6560
6561/*
6562 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6563 */
6564 static void
6565set_ref_in_item(tv, copyID)
6566 typval_T *tv;
6567 int copyID;
6568{
6569 dict_T *dd;
6570 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006571
6572 switch (tv->v_type)
6573 {
6574 case VAR_DICT:
6575 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006576 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006577 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006578 /* Didn't see this dict yet. */
6579 dd->dv_copyID = copyID;
6580 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006581 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006583
6584 case VAR_LIST:
6585 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006586 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006587 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006588 /* Didn't see this list yet. */
6589 ll->lv_copyID = copyID;
6590 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006591 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006592 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006593 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006594 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006595}
6596
6597/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598 * Allocate an empty header for a dictionary.
6599 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006600 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601dict_alloc()
6602{
Bram Moolenaar33570922005-01-25 22:26:29 +00006603 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006604
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006606 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006607 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006608 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006609 if (first_dict != NULL)
6610 first_dict->dv_used_prev = d;
6611 d->dv_used_next = first_dict;
6612 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006613 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006614
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006616 d->dv_lock = 0;
6617 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006618 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006619 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006620 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006621}
6622
6623/*
6624 * Unreference a Dictionary: decrement the reference count and free it when it
6625 * becomes zero.
6626 */
6627 static void
6628dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006629 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006630{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006631 if (d != NULL && --d->dv_refcount <= 0)
6632 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006633}
6634
6635/*
6636 * Free a Dictionary, including all items it contains.
6637 * Ignores the reference count.
6638 */
6639 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006640dict_free(d, recurse)
6641 dict_T *d;
6642 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006644 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006646 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006648 /* Remove the dict from the list of dicts for garbage collection. */
6649 if (d->dv_used_prev == NULL)
6650 first_dict = d->dv_used_next;
6651 else
6652 d->dv_used_prev->dv_used_next = d->dv_used_next;
6653 if (d->dv_used_next != NULL)
6654 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6655
6656 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006657 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006658 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 if (!HASHITEM_EMPTY(hi))
6662 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006663 /* Remove the item before deleting it, just in case there is
6664 * something recursive causing trouble. */
6665 di = HI2DI(hi);
6666 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006667 if (recurse || (di->di_tv.v_type != VAR_LIST
6668 && di->di_tv.v_type != VAR_DICT))
6669 clear_tv(&di->di_tv);
6670 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006671 --todo;
6672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006673 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006675 vim_free(d);
6676}
6677
6678/*
6679 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006680 * The "key" is copied to the new item.
6681 * Note that the value of the item "di_tv" still needs to be initialized!
6682 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006685dictitem_alloc(key)
6686 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687{
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006689
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006690 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006691 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006692 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006693 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006694 di->di_flags = 0;
6695 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006696 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006697}
6698
6699/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006700 * Make a copy of a Dictionary item.
6701 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006703dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006704 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006705{
Bram Moolenaar33570922005-01-25 22:26:29 +00006706 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006707
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006708 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6709 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 if (di != NULL)
6711 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006712 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006713 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006714 copy_tv(&org->di_tv, &di->di_tv);
6715 }
6716 return di;
6717}
6718
6719/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006720 * Remove item "item" from Dictionary "dict" and free it.
6721 */
6722 static void
6723dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006724 dict_T *dict;
6725 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006726{
Bram Moolenaar33570922005-01-25 22:26:29 +00006727 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006728
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006730 if (HASHITEM_EMPTY(hi))
6731 EMSG2(_(e_intern2), "dictitem_remove()");
6732 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006733 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006734 dictitem_free(item);
6735}
6736
6737/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738 * Free a dict item. Also clears the value.
6739 */
6740 static void
6741dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006742 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006743{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006744 clear_tv(&item->di_tv);
6745 vim_free(item);
6746}
6747
6748/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006749 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6750 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006751 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006752 * Returns NULL when out of memory.
6753 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006754 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006755dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006757 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006758 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759{
Bram Moolenaar33570922005-01-25 22:26:29 +00006760 dict_T *copy;
6761 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006762 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006763 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006764
6765 if (orig == NULL)
6766 return NULL;
6767
6768 copy = dict_alloc();
6769 if (copy != NULL)
6770 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006771 if (copyID != 0)
6772 {
6773 orig->dv_copyID = copyID;
6774 orig->dv_copydict = copy;
6775 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006776 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006777 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006778 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006779 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006780 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006781 --todo;
6782
6783 di = dictitem_alloc(hi->hi_key);
6784 if (di == NULL)
6785 break;
6786 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006787 {
6788 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6789 copyID) == FAIL)
6790 {
6791 vim_free(di);
6792 break;
6793 }
6794 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006795 else
6796 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6797 if (dict_add(copy, di) == FAIL)
6798 {
6799 dictitem_free(di);
6800 break;
6801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006803 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006804
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006806 if (todo > 0)
6807 {
6808 dict_unref(copy);
6809 copy = NULL;
6810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006811 }
6812
6813 return copy;
6814}
6815
6816/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006818 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006819 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006820 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006821dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 dict_T *d;
6823 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824{
Bram Moolenaar33570922005-01-25 22:26:29 +00006825 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006826}
6827
Bram Moolenaar8c711452005-01-14 21:53:12 +00006828/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006829 * Add a number or string entry to dictionary "d".
6830 * When "str" is NULL use number "nr", otherwise use "str".
6831 * Returns FAIL when out of memory and when key already exists.
6832 */
6833 int
6834dict_add_nr_str(d, key, nr, str)
6835 dict_T *d;
6836 char *key;
6837 long nr;
6838 char_u *str;
6839{
6840 dictitem_T *item;
6841
6842 item = dictitem_alloc((char_u *)key);
6843 if (item == NULL)
6844 return FAIL;
6845 item->di_tv.v_lock = 0;
6846 if (str == NULL)
6847 {
6848 item->di_tv.v_type = VAR_NUMBER;
6849 item->di_tv.vval.v_number = nr;
6850 }
6851 else
6852 {
6853 item->di_tv.v_type = VAR_STRING;
6854 item->di_tv.vval.v_string = vim_strsave(str);
6855 }
6856 if (dict_add(d, item) == FAIL)
6857 {
6858 dictitem_free(item);
6859 return FAIL;
6860 }
6861 return OK;
6862}
6863
6864/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006865 * Get the number of items in a Dictionary.
6866 */
6867 static long
6868dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006869 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871 if (d == NULL)
6872 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006873 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006874}
6875
6876/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006877 * Find item "key[len]" in Dictionary "d".
6878 * If "len" is negative use strlen(key).
6879 * Returns NULL when not found.
6880 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006881 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006883 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006884 char_u *key;
6885 int len;
6886{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006887#define AKEYLEN 200
6888 char_u buf[AKEYLEN];
6889 char_u *akey;
6890 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006891 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006892
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893 if (len < 0)
6894 akey = key;
6895 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006897 tofree = akey = vim_strnsave(key, len);
6898 if (akey == NULL)
6899 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006900 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006901 else
6902 {
6903 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006904 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 akey = buf;
6906 }
6907
Bram Moolenaar33570922005-01-25 22:26:29 +00006908 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006909 vim_free(tofree);
6910 if (HASHITEM_EMPTY(hi))
6911 return NULL;
6912 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006913}
6914
6915/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006916 * Get a string item from a dictionary.
6917 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006918 * Returns NULL if the entry doesn't exist or out of memory.
6919 */
6920 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006921get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006922 dict_T *d;
6923 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006924 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006925{
6926 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006927 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006928
6929 di = dict_find(d, key, -1);
6930 if (di == NULL)
6931 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006932 s = get_tv_string(&di->di_tv);
6933 if (save && s != NULL)
6934 s = vim_strsave(s);
6935 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006936}
6937
6938/*
6939 * Get a number item from a dictionary.
6940 * Returns 0 if the entry doesn't exist or out of memory.
6941 */
6942 long
6943get_dict_number(d, key)
6944 dict_T *d;
6945 char_u *key;
6946{
6947 dictitem_T *di;
6948
6949 di = dict_find(d, key, -1);
6950 if (di == NULL)
6951 return 0;
6952 return get_tv_number(&di->di_tv);
6953}
6954
6955/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006956 * Return an allocated string with the string representation of a Dictionary.
6957 * May return NULL.
6958 */
6959 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006960dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006962 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006963{
6964 garray_T ga;
6965 int first = TRUE;
6966 char_u *tofree;
6967 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006973 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974 return NULL;
6975 ga_init2(&ga, (int)sizeof(char), 80);
6976 ga_append(&ga, '{');
6977
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006978 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006979 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006981 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983 --todo;
6984
6985 if (first)
6986 first = FALSE;
6987 else
6988 ga_concat(&ga, (char_u *)", ");
6989
6990 tofree = string_quote(hi->hi_key, FALSE);
6991 if (tofree != NULL)
6992 {
6993 ga_concat(&ga, tofree);
6994 vim_free(tofree);
6995 }
6996 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006997 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006998 if (s != NULL)
6999 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007000 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007001 if (s == NULL)
7002 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007005 if (todo > 0)
7006 {
7007 vim_free(ga.ga_data);
7008 return NULL;
7009 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010
7011 ga_append(&ga, '}');
7012 ga_append(&ga, NUL);
7013 return (char_u *)ga.ga_data;
7014}
7015
7016/*
7017 * Allocate a variable for a Dictionary and fill it from "*arg".
7018 * Return OK or FAIL. Returns NOTDONE for {expr}.
7019 */
7020 static int
7021get_dict_tv(arg, rettv, evaluate)
7022 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007023 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024 int evaluate;
7025{
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 dict_T *d = NULL;
7027 typval_T tvkey;
7028 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007029 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007032 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033
7034 /*
7035 * First check if it's not a curly-braces thing: {expr}.
7036 * Must do this without evaluating, otherwise a function may be called
7037 * twice. Unfortunately this means we need to call eval1() twice for the
7038 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007041 if (*start != '}')
7042 {
7043 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7044 return FAIL;
7045 if (*start == '}')
7046 return NOTDONE;
7047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048
7049 if (evaluate)
7050 {
7051 d = dict_alloc();
7052 if (d == NULL)
7053 return FAIL;
7054 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007055 tvkey.v_type = VAR_UNKNOWN;
7056 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057
7058 *arg = skipwhite(*arg + 1);
7059 while (**arg != '}' && **arg != NUL)
7060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062 goto failret;
7063 if (**arg != ':')
7064 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007065 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007067 goto failret;
7068 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007069 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007071 key = get_tv_string_buf_chk(&tvkey, buf);
7072 if (key == NULL || *key == NUL)
7073 {
7074 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7075 if (key != NULL)
7076 EMSG(_(e_emptykey));
7077 clear_tv(&tvkey);
7078 goto failret;
7079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081
7082 *arg = skipwhite(*arg + 1);
7083 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7084 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007085 if (evaluate)
7086 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087 goto failret;
7088 }
7089 if (evaluate)
7090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 if (item != NULL)
7093 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007094 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007095 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 clear_tv(&tv);
7097 goto failret;
7098 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 item = dictitem_alloc(key);
7100 clear_tv(&tvkey);
7101 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007104 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 if (dict_add(d, item) == FAIL)
7106 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107 }
7108 }
7109
7110 if (**arg == '}')
7111 break;
7112 if (**arg != ',')
7113 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007114 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115 goto failret;
7116 }
7117 *arg = skipwhite(*arg + 1);
7118 }
7119
7120 if (**arg != '}')
7121 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007122 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007123failret:
7124 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007125 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007126 return FAIL;
7127 }
7128
7129 *arg = skipwhite(*arg + 1);
7130 if (evaluate)
7131 {
7132 rettv->v_type = VAR_DICT;
7133 rettv->vval.v_dict = d;
7134 ++d->dv_refcount;
7135 }
7136
7137 return OK;
7138}
7139
Bram Moolenaar8c711452005-01-14 21:53:12 +00007140/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007141 * Return a string with the string representation of a variable.
7142 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007143 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007144 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007145 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007146 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007147 */
7148 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007149echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007151 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007152 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007153 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007154{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 static int recurse = 0;
7156 char_u *r = NULL;
7157
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007160 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007161 *tofree = NULL;
7162 return NULL;
7163 }
7164 ++recurse;
7165
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007166 switch (tv->v_type)
7167 {
7168 case VAR_FUNC:
7169 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 r = tv->vval.v_string;
7171 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007172
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007173 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007174 if (tv->vval.v_list == NULL)
7175 {
7176 *tofree = NULL;
7177 r = NULL;
7178 }
7179 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7180 {
7181 *tofree = NULL;
7182 r = (char_u *)"[...]";
7183 }
7184 else
7185 {
7186 tv->vval.v_list->lv_copyID = copyID;
7187 *tofree = list2string(tv, copyID);
7188 r = *tofree;
7189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007191
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007193 if (tv->vval.v_dict == NULL)
7194 {
7195 *tofree = NULL;
7196 r = NULL;
7197 }
7198 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7199 {
7200 *tofree = NULL;
7201 r = (char_u *)"{...}";
7202 }
7203 else
7204 {
7205 tv->vval.v_dict->dv_copyID = copyID;
7206 *tofree = dict2string(tv, copyID);
7207 r = *tofree;
7208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007211 case VAR_STRING:
7212 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 *tofree = NULL;
7214 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007216
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007217#ifdef FEAT_FLOAT
7218 case VAR_FLOAT:
7219 *tofree = NULL;
7220 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7221 r = numbuf;
7222 break;
7223#endif
7224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007225 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007226 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007229
7230 --recurse;
7231 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007232}
7233
7234/*
7235 * Return a string with the string representation of a variable.
7236 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7237 * "numbuf" is used for a number.
7238 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007239 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007240 */
7241 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007242tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007244 char_u **tofree;
7245 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007246 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007247{
7248 switch (tv->v_type)
7249 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007250 case VAR_FUNC:
7251 *tofree = string_quote(tv->vval.v_string, TRUE);
7252 return *tofree;
7253 case VAR_STRING:
7254 *tofree = string_quote(tv->vval.v_string, FALSE);
7255 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007256#ifdef FEAT_FLOAT
7257 case VAR_FLOAT:
7258 *tofree = NULL;
7259 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7260 return numbuf;
7261#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007262 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007263 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007265 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007266 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007268 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007269 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007270}
7271
7272/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 * Return string "str" in ' quotes, doubling ' characters.
7274 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007276 */
7277 static char_u *
7278string_quote(str, function)
7279 char_u *str;
7280 int function;
7281{
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007283 char_u *p, *r, *s;
7284
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 len = (function ? 13 : 3);
7286 if (str != NULL)
7287 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007288 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 for (p = str; *p != NUL; mb_ptr_adv(p))
7290 if (*p == '\'')
7291 ++len;
7292 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007293 s = r = alloc(len);
7294 if (r != NULL)
7295 {
7296 if (function)
7297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007299 r += 10;
7300 }
7301 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 if (str != NULL)
7304 for (p = str; *p != NUL; )
7305 {
7306 if (*p == '\'')
7307 *r++ = '\'';
7308 MB_COPY_CHAR(p, r);
7309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007311 if (function)
7312 *r++ = ')';
7313 *r++ = NUL;
7314 }
7315 return s;
7316}
7317
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007318#ifdef FEAT_FLOAT
7319/*
7320 * Convert the string "text" to a floating point number.
7321 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7322 * this always uses a decimal point.
7323 * Returns the length of the text that was consumed.
7324 */
7325 static int
7326string2float(text, value)
7327 char_u *text;
7328 float_T *value; /* result stored here */
7329{
7330 char *s = (char *)text;
7331 float_T f;
7332
7333 f = strtod(s, &s);
7334 *value = f;
7335 return (int)((char_u *)s - text);
7336}
7337#endif
7338
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 * Get the value of an environment variable.
7341 * "arg" is pointing to the '$'. It is advanced to after the name.
7342 * If the environment variable was not set, silently assume it is empty.
7343 * Always return OK.
7344 */
7345 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 int evaluate;
7350{
7351 char_u *string = NULL;
7352 int len;
7353 int cc;
7354 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007355 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
7357 ++*arg;
7358 name = *arg;
7359 len = get_env_len(arg);
7360 if (evaluate)
7361 {
7362 if (len != 0)
7363 {
7364 cc = name[len];
7365 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007366 /* first try vim_getenv(), fast for normal environment vars */
7367 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007369 {
7370 if (!mustfree)
7371 string = vim_strsave(string);
7372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 else
7374 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007375 if (mustfree)
7376 vim_free(string);
7377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 /* next try expanding things like $VIM and ${HOME} */
7379 string = expand_env_save(name - 1);
7380 if (string != NULL && *string == '$')
7381 {
7382 vim_free(string);
7383 string = NULL;
7384 }
7385 }
7386 name[len] = cc;
7387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007388 rettv->v_type = VAR_STRING;
7389 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
7391
7392 return OK;
7393}
7394
7395/*
7396 * Array with names and number of arguments of all internal functions
7397 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7398 */
7399static struct fst
7400{
7401 char *f_name; /* function name */
7402 char f_min_argc; /* minimal number of arguments */
7403 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007405 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406} functions[] =
7407{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007408#ifdef FEAT_FLOAT
7409 {"abs", 1, 1, f_abs},
7410#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007411 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 {"append", 2, 2, f_append},
7413 {"argc", 0, 0, f_argc},
7414 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007415 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007416#ifdef FEAT_FLOAT
7417 {"atan", 1, 1, f_atan},
7418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007420 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 {"bufexists", 1, 1, f_bufexists},
7422 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7423 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7424 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7425 {"buflisted", 1, 1, f_buflisted},
7426 {"bufloaded", 1, 1, f_bufloaded},
7427 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007428 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {"bufwinnr", 1, 1, f_bufwinnr},
7430 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007431 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007433#ifdef FEAT_FLOAT
7434 {"ceil", 1, 1, f_ceil},
7435#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007436 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {"char2nr", 1, 1, f_char2nr},
7438 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007439 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007441#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007442 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007443 {"complete_add", 1, 1, f_complete_add},
7444 {"complete_check", 0, 0, f_complete_check},
7445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007447 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007448#ifdef FEAT_FLOAT
7449 {"cos", 1, 1, f_cos},
7450#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007451 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007453 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007454 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 {"delete", 1, 1, f_delete},
7456 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007457 {"diff_filler", 1, 1, f_diff_filler},
7458 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007459 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007461 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 {"eventhandler", 0, 0, f_eventhandler},
7463 {"executable", 1, 1, f_executable},
7464 {"exists", 1, 1, f_exists},
7465 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007466 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007467 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7469 {"filereadable", 1, 1, f_filereadable},
7470 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007471 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007472 {"finddir", 1, 3, f_finddir},
7473 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007474#ifdef FEAT_FLOAT
7475 {"float2nr", 1, 1, f_float2nr},
7476 {"floor", 1, 1, f_floor},
7477#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007478 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {"fnamemodify", 2, 2, f_fnamemodify},
7480 {"foldclosed", 1, 1, f_foldclosed},
7481 {"foldclosedend", 1, 1, f_foldclosedend},
7482 {"foldlevel", 1, 1, f_foldlevel},
7483 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007484 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007486 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007487 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007488 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007489 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 {"getbufvar", 2, 2, f_getbufvar},
7491 {"getchar", 0, 1, f_getchar},
7492 {"getcharmod", 0, 0, f_getcharmod},
7493 {"getcmdline", 0, 0, f_getcmdline},
7494 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007495 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007497 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007498 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 {"getfsize", 1, 1, f_getfsize},
7500 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007501 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007502 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007503 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007504 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007505 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007506 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007507 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007508 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007510 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 {"getwinposx", 0, 0, f_getwinposx},
7512 {"getwinposy", 0, 0, f_getwinposy},
7513 {"getwinvar", 2, 2, f_getwinvar},
7514 {"glob", 1, 1, f_glob},
7515 {"globpath", 2, 2, f_globpath},
7516 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007517 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007518 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007519 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7521 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7522 {"histadd", 2, 2, f_histadd},
7523 {"histdel", 1, 2, f_histdel},
7524 {"histget", 1, 2, f_histget},
7525 {"histnr", 1, 1, f_histnr},
7526 {"hlID", 1, 1, f_hlID},
7527 {"hlexists", 1, 1, f_hlexists},
7528 {"hostname", 0, 0, f_hostname},
7529 {"iconv", 3, 3, f_iconv},
7530 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007531 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007532 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007534 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {"inputrestore", 0, 0, f_inputrestore},
7536 {"inputsave", 0, 0, f_inputsave},
7537 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007538 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007540 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007542 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007545 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {"libcall", 3, 3, f_libcall},
7547 {"libcallnr", 3, 3, f_libcallnr},
7548 {"line", 1, 1, f_line},
7549 {"line2byte", 1, 1, f_line2byte},
7550 {"lispindent", 1, 1, f_lispindent},
7551 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007552#ifdef FEAT_FLOAT
7553 {"log10", 1, 1, f_log10},
7554#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007555 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007556 {"maparg", 1, 3, f_maparg},
7557 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007558 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007559 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007560 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007561 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007562 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007563 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007564 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007565 {"max", 1, 1, f_max},
7566 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007567#ifdef vim_mkdir
7568 {"mkdir", 1, 3, f_mkdir},
7569#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007570 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 {"nextnonblank", 1, 1, f_nextnonblank},
7572 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007573 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007574#ifdef FEAT_FLOAT
7575 {"pow", 2, 2, f_pow},
7576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007578 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007579 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007581 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007582 {"reltime", 0, 2, f_reltime},
7583 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {"remote_expr", 2, 3, f_remote_expr},
7585 {"remote_foreground", 1, 1, f_remote_foreground},
7586 {"remote_peek", 1, 2, f_remote_peek},
7587 {"remote_read", 1, 1, f_remote_read},
7588 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007589 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007591 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007593 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007594#ifdef FEAT_FLOAT
7595 {"round", 1, 1, f_round},
7596#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007597 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007598 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007599 {"searchpair", 3, 7, f_searchpair},
7600 {"searchpairpos", 3, 7, f_searchpairpos},
7601 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 {"server2client", 2, 2, f_server2client},
7603 {"serverlist", 0, 0, f_serverlist},
7604 {"setbufvar", 3, 3, f_setbufvar},
7605 {"setcmdpos", 1, 1, f_setcmdpos},
7606 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007607 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007608 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007609 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007610 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007612 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007614 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007616#ifdef FEAT_FLOAT
7617 {"sin", 1, 1, f_sin},
7618#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007619 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007620 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007621 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007622 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007623 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007624#ifdef FEAT_FLOAT
7625 {"sqrt", 1, 1, f_sqrt},
7626 {"str2float", 1, 1, f_str2float},
7627#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007628 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629#ifdef HAVE_STRFTIME
7630 {"strftime", 1, 2, f_strftime},
7631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {"strlen", 1, 1, f_strlen},
7635 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007636 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {"strtrans", 1, 1, f_strtrans},
7638 {"submatch", 1, 1, f_submatch},
7639 {"substitute", 4, 4, f_substitute},
7640 {"synID", 3, 3, f_synID},
7641 {"synIDattr", 2, 3, f_synIDattr},
7642 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007643 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007644 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007645 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007646 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007647 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007648 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007649 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007651 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"tolower", 1, 1, f_tolower},
7653 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007654 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007655#ifdef FEAT_FLOAT
7656 {"trunc", 1, 1, f_trunc},
7657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {"virtcol", 1, 1, f_virtcol},
7661 {"visualmode", 0, 1, f_visualmode},
7662 {"winbufnr", 1, 1, f_winbufnr},
7663 {"wincol", 0, 0, f_wincol},
7664 {"winheight", 1, 1, f_winheight},
7665 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007666 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007668 {"winrestview", 1, 1, f_winrestview},
7669 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007671 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672};
7673
7674#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7675
7676/*
7677 * Function given to ExpandGeneric() to obtain the list of internal
7678 * or user defined function names.
7679 */
7680 char_u *
7681get_function_name(xp, idx)
7682 expand_T *xp;
7683 int idx;
7684{
7685 static int intidx = -1;
7686 char_u *name;
7687
7688 if (idx == 0)
7689 intidx = -1;
7690 if (intidx < 0)
7691 {
7692 name = get_user_func_name(xp, idx);
7693 if (name != NULL)
7694 return name;
7695 }
7696 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7697 {
7698 STRCPY(IObuff, functions[intidx].f_name);
7699 STRCAT(IObuff, "(");
7700 if (functions[intidx].f_max_argc == 0)
7701 STRCAT(IObuff, ")");
7702 return IObuff;
7703 }
7704
7705 return NULL;
7706}
7707
7708/*
7709 * Function given to ExpandGeneric() to obtain the list of internal or
7710 * user defined variable or function names.
7711 */
7712/*ARGSUSED*/
7713 char_u *
7714get_expr_name(xp, idx)
7715 expand_T *xp;
7716 int idx;
7717{
7718 static int intidx = -1;
7719 char_u *name;
7720
7721 if (idx == 0)
7722 intidx = -1;
7723 if (intidx < 0)
7724 {
7725 name = get_function_name(xp, idx);
7726 if (name != NULL)
7727 return name;
7728 }
7729 return get_user_var_name(xp, ++intidx);
7730}
7731
7732#endif /* FEAT_CMDL_COMPL */
7733
7734/*
7735 * Find internal function in table above.
7736 * Return index, or -1 if not found
7737 */
7738 static int
7739find_internal_func(name)
7740 char_u *name; /* name of the function */
7741{
7742 int first = 0;
7743 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7744 int cmp;
7745 int x;
7746
7747 /*
7748 * Find the function name in the table. Binary search.
7749 */
7750 while (first <= last)
7751 {
7752 x = first + ((unsigned)(last - first) >> 1);
7753 cmp = STRCMP(name, functions[x].f_name);
7754 if (cmp < 0)
7755 last = x - 1;
7756 else if (cmp > 0)
7757 first = x + 1;
7758 else
7759 return x;
7760 }
7761 return -1;
7762}
7763
7764/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007765 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7766 * name it contains, otherwise return "name".
7767 */
7768 static char_u *
7769deref_func_name(name, lenp)
7770 char_u *name;
7771 int *lenp;
7772{
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007774 int cc;
7775
7776 cc = name[*lenp];
7777 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007778 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007783 {
7784 *lenp = 0;
7785 return (char_u *)""; /* just in case */
7786 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007787 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 }
7790
7791 return name;
7792}
7793
7794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 * Allocate a variable for the result of a function.
7796 * Return OK or FAIL.
7797 */
7798 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007799get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7800 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 char_u *name; /* name of the function */
7802 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 char_u **arg; /* argument, pointing to the '(' */
7805 linenr_T firstline; /* first line of range */
7806 linenr_T lastline; /* last line of range */
7807 int *doesrange; /* return: function handled range */
7808 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 char_u *argp;
7812 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007813 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 int argcount = 0; /* number of arguments found */
7815
7816 /*
7817 * Get the arguments.
7818 */
7819 argp = *arg;
7820 while (argcount < MAX_FUNC_ARGS)
7821 {
7822 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7823 if (*argp == ')' || *argp == ',' || *argp == NUL)
7824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7826 {
7827 ret = FAIL;
7828 break;
7829 }
7830 ++argcount;
7831 if (*argp != ',')
7832 break;
7833 }
7834 if (*argp == ')')
7835 ++argp;
7836 else
7837 ret = FAIL;
7838
7839 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 {
7844 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007845 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007846 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007847 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849
7850 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
7853 *arg = skipwhite(argp);
7854 return ret;
7855}
7856
7857
7858/*
7859 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007860 * Return OK when the function can't be called, FAIL otherwise.
7861 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 */
7863 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007865 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 char_u *name; /* name of the function */
7867 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007870 typval_T *argvars; /* vars for arguments, must have "argcount"
7871 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 linenr_T firstline; /* first line of range */
7873 linenr_T lastline; /* last line of range */
7874 int *doesrange; /* return: function handled range */
7875 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007876 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879#define ERROR_UNKNOWN 0
7880#define ERROR_TOOMANY 1
7881#define ERROR_TOOFEW 2
7882#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883#define ERROR_DICT 4
7884#define ERROR_NONE 5
7885#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 int error = ERROR_NONE;
7887 int i;
7888 int llen;
7889 ufunc_T *fp;
7890 int cc;
7891#define FLEN_FIXED 40
7892 char_u fname_buf[FLEN_FIXED + 1];
7893 char_u *fname;
7894
7895 /*
7896 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7897 * Change <SNR>123_name() to K_SNR 123_name().
7898 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7899 */
7900 cc = name[len];
7901 name[len] = NUL;
7902 llen = eval_fname_script(name);
7903 if (llen > 0)
7904 {
7905 fname_buf[0] = K_SPECIAL;
7906 fname_buf[1] = KS_EXTRA;
7907 fname_buf[2] = (int)KE_SNR;
7908 i = 3;
7909 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7910 {
7911 if (current_SID <= 0)
7912 error = ERROR_SCRIPT;
7913 else
7914 {
7915 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7916 i = (int)STRLEN(fname_buf);
7917 }
7918 }
7919 if (i + STRLEN(name + llen) < FLEN_FIXED)
7920 {
7921 STRCPY(fname_buf + i, name + llen);
7922 fname = fname_buf;
7923 }
7924 else
7925 {
7926 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7927 if (fname == NULL)
7928 error = ERROR_OTHER;
7929 else
7930 {
7931 mch_memmove(fname, fname_buf, (size_t)i);
7932 STRCPY(fname + i, name + llen);
7933 }
7934 }
7935 }
7936 else
7937 fname = name;
7938
7939 *doesrange = FALSE;
7940
7941
7942 /* execute the function if no errors detected and executing */
7943 if (evaluate && error == ERROR_NONE)
7944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007945 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 error = ERROR_UNKNOWN;
7947
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007948 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {
7950 /*
7951 * User defined function.
7952 */
7953 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007954
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007956 /* Trigger FuncUndefined event, may load the function. */
7957 if (fp == NULL
7958 && apply_autocmds(EVENT_FUNCUNDEFINED,
7959 fname, fname, TRUE, NULL)
7960 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007962 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 fp = find_func(fname);
7964 }
7965#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007966 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007967 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007968 {
7969 /* loaded a package, search for the function again */
7970 fp = find_func(fname);
7971 }
7972
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 if (fp != NULL)
7974 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007975 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007977 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007979 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007981 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007982 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 else
7984 {
7985 /*
7986 * Call the user function.
7987 * Save and restore search patterns, script variables and
7988 * redo buffer.
7989 */
7990 save_search_patterns();
7991 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007992 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007994 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007995 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7996 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7997 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007998 /* Function was unreferenced while being used, free it
7999 * now. */
8000 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 restoreRedobuff();
8002 restore_search_patterns();
8003 error = ERROR_NONE;
8004 }
8005 }
8006 }
8007 else
8008 {
8009 /*
8010 * Find the function name in the table, call its implementation.
8011 */
8012 i = find_internal_func(fname);
8013 if (i >= 0)
8014 {
8015 if (argcount < functions[i].f_min_argc)
8016 error = ERROR_TOOFEW;
8017 else if (argcount > functions[i].f_max_argc)
8018 error = ERROR_TOOMANY;
8019 else
8020 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008021 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 error = ERROR_NONE;
8024 }
8025 }
8026 }
8027 /*
8028 * The function call (or "FuncUndefined" autocommand sequence) might
8029 * have been aborted by an error, an interrupt, or an explicitly thrown
8030 * exception that has not been caught so far. This situation can be
8031 * tested for by calling aborting(). For an error in an internal
8032 * function or for the "E132" error in call_user_func(), however, the
8033 * throw point at which the "force_abort" flag (temporarily reset by
8034 * emsg()) is normally updated has not been reached yet. We need to
8035 * update that flag first to make aborting() reliable.
8036 */
8037 update_force_abort();
8038 }
8039 if (error == ERROR_NONE)
8040 ret = OK;
8041
8042 /*
8043 * Report an error unless the argument evaluation or function call has been
8044 * cancelled due to an aborting error, an interrupt, or an exception.
8045 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008046 if (!aborting())
8047 {
8048 switch (error)
8049 {
8050 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008051 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008052 break;
8053 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008054 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008055 break;
8056 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008057 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008058 name);
8059 break;
8060 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008061 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008062 name);
8063 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008065 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008066 name);
8067 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 }
8069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070
8071 name[len] = cc;
8072 if (fname != name && fname != fname_buf)
8073 vim_free(fname);
8074
8075 return ret;
8076}
8077
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008078/*
8079 * Give an error message with a function name. Handle <SNR> things.
8080 */
8081 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008082emsg_funcname(ermsg, name)
8083 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008084 char_u *name;
8085{
8086 char_u *p;
8087
8088 if (*name == K_SPECIAL)
8089 p = concat_str((char_u *)"<SNR>", name + 3);
8090 else
8091 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008092 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008093 if (p != name)
8094 vim_free(p);
8095}
8096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097/*********************************************
8098 * Implementation of the built-in functions
8099 */
8100
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102/*
8103 * "abs(expr)" function
8104 */
8105 static void
8106f_abs(argvars, rettv)
8107 typval_T *argvars;
8108 typval_T *rettv;
8109{
8110 if (argvars[0].v_type == VAR_FLOAT)
8111 {
8112 rettv->v_type = VAR_FLOAT;
8113 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8114 }
8115 else
8116 {
8117 varnumber_T n;
8118 int error = FALSE;
8119
8120 n = get_tv_number_chk(&argvars[0], &error);
8121 if (error)
8122 rettv->vval.v_number = -1;
8123 else if (n > 0)
8124 rettv->vval.v_number = n;
8125 else
8126 rettv->vval.v_number = -n;
8127 }
8128}
8129#endif
8130
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008132 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 */
8134 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008135f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 typval_T *argvars;
8137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
Bram Moolenaar33570922005-01-25 22:26:29 +00008139 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008144 if ((l = argvars[0].vval.v_list) != NULL
8145 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8146 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008147 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 }
8149 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008150 EMSG(_(e_listreq));
8151}
8152
8153/*
8154 * "append(lnum, string/list)" function
8155 */
8156 static void
8157f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 typval_T *argvars;
8159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008160{
8161 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008162 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 list_T *l = NULL;
8164 listitem_T *li = NULL;
8165 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008166 long added = 0;
8167
Bram Moolenaar0d660222005-01-07 21:51:51 +00008168 lnum = get_tv_lnum(argvars);
8169 if (lnum >= 0
8170 && lnum <= curbuf->b_ml.ml_line_count
8171 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008172 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008173 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008174 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008175 l = argvars[1].vval.v_list;
8176 if (l == NULL)
8177 return;
8178 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008180 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008181 for (;;)
8182 {
8183 if (l == NULL)
8184 tv = &argvars[1]; /* append a string */
8185 else if (li == NULL)
8186 break; /* end of list */
8187 else
8188 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008189 line = get_tv_string_chk(tv);
8190 if (line == NULL) /* type error */
8191 {
8192 rettv->vval.v_number = 1; /* Failed */
8193 break;
8194 }
8195 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008196 ++added;
8197 if (l == NULL)
8198 break;
8199 li = li->li_next;
8200 }
8201
8202 appended_lines_mark(lnum, added);
8203 if (curwin->w_cursor.lnum > lnum)
8204 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008206 else
8207 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208}
8209
8210/*
8211 * "argc()" function
8212 */
8213/* ARGSUSED */
8214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008216 typval_T *argvars;
8217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220}
8221
8222/*
8223 * "argidx()" function
8224 */
8225/* ARGSUSED */
8226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 typval_T *argvars;
8229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008231 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232}
8233
8234/*
8235 * "argv(nr)" function
8236 */
8237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 typval_T *argvars;
8240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 int idx;
8243
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008244 if (argvars[0].v_type != VAR_UNKNOWN)
8245 {
8246 idx = get_tv_number_chk(&argvars[0], NULL);
8247 if (idx >= 0 && idx < ARGCOUNT)
8248 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8249 else
8250 rettv->vval.v_string = NULL;
8251 rettv->v_type = VAR_STRING;
8252 }
8253 else if (rettv_list_alloc(rettv) == OK)
8254 for (idx = 0; idx < ARGCOUNT; ++idx)
8255 list_append_string(rettv->vval.v_list,
8256 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257}
8258
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008259#ifdef FEAT_FLOAT
8260static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8261
8262/*
8263 * Get the float value of "argvars[0]" into "f".
8264 * Returns FAIL when the argument is not a Number or Float.
8265 */
8266 static int
8267get_float_arg(argvars, f)
8268 typval_T *argvars;
8269 float_T *f;
8270{
8271 if (argvars[0].v_type == VAR_FLOAT)
8272 {
8273 *f = argvars[0].vval.v_float;
8274 return OK;
8275 }
8276 if (argvars[0].v_type == VAR_NUMBER)
8277 {
8278 *f = (float_T)argvars[0].vval.v_number;
8279 return OK;
8280 }
8281 EMSG(_("E808: Number or Float required"));
8282 return FAIL;
8283}
8284
8285/*
8286 * "atan()" function
8287 */
8288 static void
8289f_atan(argvars, rettv)
8290 typval_T *argvars;
8291 typval_T *rettv;
8292{
8293 float_T f;
8294
8295 rettv->v_type = VAR_FLOAT;
8296 if (get_float_arg(argvars, &f) == OK)
8297 rettv->vval.v_float = atan(f);
8298 else
8299 rettv->vval.v_float = 0.0;
8300}
8301#endif
8302
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303/*
8304 * "browse(save, title, initdir, default)" function
8305 */
8306/* ARGSUSED */
8307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008308f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008309 typval_T *argvars;
8310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
8312#ifdef FEAT_BROWSE
8313 int save;
8314 char_u *title;
8315 char_u *initdir;
8316 char_u *defname;
8317 char_u buf[NUMBUFLEN];
8318 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008319 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008321 save = get_tv_number_chk(&argvars[0], &error);
8322 title = get_tv_string_chk(&argvars[1]);
8323 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8324 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008326 if (error || title == NULL || initdir == NULL || defname == NULL)
8327 rettv->vval.v_string = NULL;
8328 else
8329 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008330 do_browse(save ? BROWSE_SAVE : 0,
8331 title, defname, NULL, initdir, NULL, curbuf);
8332#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008334#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008335 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008336}
8337
8338/*
8339 * "browsedir(title, initdir)" function
8340 */
8341/* ARGSUSED */
8342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 typval_T *argvars;
8345 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008346{
8347#ifdef FEAT_BROWSE
8348 char_u *title;
8349 char_u *initdir;
8350 char_u buf[NUMBUFLEN];
8351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008352 title = get_tv_string_chk(&argvars[0]);
8353 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008355 if (title == NULL || initdir == NULL)
8356 rettv->vval.v_string = NULL;
8357 else
8358 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008359 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364}
8365
Bram Moolenaar33570922005-01-25 22:26:29 +00008366static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008367
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368/*
8369 * Find a buffer by number or exact name.
8370 */
8371 static buf_T *
8372find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
8375 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008377 if (avar->v_type == VAR_NUMBER)
8378 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008379 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008381 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008382 if (buf == NULL)
8383 {
8384 /* No full path name match, try a match with a URL or a "nofile"
8385 * buffer, these don't use the full path. */
8386 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8387 if (buf->b_fname != NULL
8388 && (path_with_url(buf->b_fname)
8389#ifdef FEAT_QUICKFIX
8390 || bt_nofile(buf)
8391#endif
8392 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008393 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008394 break;
8395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 }
8397 return buf;
8398}
8399
8400/*
8401 * "bufexists(expr)" function
8402 */
8403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008405 typval_T *argvars;
8406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008408 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409}
8410
8411/*
8412 * "buflisted(expr)" function
8413 */
8414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008416 typval_T *argvars;
8417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 buf_T *buf;
8420
8421 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008422 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423}
8424
8425/*
8426 * "bufloaded(expr)" function
8427 */
8428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008429f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008430 typval_T *argvars;
8431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 buf_T *buf;
8434
8435 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008436 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437}
8438
Bram Moolenaar33570922005-01-25 22:26:29 +00008439static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008440
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441/*
8442 * Get buffer by number or pattern.
8443 */
8444 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008445get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008446 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 int save_magic;
8450 char_u *save_cpo;
8451 buf_T *buf;
8452
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008453 if (tv->v_type == VAR_NUMBER)
8454 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008455 if (tv->v_type != VAR_STRING)
8456 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (name == NULL || *name == NUL)
8458 return curbuf;
8459 if (name[0] == '$' && name[1] == NUL)
8460 return lastbuf;
8461
8462 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8463 save_magic = p_magic;
8464 p_magic = TRUE;
8465 save_cpo = p_cpo;
8466 p_cpo = (char_u *)"";
8467
8468 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8469 TRUE, FALSE));
8470
8471 p_magic = save_magic;
8472 p_cpo = save_cpo;
8473
8474 /* If not found, try expanding the name, like done for bufexists(). */
8475 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477
8478 return buf;
8479}
8480
8481/*
8482 * "bufname(expr)" function
8483 */
8484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008485f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008486 typval_T *argvars;
8487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 buf_T *buf;
8490
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008491 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008493 buf = get_buf_tv(&argvars[0]);
8494 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 --emsg_off;
8500}
8501
8502/*
8503 * "bufnr(expr)" function
8504 */
8505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008506f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 typval_T *argvars;
8508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509{
8510 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008511 int error = FALSE;
8512 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008514 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008517 --emsg_off;
8518
8519 /* If the buffer isn't found and the second argument is not zero create a
8520 * new buffer. */
8521 if (buf == NULL
8522 && argvars[1].v_type != VAR_UNKNOWN
8523 && get_tv_number_chk(&argvars[1], &error) != 0
8524 && !error
8525 && (name = get_tv_string_chk(&argvars[0])) != NULL
8526 && !error)
8527 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533}
8534
8535/*
8536 * "bufwinnr(nr)" function
8537 */
8538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T *argvars;
8541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
8543#ifdef FEAT_WINDOWS
8544 win_T *wp;
8545 int winnr = 0;
8546#endif
8547 buf_T *buf;
8548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008549 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552#ifdef FEAT_WINDOWS
8553 for (wp = firstwin; wp; wp = wp->w_next)
8554 {
8555 ++winnr;
8556 if (wp->w_buffer == buf)
8557 break;
8558 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562#endif
8563 --emsg_off;
8564}
8565
8566/*
8567 * "byte2line(byte)" function
8568 */
8569/*ARGSUSED*/
8570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008571f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577#else
8578 long boff = 0;
8579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008580 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 (linenr_T)0, &boff);
8586#endif
8587}
8588
8589/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008590 * "byteidx()" function
8591 */
8592/*ARGSUSED*/
8593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *argvars;
8596 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008597{
8598#ifdef FEAT_MBYTE
8599 char_u *t;
8600#endif
8601 char_u *str;
8602 long idx;
8603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 str = get_tv_string_chk(&argvars[0]);
8605 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008607 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008608 return;
8609
8610#ifdef FEAT_MBYTE
8611 t = str;
8612 for ( ; idx > 0; idx--)
8613 {
8614 if (*t == NUL) /* EOL reached */
8615 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008616 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008617 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008618 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008619#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008620 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008622#endif
8623}
8624
8625/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008626 * "call(func, arglist)" function
8627 */
8628 static void
8629f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *argvars;
8631 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008632{
8633 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008634 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008635 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008637 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008639
8640 rettv->vval.v_number = 0;
8641 if (argvars[1].v_type != VAR_LIST)
8642 {
8643 EMSG(_(e_listreq));
8644 return;
8645 }
8646 if (argvars[1].vval.v_list == NULL)
8647 return;
8648
8649 if (argvars[0].v_type == VAR_FUNC)
8650 func = argvars[0].vval.v_string;
8651 else
8652 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008653 if (*func == NUL)
8654 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008655
Bram Moolenaare9a41262005-01-15 22:18:47 +00008656 if (argvars[2].v_type != VAR_UNKNOWN)
8657 {
8658 if (argvars[2].v_type != VAR_DICT)
8659 {
8660 EMSG(_(e_dictreq));
8661 return;
8662 }
8663 selfdict = argvars[2].vval.v_dict;
8664 }
8665
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008666 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8667 item = item->li_next)
8668 {
8669 if (argc == MAX_FUNC_ARGS)
8670 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008671 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008672 break;
8673 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008674 /* Make a copy of each argument. This is needed to be able to set
8675 * v_lock to VAR_FIXED in the copy without changing the original list.
8676 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008677 copy_tv(&item->li_tv, &argv[argc++]);
8678 }
8679
8680 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008681 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008682 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8683 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008684
8685 /* Free the arguments. */
8686 while (argc > 0)
8687 clear_tv(&argv[--argc]);
8688}
8689
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008690#ifdef FEAT_FLOAT
8691/*
8692 * "ceil({float})" function
8693 */
8694 static void
8695f_ceil(argvars, rettv)
8696 typval_T *argvars;
8697 typval_T *rettv;
8698{
8699 float_T f;
8700
8701 rettv->v_type = VAR_FLOAT;
8702 if (get_float_arg(argvars, &f) == OK)
8703 rettv->vval.v_float = ceil(f);
8704 else
8705 rettv->vval.v_float = 0.0;
8706}
8707#endif
8708
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008709/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008710 * "changenr()" function
8711 */
8712/*ARGSUSED*/
8713 static void
8714f_changenr(argvars, rettv)
8715 typval_T *argvars;
8716 typval_T *rettv;
8717{
8718 rettv->vval.v_number = curbuf->b_u_seq_cur;
8719}
8720
8721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 * "char2nr(string)" function
8723 */
8724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729#ifdef FEAT_MBYTE
8730 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008731 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 else
8733#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735}
8736
8737/*
8738 * "cindent(lnum)" function
8739 */
8740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008742 typval_T *argvars;
8743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
8745#ifdef FEAT_CINDENT
8746 pos_T pos;
8747 linenr_T lnum;
8748
8749 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8752 {
8753 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 curwin->w_cursor = pos;
8756 }
8757 else
8758#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760}
8761
8762/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008763 * "clearmatches()" function
8764 */
8765/*ARGSUSED*/
8766 static void
8767f_clearmatches(argvars, rettv)
8768 typval_T *argvars;
8769 typval_T *rettv;
8770{
8771#ifdef FEAT_SEARCH_EXTRA
8772 clear_matches(curwin);
8773#endif
8774}
8775
8776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 * "col(string)" function
8778 */
8779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *argvars;
8782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
8784 colnr_T col = 0;
8785 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008786 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008788 fp = var2fpos(&argvars[0], FALSE, &fnum);
8789 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 {
8791 if (fp->col == MAXCOL)
8792 {
8793 /* '> can be MAXCOL, get the length of the line then */
8794 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008795 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 else
8797 col = MAXCOL;
8798 }
8799 else
8800 {
8801 col = fp->col + 1;
8802#ifdef FEAT_VIRTUALEDIT
8803 /* col(".") when the cursor is on the NUL at the end of the line
8804 * because of "coladd" can be seen as an extra column. */
8805 if (virtual_active() && fp == &curwin->w_cursor)
8806 {
8807 char_u *p = ml_get_cursor();
8808
8809 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8810 curwin->w_virtcol - curwin->w_cursor.coladd))
8811 {
8812# ifdef FEAT_MBYTE
8813 int l;
8814
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008815 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 col += l;
8817# else
8818 if (*p != NUL && p[1] == NUL)
8819 ++col;
8820# endif
8821 }
8822 }
8823#endif
8824 }
8825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
8828
Bram Moolenaar572cb562005-08-05 21:35:02 +00008829#if defined(FEAT_INS_EXPAND)
8830/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008831 * "complete()" function
8832 */
8833/*ARGSUSED*/
8834 static void
8835f_complete(argvars, rettv)
8836 typval_T *argvars;
8837 typval_T *rettv;
8838{
8839 int startcol;
8840
8841 if ((State & INSERT) == 0)
8842 {
8843 EMSG(_("E785: complete() can only be used in Insert mode"));
8844 return;
8845 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008846
8847 /* Check for undo allowed here, because if something was already inserted
8848 * the line was already saved for undo and this check isn't done. */
8849 if (!undo_allowed())
8850 return;
8851
Bram Moolenaarade00832006-03-10 21:46:58 +00008852 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8853 {
8854 EMSG(_(e_invarg));
8855 return;
8856 }
8857
8858 startcol = get_tv_number_chk(&argvars[0], NULL);
8859 if (startcol <= 0)
8860 return;
8861
8862 set_completion(startcol - 1, argvars[1].vval.v_list);
8863}
8864
8865/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008866 * "complete_add()" function
8867 */
8868/*ARGSUSED*/
8869 static void
8870f_complete_add(argvars, rettv)
8871 typval_T *argvars;
8872 typval_T *rettv;
8873{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008874 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008875}
8876
8877/*
8878 * "complete_check()" function
8879 */
8880/*ARGSUSED*/
8881 static void
8882f_complete_check(argvars, rettv)
8883 typval_T *argvars;
8884 typval_T *rettv;
8885{
8886 int saved = RedrawingDisabled;
8887
8888 RedrawingDisabled = 0;
8889 ins_compl_check_keys(0);
8890 rettv->vval.v_number = compl_interrupted;
8891 RedrawingDisabled = saved;
8892}
8893#endif
8894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895/*
8896 * "confirm(message, buttons[, default [, type]])" function
8897 */
8898/*ARGSUSED*/
8899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *argvars;
8902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8905 char_u *message;
8906 char_u *buttons = NULL;
8907 char_u buf[NUMBUFLEN];
8908 char_u buf2[NUMBUFLEN];
8909 int def = 1;
8910 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 char_u *typestr;
8912 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008914 message = get_tv_string_chk(&argvars[0]);
8915 if (message == NULL)
8916 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008917 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008919 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8920 if (buttons == NULL)
8921 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008922 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008924 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008925 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008927 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8928 if (typestr == NULL)
8929 error = TRUE;
8930 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 switch (TOUPPER_ASC(*typestr))
8933 {
8934 case 'E': type = VIM_ERROR; break;
8935 case 'Q': type = VIM_QUESTION; break;
8936 case 'I': type = VIM_INFO; break;
8937 case 'W': type = VIM_WARNING; break;
8938 case 'G': type = VIM_GENERIC; break;
8939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 }
8941 }
8942 }
8943 }
8944
8945 if (buttons == NULL || *buttons == NUL)
8946 buttons = (char_u *)_("&Ok");
8947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 if (error)
8949 rettv->vval.v_number = 0;
8950 else
8951 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 def, NULL);
8953#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955#endif
8956}
8957
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008958/*
8959 * "copy()" function
8960 */
8961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *argvars;
8964 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008965{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008966 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008969#ifdef FEAT_FLOAT
8970/*
8971 * "cos()" function
8972 */
8973 static void
8974f_cos(argvars, rettv)
8975 typval_T *argvars;
8976 typval_T *rettv;
8977{
8978 float_T f;
8979
8980 rettv->v_type = VAR_FLOAT;
8981 if (get_float_arg(argvars, &f) == OK)
8982 rettv->vval.v_float = cos(f);
8983 else
8984 rettv->vval.v_float = 0.0;
8985}
8986#endif
8987
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008989 * "count()" function
8990 */
8991 static void
8992f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008993 typval_T *argvars;
8994 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008995{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008996 long n = 0;
8997 int ic = FALSE;
8998
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009000 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 listitem_T *li;
9002 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009004
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005 if ((l = argvars[0].vval.v_list) != NULL)
9006 {
9007 li = l->lv_first;
9008 if (argvars[2].v_type != VAR_UNKNOWN)
9009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 int error = FALSE;
9011
9012 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 if (argvars[3].v_type != VAR_UNKNOWN)
9014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009015 idx = get_tv_number_chk(&argvars[3], &error);
9016 if (!error)
9017 {
9018 li = list_find(l, idx);
9019 if (li == NULL)
9020 EMSGN(_(e_listidx), idx);
9021 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009022 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 if (error)
9024 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 }
9026
9027 for ( ; li != NULL; li = li->li_next)
9028 if (tv_equal(&li->li_tv, &argvars[1], ic))
9029 ++n;
9030 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009031 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 else if (argvars[0].v_type == VAR_DICT)
9033 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 int todo;
9035 dict_T *d;
9036 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009038 if ((d = argvars[0].vval.v_dict) != NULL)
9039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009040 int error = FALSE;
9041
Bram Moolenaare9a41262005-01-15 22:18:47 +00009042 if (argvars[2].v_type != VAR_UNKNOWN)
9043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 if (argvars[3].v_type != VAR_UNKNOWN)
9046 EMSG(_(e_invarg));
9047 }
9048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009049 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009050 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009051 {
9052 if (!HASHITEM_EMPTY(hi))
9053 {
9054 --todo;
9055 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9056 ++n;
9057 }
9058 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009059 }
9060 }
9061 else
9062 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009063 rettv->vval.v_number = n;
9064}
9065
9066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9068 *
9069 * Checks the existence of a cscope connection.
9070 */
9071/*ARGSUSED*/
9072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *argvars;
9075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077#ifdef FEAT_CSCOPE
9078 int num = 0;
9079 char_u *dbpath = NULL;
9080 char_u *prepend = NULL;
9081 char_u buf[NUMBUFLEN];
9082
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009083 if (argvars[0].v_type != VAR_UNKNOWN
9084 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 num = (int)get_tv_number(&argvars[0]);
9087 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009088 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095#endif
9096}
9097
9098/*
9099 * "cursor(lnum, col)" function
9100 *
9101 * Moves the cursor to the specified line and column
9102 */
9103/*ARGSUSED*/
9104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 typval_T *argvars;
9107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009110#ifdef FEAT_VIRTUALEDIT
9111 long coladd = 0;
9112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
Bram Moolenaara5525202006-03-02 22:52:09 +00009114 if (argvars[1].v_type == VAR_UNKNOWN)
9115 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009116 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009117
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009118 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009119 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009120 line = pos.lnum;
9121 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009122#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009123 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009124#endif
9125 }
9126 else
9127 {
9128 line = get_tv_lnum(argvars);
9129 col = get_tv_number_chk(&argvars[1], NULL);
9130#ifdef FEAT_VIRTUALEDIT
9131 if (argvars[2].v_type != VAR_UNKNOWN)
9132 coladd = get_tv_number_chk(&argvars[2], NULL);
9133#endif
9134 }
9135 if (line < 0 || col < 0
9136#ifdef FEAT_VIRTUALEDIT
9137 || coladd < 0
9138#endif
9139 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 if (line > 0)
9142 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 if (col > 0)
9144 curwin->w_cursor.col = col - 1;
9145#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009146 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147#endif
9148
9149 /* Make sure the cursor is in a valid position. */
9150 check_cursor();
9151#ifdef FEAT_MBYTE
9152 /* Correct cursor for multi-byte character. */
9153 if (has_mbyte)
9154 mb_adjust_cursor();
9155#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009156
9157 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158}
9159
9160/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009161 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 */
9163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 typval_T *argvars;
9166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009168 int noref = 0;
9169
9170 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009172 if (noref < 0 || noref > 1)
9173 EMSG(_(e_invarg));
9174 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009175 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176}
9177
9178/*
9179 * "delete()" function
9180 */
9181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009183 typval_T *argvars;
9184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
9193 * "did_filetype()" function
9194 */
9195/*ARGSUSED*/
9196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009198 typval_T *argvars;
9199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#endif
9206}
9207
9208/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009209 * "diff_filler()" function
9210 */
9211/*ARGSUSED*/
9212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009214 typval_T *argvars;
9215 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009216{
9217#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009219#endif
9220}
9221
9222/*
9223 * "diff_hlID()" function
9224 */
9225/*ARGSUSED*/
9226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009228 typval_T *argvars;
9229 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009230{
9231#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009233 static linenr_T prev_lnum = 0;
9234 static int changedtick = 0;
9235 static int fnum = 0;
9236 static int change_start = 0;
9237 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009238 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009239 int filler_lines;
9240 int col;
9241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 if (lnum < 0) /* ignore type error in {lnum} arg */
9243 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009244 if (lnum != prev_lnum
9245 || changedtick != curbuf->b_changedtick
9246 || fnum != curbuf->b_fnum)
9247 {
9248 /* New line, buffer, change: need to get the values. */
9249 filler_lines = diff_check(curwin, lnum);
9250 if (filler_lines < 0)
9251 {
9252 if (filler_lines == -1)
9253 {
9254 change_start = MAXCOL;
9255 change_end = -1;
9256 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9257 hlID = HLF_ADD; /* added line */
9258 else
9259 hlID = HLF_CHD; /* changed line */
9260 }
9261 else
9262 hlID = HLF_ADD; /* added line */
9263 }
9264 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009265 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009266 prev_lnum = lnum;
9267 changedtick = curbuf->b_changedtick;
9268 fnum = curbuf->b_fnum;
9269 }
9270
9271 if (hlID == HLF_CHD || hlID == HLF_TXD)
9272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009274 if (col >= change_start && col <= change_end)
9275 hlID = HLF_TXD; /* changed text */
9276 else
9277 hlID = HLF_CHD; /* changed line */
9278 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009279 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009280#endif
9281}
9282
9283/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009284 * "empty({expr})" function
9285 */
9286 static void
9287f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009288 typval_T *argvars;
9289 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009290{
9291 int n;
9292
9293 switch (argvars[0].v_type)
9294 {
9295 case VAR_STRING:
9296 case VAR_FUNC:
9297 n = argvars[0].vval.v_string == NULL
9298 || *argvars[0].vval.v_string == NUL;
9299 break;
9300 case VAR_NUMBER:
9301 n = argvars[0].vval.v_number == 0;
9302 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009303#ifdef FEAT_FLOAT
9304 case VAR_FLOAT:
9305 n = argvars[0].vval.v_float == 0.0;
9306 break;
9307#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009308 case VAR_LIST:
9309 n = argvars[0].vval.v_list == NULL
9310 || argvars[0].vval.v_list->lv_first == NULL;
9311 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009312 case VAR_DICT:
9313 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009316 default:
9317 EMSG2(_(e_intern2), "f_empty()");
9318 n = 0;
9319 }
9320
9321 rettv->vval.v_number = n;
9322}
9323
9324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 * "escape({string}, {chars})" function
9326 */
9327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 typval_T *argvars;
9330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332 char_u buf[NUMBUFLEN];
9333
Bram Moolenaar758711c2005-02-02 23:11:38 +00009334 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9335 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337}
9338
9339/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009340 * "eval()" function
9341 */
9342/*ARGSUSED*/
9343 static void
9344f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009345 typval_T *argvars;
9346 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009347{
9348 char_u *s;
9349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009350 s = get_tv_string_chk(&argvars[0]);
9351 if (s != NULL)
9352 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9355 {
9356 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009357 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009359 else if (*s != NUL)
9360 EMSG(_(e_trailing));
9361}
9362
9363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 * "eventhandler()" function
9365 */
9366/*ARGSUSED*/
9367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373}
9374
9375/*
9376 * "executable()" function
9377 */
9378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 typval_T *argvars;
9381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009383 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384}
9385
9386/*
9387 * "exists()" function
9388 */
9389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *argvars;
9392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 char_u *p;
9395 char_u *name;
9396 int n = FALSE;
9397 int len = 0;
9398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 if (*p == '$') /* environment variable */
9401 {
9402 /* first try "normal" environment variables (fast) */
9403 if (mch_getenv(p + 1) != NULL)
9404 n = TRUE;
9405 else
9406 {
9407 /* try expanding things like $VIM and ${HOME} */
9408 p = expand_env_save(p);
9409 if (p != NULL && *p != '$')
9410 n = TRUE;
9411 vim_free(p);
9412 }
9413 }
9414 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009417 if (*skipwhite(p) != NUL)
9418 n = FALSE; /* trailing garbage */
9419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 else if (*p == '*') /* internal or user defined function */
9421 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009422 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 }
9424 else if (*p == ':')
9425 {
9426 n = cmd_exists(p + 1);
9427 }
9428 else if (*p == '#')
9429 {
9430#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009431 if (p[1] == '#')
9432 n = autocmd_supported(p + 2);
9433 else
9434 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435#endif
9436 }
9437 else /* internal variable */
9438 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009439 char_u *tofree;
9440 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009442 /* get_name_len() takes care of expanding curly braces */
9443 name = p;
9444 len = get_name_len(&p, &tofree, TRUE, FALSE);
9445 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009447 if (tofree != NULL)
9448 name = tofree;
9449 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9450 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009452 /* handle d.key, l[idx], f(expr) */
9453 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9454 if (n)
9455 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 }
9457 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009458 if (*p != NUL)
9459 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009461 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 }
9463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465}
9466
9467/*
9468 * "expand()" function
9469 */
9470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
9475 char_u *s;
9476 int len;
9477 char_u *errormsg;
9478 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9479 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->v_type = VAR_STRING;
9483 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (*s == '%' || *s == '#' || *s == '<')
9485 {
9486 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009487 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 --emsg_off;
9489 }
9490 else
9491 {
9492 /* When the optional second argument is non-zero, don't remove matches
9493 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009494 if (argvars[1].v_type != VAR_UNKNOWN
9495 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009497 if (!error)
9498 {
9499 ExpandInit(&xpc);
9500 xpc.xp_context = EXPAND_FILES;
9501 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 }
9503 else
9504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 }
9506}
9507
9508/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009509 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009510 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009511 */
9512 static void
9513f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009514 typval_T *argvars;
9515 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009516{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009517 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009518 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009520 list_T *l1, *l2;
9521 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009522 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009523 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009524
Bram Moolenaare9a41262005-01-15 22:18:47 +00009525 l1 = argvars[0].vval.v_list;
9526 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009527 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9528 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009529 {
9530 if (argvars[2].v_type != VAR_UNKNOWN)
9531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009532 before = get_tv_number_chk(&argvars[2], &error);
9533 if (error)
9534 return; /* type error; errmsg already given */
9535
Bram Moolenaar758711c2005-02-02 23:11:38 +00009536 if (before == l1->lv_len)
9537 item = NULL;
9538 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009539 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009540 item = list_find(l1, before);
9541 if (item == NULL)
9542 {
9543 EMSGN(_(e_listidx), before);
9544 return;
9545 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009546 }
9547 }
9548 else
9549 item = NULL;
9550 list_extend(l1, l2, item);
9551
Bram Moolenaare9a41262005-01-15 22:18:47 +00009552 copy_tv(&argvars[0], rettv);
9553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9556 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 dict_T *d1, *d2;
9558 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009559 char_u *action;
9560 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009562 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009563
9564 d1 = argvars[0].vval.v_dict;
9565 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009566 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9567 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009568 {
9569 /* Check the third argument. */
9570 if (argvars[2].v_type != VAR_UNKNOWN)
9571 {
9572 static char *(av[]) = {"keep", "force", "error"};
9573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 action = get_tv_string_chk(&argvars[2]);
9575 if (action == NULL)
9576 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009577 for (i = 0; i < 3; ++i)
9578 if (STRCMP(action, av[i]) == 0)
9579 break;
9580 if (i == 3)
9581 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009582 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009583 return;
9584 }
9585 }
9586 else
9587 action = (char_u *)"force";
9588
9589 /* Go over all entries in the second dict and add them to the
9590 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009591 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009592 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009594 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009595 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009596 --todo;
9597 di1 = dict_find(d1, hi2->hi_key, -1);
9598 if (di1 == NULL)
9599 {
9600 di1 = dictitem_copy(HI2DI(hi2));
9601 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9602 dictitem_free(di1);
9603 }
9604 else if (*action == 'e')
9605 {
9606 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9607 break;
9608 }
9609 else if (*action == 'f')
9610 {
9611 clear_tv(&di1->di_tv);
9612 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9613 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 }
9615 }
9616
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 copy_tv(&argvars[0], rettv);
9618 }
9619 }
9620 else
9621 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009622}
9623
9624/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009625 * "feedkeys()" function
9626 */
9627/*ARGSUSED*/
9628 static void
9629f_feedkeys(argvars, rettv)
9630 typval_T *argvars;
9631 typval_T *rettv;
9632{
9633 int remap = TRUE;
9634 char_u *keys, *flags;
9635 char_u nbuf[NUMBUFLEN];
9636 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009637 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009638
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009639 /* This is not allowed in the sandbox. If the commands would still be
9640 * executed in the sandbox it would be OK, but it probably happens later,
9641 * when "sandbox" is no longer set. */
9642 if (check_secure())
9643 return;
9644
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009645 rettv->vval.v_number = 0;
9646 keys = get_tv_string(&argvars[0]);
9647 if (*keys != NUL)
9648 {
9649 if (argvars[1].v_type != VAR_UNKNOWN)
9650 {
9651 flags = get_tv_string_buf(&argvars[1], nbuf);
9652 for ( ; *flags != NUL; ++flags)
9653 {
9654 switch (*flags)
9655 {
9656 case 'n': remap = FALSE; break;
9657 case 'm': remap = TRUE; break;
9658 case 't': typed = TRUE; break;
9659 }
9660 }
9661 }
9662
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009663 /* Need to escape K_SPECIAL and CSI before putting the string in the
9664 * typeahead buffer. */
9665 keys_esc = vim_strsave_escape_csi(keys);
9666 if (keys_esc != NULL)
9667 {
9668 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009669 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009670 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009671 if (vgetc_busy)
9672 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009673 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009674 }
9675}
9676
9677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 * "filereadable()" function
9679 */
9680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 typval_T *argvars;
9683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 FILE *fd;
9686 char_u *p;
9687 int n;
9688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9691 {
9692 n = TRUE;
9693 fclose(fd);
9694 }
9695 else
9696 n = FALSE;
9697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699}
9700
9701/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009702 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 * rights to write into.
9704 */
9705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 typval_T *argvars;
9708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009710 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711}
9712
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009713static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009714
9715 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009716findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009717 typval_T *argvars;
9718 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009719 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009720{
9721#ifdef FEAT_SEARCHPATH
9722 char_u *fname;
9723 char_u *fresult = NULL;
9724 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9725 char_u *p;
9726 char_u pathbuf[NUMBUFLEN];
9727 int count = 1;
9728 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009729 int error = FALSE;
9730#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009731
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009732 rettv->vval.v_string = NULL;
9733 rettv->v_type = VAR_STRING;
9734
9735#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009737
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009738 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9741 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009742 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009743 else
9744 {
9745 if (*p != NUL)
9746 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009748 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009749 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009751 }
9752
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009753 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9754 error = TRUE;
9755
9756 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 do
9759 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009760 if (rettv->v_type == VAR_STRING)
9761 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009762 fresult = find_file_in_path_option(first ? fname : NULL,
9763 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009764 0, first, path,
9765 find_what,
9766 curbuf->b_ffname,
9767 find_what == FINDFILE_DIR
9768 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009770
9771 if (fresult != NULL && rettv->v_type == VAR_LIST)
9772 list_append_string(rettv->vval.v_list, fresult, -1);
9773
9774 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009776
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009777 if (rettv->v_type == VAR_STRING)
9778 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009779#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009780}
9781
Bram Moolenaar33570922005-01-25 22:26:29 +00009782static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9783static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009784
9785/*
9786 * Implementation of map() and filter().
9787 */
9788 static void
9789filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009790 typval_T *argvars;
9791 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009792 int map;
9793{
9794 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009795 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 listitem_T *li, *nli;
9797 list_T *l = NULL;
9798 dictitem_T *di;
9799 hashtab_T *ht;
9800 hashitem_T *hi;
9801 dict_T *d = NULL;
9802 typval_T save_val;
9803 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009804 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009805 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009806 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009807 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009808
9809 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009810 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009811 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009812 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009813 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009814 return;
9815 }
9816 else if (argvars[0].v_type == VAR_DICT)
9817 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009818 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009819 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009820 return;
9821 }
9822 else
9823 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009824 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009825 return;
9826 }
9827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009828 expr = get_tv_string_buf_chk(&argvars[1], buf);
9829 /* On type errors, the preceding call has already displayed an error
9830 * message. Avoid a misleading error message for an empty string that
9831 * was not passed as argument. */
9832 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 prepare_vimvar(VV_VAL, &save_val);
9835 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009836
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009837 /* We reset "did_emsg" to be able to detect whether an error
9838 * occurred during evaluation of the expression. */
9839 save_did_emsg = did_emsg;
9840 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 prepare_vimvar(VV_KEY, &save_key);
9845 vimvars[VV_KEY].vv_type = VAR_STRING;
9846
9847 ht = &d->dv_hashtab;
9848 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009849 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009850 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009852 if (!HASHITEM_EMPTY(hi))
9853 {
9854 --todo;
9855 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009856 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 break;
9858 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009859 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009860 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 break;
9862 if (!map && rem)
9863 dictitem_remove(d, di);
9864 clear_tv(&vimvars[VV_KEY].vv_tv);
9865 }
9866 }
9867 hash_unlock(ht);
9868
9869 restore_vimvar(VV_KEY, &save_key);
9870 }
9871 else
9872 {
9873 for (li = l->lv_first; li != NULL; li = nli)
9874 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009875 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009876 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009878 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009879 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009880 break;
9881 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009883 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009884 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009887
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009888 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009889 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009890
9891 copy_tv(&argvars[0], rettv);
9892}
9893
9894 static int
9895filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009897 char_u *expr;
9898 int map;
9899 int *remp;
9900{
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009903 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009904
Bram Moolenaar33570922005-01-25 22:26:29 +00009905 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009906 s = expr;
9907 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009908 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 if (*s != NUL) /* check for trailing chars after expr */
9910 {
9911 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009912 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 }
9914 if (map)
9915 {
9916 /* map(): replace the list item value */
9917 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009918 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009919 *tv = rettv;
9920 }
9921 else
9922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 int error = FALSE;
9924
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 /* On type error, nothing has been removed; return FAIL to stop the
9929 * loop. The error message was given by get_tv_number_chk(). */
9930 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009931 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009932 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009933 retval = OK;
9934theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009935 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009936 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009937}
9938
9939/*
9940 * "filter()" function
9941 */
9942 static void
9943f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009944 typval_T *argvars;
9945 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009946{
9947 filter_map(argvars, rettv, FALSE);
9948}
9949
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009950/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009951 * "finddir({fname}[, {path}[, {count}]])" function
9952 */
9953 static void
9954f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 typval_T *argvars;
9956 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009957{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009958 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009959}
9960
9961/*
9962 * "findfile({fname}[, {path}[, {count}]])" function
9963 */
9964 static void
9965f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 typval_T *argvars;
9967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009968{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009969 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009970}
9971
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009972#ifdef FEAT_FLOAT
9973/*
9974 * "float2nr({float})" function
9975 */
9976 static void
9977f_float2nr(argvars, rettv)
9978 typval_T *argvars;
9979 typval_T *rettv;
9980{
9981 float_T f;
9982
9983 if (get_float_arg(argvars, &f) == OK)
9984 {
9985 if (f < -0x7fffffff)
9986 rettv->vval.v_number = -0x7fffffff;
9987 else if (f > 0x7fffffff)
9988 rettv->vval.v_number = 0x7fffffff;
9989 else
9990 rettv->vval.v_number = (varnumber_T)f;
9991 }
9992 else
9993 rettv->vval.v_number = 0;
9994}
9995
9996/*
9997 * "floor({float})" function
9998 */
9999 static void
10000f_floor(argvars, rettv)
10001 typval_T *argvars;
10002 typval_T *rettv;
10003{
10004 float_T f;
10005
10006 rettv->v_type = VAR_FLOAT;
10007 if (get_float_arg(argvars, &f) == OK)
10008 rettv->vval.v_float = floor(f);
10009 else
10010 rettv->vval.v_float = 0.0;
10011}
10012#endif
10013
Bram Moolenaar0d660222005-01-07 21:51:51 +000010014/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010015 * "fnameescape({string})" function
10016 */
10017 static void
10018f_fnameescape(argvars, rettv)
10019 typval_T *argvars;
10020 typval_T *rettv;
10021{
10022 rettv->vval.v_string = vim_strsave_fnameescape(
10023 get_tv_string(&argvars[0]), FALSE);
10024 rettv->v_type = VAR_STRING;
10025}
10026
10027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 * "fnamemodify({fname}, {mods})" function
10029 */
10030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010032 typval_T *argvars;
10033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034{
10035 char_u *fname;
10036 char_u *mods;
10037 int usedlen = 0;
10038 int len;
10039 char_u *fbuf = NULL;
10040 char_u buf[NUMBUFLEN];
10041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010042 fname = get_tv_string_chk(&argvars[0]);
10043 mods = get_tv_string_buf_chk(&argvars[1], buf);
10044 if (fname == NULL || mods == NULL)
10045 fname = NULL;
10046 else
10047 {
10048 len = (int)STRLEN(fname);
10049 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 vim_free(fbuf);
10058}
10059
Bram Moolenaar33570922005-01-25 22:26:29 +000010060static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
10062/*
10063 * "foldclosed()" function
10064 */
10065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010067 typval_T *argvars;
10068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 int end;
10070{
10071#ifdef FEAT_FOLDING
10072 linenr_T lnum;
10073 linenr_T first, last;
10074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10077 {
10078 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10079 {
10080 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 return;
10085 }
10086 }
10087#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089}
10090
10091/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010092 * "foldclosed()" function
10093 */
10094 static void
10095f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010098{
10099 foldclosed_both(argvars, rettv, FALSE);
10100}
10101
10102/*
10103 * "foldclosedend()" function
10104 */
10105 static void
10106f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010107 typval_T *argvars;
10108 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010109{
10110 foldclosed_both(argvars, rettv, TRUE);
10111}
10112
10113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 * "foldlevel()" function
10115 */
10116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010118 typval_T *argvars;
10119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121#ifdef FEAT_FOLDING
10122 linenr_T lnum;
10123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 else
10128#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130}
10131
10132/*
10133 * "foldtext()" function
10134 */
10135/*ARGSUSED*/
10136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010138 typval_T *argvars;
10139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140{
10141#ifdef FEAT_FOLDING
10142 linenr_T lnum;
10143 char_u *s;
10144 char_u *r;
10145 int len;
10146 char *txt;
10147#endif
10148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->v_type = VAR_STRING;
10150 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010152 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10153 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10154 <= curbuf->b_ml.ml_line_count
10155 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 {
10157 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010158 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10159 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 {
10161 if (!linewhite(lnum))
10162 break;
10163 ++lnum;
10164 }
10165
10166 /* Find interesting text in this line. */
10167 s = skipwhite(ml_get(lnum));
10168 /* skip C comment-start */
10169 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010172 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010174 {
10175 s = skipwhite(ml_get(lnum + 1));
10176 if (*s == '*')
10177 s = skipwhite(s + 1);
10178 }
10179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 txt = _("+-%s%3ld lines: ");
10181 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 + 20 /* for %3ld */
10184 + STRLEN(s))); /* concatenated */
10185 if (r != NULL)
10186 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10188 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10189 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 len = (int)STRLEN(r);
10191 STRCAT(r, s);
10192 /* remove 'foldmarker' and 'commentstring' */
10193 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 }
10196 }
10197#endif
10198}
10199
10200/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010201 * "foldtextresult(lnum)" function
10202 */
10203/*ARGSUSED*/
10204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010206 typval_T *argvars;
10207 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010208{
10209#ifdef FEAT_FOLDING
10210 linenr_T lnum;
10211 char_u *text;
10212 char_u buf[51];
10213 foldinfo_T foldinfo;
10214 int fold_count;
10215#endif
10216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 rettv->v_type = VAR_STRING;
10218 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010219#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 /* treat illegal types and illegal string values for {lnum} the same */
10222 if (lnum < 0)
10223 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010224 fold_count = foldedCount(curwin, lnum, &foldinfo);
10225 if (fold_count > 0)
10226 {
10227 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10228 &foldinfo, buf);
10229 if (text == buf)
10230 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010231 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010232 }
10233#endif
10234}
10235
10236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 * "foreground()" function
10238 */
10239/*ARGSUSED*/
10240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 typval_T *argvars;
10243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#ifdef FEAT_GUI
10247 if (gui.in_use)
10248 gui_mch_set_foreground();
10249#else
10250# ifdef WIN32
10251 win32_set_foreground();
10252# endif
10253#endif
10254}
10255
10256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010257 * "function()" function
10258 */
10259/*ARGSUSED*/
10260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010261f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T *argvars;
10263 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010264{
10265 char_u *s;
10266
Bram Moolenaara7043832005-01-21 11:56:39 +000010267 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010270 EMSG2(_(e_invarg2), s);
10271 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010272 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010273 else
10274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 rettv->vval.v_string = vim_strsave(s);
10276 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010277 }
10278}
10279
10280/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010281 * "garbagecollect()" function
10282 */
10283/*ARGSUSED*/
10284 static void
10285f_garbagecollect(argvars, rettv)
10286 typval_T *argvars;
10287 typval_T *rettv;
10288{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010289 /* This is postponed until we are back at the toplevel, because we may be
10290 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10291 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010292
10293 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10294 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010295}
10296
10297/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010298 * "get()" function
10299 */
10300 static void
10301f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010302 typval_T *argvars;
10303 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010304{
Bram Moolenaar33570922005-01-25 22:26:29 +000010305 listitem_T *li;
10306 list_T *l;
10307 dictitem_T *di;
10308 dict_T *d;
10309 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010310
Bram Moolenaare9a41262005-01-15 22:18:47 +000010311 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010312 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 int error = FALSE;
10316
10317 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10318 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010319 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010320 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010321 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010322 else if (argvars[0].v_type == VAR_DICT)
10323 {
10324 if ((d = argvars[0].vval.v_dict) != NULL)
10325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010326 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010327 if (di != NULL)
10328 tv = &di->di_tv;
10329 }
10330 }
10331 else
10332 EMSG2(_(e_listdictarg), "get()");
10333
10334 if (tv == NULL)
10335 {
10336 if (argvars[2].v_type == VAR_UNKNOWN)
10337 rettv->vval.v_number = 0;
10338 else
10339 copy_tv(&argvars[2], rettv);
10340 }
10341 else
10342 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010343}
10344
Bram Moolenaar342337a2005-07-21 21:11:17 +000010345static 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 +000010346
10347/*
10348 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010349 * Return a range (from start to end) of lines in rettv from the specified
10350 * buffer.
10351 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010352 */
10353 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010354get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010355 buf_T *buf;
10356 linenr_T start;
10357 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010358 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010359 typval_T *rettv;
10360{
10361 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010362
Bram Moolenaar342337a2005-07-21 21:11:17 +000010363 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010364 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010365 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010366 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010367 }
10368 else
10369 rettv->vval.v_number = 0;
10370
10371 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10372 return;
10373
10374 if (!retlist)
10375 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010376 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10377 p = ml_get_buf(buf, start, FALSE);
10378 else
10379 p = (char_u *)"";
10380
10381 rettv->v_type = VAR_STRING;
10382 rettv->vval.v_string = vim_strsave(p);
10383 }
10384 else
10385 {
10386 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010387 return;
10388
10389 if (start < 1)
10390 start = 1;
10391 if (end > buf->b_ml.ml_line_count)
10392 end = buf->b_ml.ml_line_count;
10393 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010394 if (list_append_string(rettv->vval.v_list,
10395 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010396 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010397 }
10398}
10399
10400/*
10401 * "getbufline()" function
10402 */
10403 static void
10404f_getbufline(argvars, rettv)
10405 typval_T *argvars;
10406 typval_T *rettv;
10407{
10408 linenr_T lnum;
10409 linenr_T end;
10410 buf_T *buf;
10411
10412 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10413 ++emsg_off;
10414 buf = get_buf_tv(&argvars[0]);
10415 --emsg_off;
10416
Bram Moolenaar661b1822005-07-28 22:36:45 +000010417 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010418 if (argvars[2].v_type == VAR_UNKNOWN)
10419 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010420 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010421 end = get_tv_lnum_buf(&argvars[2], buf);
10422
Bram Moolenaar342337a2005-07-21 21:11:17 +000010423 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010424}
10425
Bram Moolenaar0d660222005-01-07 21:51:51 +000010426/*
10427 * "getbufvar()" function
10428 */
10429 static void
10430f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010431 typval_T *argvars;
10432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010433{
10434 buf_T *buf;
10435 buf_T *save_curbuf;
10436 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10440 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010441 ++emsg_off;
10442 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010443
10444 rettv->v_type = VAR_STRING;
10445 rettv->vval.v_string = NULL;
10446
10447 if (buf != NULL && varname != NULL)
10448 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010449 /* set curbuf to be our buf, temporarily */
10450 save_curbuf = curbuf;
10451 curbuf = buf;
10452
Bram Moolenaar0d660222005-01-07 21:51:51 +000010453 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010454 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010455 else
10456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 if (*varname == NUL)
10458 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10459 * scope prefix before the NUL byte is required by
10460 * find_var_in_ht(). */
10461 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010462 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010463 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010464 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010466 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010467
10468 /* restore previous notion of curbuf */
10469 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010470 }
10471
10472 --emsg_off;
10473}
10474
10475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 * "getchar()" function
10477 */
10478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 typval_T *argvars;
10481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
10483 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010486 /* Position the cursor. Needed after a message that ends in a space. */
10487 windgoto(msg_row, msg_col);
10488
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 ++no_mapping;
10490 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010491 for (;;)
10492 {
10493 if (argvars[0].v_type == VAR_UNKNOWN)
10494 /* getchar(): blocking wait. */
10495 n = safe_vgetc();
10496 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10497 /* getchar(1): only check if char avail */
10498 n = vpeekc();
10499 else if (error || vpeekc() == NUL)
10500 /* illegal argument or getchar(0) and no char avail: return zero */
10501 n = 0;
10502 else
10503 /* getchar(0) and char avail: return char */
10504 n = safe_vgetc();
10505 if (n == K_IGNORE)
10506 continue;
10507 break;
10508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 --no_mapping;
10510 --allow_keys;
10511
Bram Moolenaar219b8702006-11-01 14:32:36 +000010512 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10513 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10514 vimvars[VV_MOUSE_COL].vv_nr = 0;
10515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 if (IS_SPECIAL(n) || mod_mask != 0)
10518 {
10519 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10520 int i = 0;
10521
10522 /* Turn a special key into three bytes, plus modifier. */
10523 if (mod_mask != 0)
10524 {
10525 temp[i++] = K_SPECIAL;
10526 temp[i++] = KS_MODIFIER;
10527 temp[i++] = mod_mask;
10528 }
10529 if (IS_SPECIAL(n))
10530 {
10531 temp[i++] = K_SPECIAL;
10532 temp[i++] = K_SECOND(n);
10533 temp[i++] = K_THIRD(n);
10534 }
10535#ifdef FEAT_MBYTE
10536 else if (has_mbyte)
10537 i += (*mb_char2bytes)(n, temp + i);
10538#endif
10539 else
10540 temp[i++] = n;
10541 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 rettv->v_type = VAR_STRING;
10543 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010544
10545#ifdef FEAT_MOUSE
10546 if (n == K_LEFTMOUSE
10547 || n == K_LEFTMOUSE_NM
10548 || n == K_LEFTDRAG
10549 || n == K_LEFTRELEASE
10550 || n == K_LEFTRELEASE_NM
10551 || n == K_MIDDLEMOUSE
10552 || n == K_MIDDLEDRAG
10553 || n == K_MIDDLERELEASE
10554 || n == K_RIGHTMOUSE
10555 || n == K_RIGHTDRAG
10556 || n == K_RIGHTRELEASE
10557 || n == K_X1MOUSE
10558 || n == K_X1DRAG
10559 || n == K_X1RELEASE
10560 || n == K_X2MOUSE
10561 || n == K_X2DRAG
10562 || n == K_X2RELEASE
10563 || n == K_MOUSEDOWN
10564 || n == K_MOUSEUP)
10565 {
10566 int row = mouse_row;
10567 int col = mouse_col;
10568 win_T *win;
10569 linenr_T lnum;
10570# ifdef FEAT_WINDOWS
10571 win_T *wp;
10572# endif
10573 int n = 1;
10574
10575 if (row >= 0 && col >= 0)
10576 {
10577 /* Find the window at the mouse coordinates and compute the
10578 * text position. */
10579 win = mouse_find_win(&row, &col);
10580 (void)mouse_comp_pos(win, &row, &col, &lnum);
10581# ifdef FEAT_WINDOWS
10582 for (wp = firstwin; wp != win; wp = wp->w_next)
10583 ++n;
10584# endif
10585 vimvars[VV_MOUSE_WIN].vv_nr = n;
10586 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10587 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10588 }
10589 }
10590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 }
10592}
10593
10594/*
10595 * "getcharmod()" function
10596 */
10597/*ARGSUSED*/
10598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010599f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010600 typval_T *argvars;
10601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604}
10605
10606/*
10607 * "getcmdline()" function
10608 */
10609/*ARGSUSED*/
10610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010612 typval_T *argvars;
10613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->v_type = VAR_STRING;
10616 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617}
10618
10619/*
10620 * "getcmdpos()" function
10621 */
10622/*ARGSUSED*/
10623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 typval_T *argvars;
10626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629}
10630
10631/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010632 * "getcmdtype()" function
10633 */
10634/*ARGSUSED*/
10635 static void
10636f_getcmdtype(argvars, rettv)
10637 typval_T *argvars;
10638 typval_T *rettv;
10639{
10640 rettv->v_type = VAR_STRING;
10641 rettv->vval.v_string = alloc(2);
10642 if (rettv->vval.v_string != NULL)
10643 {
10644 rettv->vval.v_string[0] = get_cmdline_type();
10645 rettv->vval.v_string[1] = NUL;
10646 }
10647}
10648
10649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 * "getcwd()" function
10651 */
10652/*ARGSUSED*/
10653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010655 typval_T *argvars;
10656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657{
10658 char_u cwd[MAXPATHL];
10659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 else
10664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010667 if (rettv->vval.v_string != NULL)
10668 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669#endif
10670 }
10671}
10672
10673/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010674 * "getfontname()" function
10675 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010676/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010679 typval_T *argvars;
10680 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 rettv->v_type = VAR_STRING;
10683 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010684#ifdef FEAT_GUI
10685 if (gui.in_use)
10686 {
10687 GuiFont font;
10688 char_u *name = NULL;
10689
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010690 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010691 {
10692 /* Get the "Normal" font. Either the name saved by
10693 * hl_set_font_name() or from the font ID. */
10694 font = gui.norm_font;
10695 name = hl_get_font_name();
10696 }
10697 else
10698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010700 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10701 return;
10702 font = gui_mch_get_font(name, FALSE);
10703 if (font == NOFONT)
10704 return; /* Invalid font name, return empty string. */
10705 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010707 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010708 gui_mch_free_font(font);
10709 }
10710#endif
10711}
10712
10713/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010714 * "getfperm({fname})" function
10715 */
10716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010718 typval_T *argvars;
10719 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010720{
10721 char_u *fname;
10722 struct stat st;
10723 char_u *perm = NULL;
10724 char_u flags[] = "rwx";
10725 int i;
10726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010730 if (mch_stat((char *)fname, &st) >= 0)
10731 {
10732 perm = vim_strsave((char_u *)"---------");
10733 if (perm != NULL)
10734 {
10735 for (i = 0; i < 9; i++)
10736 {
10737 if (st.st_mode & (1 << (8 - i)))
10738 perm[i] = flags[i % 3];
10739 }
10740 }
10741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010743}
10744
10745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 * "getfsize({fname})" function
10747 */
10748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 typval_T *argvars;
10751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752{
10753 char_u *fname;
10754 struct stat st;
10755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759
10760 if (mch_stat((char *)fname, &st) >= 0)
10761 {
10762 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010767
10768 /* non-perfect check for overflow */
10769 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10770 rettv->vval.v_number = -2;
10771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 }
10773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775}
10776
10777/*
10778 * "getftime({fname})" function
10779 */
10780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010782 typval_T *argvars;
10783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784{
10785 char_u *fname;
10786 struct stat st;
10787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
10790 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794}
10795
10796/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010797 * "getftype({fname})" function
10798 */
10799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *argvars;
10802 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010803{
10804 char_u *fname;
10805 struct stat st;
10806 char_u *type = NULL;
10807 char *t;
10808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010812 if (mch_lstat((char *)fname, &st) >= 0)
10813 {
10814#ifdef S_ISREG
10815 if (S_ISREG(st.st_mode))
10816 t = "file";
10817 else if (S_ISDIR(st.st_mode))
10818 t = "dir";
10819# ifdef S_ISLNK
10820 else if (S_ISLNK(st.st_mode))
10821 t = "link";
10822# endif
10823# ifdef S_ISBLK
10824 else if (S_ISBLK(st.st_mode))
10825 t = "bdev";
10826# endif
10827# ifdef S_ISCHR
10828 else if (S_ISCHR(st.st_mode))
10829 t = "cdev";
10830# endif
10831# ifdef S_ISFIFO
10832 else if (S_ISFIFO(st.st_mode))
10833 t = "fifo";
10834# endif
10835# ifdef S_ISSOCK
10836 else if (S_ISSOCK(st.st_mode))
10837 t = "fifo";
10838# endif
10839 else
10840 t = "other";
10841#else
10842# ifdef S_IFMT
10843 switch (st.st_mode & S_IFMT)
10844 {
10845 case S_IFREG: t = "file"; break;
10846 case S_IFDIR: t = "dir"; break;
10847# ifdef S_IFLNK
10848 case S_IFLNK: t = "link"; break;
10849# endif
10850# ifdef S_IFBLK
10851 case S_IFBLK: t = "bdev"; break;
10852# endif
10853# ifdef S_IFCHR
10854 case S_IFCHR: t = "cdev"; break;
10855# endif
10856# ifdef S_IFIFO
10857 case S_IFIFO: t = "fifo"; break;
10858# endif
10859# ifdef S_IFSOCK
10860 case S_IFSOCK: t = "socket"; break;
10861# endif
10862 default: t = "other";
10863 }
10864# else
10865 if (mch_isdir(fname))
10866 t = "dir";
10867 else
10868 t = "file";
10869# endif
10870#endif
10871 type = vim_strsave((char_u *)t);
10872 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010874}
10875
10876/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010877 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010878 */
10879 static void
10880f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010881 typval_T *argvars;
10882 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883{
10884 linenr_T lnum;
10885 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010886 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010887
10888 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010889 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010890 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010891 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010892 retlist = FALSE;
10893 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010895 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010896 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010897 retlist = TRUE;
10898 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010899
Bram Moolenaar342337a2005-07-21 21:11:17 +000010900 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010901}
10902
10903/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010904 * "getmatches()" function
10905 */
10906/*ARGSUSED*/
10907 static void
10908f_getmatches(argvars, rettv)
10909 typval_T *argvars;
10910 typval_T *rettv;
10911{
10912#ifdef FEAT_SEARCH_EXTRA
10913 dict_T *dict;
10914 matchitem_T *cur = curwin->w_match_head;
10915
10916 rettv->vval.v_number = 0;
10917
10918 if (rettv_list_alloc(rettv) == OK)
10919 {
10920 while (cur != NULL)
10921 {
10922 dict = dict_alloc();
10923 if (dict == NULL)
10924 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010925 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10926 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10927 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10928 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10929 list_append_dict(rettv->vval.v_list, dict);
10930 cur = cur->next;
10931 }
10932 }
10933#endif
10934}
10935
10936/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000010937 * "getpid()" function
10938 */
10939/*ARGSUSED*/
10940 static void
10941f_getpid(argvars, rettv)
10942 typval_T *argvars;
10943 typval_T *rettv;
10944{
10945 rettv->vval.v_number = mch_get_pid();
10946}
10947
10948/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010949 * "getpos(string)" function
10950 */
10951 static void
10952f_getpos(argvars, rettv)
10953 typval_T *argvars;
10954 typval_T *rettv;
10955{
10956 pos_T *fp;
10957 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010958 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010959
10960 if (rettv_list_alloc(rettv) == OK)
10961 {
10962 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010963 fp = var2fpos(&argvars[0], TRUE, &fnum);
10964 if (fnum != -1)
10965 list_append_number(l, (varnumber_T)fnum);
10966 else
10967 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010968 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10969 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010970 list_append_number(l, (fp != NULL)
10971 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010972 : (varnumber_T)0);
10973 list_append_number(l,
10974#ifdef FEAT_VIRTUALEDIT
10975 (fp != NULL) ? (varnumber_T)fp->coladd :
10976#endif
10977 (varnumber_T)0);
10978 }
10979 else
10980 rettv->vval.v_number = FALSE;
10981}
10982
10983/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010984 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010985 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010986/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010987 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010988f_getqflist(argvars, rettv)
10989 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010990 typval_T *rettv;
10991{
10992#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010993 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010994#endif
10995
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010996 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010997#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010998 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010999 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011000 wp = NULL;
11001 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11002 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011003 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011004 if (wp == NULL)
11005 return;
11006 }
11007
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011008 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011009 }
11010#endif
11011}
11012
11013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 * "getreg()" function
11015 */
11016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *argvars;
11019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
11021 char_u *strregname;
11022 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011023 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011026 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 strregname = get_tv_string_chk(&argvars[0]);
11029 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011030 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011034 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 regname = (strregname == NULL ? '"' : *strregname);
11036 if (regname == 0)
11037 regname = '"';
11038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011040 rettv->vval.v_string = error ? NULL :
11041 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042}
11043
11044/*
11045 * "getregtype()" function
11046 */
11047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011049 typval_T *argvars;
11050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051{
11052 char_u *strregname;
11053 int regname;
11054 char_u buf[NUMBUFLEN + 2];
11055 long reglen = 0;
11056
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011057 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 {
11059 strregname = get_tv_string_chk(&argvars[0]);
11060 if (strregname == NULL) /* type error; errmsg already given */
11061 {
11062 rettv->v_type = VAR_STRING;
11063 rettv->vval.v_string = NULL;
11064 return;
11065 }
11066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 else
11068 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011069 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070
11071 regname = (strregname == NULL ? '"' : *strregname);
11072 if (regname == 0)
11073 regname = '"';
11074
11075 buf[0] = NUL;
11076 buf[1] = NUL;
11077 switch (get_reg_type(regname, &reglen))
11078 {
11079 case MLINE: buf[0] = 'V'; break;
11080 case MCHAR: buf[0] = 'v'; break;
11081#ifdef FEAT_VISUAL
11082 case MBLOCK:
11083 buf[0] = Ctrl_V;
11084 sprintf((char *)buf + 1, "%ld", reglen + 1);
11085 break;
11086#endif
11087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->v_type = VAR_STRING;
11089 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090}
11091
11092/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011093 * "gettabwinvar()" function
11094 */
11095 static void
11096f_gettabwinvar(argvars, rettv)
11097 typval_T *argvars;
11098 typval_T *rettv;
11099{
11100 getwinvar(argvars, rettv, 1);
11101}
11102
11103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 * "getwinposx()" function
11105 */
11106/*ARGSUSED*/
11107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011109 typval_T *argvars;
11110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113#ifdef FEAT_GUI
11114 if (gui.in_use)
11115 {
11116 int x, y;
11117
11118 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 }
11121#endif
11122}
11123
11124/*
11125 * "getwinposy()" function
11126 */
11127/*ARGSUSED*/
11128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011130 typval_T *argvars;
11131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134#ifdef FEAT_GUI
11135 if (gui.in_use)
11136 {
11137 int x, y;
11138
11139 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 }
11142#endif
11143}
11144
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011145/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011146 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011147 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011148 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011149find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011150 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011151 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011152{
11153#ifdef FEAT_WINDOWS
11154 win_T *wp;
11155#endif
11156 int nr;
11157
11158 nr = get_tv_number_chk(vp, NULL);
11159
11160#ifdef FEAT_WINDOWS
11161 if (nr < 0)
11162 return NULL;
11163 if (nr == 0)
11164 return curwin;
11165
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011166 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11167 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011168 if (--nr <= 0)
11169 break;
11170 return wp;
11171#else
11172 if (nr == 0 || nr == 1)
11173 return curwin;
11174 return NULL;
11175#endif
11176}
11177
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178/*
11179 * "getwinvar()" function
11180 */
11181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *argvars;
11184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011186 getwinvar(argvars, rettv, 0);
11187}
11188
11189/*
11190 * getwinvar() and gettabwinvar()
11191 */
11192 static void
11193getwinvar(argvars, rettv, off)
11194 typval_T *argvars;
11195 typval_T *rettv;
11196 int off; /* 1 for gettabwinvar() */
11197{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 win_T *win, *oldcurwin;
11199 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011200 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011201 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011203#ifdef FEAT_WINDOWS
11204 if (off == 1)
11205 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11206 else
11207 tp = curtab;
11208#endif
11209 win = find_win_by_nr(&argvars[off], tp);
11210 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->v_type = VAR_STRING;
11214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215
11216 if (win != NULL && varname != NULL)
11217 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011218 /* Set curwin to be our win, temporarily. Also set curbuf, so
11219 * that we can get buffer-local options. */
11220 oldcurwin = curwin;
11221 curwin = win;
11222 curbuf = win->w_buffer;
11223
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 else
11227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 if (*varname == NUL)
11229 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11230 * scope prefix before the NUL byte is required by
11231 * find_var_in_ht(). */
11232 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011234 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011236 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011238
11239 /* restore previous notion of curwin */
11240 curwin = oldcurwin;
11241 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 }
11243
11244 --emsg_off;
11245}
11246
11247/*
11248 * "glob()" function
11249 */
11250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011252 typval_T *argvars;
11253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254{
11255 expand_T xpc;
11256
11257 ExpandInit(&xpc);
11258 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->v_type = VAR_STRING;
11260 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262}
11263
11264/*
11265 * "globpath()" function
11266 */
11267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011269 typval_T *argvars;
11270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271{
11272 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 if (file == NULL)
11277 rettv->vval.v_string = NULL;
11278 else
11279 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280}
11281
11282/*
11283 * "has()" function
11284 */
11285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *argvars;
11288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289{
11290 int i;
11291 char_u *name;
11292 int n = FALSE;
11293 static char *(has_list[]) =
11294 {
11295#ifdef AMIGA
11296 "amiga",
11297# ifdef FEAT_ARP
11298 "arp",
11299# endif
11300#endif
11301#ifdef __BEOS__
11302 "beos",
11303#endif
11304#ifdef MSDOS
11305# ifdef DJGPP
11306 "dos32",
11307# else
11308 "dos16",
11309# endif
11310#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011311#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 "mac",
11313#endif
11314#if defined(MACOS_X_UNIX)
11315 "macunix",
11316#endif
11317#ifdef OS2
11318 "os2",
11319#endif
11320#ifdef __QNX__
11321 "qnx",
11322#endif
11323#ifdef RISCOS
11324 "riscos",
11325#endif
11326#ifdef UNIX
11327 "unix",
11328#endif
11329#ifdef VMS
11330 "vms",
11331#endif
11332#ifdef WIN16
11333 "win16",
11334#endif
11335#ifdef WIN32
11336 "win32",
11337#endif
11338#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11339 "win32unix",
11340#endif
11341#ifdef WIN64
11342 "win64",
11343#endif
11344#ifdef EBCDIC
11345 "ebcdic",
11346#endif
11347#ifndef CASE_INSENSITIVE_FILENAME
11348 "fname_case",
11349#endif
11350#ifdef FEAT_ARABIC
11351 "arabic",
11352#endif
11353#ifdef FEAT_AUTOCMD
11354 "autocmd",
11355#endif
11356#ifdef FEAT_BEVAL
11357 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011358# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11359 "balloon_multiline",
11360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361#endif
11362#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11363 "builtin_terms",
11364# ifdef ALL_BUILTIN_TCAPS
11365 "all_builtin_terms",
11366# endif
11367#endif
11368#ifdef FEAT_BYTEOFF
11369 "byte_offset",
11370#endif
11371#ifdef FEAT_CINDENT
11372 "cindent",
11373#endif
11374#ifdef FEAT_CLIENTSERVER
11375 "clientserver",
11376#endif
11377#ifdef FEAT_CLIPBOARD
11378 "clipboard",
11379#endif
11380#ifdef FEAT_CMDL_COMPL
11381 "cmdline_compl",
11382#endif
11383#ifdef FEAT_CMDHIST
11384 "cmdline_hist",
11385#endif
11386#ifdef FEAT_COMMENTS
11387 "comments",
11388#endif
11389#ifdef FEAT_CRYPT
11390 "cryptv",
11391#endif
11392#ifdef FEAT_CSCOPE
11393 "cscope",
11394#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011395#ifdef CURSOR_SHAPE
11396 "cursorshape",
11397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398#ifdef DEBUG
11399 "debug",
11400#endif
11401#ifdef FEAT_CON_DIALOG
11402 "dialog_con",
11403#endif
11404#ifdef FEAT_GUI_DIALOG
11405 "dialog_gui",
11406#endif
11407#ifdef FEAT_DIFF
11408 "diff",
11409#endif
11410#ifdef FEAT_DIGRAPHS
11411 "digraphs",
11412#endif
11413#ifdef FEAT_DND
11414 "dnd",
11415#endif
11416#ifdef FEAT_EMACS_TAGS
11417 "emacs_tags",
11418#endif
11419 "eval", /* always present, of course! */
11420#ifdef FEAT_EX_EXTRA
11421 "ex_extra",
11422#endif
11423#ifdef FEAT_SEARCH_EXTRA
11424 "extra_search",
11425#endif
11426#ifdef FEAT_FKMAP
11427 "farsi",
11428#endif
11429#ifdef FEAT_SEARCHPATH
11430 "file_in_path",
11431#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011432#if defined(UNIX) && !defined(USE_SYSTEM)
11433 "filterpipe",
11434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435#ifdef FEAT_FIND_ID
11436 "find_in_path",
11437#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011438#ifdef FEAT_FLOAT
11439 "float",
11440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441#ifdef FEAT_FOLDING
11442 "folding",
11443#endif
11444#ifdef FEAT_FOOTER
11445 "footer",
11446#endif
11447#if !defined(USE_SYSTEM) && defined(UNIX)
11448 "fork",
11449#endif
11450#ifdef FEAT_GETTEXT
11451 "gettext",
11452#endif
11453#ifdef FEAT_GUI
11454 "gui",
11455#endif
11456#ifdef FEAT_GUI_ATHENA
11457# ifdef FEAT_GUI_NEXTAW
11458 "gui_neXtaw",
11459# else
11460 "gui_athena",
11461# endif
11462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463#ifdef FEAT_GUI_GTK
11464 "gui_gtk",
11465# ifdef HAVE_GTK2
11466 "gui_gtk2",
11467# endif
11468#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011469#ifdef FEAT_GUI_GNOME
11470 "gui_gnome",
11471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472#ifdef FEAT_GUI_MAC
11473 "gui_mac",
11474#endif
11475#ifdef FEAT_GUI_MOTIF
11476 "gui_motif",
11477#endif
11478#ifdef FEAT_GUI_PHOTON
11479 "gui_photon",
11480#endif
11481#ifdef FEAT_GUI_W16
11482 "gui_win16",
11483#endif
11484#ifdef FEAT_GUI_W32
11485 "gui_win32",
11486#endif
11487#ifdef FEAT_HANGULIN
11488 "hangul_input",
11489#endif
11490#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11491 "iconv",
11492#endif
11493#ifdef FEAT_INS_EXPAND
11494 "insert_expand",
11495#endif
11496#ifdef FEAT_JUMPLIST
11497 "jumplist",
11498#endif
11499#ifdef FEAT_KEYMAP
11500 "keymap",
11501#endif
11502#ifdef FEAT_LANGMAP
11503 "langmap",
11504#endif
11505#ifdef FEAT_LIBCALL
11506 "libcall",
11507#endif
11508#ifdef FEAT_LINEBREAK
11509 "linebreak",
11510#endif
11511#ifdef FEAT_LISP
11512 "lispindent",
11513#endif
11514#ifdef FEAT_LISTCMDS
11515 "listcmds",
11516#endif
11517#ifdef FEAT_LOCALMAP
11518 "localmap",
11519#endif
11520#ifdef FEAT_MENU
11521 "menu",
11522#endif
11523#ifdef FEAT_SESSION
11524 "mksession",
11525#endif
11526#ifdef FEAT_MODIFY_FNAME
11527 "modify_fname",
11528#endif
11529#ifdef FEAT_MOUSE
11530 "mouse",
11531#endif
11532#ifdef FEAT_MOUSESHAPE
11533 "mouseshape",
11534#endif
11535#if defined(UNIX) || defined(VMS)
11536# ifdef FEAT_MOUSE_DEC
11537 "mouse_dec",
11538# endif
11539# ifdef FEAT_MOUSE_GPM
11540 "mouse_gpm",
11541# endif
11542# ifdef FEAT_MOUSE_JSB
11543 "mouse_jsbterm",
11544# endif
11545# ifdef FEAT_MOUSE_NET
11546 "mouse_netterm",
11547# endif
11548# ifdef FEAT_MOUSE_PTERM
11549 "mouse_pterm",
11550# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011551# ifdef FEAT_SYSMOUSE
11552 "mouse_sysmouse",
11553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554# ifdef FEAT_MOUSE_XTERM
11555 "mouse_xterm",
11556# endif
11557#endif
11558#ifdef FEAT_MBYTE
11559 "multi_byte",
11560#endif
11561#ifdef FEAT_MBYTE_IME
11562 "multi_byte_ime",
11563#endif
11564#ifdef FEAT_MULTI_LANG
11565 "multi_lang",
11566#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011567#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011568#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011569 "mzscheme",
11570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572#ifdef FEAT_OLE
11573 "ole",
11574#endif
11575#ifdef FEAT_OSFILETYPE
11576 "osfiletype",
11577#endif
11578#ifdef FEAT_PATH_EXTRA
11579 "path_extra",
11580#endif
11581#ifdef FEAT_PERL
11582#ifndef DYNAMIC_PERL
11583 "perl",
11584#endif
11585#endif
11586#ifdef FEAT_PYTHON
11587#ifndef DYNAMIC_PYTHON
11588 "python",
11589#endif
11590#endif
11591#ifdef FEAT_POSTSCRIPT
11592 "postscript",
11593#endif
11594#ifdef FEAT_PRINTER
11595 "printer",
11596#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011597#ifdef FEAT_PROFILE
11598 "profile",
11599#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011600#ifdef FEAT_RELTIME
11601 "reltime",
11602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603#ifdef FEAT_QUICKFIX
11604 "quickfix",
11605#endif
11606#ifdef FEAT_RIGHTLEFT
11607 "rightleft",
11608#endif
11609#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11610 "ruby",
11611#endif
11612#ifdef FEAT_SCROLLBIND
11613 "scrollbind",
11614#endif
11615#ifdef FEAT_CMDL_INFO
11616 "showcmd",
11617 "cmdline_info",
11618#endif
11619#ifdef FEAT_SIGNS
11620 "signs",
11621#endif
11622#ifdef FEAT_SMARTINDENT
11623 "smartindent",
11624#endif
11625#ifdef FEAT_SNIFF
11626 "sniff",
11627#endif
11628#ifdef FEAT_STL_OPT
11629 "statusline",
11630#endif
11631#ifdef FEAT_SUN_WORKSHOP
11632 "sun_workshop",
11633#endif
11634#ifdef FEAT_NETBEANS_INTG
11635 "netbeans_intg",
11636#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011637#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011638 "spell",
11639#endif
11640#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 "syntax",
11642#endif
11643#if defined(USE_SYSTEM) || !defined(UNIX)
11644 "system",
11645#endif
11646#ifdef FEAT_TAG_BINS
11647 "tag_binary",
11648#endif
11649#ifdef FEAT_TAG_OLDSTATIC
11650 "tag_old_static",
11651#endif
11652#ifdef FEAT_TAG_ANYWHITE
11653 "tag_any_white",
11654#endif
11655#ifdef FEAT_TCL
11656# ifndef DYNAMIC_TCL
11657 "tcl",
11658# endif
11659#endif
11660#ifdef TERMINFO
11661 "terminfo",
11662#endif
11663#ifdef FEAT_TERMRESPONSE
11664 "termresponse",
11665#endif
11666#ifdef FEAT_TEXTOBJ
11667 "textobjects",
11668#endif
11669#ifdef HAVE_TGETENT
11670 "tgetent",
11671#endif
11672#ifdef FEAT_TITLE
11673 "title",
11674#endif
11675#ifdef FEAT_TOOLBAR
11676 "toolbar",
11677#endif
11678#ifdef FEAT_USR_CMDS
11679 "user-commands", /* was accidentally included in 5.4 */
11680 "user_commands",
11681#endif
11682#ifdef FEAT_VIMINFO
11683 "viminfo",
11684#endif
11685#ifdef FEAT_VERTSPLIT
11686 "vertsplit",
11687#endif
11688#ifdef FEAT_VIRTUALEDIT
11689 "virtualedit",
11690#endif
11691#ifdef FEAT_VISUAL
11692 "visual",
11693#endif
11694#ifdef FEAT_VISUALEXTRA
11695 "visualextra",
11696#endif
11697#ifdef FEAT_VREPLACE
11698 "vreplace",
11699#endif
11700#ifdef FEAT_WILDIGN
11701 "wildignore",
11702#endif
11703#ifdef FEAT_WILDMENU
11704 "wildmenu",
11705#endif
11706#ifdef FEAT_WINDOWS
11707 "windows",
11708#endif
11709#ifdef FEAT_WAK
11710 "winaltkeys",
11711#endif
11712#ifdef FEAT_WRITEBACKUP
11713 "writebackup",
11714#endif
11715#ifdef FEAT_XIM
11716 "xim",
11717#endif
11718#ifdef FEAT_XFONTSET
11719 "xfontset",
11720#endif
11721#ifdef USE_XSMP
11722 "xsmp",
11723#endif
11724#ifdef USE_XSMP_INTERACT
11725 "xsmp_interact",
11726#endif
11727#ifdef FEAT_XCLIPBOARD
11728 "xterm_clipboard",
11729#endif
11730#ifdef FEAT_XTERM_SAVE
11731 "xterm_save",
11732#endif
11733#if defined(UNIX) && defined(FEAT_X11)
11734 "X11",
11735#endif
11736 NULL
11737 };
11738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 for (i = 0; has_list[i] != NULL; ++i)
11741 if (STRICMP(name, has_list[i]) == 0)
11742 {
11743 n = TRUE;
11744 break;
11745 }
11746
11747 if (n == FALSE)
11748 {
11749 if (STRNICMP(name, "patch", 5) == 0)
11750 n = has_patch(atoi((char *)name + 5));
11751 else if (STRICMP(name, "vim_starting") == 0)
11752 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011753#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11754 else if (STRICMP(name, "balloon_multiline") == 0)
11755 n = multiline_balloon_available();
11756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757#ifdef DYNAMIC_TCL
11758 else if (STRICMP(name, "tcl") == 0)
11759 n = tcl_enabled(FALSE);
11760#endif
11761#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11762 else if (STRICMP(name, "iconv") == 0)
11763 n = iconv_enabled(FALSE);
11764#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011765#ifdef DYNAMIC_MZSCHEME
11766 else if (STRICMP(name, "mzscheme") == 0)
11767 n = mzscheme_enabled(FALSE);
11768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769#ifdef DYNAMIC_RUBY
11770 else if (STRICMP(name, "ruby") == 0)
11771 n = ruby_enabled(FALSE);
11772#endif
11773#ifdef DYNAMIC_PYTHON
11774 else if (STRICMP(name, "python") == 0)
11775 n = python_enabled(FALSE);
11776#endif
11777#ifdef DYNAMIC_PERL
11778 else if (STRICMP(name, "perl") == 0)
11779 n = perl_enabled(FALSE);
11780#endif
11781#ifdef FEAT_GUI
11782 else if (STRICMP(name, "gui_running") == 0)
11783 n = (gui.in_use || gui.starting);
11784# ifdef FEAT_GUI_W32
11785 else if (STRICMP(name, "gui_win32s") == 0)
11786 n = gui_is_win32s();
11787# endif
11788# ifdef FEAT_BROWSE
11789 else if (STRICMP(name, "browse") == 0)
11790 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11791# endif
11792#endif
11793#ifdef FEAT_SYN_HL
11794 else if (STRICMP(name, "syntax_items") == 0)
11795 n = syntax_present(curbuf);
11796#endif
11797#if defined(WIN3264)
11798 else if (STRICMP(name, "win95") == 0)
11799 n = mch_windows95();
11800#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011801#ifdef FEAT_NETBEANS_INTG
11802 else if (STRICMP(name, "netbeans_enabled") == 0)
11803 n = usingNetbeans;
11804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 }
11806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808}
11809
11810/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011811 * "has_key()" function
11812 */
11813 static void
11814f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011815 typval_T *argvars;
11816 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011817{
11818 rettv->vval.v_number = 0;
11819 if (argvars[0].v_type != VAR_DICT)
11820 {
11821 EMSG(_(e_dictreq));
11822 return;
11823 }
11824 if (argvars[0].vval.v_dict == NULL)
11825 return;
11826
11827 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011828 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011829}
11830
11831/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011832 * "haslocaldir()" function
11833 */
11834/*ARGSUSED*/
11835 static void
11836f_haslocaldir(argvars, rettv)
11837 typval_T *argvars;
11838 typval_T *rettv;
11839{
11840 rettv->vval.v_number = (curwin->w_localdir != NULL);
11841}
11842
11843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 * "hasmapto()" function
11845 */
11846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 typval_T *argvars;
11849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850{
11851 char_u *name;
11852 char_u *mode;
11853 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011854 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011857 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 mode = (char_u *)"nvo";
11859 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011862 if (argvars[2].v_type != VAR_UNKNOWN)
11863 abbr = get_tv_number(&argvars[2]);
11864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865
Bram Moolenaar2c932302006-03-18 21:42:09 +000011866 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870}
11871
11872/*
11873 * "histadd()" function
11874 */
11875/*ARGSUSED*/
11876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011878 typval_T *argvars;
11879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880{
11881#ifdef FEAT_CMDHIST
11882 int histype;
11883 char_u *str;
11884 char_u buf[NUMBUFLEN];
11885#endif
11886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 if (check_restricted() || check_secure())
11889 return;
11890#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11892 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 if (histype >= 0)
11894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 if (*str != NUL)
11897 {
11898 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 return;
11901 }
11902 }
11903#endif
11904}
11905
11906/*
11907 * "histdel()" function
11908 */
11909/*ARGSUSED*/
11910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011912 typval_T *argvars;
11913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914{
11915#ifdef FEAT_CMDHIST
11916 int n;
11917 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011920 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11921 if (str == NULL)
11922 n = 0;
11923 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011925 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011926 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011929 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 else
11931 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011932 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 get_tv_string_buf(&argvars[1], buf));
11934 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011936 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937#endif
11938}
11939
11940/*
11941 * "histget()" function
11942 */
11943/*ARGSUSED*/
11944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011946 typval_T *argvars;
11947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948{
11949#ifdef FEAT_CMDHIST
11950 int type;
11951 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011952 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11955 if (str == NULL)
11956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 {
11959 type = get_histtype(str);
11960 if (argvars[1].v_type == VAR_UNKNOWN)
11961 idx = get_history_idx(type);
11962 else
11963 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11964 /* -1 on type error */
11965 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971}
11972
11973/*
11974 * "histnr()" function
11975 */
11976/*ARGSUSED*/
11977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011979 typval_T *argvars;
11980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981{
11982 int i;
11983
11984#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 char_u *history = get_tv_string_chk(&argvars[0]);
11986
11987 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 if (i >= HIST_CMD && i < HIST_COUNT)
11989 i = get_history_idx(i);
11990 else
11991#endif
11992 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994}
11995
11996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 * "highlightID(name)" function
11998 */
11999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 typval_T *argvars;
12002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012004 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005}
12006
12007/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012008 * "highlight_exists()" function
12009 */
12010 static void
12011f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012012 typval_T *argvars;
12013 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014{
12015 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12016}
12017
12018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 * "hostname()" function
12020 */
12021/*ARGSUSED*/
12022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012024 typval_T *argvars;
12025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026{
12027 char_u hostname[256];
12028
12029 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012030 rettv->v_type = VAR_STRING;
12031 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032}
12033
12034/*
12035 * iconv() function
12036 */
12037/*ARGSUSED*/
12038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012040 typval_T *argvars;
12041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042{
12043#ifdef FEAT_MBYTE
12044 char_u buf1[NUMBUFLEN];
12045 char_u buf2[NUMBUFLEN];
12046 char_u *from, *to, *str;
12047 vimconv_T vimconv;
12048#endif
12049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->v_type = VAR_STRING;
12051 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052
12053#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054 str = get_tv_string(&argvars[0]);
12055 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12056 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 vimconv.vc_type = CONV_NONE;
12058 convert_setup(&vimconv, from, to);
12059
12060 /* If the encodings are equal, no conversion needed. */
12061 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065
12066 convert_setup(&vimconv, NULL, NULL);
12067 vim_free(from);
12068 vim_free(to);
12069#endif
12070}
12071
12072/*
12073 * "indent()" function
12074 */
12075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012077 typval_T *argvars;
12078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079{
12080 linenr_T lnum;
12081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087}
12088
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012089/*
12090 * "index()" function
12091 */
12092 static void
12093f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012096{
Bram Moolenaar33570922005-01-25 22:26:29 +000012097 list_T *l;
12098 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012099 long idx = 0;
12100 int ic = FALSE;
12101
12102 rettv->vval.v_number = -1;
12103 if (argvars[0].v_type != VAR_LIST)
12104 {
12105 EMSG(_(e_listreq));
12106 return;
12107 }
12108 l = argvars[0].vval.v_list;
12109 if (l != NULL)
12110 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012111 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012112 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012114 int error = FALSE;
12115
Bram Moolenaar758711c2005-02-02 23:11:38 +000012116 /* Start at specified item. Use the cached index that list_find()
12117 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012118 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012119 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012120 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012121 ic = get_tv_number_chk(&argvars[3], &error);
12122 if (error)
12123 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012124 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012125
Bram Moolenaar758711c2005-02-02 23:11:38 +000012126 for ( ; item != NULL; item = item->li_next, ++idx)
12127 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012128 {
12129 rettv->vval.v_number = idx;
12130 break;
12131 }
12132 }
12133}
12134
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135static int inputsecret_flag = 0;
12136
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012137static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12138
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012140 * This function is used by f_input() and f_inputdialog() functions. The third
12141 * argument to f_input() specifies the type of completion to use at the
12142 * prompt. The third argument to f_inputdialog() specifies the value to return
12143 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 */
12145 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012146get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012147 typval_T *argvars;
12148 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012149 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012151 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 char_u *p = NULL;
12153 int c;
12154 char_u buf[NUMBUFLEN];
12155 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012156 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012157 int xp_type = EXPAND_NOTHING;
12158 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012161 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
12163#ifdef NO_CONSOLE_INPUT
12164 /* While starting up, there is no place to enter text. */
12165 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167#endif
12168
12169 cmd_silent = FALSE; /* Want to see the prompt. */
12170 if (prompt != NULL)
12171 {
12172 /* Only the part of the message after the last NL is considered as
12173 * prompt for the command line */
12174 p = vim_strrchr(prompt, '\n');
12175 if (p == NULL)
12176 p = prompt;
12177 else
12178 {
12179 ++p;
12180 c = *p;
12181 *p = NUL;
12182 msg_start();
12183 msg_clr_eos();
12184 msg_puts_attr(prompt, echo_attr);
12185 msg_didout = FALSE;
12186 msg_starthere();
12187 *p = c;
12188 }
12189 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012191 if (argvars[1].v_type != VAR_UNKNOWN)
12192 {
12193 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12194 if (defstr != NULL)
12195 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012197 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012198 {
12199 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012200 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012201 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012202
Bram Moolenaar4463f292005-09-25 22:20:24 +000012203 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012204
Bram Moolenaar4463f292005-09-25 22:20:24 +000012205 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12206 if (xp_name == NULL)
12207 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012208
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012209 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012210
Bram Moolenaar4463f292005-09-25 22:20:24 +000012211 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12212 &xp_arg) == FAIL)
12213 return;
12214 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012215 }
12216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012217 if (defstr != NULL)
12218 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012219 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12220 xp_type, xp_arg);
12221
12222 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012224 /* since the user typed this, no need to wait for return */
12225 need_wait_return = FALSE;
12226 msg_didout = FALSE;
12227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 cmd_silent = cmd_silent_save;
12229}
12230
12231/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012232 * "input()" function
12233 * Also handles inputsecret() when inputsecret is set.
12234 */
12235 static void
12236f_input(argvars, rettv)
12237 typval_T *argvars;
12238 typval_T *rettv;
12239{
12240 get_user_input(argvars, rettv, FALSE);
12241}
12242
12243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 * "inputdialog()" function
12245 */
12246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012247f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012248 typval_T *argvars;
12249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250{
12251#if defined(FEAT_GUI_TEXTDIALOG)
12252 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12253 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12254 {
12255 char_u *message;
12256 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012257 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012259 message = get_tv_string_chk(&argvars[0]);
12260 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012261 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012262 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 else
12264 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012265 if (message != NULL && defstr != NULL
12266 && do_dialog(VIM_QUESTION, NULL, message,
12267 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 else
12270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 if (message != NULL && defstr != NULL
12272 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012273 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274 rettv->vval.v_string = vim_strsave(
12275 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 }
12281 else
12282#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012283 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284}
12285
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012286/*
12287 * "inputlist()" function
12288 */
12289 static void
12290f_inputlist(argvars, rettv)
12291 typval_T *argvars;
12292 typval_T *rettv;
12293{
12294 listitem_T *li;
12295 int selected;
12296 int mouse_used;
12297
12298 rettv->vval.v_number = 0;
12299#ifdef NO_CONSOLE_INPUT
12300 /* While starting up, there is no place to enter text. */
12301 if (no_console_input())
12302 return;
12303#endif
12304 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12305 {
12306 EMSG2(_(e_listarg), "inputlist()");
12307 return;
12308 }
12309
12310 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012311 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012312 lines_left = Rows; /* avoid more prompt */
12313 msg_scroll = TRUE;
12314 msg_clr_eos();
12315
12316 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12317 {
12318 msg_puts(get_tv_string(&li->li_tv));
12319 msg_putchar('\n');
12320 }
12321
12322 /* Ask for choice. */
12323 selected = prompt_for_number(&mouse_used);
12324 if (mouse_used)
12325 selected -= lines_left;
12326
12327 rettv->vval.v_number = selected;
12328}
12329
12330
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12332
12333/*
12334 * "inputrestore()" function
12335 */
12336/*ARGSUSED*/
12337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012339 typval_T *argvars;
12340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341{
12342 if (ga_userinput.ga_len > 0)
12343 {
12344 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12346 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 }
12349 else if (p_verbose > 1)
12350 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012351 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012352 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 }
12354}
12355
12356/*
12357 * "inputsave()" function
12358 */
12359/*ARGSUSED*/
12360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012362 typval_T *argvars;
12363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012365 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 if (ga_grow(&ga_userinput, 1) == OK)
12367 {
12368 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12369 + ga_userinput.ga_len);
12370 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 }
12373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375}
12376
12377/*
12378 * "inputsecret()" function
12379 */
12380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012382 typval_T *argvars;
12383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384{
12385 ++cmdline_star;
12386 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 --cmdline_star;
12389 --inputsecret_flag;
12390}
12391
12392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012393 * "insert()" function
12394 */
12395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012397 typval_T *argvars;
12398 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012399{
12400 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012401 listitem_T *item;
12402 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012404
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012405 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012406 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012407 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012408 else if ((l = argvars[0].vval.v_list) != NULL
12409 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012410 {
12411 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412 before = get_tv_number_chk(&argvars[2], &error);
12413 if (error)
12414 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012415
Bram Moolenaar758711c2005-02-02 23:11:38 +000012416 if (before == l->lv_len)
12417 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012418 else
12419 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012420 item = list_find(l, before);
12421 if (item == NULL)
12422 {
12423 EMSGN(_(e_listidx), before);
12424 l = NULL;
12425 }
12426 }
12427 if (l != NULL)
12428 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012429 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012430 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012431 }
12432 }
12433}
12434
12435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 * "isdirectory()" function
12437 */
12438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012440 typval_T *argvars;
12441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444}
12445
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012446/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012447 * "islocked()" function
12448 */
12449 static void
12450f_islocked(argvars, rettv)
12451 typval_T *argvars;
12452 typval_T *rettv;
12453{
12454 lval_T lv;
12455 char_u *end;
12456 dictitem_T *di;
12457
12458 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012459 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12460 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012461 if (end != NULL && lv.ll_name != NULL)
12462 {
12463 if (*end != NUL)
12464 EMSG(_(e_trailing));
12465 else
12466 {
12467 if (lv.ll_tv == NULL)
12468 {
12469 if (check_changedtick(lv.ll_name))
12470 rettv->vval.v_number = 1; /* always locked */
12471 else
12472 {
12473 di = find_var(lv.ll_name, NULL);
12474 if (di != NULL)
12475 {
12476 /* Consider a variable locked when:
12477 * 1. the variable itself is locked
12478 * 2. the value of the variable is locked.
12479 * 3. the List or Dict value is locked.
12480 */
12481 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12482 || tv_islocked(&di->di_tv));
12483 }
12484 }
12485 }
12486 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012487 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012488 else if (lv.ll_newkey != NULL)
12489 EMSG2(_(e_dictkey), lv.ll_newkey);
12490 else if (lv.ll_list != NULL)
12491 /* List item. */
12492 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12493 else
12494 /* Dictionary item. */
12495 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12496 }
12497 }
12498
12499 clear_lval(&lv);
12500}
12501
Bram Moolenaar33570922005-01-25 22:26:29 +000012502static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012503
12504/*
12505 * Turn a dict into a list:
12506 * "what" == 0: list of keys
12507 * "what" == 1: list of values
12508 * "what" == 2: list of items
12509 */
12510 static void
12511dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 typval_T *argvars;
12513 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012514 int what;
12515{
Bram Moolenaar33570922005-01-25 22:26:29 +000012516 list_T *l2;
12517 dictitem_T *di;
12518 hashitem_T *hi;
12519 listitem_T *li;
12520 listitem_T *li2;
12521 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012522 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012523
12524 rettv->vval.v_number = 0;
12525 if (argvars[0].v_type != VAR_DICT)
12526 {
12527 EMSG(_(e_dictreq));
12528 return;
12529 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012530 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012531 return;
12532
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012533 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012534 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012535
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012536 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012537 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012539 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012540 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012541 --todo;
12542 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012543
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012544 li = listitem_alloc();
12545 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012546 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012547 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012548
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012549 if (what == 0)
12550 {
12551 /* keys() */
12552 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012553 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012554 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12555 }
12556 else if (what == 1)
12557 {
12558 /* values() */
12559 copy_tv(&di->di_tv, &li->li_tv);
12560 }
12561 else
12562 {
12563 /* items() */
12564 l2 = list_alloc();
12565 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012566 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012567 li->li_tv.vval.v_list = l2;
12568 if (l2 == NULL)
12569 break;
12570 ++l2->lv_refcount;
12571
12572 li2 = listitem_alloc();
12573 if (li2 == NULL)
12574 break;
12575 list_append(l2, li2);
12576 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012577 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012578 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12579
12580 li2 = listitem_alloc();
12581 if (li2 == NULL)
12582 break;
12583 list_append(l2, li2);
12584 copy_tv(&di->di_tv, &li2->li_tv);
12585 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012586 }
12587 }
12588}
12589
12590/*
12591 * "items(dict)" function
12592 */
12593 static void
12594f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 typval_T *argvars;
12596 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012597{
12598 dict_list(argvars, rettv, 2);
12599}
12600
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012602 * "join()" function
12603 */
12604 static void
12605f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012606 typval_T *argvars;
12607 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012608{
12609 garray_T ga;
12610 char_u *sep;
12611
12612 rettv->vval.v_number = 0;
12613 if (argvars[0].v_type != VAR_LIST)
12614 {
12615 EMSG(_(e_listreq));
12616 return;
12617 }
12618 if (argvars[0].vval.v_list == NULL)
12619 return;
12620 if (argvars[1].v_type == VAR_UNKNOWN)
12621 sep = (char_u *)" ";
12622 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012624
12625 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012626
12627 if (sep != NULL)
12628 {
12629 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012630 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 ga_append(&ga, NUL);
12632 rettv->vval.v_string = (char_u *)ga.ga_data;
12633 }
12634 else
12635 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012636}
12637
12638/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012639 * "keys()" function
12640 */
12641 static void
12642f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012643 typval_T *argvars;
12644 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012645{
12646 dict_list(argvars, rettv, 0);
12647}
12648
12649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 * "last_buffer_nr()" function.
12651 */
12652/*ARGSUSED*/
12653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012655 typval_T *argvars;
12656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657{
12658 int n = 0;
12659 buf_T *buf;
12660
12661 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12662 if (n < buf->b_fnum)
12663 n = buf->b_fnum;
12664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012666}
12667
12668/*
12669 * "len()" function
12670 */
12671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012672f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012673 typval_T *argvars;
12674 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012675{
12676 switch (argvars[0].v_type)
12677 {
12678 case VAR_STRING:
12679 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 rettv->vval.v_number = (varnumber_T)STRLEN(
12681 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012682 break;
12683 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012686 case VAR_DICT:
12687 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12688 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012689 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012690 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012691 break;
12692 }
12693}
12694
Bram Moolenaar33570922005-01-25 22:26:29 +000012695static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012696
12697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 typval_T *argvars;
12700 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 int type;
12702{
12703#ifdef FEAT_LIBCALL
12704 char_u *string_in;
12705 char_u **string_result;
12706 int nr_result;
12707#endif
12708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012709 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012714
12715 if (check_restricted() || check_secure())
12716 return;
12717
12718#ifdef FEAT_LIBCALL
12719 /* The first two args must be strings, otherwise its meaningless */
12720 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12721 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012722 string_in = NULL;
12723 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012724 string_in = argvars[2].vval.v_string;
12725 if (type == VAR_NUMBER)
12726 string_result = NULL;
12727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012729 if (mch_libcall(argvars[0].vval.v_string,
12730 argvars[1].vval.v_string,
12731 string_in,
12732 argvars[2].vval.v_number,
12733 string_result,
12734 &nr_result) == OK
12735 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012737 }
12738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739}
12740
12741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012742 * "libcall()" function
12743 */
12744 static void
12745f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012746 typval_T *argvars;
12747 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012748{
12749 libcall_common(argvars, rettv, VAR_STRING);
12750}
12751
12752/*
12753 * "libcallnr()" function
12754 */
12755 static void
12756f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012757 typval_T *argvars;
12758 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012759{
12760 libcall_common(argvars, rettv, VAR_NUMBER);
12761}
12762
12763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 * "line(string)" function
12765 */
12766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012768 typval_T *argvars;
12769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770{
12771 linenr_T lnum = 0;
12772 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012773 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012775 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 if (fp != NULL)
12777 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779}
12780
12781/*
12782 * "line2byte(lnum)" function
12783 */
12784/*ARGSUSED*/
12785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012787 typval_T *argvars;
12788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
12790#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792#else
12793 linenr_T lnum;
12794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12800 if (rettv->vval.v_number >= 0)
12801 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802#endif
12803}
12804
12805/*
12806 * "lispindent(lnum)" function
12807 */
12808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012810 typval_T *argvars;
12811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812{
12813#ifdef FEAT_LISP
12814 pos_T pos;
12815 linenr_T lnum;
12816
12817 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12820 {
12821 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 curwin->w_cursor = pos;
12824 }
12825 else
12826#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828}
12829
12830/*
12831 * "localtime()" function
12832 */
12833/*ARGSUSED*/
12834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012836 typval_T *argvars;
12837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840}
12841
Bram Moolenaar33570922005-01-25 22:26:29 +000012842static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
12844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 int exact;
12849{
12850 char_u *keys;
12851 char_u *which;
12852 char_u buf[NUMBUFLEN];
12853 char_u *keys_buf = NULL;
12854 char_u *rhs;
12855 int mode;
12856 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012857 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858
12859 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 rettv->v_type = VAR_STRING;
12861 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 if (*keys == NUL)
12865 return;
12866
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012867 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012869 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012870 if (argvars[2].v_type != VAR_UNKNOWN)
12871 abbr = get_tv_number(&argvars[2]);
12872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 else
12874 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012875 if (which == NULL)
12876 return;
12877
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 mode = get_map_mode(&which, 0);
12879
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012880 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012881 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 vim_free(keys_buf);
12883 if (rhs != NULL)
12884 {
12885 ga_init(&ga);
12886 ga.ga_itemsize = 1;
12887 ga.ga_growsize = 40;
12888
12889 while (*rhs != NUL)
12890 ga_concat(&ga, str2special(&rhs, FALSE));
12891
12892 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 }
12895}
12896
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012897#ifdef FEAT_FLOAT
12898/*
12899 * "log10()" function
12900 */
12901 static void
12902f_log10(argvars, rettv)
12903 typval_T *argvars;
12904 typval_T *rettv;
12905{
12906 float_T f;
12907
12908 rettv->v_type = VAR_FLOAT;
12909 if (get_float_arg(argvars, &f) == OK)
12910 rettv->vval.v_float = log10(f);
12911 else
12912 rettv->vval.v_float = 0.0;
12913}
12914#endif
12915
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012917 * "map()" function
12918 */
12919 static void
12920f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012921 typval_T *argvars;
12922 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012923{
12924 filter_map(argvars, rettv, TRUE);
12925}
12926
12927/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012928 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 */
12930 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012931f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012935 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936}
12937
12938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012939 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 */
12941 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012942f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 typval_T *argvars;
12944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012946 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947}
12948
Bram Moolenaar33570922005-01-25 22:26:29 +000012949static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950
12951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012953 typval_T *argvars;
12954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 int type;
12956{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012957 char_u *str = NULL;
12958 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 char_u *pat;
12960 regmatch_T regmatch;
12961 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012962 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 char_u *save_cpo;
12964 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012965 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012966 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012967 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012968 list_T *l = NULL;
12969 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012970 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012971 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972
12973 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12974 save_cpo = p_cpo;
12975 p_cpo = (char_u *)"";
12976
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012977 rettv->vval.v_number = -1;
12978 if (type == 3)
12979 {
12980 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012981 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012982 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012983 }
12984 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->v_type = VAR_STRING;
12987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012990 if (argvars[0].v_type == VAR_LIST)
12991 {
12992 if ((l = argvars[0].vval.v_list) == NULL)
12993 goto theend;
12994 li = l->lv_first;
12995 }
12996 else
12997 expr = str = get_tv_string(&argvars[0]);
12998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012999 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13000 if (pat == NULL)
13001 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013002
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013005 int error = FALSE;
13006
13007 start = get_tv_number_chk(&argvars[2], &error);
13008 if (error)
13009 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013010 if (l != NULL)
13011 {
13012 li = list_find(l, start);
13013 if (li == NULL)
13014 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013015 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013016 }
13017 else
13018 {
13019 if (start < 0)
13020 start = 0;
13021 if (start > (long)STRLEN(str))
13022 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013023 /* When "count" argument is there ignore matches before "start",
13024 * otherwise skip part of the string. Differs when pattern is "^"
13025 * or "\<". */
13026 if (argvars[3].v_type != VAR_UNKNOWN)
13027 startcol = start;
13028 else
13029 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013030 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013031
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013032 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013033 nth = get_tv_number_chk(&argvars[3], &error);
13034 if (error)
13035 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 }
13037
13038 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13039 if (regmatch.regprog != NULL)
13040 {
13041 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013042
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013043 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013044 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013045 if (l != NULL)
13046 {
13047 if (li == NULL)
13048 {
13049 match = FALSE;
13050 break;
13051 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013052 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013053 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013054 if (str == NULL)
13055 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013056 }
13057
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013058 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013059
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013061 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013062 if (l == NULL && !match)
13063 break;
13064
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013065 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013066 if (l != NULL)
13067 {
13068 li = li->li_next;
13069 ++idx;
13070 }
13071 else
13072 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013073#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013074 startcol = (colnr_T)(regmatch.startp[0]
13075 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013076#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013077 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013078#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013079 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013080 }
13081
13082 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013084 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013085 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013086 int i;
13087
13088 /* return list with matched string and submatches */
13089 for (i = 0; i < NSUBEXP; ++i)
13090 {
13091 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013092 {
13093 if (list_append_string(rettv->vval.v_list,
13094 (char_u *)"", 0) == FAIL)
13095 break;
13096 }
13097 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013098 regmatch.startp[i],
13099 (int)(regmatch.endp[i] - regmatch.startp[i]))
13100 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013101 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013102 }
13103 }
13104 else if (type == 2)
13105 {
13106 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013107 if (l != NULL)
13108 copy_tv(&li->li_tv, rettv);
13109 else
13110 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013112 }
13113 else if (l != NULL)
13114 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 else
13116 {
13117 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 (varnumber_T)(regmatch.startp[0] - str);
13120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013123 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 }
13125 }
13126 vim_free(regmatch.regprog);
13127 }
13128
13129theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013130 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 p_cpo = save_cpo;
13132}
13133
13134/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135 * "match()" function
13136 */
13137 static void
13138f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013139 typval_T *argvars;
13140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013141{
13142 find_some_match(argvars, rettv, 1);
13143}
13144
13145/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013146 * "matchadd()" function
13147 */
13148 static void
13149f_matchadd(argvars, rettv)
13150 typval_T *argvars;
13151 typval_T *rettv;
13152{
13153#ifdef FEAT_SEARCH_EXTRA
13154 char_u buf[NUMBUFLEN];
13155 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13156 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13157 int prio = 10; /* default priority */
13158 int id = -1;
13159 int error = FALSE;
13160
13161 rettv->vval.v_number = -1;
13162
13163 if (grp == NULL || pat == NULL)
13164 return;
13165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013166 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013167 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013168 if (argvars[3].v_type != VAR_UNKNOWN)
13169 id = get_tv_number_chk(&argvars[3], &error);
13170 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013171 if (error == TRUE)
13172 return;
13173 if (id >= 1 && id <= 3)
13174 {
13175 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13176 return;
13177 }
13178
13179 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13180#endif
13181}
13182
13183/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013184 * "matcharg()" function
13185 */
13186 static void
13187f_matcharg(argvars, rettv)
13188 typval_T *argvars;
13189 typval_T *rettv;
13190{
13191 if (rettv_list_alloc(rettv) == OK)
13192 {
13193#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013194 int id = get_tv_number(&argvars[0]);
13195 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013196
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013197 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013198 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013199 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13200 {
13201 list_append_string(rettv->vval.v_list,
13202 syn_id2name(m->hlg_id), -1);
13203 list_append_string(rettv->vval.v_list, m->pattern, -1);
13204 }
13205 else
13206 {
13207 list_append_string(rettv->vval.v_list, NUL, -1);
13208 list_append_string(rettv->vval.v_list, NUL, -1);
13209 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013210 }
13211#endif
13212 }
13213}
13214
13215/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013216 * "matchdelete()" function
13217 */
13218 static void
13219f_matchdelete(argvars, rettv)
13220 typval_T *argvars;
13221 typval_T *rettv;
13222{
13223#ifdef FEAT_SEARCH_EXTRA
13224 rettv->vval.v_number = match_delete(curwin,
13225 (int)get_tv_number(&argvars[0]), TRUE);
13226#endif
13227}
13228
13229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 * "matchend()" function
13231 */
13232 static void
13233f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013234 typval_T *argvars;
13235 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013236{
13237 find_some_match(argvars, rettv, 0);
13238}
13239
13240/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013241 * "matchlist()" function
13242 */
13243 static void
13244f_matchlist(argvars, rettv)
13245 typval_T *argvars;
13246 typval_T *rettv;
13247{
13248 find_some_match(argvars, rettv, 3);
13249}
13250
13251/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013252 * "matchstr()" function
13253 */
13254 static void
13255f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013256 typval_T *argvars;
13257 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013258{
13259 find_some_match(argvars, rettv, 2);
13260}
13261
Bram Moolenaar33570922005-01-25 22:26:29 +000013262static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013263
13264 static void
13265max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013266 typval_T *argvars;
13267 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013268 int domax;
13269{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013270 long n = 0;
13271 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013273
13274 if (argvars[0].v_type == VAR_LIST)
13275 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013276 list_T *l;
13277 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013278
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013279 l = argvars[0].vval.v_list;
13280 if (l != NULL)
13281 {
13282 li = l->lv_first;
13283 if (li != NULL)
13284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013286 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013287 {
13288 li = li->li_next;
13289 if (li == NULL)
13290 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013292 if (domax ? i > n : i < n)
13293 n = i;
13294 }
13295 }
13296 }
13297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013298 else if (argvars[0].v_type == VAR_DICT)
13299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013301 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013302 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013303 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013304
13305 d = argvars[0].vval.v_dict;
13306 if (d != NULL)
13307 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013308 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013309 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013311 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013313 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013315 if (first)
13316 {
13317 n = i;
13318 first = FALSE;
13319 }
13320 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013321 n = i;
13322 }
13323 }
13324 }
13325 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013326 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013327 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013328 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013329}
13330
13331/*
13332 * "max()" function
13333 */
13334 static void
13335f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013336 typval_T *argvars;
13337 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013338{
13339 max_min(argvars, rettv, TRUE);
13340}
13341
13342/*
13343 * "min()" function
13344 */
13345 static void
13346f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 typval_T *argvars;
13348 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013349{
13350 max_min(argvars, rettv, FALSE);
13351}
13352
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013353static int mkdir_recurse __ARGS((char_u *dir, int prot));
13354
13355/*
13356 * Create the directory in which "dir" is located, and higher levels when
13357 * needed.
13358 */
13359 static int
13360mkdir_recurse(dir, prot)
13361 char_u *dir;
13362 int prot;
13363{
13364 char_u *p;
13365 char_u *updir;
13366 int r = FAIL;
13367
13368 /* Get end of directory name in "dir".
13369 * We're done when it's "/" or "c:/". */
13370 p = gettail_sep(dir);
13371 if (p <= get_past_head(dir))
13372 return OK;
13373
13374 /* If the directory exists we're done. Otherwise: create it.*/
13375 updir = vim_strnsave(dir, (int)(p - dir));
13376 if (updir == NULL)
13377 return FAIL;
13378 if (mch_isdir(updir))
13379 r = OK;
13380 else if (mkdir_recurse(updir, prot) == OK)
13381 r = vim_mkdir_emsg(updir, prot);
13382 vim_free(updir);
13383 return r;
13384}
13385
13386#ifdef vim_mkdir
13387/*
13388 * "mkdir()" function
13389 */
13390 static void
13391f_mkdir(argvars, rettv)
13392 typval_T *argvars;
13393 typval_T *rettv;
13394{
13395 char_u *dir;
13396 char_u buf[NUMBUFLEN];
13397 int prot = 0755;
13398
13399 rettv->vval.v_number = FAIL;
13400 if (check_restricted() || check_secure())
13401 return;
13402
13403 dir = get_tv_string_buf(&argvars[0], buf);
13404 if (argvars[1].v_type != VAR_UNKNOWN)
13405 {
13406 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 prot = get_tv_number_chk(&argvars[2], NULL);
13408 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013409 mkdir_recurse(dir, prot);
13410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013412}
13413#endif
13414
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 * "mode()" function
13417 */
13418/*ARGSUSED*/
13419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013420f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 typval_T *argvars;
13422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013424 char_u buf[3];
13425
13426 buf[1] = NUL;
13427 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428
13429#ifdef FEAT_VISUAL
13430 if (VIsual_active)
13431 {
13432 if (VIsual_select)
13433 buf[0] = VIsual_mode + 's' - 'v';
13434 else
13435 buf[0] = VIsual_mode;
13436 }
13437 else
13438#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013439 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13440 || State == CONFIRM)
13441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013443 if (State == ASKMORE)
13444 buf[1] = 'm';
13445 else if (State == CONFIRM)
13446 buf[1] = '?';
13447 }
13448 else if (State == EXTERNCMD)
13449 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 else if (State & INSERT)
13451 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013452#ifdef FEAT_VREPLACE
13453 if (State & VREPLACE_FLAG)
13454 {
13455 buf[0] = 'R';
13456 buf[1] = 'v';
13457 }
13458 else
13459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 if (State & REPLACE_FLAG)
13461 buf[0] = 'R';
13462 else
13463 buf[0] = 'i';
13464 }
13465 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013468 if (exmode_active)
13469 buf[1] = 'v';
13470 }
13471 else if (exmode_active)
13472 {
13473 buf[0] = 'c';
13474 buf[1] = 'e';
13475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013479 if (finish_op)
13480 buf[1] = 'o';
13481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013483 /* A zero number or empty string argument: return only major mode. */
13484 if (!(argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number != 0)
13485 && !(argvars[0].v_type == VAR_STRING
13486 && *get_tv_string(&argvars[0]) != NUL))
13487 buf[1] = NUL;
13488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv->vval.v_string = vim_strsave(buf);
13490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491}
13492
13493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013494 * "nextnonblank()" function
13495 */
13496 static void
13497f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 typval_T *argvars;
13499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013500{
13501 linenr_T lnum;
13502
13503 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013506 {
13507 lnum = 0;
13508 break;
13509 }
13510 if (*skipwhite(ml_get(lnum)) != NUL)
13511 break;
13512 }
13513 rettv->vval.v_number = lnum;
13514}
13515
13516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 * "nr2char()" function
13518 */
13519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013521 typval_T *argvars;
13522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523{
13524 char_u buf[NUMBUFLEN];
13525
13526#ifdef FEAT_MBYTE
13527 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 else
13530#endif
13531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 buf[1] = NUL;
13534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->v_type = VAR_STRING;
13536 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013537}
13538
13539/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013540 * "pathshorten()" function
13541 */
13542 static void
13543f_pathshorten(argvars, rettv)
13544 typval_T *argvars;
13545 typval_T *rettv;
13546{
13547 char_u *p;
13548
13549 rettv->v_type = VAR_STRING;
13550 p = get_tv_string_chk(&argvars[0]);
13551 if (p == NULL)
13552 rettv->vval.v_string = NULL;
13553 else
13554 {
13555 p = vim_strsave(p);
13556 rettv->vval.v_string = p;
13557 if (p != NULL)
13558 shorten_dir(p);
13559 }
13560}
13561
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013562#ifdef FEAT_FLOAT
13563/*
13564 * "pow()" function
13565 */
13566 static void
13567f_pow(argvars, rettv)
13568 typval_T *argvars;
13569 typval_T *rettv;
13570{
13571 float_T fx, fy;
13572
13573 rettv->v_type = VAR_FLOAT;
13574 if (get_float_arg(argvars, &fx) == OK
13575 && get_float_arg(&argvars[1], &fy) == OK)
13576 rettv->vval.v_float = pow(fx, fy);
13577 else
13578 rettv->vval.v_float = 0.0;
13579}
13580#endif
13581
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013582/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013583 * "prevnonblank()" function
13584 */
13585 static void
13586f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013587 typval_T *argvars;
13588 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013589{
13590 linenr_T lnum;
13591
13592 lnum = get_tv_lnum(argvars);
13593 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13594 lnum = 0;
13595 else
13596 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13597 --lnum;
13598 rettv->vval.v_number = lnum;
13599}
13600
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013601#ifdef HAVE_STDARG_H
13602/* This dummy va_list is here because:
13603 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13604 * - locally in the function results in a "used before set" warning
13605 * - using va_start() to initialize it gives "function with fixed args" error */
13606static va_list ap;
13607#endif
13608
Bram Moolenaar8c711452005-01-14 21:53:12 +000013609/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013610 * "printf()" function
13611 */
13612 static void
13613f_printf(argvars, rettv)
13614 typval_T *argvars;
13615 typval_T *rettv;
13616{
13617 rettv->v_type = VAR_STRING;
13618 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013619#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013620 {
13621 char_u buf[NUMBUFLEN];
13622 int len;
13623 char_u *s;
13624 int saved_did_emsg = did_emsg;
13625 char *fmt;
13626
13627 /* Get the required length, allocate the buffer and do it for real. */
13628 did_emsg = FALSE;
13629 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013630 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013631 if (!did_emsg)
13632 {
13633 s = alloc(len + 1);
13634 if (s != NULL)
13635 {
13636 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013637 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013638 }
13639 }
13640 did_emsg |= saved_did_emsg;
13641 }
13642#endif
13643}
13644
13645/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013646 * "pumvisible()" function
13647 */
13648/*ARGSUSED*/
13649 static void
13650f_pumvisible(argvars, rettv)
13651 typval_T *argvars;
13652 typval_T *rettv;
13653{
13654 rettv->vval.v_number = 0;
13655#ifdef FEAT_INS_EXPAND
13656 if (pum_visible())
13657 rettv->vval.v_number = 1;
13658#endif
13659}
13660
13661/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013662 * "range()" function
13663 */
13664 static void
13665f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013666 typval_T *argvars;
13667 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013668{
13669 long start;
13670 long end;
13671 long stride = 1;
13672 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013673 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013675 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013676 if (argvars[1].v_type == VAR_UNKNOWN)
13677 {
13678 end = start - 1;
13679 start = 0;
13680 }
13681 else
13682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013684 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013686 }
13687
13688 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 if (error)
13690 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013691 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013692 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013693 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013694 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013695 else
13696 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013697 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013698 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013699 if (list_append_number(rettv->vval.v_list,
13700 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013701 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013702 }
13703}
13704
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013705/*
13706 * "readfile()" function
13707 */
13708 static void
13709f_readfile(argvars, rettv)
13710 typval_T *argvars;
13711 typval_T *rettv;
13712{
13713 int binary = FALSE;
13714 char_u *fname;
13715 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013716 listitem_T *li;
13717#define FREAD_SIZE 200 /* optimized for text lines */
13718 char_u buf[FREAD_SIZE];
13719 int readlen; /* size of last fread() */
13720 int buflen; /* nr of valid chars in buf[] */
13721 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13722 int tolist; /* first byte in buf[] still to be put in list */
13723 int chop; /* how many CR to chop off */
13724 char_u *prev = NULL; /* previously read bytes, if any */
13725 int prevlen = 0; /* length of "prev" if not NULL */
13726 char_u *s;
13727 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013728 long maxline = MAXLNUM;
13729 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013730
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013731 if (argvars[1].v_type != VAR_UNKNOWN)
13732 {
13733 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13734 binary = TRUE;
13735 if (argvars[2].v_type != VAR_UNKNOWN)
13736 maxline = get_tv_number(&argvars[2]);
13737 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013738
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013739 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013740 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013741
13742 /* Always open the file in binary mode, library functions have a mind of
13743 * their own about CR-LF conversion. */
13744 fname = get_tv_string(&argvars[0]);
13745 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13746 {
13747 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13748 return;
13749 }
13750
13751 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013752 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013753 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013754 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013755 buflen = filtd + readlen;
13756 tolist = 0;
13757 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13758 {
13759 if (buf[filtd] == '\n' || readlen <= 0)
13760 {
13761 /* Only when in binary mode add an empty list item when the
13762 * last line ends in a '\n'. */
13763 if (!binary && readlen == 0 && filtd == 0)
13764 break;
13765
13766 /* Found end-of-line or end-of-file: add a text line to the
13767 * list. */
13768 chop = 0;
13769 if (!binary)
13770 while (filtd - chop - 1 >= tolist
13771 && buf[filtd - chop - 1] == '\r')
13772 ++chop;
13773 len = filtd - tolist - chop;
13774 if (prev == NULL)
13775 s = vim_strnsave(buf + tolist, len);
13776 else
13777 {
13778 s = alloc((unsigned)(prevlen + len + 1));
13779 if (s != NULL)
13780 {
13781 mch_memmove(s, prev, prevlen);
13782 vim_free(prev);
13783 prev = NULL;
13784 mch_memmove(s + prevlen, buf + tolist, len);
13785 s[prevlen + len] = NUL;
13786 }
13787 }
13788 tolist = filtd + 1;
13789
13790 li = listitem_alloc();
13791 if (li == NULL)
13792 {
13793 vim_free(s);
13794 break;
13795 }
13796 li->li_tv.v_type = VAR_STRING;
13797 li->li_tv.v_lock = 0;
13798 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013799 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013800
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013801 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013802 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013803 if (readlen <= 0)
13804 break;
13805 }
13806 else if (buf[filtd] == NUL)
13807 buf[filtd] = '\n';
13808 }
13809 if (readlen <= 0)
13810 break;
13811
13812 if (tolist == 0)
13813 {
13814 /* "buf" is full, need to move text to an allocated buffer */
13815 if (prev == NULL)
13816 {
13817 prev = vim_strnsave(buf, buflen);
13818 prevlen = buflen;
13819 }
13820 else
13821 {
13822 s = alloc((unsigned)(prevlen + buflen));
13823 if (s != NULL)
13824 {
13825 mch_memmove(s, prev, prevlen);
13826 mch_memmove(s + prevlen, buf, buflen);
13827 vim_free(prev);
13828 prev = s;
13829 prevlen += buflen;
13830 }
13831 }
13832 filtd = 0;
13833 }
13834 else
13835 {
13836 mch_memmove(buf, buf + tolist, buflen - tolist);
13837 filtd -= tolist;
13838 }
13839 }
13840
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013841 /*
13842 * For a negative line count use only the lines at the end of the file,
13843 * free the rest.
13844 */
13845 if (maxline < 0)
13846 while (cnt > -maxline)
13847 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013848 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013849 --cnt;
13850 }
13851
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013852 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013853 fclose(fd);
13854}
13855
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013856#if defined(FEAT_RELTIME)
13857static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13858
13859/*
13860 * Convert a List to proftime_T.
13861 * Return FAIL when there is something wrong.
13862 */
13863 static int
13864list2proftime(arg, tm)
13865 typval_T *arg;
13866 proftime_T *tm;
13867{
13868 long n1, n2;
13869 int error = FALSE;
13870
13871 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13872 || arg->vval.v_list->lv_len != 2)
13873 return FAIL;
13874 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13875 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13876# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013877 tm->HighPart = n1;
13878 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013879# else
13880 tm->tv_sec = n1;
13881 tm->tv_usec = n2;
13882# endif
13883 return error ? FAIL : OK;
13884}
13885#endif /* FEAT_RELTIME */
13886
13887/*
13888 * "reltime()" function
13889 */
13890 static void
13891f_reltime(argvars, rettv)
13892 typval_T *argvars;
13893 typval_T *rettv;
13894{
13895#ifdef FEAT_RELTIME
13896 proftime_T res;
13897 proftime_T start;
13898
13899 if (argvars[0].v_type == VAR_UNKNOWN)
13900 {
13901 /* No arguments: get current time. */
13902 profile_start(&res);
13903 }
13904 else if (argvars[1].v_type == VAR_UNKNOWN)
13905 {
13906 if (list2proftime(&argvars[0], &res) == FAIL)
13907 return;
13908 profile_end(&res);
13909 }
13910 else
13911 {
13912 /* Two arguments: compute the difference. */
13913 if (list2proftime(&argvars[0], &start) == FAIL
13914 || list2proftime(&argvars[1], &res) == FAIL)
13915 return;
13916 profile_sub(&res, &start);
13917 }
13918
13919 if (rettv_list_alloc(rettv) == OK)
13920 {
13921 long n1, n2;
13922
13923# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013924 n1 = res.HighPart;
13925 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013926# else
13927 n1 = res.tv_sec;
13928 n2 = res.tv_usec;
13929# endif
13930 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13931 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13932 }
13933#endif
13934}
13935
13936/*
13937 * "reltimestr()" function
13938 */
13939 static void
13940f_reltimestr(argvars, rettv)
13941 typval_T *argvars;
13942 typval_T *rettv;
13943{
13944#ifdef FEAT_RELTIME
13945 proftime_T tm;
13946#endif
13947
13948 rettv->v_type = VAR_STRING;
13949 rettv->vval.v_string = NULL;
13950#ifdef FEAT_RELTIME
13951 if (list2proftime(&argvars[0], &tm) == OK)
13952 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13953#endif
13954}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013955
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13957static void make_connection __ARGS((void));
13958static int check_connection __ARGS((void));
13959
13960 static void
13961make_connection()
13962{
13963 if (X_DISPLAY == NULL
13964# ifdef FEAT_GUI
13965 && !gui.in_use
13966# endif
13967 )
13968 {
13969 x_force_connect = TRUE;
13970 setup_term_clip();
13971 x_force_connect = FALSE;
13972 }
13973}
13974
13975 static int
13976check_connection()
13977{
13978 make_connection();
13979 if (X_DISPLAY == NULL)
13980 {
13981 EMSG(_("E240: No connection to Vim server"));
13982 return FAIL;
13983 }
13984 return OK;
13985}
13986#endif
13987
13988#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013989static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013990
13991 static void
13992remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013993 typval_T *argvars;
13994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013995 int expr;
13996{
13997 char_u *server_name;
13998 char_u *keys;
13999 char_u *r = NULL;
14000 char_u buf[NUMBUFLEN];
14001# ifdef WIN32
14002 HWND w;
14003# else
14004 Window w;
14005# endif
14006
14007 if (check_restricted() || check_secure())
14008 return;
14009
14010# ifdef FEAT_X11
14011 if (check_connection() == FAIL)
14012 return;
14013# endif
14014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015 server_name = get_tv_string_chk(&argvars[0]);
14016 if (server_name == NULL)
14017 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014018 keys = get_tv_string_buf(&argvars[1], buf);
14019# ifdef WIN32
14020 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14021# else
14022 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14023 < 0)
14024# endif
14025 {
14026 if (r != NULL)
14027 EMSG(r); /* sending worked but evaluation failed */
14028 else
14029 EMSG2(_("E241: Unable to send to %s"), server_name);
14030 return;
14031 }
14032
14033 rettv->vval.v_string = r;
14034
14035 if (argvars[2].v_type != VAR_UNKNOWN)
14036 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014037 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014038 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014039 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014040
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014041 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 v.di_tv.v_type = VAR_STRING;
14043 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 idvar = get_tv_string_chk(&argvars[2]);
14045 if (idvar != NULL)
14046 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014047 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014048 }
14049}
14050#endif
14051
14052/*
14053 * "remote_expr()" function
14054 */
14055/*ARGSUSED*/
14056 static void
14057f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 typval_T *argvars;
14059 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060{
14061 rettv->v_type = VAR_STRING;
14062 rettv->vval.v_string = NULL;
14063#ifdef FEAT_CLIENTSERVER
14064 remote_common(argvars, rettv, TRUE);
14065#endif
14066}
14067
14068/*
14069 * "remote_foreground()" function
14070 */
14071/*ARGSUSED*/
14072 static void
14073f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 typval_T *argvars;
14075 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076{
14077 rettv->vval.v_number = 0;
14078#ifdef FEAT_CLIENTSERVER
14079# ifdef WIN32
14080 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014081 {
14082 char_u *server_name = get_tv_string_chk(&argvars[0]);
14083
14084 if (server_name != NULL)
14085 serverForeground(server_name);
14086 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014087# else
14088 /* Send a foreground() expression to the server. */
14089 argvars[1].v_type = VAR_STRING;
14090 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14091 argvars[2].v_type = VAR_UNKNOWN;
14092 remote_common(argvars, rettv, TRUE);
14093 vim_free(argvars[1].vval.v_string);
14094# endif
14095#endif
14096}
14097
14098/*ARGSUSED*/
14099 static void
14100f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 typval_T *argvars;
14102 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103{
14104#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014106 char_u *s = NULL;
14107# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014108 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014109# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014110 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111
14112 if (check_restricted() || check_secure())
14113 {
14114 rettv->vval.v_number = -1;
14115 return;
14116 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014117 serverid = get_tv_string_chk(&argvars[0]);
14118 if (serverid == NULL)
14119 {
14120 rettv->vval.v_number = -1;
14121 return; /* type error; errmsg already given */
14122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014124 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014125 if (n == 0)
14126 rettv->vval.v_number = -1;
14127 else
14128 {
14129 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14130 rettv->vval.v_number = (s != NULL);
14131 }
14132# else
14133 rettv->vval.v_number = 0;
14134 if (check_connection() == FAIL)
14135 return;
14136
14137 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014138 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014139# endif
14140
14141 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 char_u *retvar;
14144
Bram Moolenaar33570922005-01-25 22:26:29 +000014145 v.di_tv.v_type = VAR_STRING;
14146 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 retvar = get_tv_string_chk(&argvars[1]);
14148 if (retvar != NULL)
14149 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014150 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014151 }
14152#else
14153 rettv->vval.v_number = -1;
14154#endif
14155}
14156
14157/*ARGSUSED*/
14158 static void
14159f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 typval_T *argvars;
14161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162{
14163 char_u *r = NULL;
14164
14165#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 char_u *serverid = get_tv_string_chk(&argvars[0]);
14167
14168 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014169 {
14170# ifdef WIN32
14171 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014172 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014174 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014175 if (n != 0)
14176 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14177 if (r == NULL)
14178# else
14179 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014181# endif
14182 EMSG(_("E277: Unable to read a server reply"));
14183 }
14184#endif
14185 rettv->v_type = VAR_STRING;
14186 rettv->vval.v_string = r;
14187}
14188
14189/*
14190 * "remote_send()" function
14191 */
14192/*ARGSUSED*/
14193 static void
14194f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014195 typval_T *argvars;
14196 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014197{
14198 rettv->v_type = VAR_STRING;
14199 rettv->vval.v_string = NULL;
14200#ifdef FEAT_CLIENTSERVER
14201 remote_common(argvars, rettv, FALSE);
14202#endif
14203}
14204
14205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014206 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014207 */
14208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014209f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014210 typval_T *argvars;
14211 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014212{
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 list_T *l;
14214 listitem_T *item, *item2;
14215 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014216 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014217 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014218 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 dict_T *d;
14220 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014221
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014222 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014223 if (argvars[0].v_type == VAR_DICT)
14224 {
14225 if (argvars[2].v_type != VAR_UNKNOWN)
14226 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014227 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014228 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 key = get_tv_string_chk(&argvars[1]);
14231 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 di = dict_find(d, key, -1);
14234 if (di == NULL)
14235 EMSG2(_(e_dictkey), key);
14236 else
14237 {
14238 *rettv = di->di_tv;
14239 init_tv(&di->di_tv);
14240 dictitem_remove(d, di);
14241 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014243 }
14244 }
14245 else if (argvars[0].v_type != VAR_LIST)
14246 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014247 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014248 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 int error = FALSE;
14251
14252 idx = get_tv_number_chk(&argvars[1], &error);
14253 if (error)
14254 ; /* type error: do nothing, errmsg already given */
14255 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014256 EMSGN(_(e_listidx), idx);
14257 else
14258 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014259 if (argvars[2].v_type == VAR_UNKNOWN)
14260 {
14261 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014262 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014263 *rettv = item->li_tv;
14264 vim_free(item);
14265 }
14266 else
14267 {
14268 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 end = get_tv_number_chk(&argvars[2], &error);
14270 if (error)
14271 ; /* type error: do nothing */
14272 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014273 EMSGN(_(e_listidx), end);
14274 else
14275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014276 int cnt = 0;
14277
14278 for (li = item; li != NULL; li = li->li_next)
14279 {
14280 ++cnt;
14281 if (li == item2)
14282 break;
14283 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014284 if (li == NULL) /* didn't find "item2" after "item" */
14285 EMSG(_(e_invrange));
14286 else
14287 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014288 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014289 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014290 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014291 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014292 l->lv_first = item;
14293 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014294 item->li_prev = NULL;
14295 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014296 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014297 }
14298 }
14299 }
14300 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014301 }
14302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303}
14304
14305/*
14306 * "rename({from}, {to})" function
14307 */
14308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 typval_T *argvars;
14311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312{
14313 char_u buf[NUMBUFLEN];
14314
14315 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14319 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320}
14321
14322/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014323 * "repeat()" function
14324 */
14325/*ARGSUSED*/
14326 static void
14327f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *argvars;
14329 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014330{
14331 char_u *p;
14332 int n;
14333 int slen;
14334 int len;
14335 char_u *r;
14336 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014337
14338 n = get_tv_number(&argvars[1]);
14339 if (argvars[0].v_type == VAR_LIST)
14340 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014341 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014342 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014343 if (list_extend(rettv->vval.v_list,
14344 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014346 }
14347 else
14348 {
14349 p = get_tv_string(&argvars[0]);
14350 rettv->v_type = VAR_STRING;
14351 rettv->vval.v_string = NULL;
14352
14353 slen = (int)STRLEN(p);
14354 len = slen * n;
14355 if (len <= 0)
14356 return;
14357
14358 r = alloc(len + 1);
14359 if (r != NULL)
14360 {
14361 for (i = 0; i < n; i++)
14362 mch_memmove(r + i * slen, p, (size_t)slen);
14363 r[len] = NUL;
14364 }
14365
14366 rettv->vval.v_string = r;
14367 }
14368}
14369
14370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 * "resolve()" function
14372 */
14373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014375 typval_T *argvars;
14376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377{
14378 char_u *p;
14379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381#ifdef FEAT_SHORTCUT
14382 {
14383 char_u *v = NULL;
14384
14385 v = mch_resolve_shortcut(p);
14386 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 }
14391#else
14392# ifdef HAVE_READLINK
14393 {
14394 char_u buf[MAXPATHL + 1];
14395 char_u *cpy;
14396 int len;
14397 char_u *remain = NULL;
14398 char_u *q;
14399 int is_relative_to_current = FALSE;
14400 int has_trailing_pathsep = FALSE;
14401 int limit = 100;
14402
14403 p = vim_strsave(p);
14404
14405 if (p[0] == '.' && (vim_ispathsep(p[1])
14406 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14407 is_relative_to_current = TRUE;
14408
14409 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014410 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 has_trailing_pathsep = TRUE;
14412
14413 q = getnextcomp(p);
14414 if (*q != NUL)
14415 {
14416 /* Separate the first path component in "p", and keep the
14417 * remainder (beginning with the path separator). */
14418 remain = vim_strsave(q - 1);
14419 q[-1] = NUL;
14420 }
14421
14422 for (;;)
14423 {
14424 for (;;)
14425 {
14426 len = readlink((char *)p, (char *)buf, MAXPATHL);
14427 if (len <= 0)
14428 break;
14429 buf[len] = NUL;
14430
14431 if (limit-- == 0)
14432 {
14433 vim_free(p);
14434 vim_free(remain);
14435 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 goto fail;
14438 }
14439
14440 /* Ensure that the result will have a trailing path separator
14441 * if the argument has one. */
14442 if (remain == NULL && has_trailing_pathsep)
14443 add_pathsep(buf);
14444
14445 /* Separate the first path component in the link value and
14446 * concatenate the remainders. */
14447 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14448 if (*q != NUL)
14449 {
14450 if (remain == NULL)
14451 remain = vim_strsave(q - 1);
14452 else
14453 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014454 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 if (cpy != NULL)
14456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 vim_free(remain);
14458 remain = cpy;
14459 }
14460 }
14461 q[-1] = NUL;
14462 }
14463
14464 q = gettail(p);
14465 if (q > p && *q == NUL)
14466 {
14467 /* Ignore trailing path separator. */
14468 q[-1] = NUL;
14469 q = gettail(p);
14470 }
14471 if (q > p && !mch_isFullName(buf))
14472 {
14473 /* symlink is relative to directory of argument */
14474 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14475 if (cpy != NULL)
14476 {
14477 STRCPY(cpy, p);
14478 STRCPY(gettail(cpy), buf);
14479 vim_free(p);
14480 p = cpy;
14481 }
14482 }
14483 else
14484 {
14485 vim_free(p);
14486 p = vim_strsave(buf);
14487 }
14488 }
14489
14490 if (remain == NULL)
14491 break;
14492
14493 /* Append the first path component of "remain" to "p". */
14494 q = getnextcomp(remain + 1);
14495 len = q - remain - (*q != NUL);
14496 cpy = vim_strnsave(p, STRLEN(p) + len);
14497 if (cpy != NULL)
14498 {
14499 STRNCAT(cpy, remain, len);
14500 vim_free(p);
14501 p = cpy;
14502 }
14503 /* Shorten "remain". */
14504 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014505 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 else
14507 {
14508 vim_free(remain);
14509 remain = NULL;
14510 }
14511 }
14512
14513 /* If the result is a relative path name, make it explicitly relative to
14514 * the current directory if and only if the argument had this form. */
14515 if (!vim_ispathsep(*p))
14516 {
14517 if (is_relative_to_current
14518 && *p != NUL
14519 && !(p[0] == '.'
14520 && (p[1] == NUL
14521 || vim_ispathsep(p[1])
14522 || (p[1] == '.'
14523 && (p[2] == NUL
14524 || vim_ispathsep(p[2]))))))
14525 {
14526 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014527 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 if (cpy != NULL)
14529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 vim_free(p);
14531 p = cpy;
14532 }
14533 }
14534 else if (!is_relative_to_current)
14535 {
14536 /* Strip leading "./". */
14537 q = p;
14538 while (q[0] == '.' && vim_ispathsep(q[1]))
14539 q += 2;
14540 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014541 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 }
14543 }
14544
14545 /* Ensure that the result will have no trailing path separator
14546 * if the argument had none. But keep "/" or "//". */
14547 if (!has_trailing_pathsep)
14548 {
14549 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014550 if (after_pathsep(p, q))
14551 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 }
14553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 }
14556# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558# endif
14559#endif
14560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562
14563#ifdef HAVE_READLINK
14564fail:
14565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567}
14568
14569/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571 */
14572 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 typval_T *argvars;
14575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576{
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 list_T *l;
14578 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580 rettv->vval.v_number = 0;
14581 if (argvars[0].v_type != VAR_LIST)
14582 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014583 else if ((l = argvars[0].vval.v_list) != NULL
14584 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014585 {
14586 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014587 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014588 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589 while (li != NULL)
14590 {
14591 ni = li->li_prev;
14592 list_append(l, li);
14593 li = ni;
14594 }
14595 rettv->vval.v_list = l;
14596 rettv->v_type = VAR_LIST;
14597 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014598 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600}
14601
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014602#define SP_NOMOVE 0x01 /* don't move cursor */
14603#define SP_REPEAT 0x02 /* repeat to find outer pair */
14604#define SP_RETCOUNT 0x04 /* return matchcount */
14605#define SP_SETPCMARK 0x08 /* set previous context mark */
14606#define SP_START 0x10 /* accept match at start position */
14607#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14608#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014609
Bram Moolenaar33570922005-01-25 22:26:29 +000014610static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611
14612/*
14613 * Get flags for a search function.
14614 * Possibly sets "p_ws".
14615 * Returns BACKWARD, FORWARD or zero (for an error).
14616 */
14617 static int
14618get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620 int *flagsp;
14621{
14622 int dir = FORWARD;
14623 char_u *flags;
14624 char_u nbuf[NUMBUFLEN];
14625 int mask;
14626
14627 if (varp->v_type != VAR_UNKNOWN)
14628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 flags = get_tv_string_buf_chk(varp, nbuf);
14630 if (flags == NULL)
14631 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632 while (*flags != NUL)
14633 {
14634 switch (*flags)
14635 {
14636 case 'b': dir = BACKWARD; break;
14637 case 'w': p_ws = TRUE; break;
14638 case 'W': p_ws = FALSE; break;
14639 default: mask = 0;
14640 if (flagsp != NULL)
14641 switch (*flags)
14642 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014643 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014644 case 'e': mask = SP_END; break;
14645 case 'm': mask = SP_RETCOUNT; break;
14646 case 'n': mask = SP_NOMOVE; break;
14647 case 'p': mask = SP_SUBPAT; break;
14648 case 'r': mask = SP_REPEAT; break;
14649 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014650 }
14651 if (mask == 0)
14652 {
14653 EMSG2(_(e_invarg2), flags);
14654 dir = 0;
14655 }
14656 else
14657 *flagsp |= mask;
14658 }
14659 if (dir == 0)
14660 break;
14661 ++flags;
14662 }
14663 }
14664 return dir;
14665}
14666
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014668 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014670 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014671search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014672 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014673 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014674 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014676 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 char_u *pat;
14678 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014679 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 int save_p_ws = p_ws;
14681 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014682 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014683 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014684 proftime_T tm;
14685#ifdef FEAT_RELTIME
14686 long time_limit = 0;
14687#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014688 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014689 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014692 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014693 if (dir == 0)
14694 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014695 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014696 if (flags & SP_START)
14697 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014698 if (flags & SP_END)
14699 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014700
Bram Moolenaar76929292008-01-06 19:07:36 +000014701 /* Optional arguments: line number to stop searching and timeout. */
14702 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014703 {
14704 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14705 if (lnum_stop < 0)
14706 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014707#ifdef FEAT_RELTIME
14708 if (argvars[3].v_type != VAR_UNKNOWN)
14709 {
14710 time_limit = get_tv_number_chk(&argvars[3], NULL);
14711 if (time_limit < 0)
14712 goto theend;
14713 }
14714#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014715 }
14716
Bram Moolenaar76929292008-01-06 19:07:36 +000014717#ifdef FEAT_RELTIME
14718 /* Set the time limit, if there is one. */
14719 profile_setlimit(time_limit, &tm);
14720#endif
14721
Bram Moolenaar231334e2005-07-25 20:46:57 +000014722 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014723 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014724 * Check to make sure only those flags are set.
14725 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14726 * flags cannot be set. Check for that condition also.
14727 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014728 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014729 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014731 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014732 goto theend;
14733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014735 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014736 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014737 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014738 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014740 if (flags & SP_SUBPAT)
14741 retval = subpatnum;
14742 else
14743 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014744 if (flags & SP_SETPCMARK)
14745 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014747 if (match_pos != NULL)
14748 {
14749 /* Store the match cursor position */
14750 match_pos->lnum = pos.lnum;
14751 match_pos->col = pos.col + 1;
14752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 /* "/$" will put the cursor after the end of the line, may need to
14754 * correct that here */
14755 check_cursor();
14756 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014757
14758 /* If 'n' flag is used: restore cursor position. */
14759 if (flags & SP_NOMOVE)
14760 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014761 else
14762 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014763theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014765
14766 return retval;
14767}
14768
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014769#ifdef FEAT_FLOAT
14770/*
14771 * "round({float})" function
14772 */
14773 static void
14774f_round(argvars, rettv)
14775 typval_T *argvars;
14776 typval_T *rettv;
14777{
14778 float_T f;
14779
14780 rettv->v_type = VAR_FLOAT;
14781 if (get_float_arg(argvars, &f) == OK)
14782 /* round() is not in C90, use ceil() or floor() instead. */
14783 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14784 else
14785 rettv->vval.v_float = 0.0;
14786}
14787#endif
14788
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014789/*
14790 * "search()" function
14791 */
14792 static void
14793f_search(argvars, rettv)
14794 typval_T *argvars;
14795 typval_T *rettv;
14796{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014797 int flags = 0;
14798
14799 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800}
14801
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014803 * "searchdecl()" function
14804 */
14805 static void
14806f_searchdecl(argvars, rettv)
14807 typval_T *argvars;
14808 typval_T *rettv;
14809{
14810 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014811 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014812 int error = FALSE;
14813 char_u *name;
14814
14815 rettv->vval.v_number = 1; /* default: FAIL */
14816
14817 name = get_tv_string_chk(&argvars[0]);
14818 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014819 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014820 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014821 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14822 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14823 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014824 if (!error && name != NULL)
14825 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014826 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014827}
14828
14829/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014830 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014832 static int
14833searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014834 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014835 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836{
14837 char_u *spat, *mpat, *epat;
14838 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 int dir;
14841 int flags = 0;
14842 char_u nbuf1[NUMBUFLEN];
14843 char_u nbuf2[NUMBUFLEN];
14844 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014845 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014846 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014847 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014850 spat = get_tv_string_chk(&argvars[0]);
14851 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14852 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14853 if (spat == NULL || mpat == NULL || epat == NULL)
14854 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 /* Handle the optional fourth argument: flags */
14857 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014858 if (dir == 0)
14859 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014860
14861 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014862 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14863 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014864 if ((flags & (SP_END | SP_SUBPAT)) != 0
14865 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014866 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014867 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014868 goto theend;
14869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014871 /* Using 'r' implies 'W', otherwise it doesn't work. */
14872 if (flags & SP_REPEAT)
14873 p_ws = FALSE;
14874
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014875 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014876 if (argvars[3].v_type == VAR_UNKNOWN
14877 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 skip = (char_u *)"";
14879 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014881 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014882 if (argvars[5].v_type != VAR_UNKNOWN)
14883 {
14884 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14885 if (lnum_stop < 0)
14886 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014887#ifdef FEAT_RELTIME
14888 if (argvars[6].v_type != VAR_UNKNOWN)
14889 {
14890 time_limit = get_tv_number_chk(&argvars[6], NULL);
14891 if (time_limit < 0)
14892 goto theend;
14893 }
14894#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014895 }
14896 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014897 if (skip == NULL)
14898 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014900 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014901 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014902
14903theend:
14904 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014905
14906 return retval;
14907}
14908
14909/*
14910 * "searchpair()" function
14911 */
14912 static void
14913f_searchpair(argvars, rettv)
14914 typval_T *argvars;
14915 typval_T *rettv;
14916{
14917 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14918}
14919
14920/*
14921 * "searchpairpos()" function
14922 */
14923 static void
14924f_searchpairpos(argvars, rettv)
14925 typval_T *argvars;
14926 typval_T *rettv;
14927{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014928 pos_T match_pos;
14929 int lnum = 0;
14930 int col = 0;
14931
14932 rettv->vval.v_number = 0;
14933
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014935 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014936
14937 if (searchpair_cmn(argvars, &match_pos) > 0)
14938 {
14939 lnum = match_pos.lnum;
14940 col = match_pos.col;
14941 }
14942
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014943 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14944 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014945}
14946
14947/*
14948 * Search for a start/middle/end thing.
14949 * Used by searchpair(), see its documentation for the details.
14950 * Returns 0 or -1 for no match,
14951 */
14952 long
Bram Moolenaar76929292008-01-06 19:07:36 +000014953do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
14954 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014955 char_u *spat; /* start pattern */
14956 char_u *mpat; /* middle pattern */
14957 char_u *epat; /* end pattern */
14958 int dir; /* BACKWARD or FORWARD */
14959 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014960 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014961 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014962 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000014963 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014964{
14965 char_u *save_cpo;
14966 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14967 long retval = 0;
14968 pos_T pos;
14969 pos_T firstpos;
14970 pos_T foundpos;
14971 pos_T save_cursor;
14972 pos_T save_pos;
14973 int n;
14974 int r;
14975 int nest = 1;
14976 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014977 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000014978 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014979
14980 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14981 save_cpo = p_cpo;
14982 p_cpo = (char_u *)"";
14983
Bram Moolenaar76929292008-01-06 19:07:36 +000014984#ifdef FEAT_RELTIME
14985 /* Set the time limit, if there is one. */
14986 profile_setlimit(time_limit, &tm);
14987#endif
14988
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014989 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14990 * start/middle/end (pat3, for the top pair). */
14991 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14992 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14993 if (pat2 == NULL || pat3 == NULL)
14994 goto theend;
14995 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14996 if (*mpat == NUL)
14997 STRCPY(pat3, pat2);
14998 else
14999 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15000 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015001 if (flags & SP_START)
15002 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015003
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 save_cursor = curwin->w_cursor;
15005 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015006 clearpos(&firstpos);
15007 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 pat = pat3;
15009 for (;;)
15010 {
15011 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015012 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15014 /* didn't find it or found the first match again: FAIL */
15015 break;
15016
15017 if (firstpos.lnum == 0)
15018 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015019 if (equalpos(pos, foundpos))
15020 {
15021 /* Found the same position again. Can happen with a pattern that
15022 * has "\zs" at the end and searching backwards. Advance one
15023 * character and try again. */
15024 if (dir == BACKWARD)
15025 decl(&pos);
15026 else
15027 incl(&pos);
15028 }
15029 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015031 /* clear the start flag to avoid getting stuck here */
15032 options &= ~SEARCH_START;
15033
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 /* If the skip pattern matches, ignore this match. */
15035 if (*skip != NUL)
15036 {
15037 save_pos = curwin->w_cursor;
15038 curwin->w_cursor = pos;
15039 r = eval_to_bool(skip, &err, NULL, FALSE);
15040 curwin->w_cursor = save_pos;
15041 if (err)
15042 {
15043 /* Evaluating {skip} caused an error, break here. */
15044 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015045 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 break;
15047 }
15048 if (r)
15049 continue;
15050 }
15051
15052 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15053 {
15054 /* Found end when searching backwards or start when searching
15055 * forward: nested pair. */
15056 ++nest;
15057 pat = pat2; /* nested, don't search for middle */
15058 }
15059 else
15060 {
15061 /* Found end when searching forward or start when searching
15062 * backward: end of (nested) pair; or found middle in outer pair. */
15063 if (--nest == 1)
15064 pat = pat3; /* outer level, search for middle */
15065 }
15066
15067 if (nest == 0)
15068 {
15069 /* Found the match: return matchcount or line number. */
15070 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015071 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015073 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015074 if (flags & SP_SETPCMARK)
15075 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 curwin->w_cursor = pos;
15077 if (!(flags & SP_REPEAT))
15078 break;
15079 nest = 1; /* search for next unmatched */
15080 }
15081 }
15082
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015083 if (match_pos != NULL)
15084 {
15085 /* Store the match cursor position */
15086 match_pos->lnum = curwin->w_cursor.lnum;
15087 match_pos->col = curwin->w_cursor.col + 1;
15088 }
15089
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015091 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092 curwin->w_cursor = save_cursor;
15093
15094theend:
15095 vim_free(pat2);
15096 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015098
15099 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100}
15101
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015102/*
15103 * "searchpos()" function
15104 */
15105 static void
15106f_searchpos(argvars, rettv)
15107 typval_T *argvars;
15108 typval_T *rettv;
15109{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015110 pos_T match_pos;
15111 int lnum = 0;
15112 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015113 int n;
15114 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015115
15116 rettv->vval.v_number = 0;
15117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015118 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015119 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015120
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015121 n = search_cmn(argvars, &match_pos, &flags);
15122 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015123 {
15124 lnum = match_pos.lnum;
15125 col = match_pos.col;
15126 }
15127
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015128 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15129 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015130 if (flags & SP_SUBPAT)
15131 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015132}
15133
15134
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135/*ARGSUSED*/
15136 static void
15137f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 typval_T *argvars;
15139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015141#ifdef FEAT_CLIENTSERVER
15142 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 char_u *server = get_tv_string_chk(&argvars[0]);
15144 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015147 if (server == NULL || reply == NULL)
15148 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 if (check_restricted() || check_secure())
15150 return;
15151# ifdef FEAT_X11
15152 if (check_connection() == FAIL)
15153 return;
15154# endif
15155
15156 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 EMSG(_("E258: Unable to send to client"));
15159 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161 rettv->vval.v_number = 0;
15162#else
15163 rettv->vval.v_number = -1;
15164#endif
15165}
15166
15167/*ARGSUSED*/
15168 static void
15169f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015170 typval_T *argvars;
15171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172{
15173 char_u *r = NULL;
15174
15175#ifdef FEAT_CLIENTSERVER
15176# ifdef WIN32
15177 r = serverGetVimNames();
15178# else
15179 make_connection();
15180 if (X_DISPLAY != NULL)
15181 r = serverGetVimNames(X_DISPLAY);
15182# endif
15183#endif
15184 rettv->v_type = VAR_STRING;
15185 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186}
15187
15188/*
15189 * "setbufvar()" function
15190 */
15191/*ARGSUSED*/
15192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015194 typval_T *argvars;
15195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196{
15197 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 char_u nbuf[NUMBUFLEN];
15202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 rettv->vval.v_number = 0;
15204
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 if (check_restricted() || check_secure())
15206 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015207 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15208 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015209 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 varp = &argvars[2];
15211
15212 if (buf != NULL && varname != NULL && varp != NULL)
15213 {
15214 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216
15217 if (*varname == '&')
15218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 long numval;
15220 char_u *strval;
15221 int error = FALSE;
15222
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 numval = get_tv_number_chk(varp, &error);
15225 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015226 if (!error && strval != NULL)
15227 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 }
15229 else
15230 {
15231 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15232 if (bufvarname != NULL)
15233 {
15234 STRCPY(bufvarname, "b:");
15235 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015236 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 vim_free(bufvarname);
15238 }
15239 }
15240
15241 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244}
15245
15246/*
15247 * "setcmdpos()" function
15248 */
15249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015250f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T *argvars;
15252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 int pos = (int)get_tv_number(&argvars[0]) - 1;
15255
15256 if (pos >= 0)
15257 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258}
15259
15260/*
15261 * "setline()" function
15262 */
15263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015264f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015265 typval_T *argvars;
15266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267{
15268 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015269 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015270 list_T *l = NULL;
15271 listitem_T *li = NULL;
15272 long added = 0;
15273 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015275 lnum = get_tv_lnum(&argvars[0]);
15276 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015278 l = argvars[1].vval.v_list;
15279 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015281 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015283
15284 rettv->vval.v_number = 0; /* OK */
15285 for (;;)
15286 {
15287 if (l != NULL)
15288 {
15289 /* list argument, get next string */
15290 if (li == NULL)
15291 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015292 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015293 li = li->li_next;
15294 }
15295
15296 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015297 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015298 break;
15299 if (lnum <= curbuf->b_ml.ml_line_count)
15300 {
15301 /* existing line, replace it */
15302 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15303 {
15304 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015305 if (lnum == curwin->w_cursor.lnum)
15306 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015307 rettv->vval.v_number = 0; /* OK */
15308 }
15309 }
15310 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15311 {
15312 /* lnum is one past the last line, append the line */
15313 ++added;
15314 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15315 rettv->vval.v_number = 0; /* OK */
15316 }
15317
15318 if (l == NULL) /* only one string argument */
15319 break;
15320 ++lnum;
15321 }
15322
15323 if (added > 0)
15324 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325}
15326
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015327static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15328
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015330 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015331 */
15332/*ARGSUSED*/
15333 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015334set_qf_ll_list(wp, list_arg, action_arg, rettv)
15335 win_T *wp;
15336 typval_T *list_arg;
15337 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015338 typval_T *rettv;
15339{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015340#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015341 char_u *act;
15342 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015343#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015344
Bram Moolenaar2641f772005-03-25 21:58:17 +000015345 rettv->vval.v_number = -1;
15346
15347#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015348 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015349 EMSG(_(e_listreq));
15350 else
15351 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015352 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015353
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015354 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015355 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015356 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015357 if (act == NULL)
15358 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015359 if (*act == 'a' || *act == 'r')
15360 action = *act;
15361 }
15362
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015363 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015364 rettv->vval.v_number = 0;
15365 }
15366#endif
15367}
15368
15369/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015370 * "setloclist()" function
15371 */
15372/*ARGSUSED*/
15373 static void
15374f_setloclist(argvars, rettv)
15375 typval_T *argvars;
15376 typval_T *rettv;
15377{
15378 win_T *win;
15379
15380 rettv->vval.v_number = -1;
15381
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015382 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015383 if (win != NULL)
15384 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15385}
15386
15387/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015388 * "setmatches()" function
15389 */
15390 static void
15391f_setmatches(argvars, rettv)
15392 typval_T *argvars;
15393 typval_T *rettv;
15394{
15395#ifdef FEAT_SEARCH_EXTRA
15396 list_T *l;
15397 listitem_T *li;
15398 dict_T *d;
15399
15400 rettv->vval.v_number = -1;
15401 if (argvars[0].v_type != VAR_LIST)
15402 {
15403 EMSG(_(e_listreq));
15404 return;
15405 }
15406 if ((l = argvars[0].vval.v_list) != NULL)
15407 {
15408
15409 /* To some extent make sure that we are dealing with a list from
15410 * "getmatches()". */
15411 li = l->lv_first;
15412 while (li != NULL)
15413 {
15414 if (li->li_tv.v_type != VAR_DICT
15415 || (d = li->li_tv.vval.v_dict) == NULL)
15416 {
15417 EMSG(_(e_invarg));
15418 return;
15419 }
15420 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15421 && dict_find(d, (char_u *)"pattern", -1) != NULL
15422 && dict_find(d, (char_u *)"priority", -1) != NULL
15423 && dict_find(d, (char_u *)"id", -1) != NULL))
15424 {
15425 EMSG(_(e_invarg));
15426 return;
15427 }
15428 li = li->li_next;
15429 }
15430
15431 clear_matches(curwin);
15432 li = l->lv_first;
15433 while (li != NULL)
15434 {
15435 d = li->li_tv.vval.v_dict;
15436 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15437 get_dict_string(d, (char_u *)"pattern", FALSE),
15438 (int)get_dict_number(d, (char_u *)"priority"),
15439 (int)get_dict_number(d, (char_u *)"id"));
15440 li = li->li_next;
15441 }
15442 rettv->vval.v_number = 0;
15443 }
15444#endif
15445}
15446
15447/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015448 * "setpos()" function
15449 */
15450/*ARGSUSED*/
15451 static void
15452f_setpos(argvars, rettv)
15453 typval_T *argvars;
15454 typval_T *rettv;
15455{
15456 pos_T pos;
15457 int fnum;
15458 char_u *name;
15459
Bram Moolenaar08250432008-02-13 11:42:46 +000015460 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015461 name = get_tv_string_chk(argvars);
15462 if (name != NULL)
15463 {
15464 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15465 {
15466 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015467 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015468 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015469 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015470 if (fnum == curbuf->b_fnum)
15471 {
15472 curwin->w_cursor = pos;
15473 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015474 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015475 }
15476 else
15477 EMSG(_(e_invarg));
15478 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015479 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15480 {
15481 /* set mark */
15482 if (setmark_pos(name[1], &pos, fnum) == OK)
15483 rettv->vval.v_number = 0;
15484 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015485 else
15486 EMSG(_(e_invarg));
15487 }
15488 }
15489}
15490
15491/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015492 * "setqflist()" function
15493 */
15494/*ARGSUSED*/
15495 static void
15496f_setqflist(argvars, rettv)
15497 typval_T *argvars;
15498 typval_T *rettv;
15499{
15500 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15501}
15502
15503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 * "setreg()" function
15505 */
15506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015507f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 typval_T *argvars;
15509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510{
15511 int regname;
15512 char_u *strregname;
15513 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015514 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 int append;
15516 char_u yank_type;
15517 long block_len;
15518
15519 block_len = -1;
15520 yank_type = MAUTO;
15521 append = FALSE;
15522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015523 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015526 if (strregname == NULL)
15527 return; /* type error; errmsg already given */
15528 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 if (regname == 0 || regname == '@')
15530 regname = '"';
15531 else if (regname == '=')
15532 return;
15533
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015534 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015536 stropt = get_tv_string_chk(&argvars[2]);
15537 if (stropt == NULL)
15538 return; /* type error */
15539 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 switch (*stropt)
15541 {
15542 case 'a': case 'A': /* append */
15543 append = TRUE;
15544 break;
15545 case 'v': case 'c': /* character-wise selection */
15546 yank_type = MCHAR;
15547 break;
15548 case 'V': case 'l': /* line-wise selection */
15549 yank_type = MLINE;
15550 break;
15551#ifdef FEAT_VISUAL
15552 case 'b': case Ctrl_V: /* block-wise selection */
15553 yank_type = MBLOCK;
15554 if (VIM_ISDIGIT(stropt[1]))
15555 {
15556 ++stropt;
15557 block_len = getdigits(&stropt) - 1;
15558 --stropt;
15559 }
15560 break;
15561#endif
15562 }
15563 }
15564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015565 strval = get_tv_string_chk(&argvars[1]);
15566 if (strval != NULL)
15567 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570}
15571
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015572/*
15573 * "settabwinvar()" function
15574 */
15575 static void
15576f_settabwinvar(argvars, rettv)
15577 typval_T *argvars;
15578 typval_T *rettv;
15579{
15580 setwinvar(argvars, rettv, 1);
15581}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582
15583/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015584 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015587f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015588 typval_T *argvars;
15589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015591 setwinvar(argvars, rettv, 0);
15592}
15593
15594/*
15595 * "setwinvar()" and "settabwinvar()" functions
15596 */
15597 static void
15598setwinvar(argvars, rettv, off)
15599 typval_T *argvars;
15600 typval_T *rettv;
15601 int off;
15602{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 win_T *win;
15604#ifdef FEAT_WINDOWS
15605 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015606 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607#endif
15608 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015609 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015611 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 rettv->vval.v_number = 0;
15614
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 if (check_restricted() || check_secure())
15616 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015617
15618#ifdef FEAT_WINDOWS
15619 if (off == 1)
15620 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15621 else
15622 tp = curtab;
15623#endif
15624 win = find_win_by_nr(&argvars[off], tp);
15625 varname = get_tv_string_chk(&argvars[off + 1]);
15626 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627
15628 if (win != NULL && varname != NULL && varp != NULL)
15629 {
15630#ifdef FEAT_WINDOWS
15631 /* set curwin to be our win, temporarily */
15632 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015633 save_curtab = curtab;
15634 goto_tabpage_tp(tp);
15635 if (!win_valid(win))
15636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 curwin = win;
15638 curbuf = curwin->w_buffer;
15639#endif
15640
15641 if (*varname == '&')
15642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015643 long numval;
15644 char_u *strval;
15645 int error = FALSE;
15646
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 numval = get_tv_number_chk(varp, &error);
15649 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015650 if (!error && strval != NULL)
15651 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 }
15653 else
15654 {
15655 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15656 if (winvarname != NULL)
15657 {
15658 STRCPY(winvarname, "w:");
15659 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015660 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 vim_free(winvarname);
15662 }
15663 }
15664
15665#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015666 /* Restore current tabpage and window, if still valid (autocomands can
15667 * make them invalid). */
15668 if (valid_tabpage(save_curtab))
15669 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 if (win_valid(save_curwin))
15671 {
15672 curwin = save_curwin;
15673 curbuf = curwin->w_buffer;
15674 }
15675#endif
15676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677}
15678
15679/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015680 * "shellescape({string})" function
15681 */
15682 static void
15683f_shellescape(argvars, rettv)
15684 typval_T *argvars;
15685 typval_T *rettv;
15686{
15687 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
15688 rettv->v_type = VAR_STRING;
15689}
15690
15691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015692 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 */
15694 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 typval_T *argvars;
15697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700
Bram Moolenaar0d660222005-01-07 21:51:51 +000015701 p = get_tv_string(&argvars[0]);
15702 rettv->vval.v_string = vim_strsave(p);
15703 simplify_filename(rettv->vval.v_string); /* simplify in place */
15704 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705}
15706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015707#ifdef FEAT_FLOAT
15708/*
15709 * "sin()" function
15710 */
15711 static void
15712f_sin(argvars, rettv)
15713 typval_T *argvars;
15714 typval_T *rettv;
15715{
15716 float_T f;
15717
15718 rettv->v_type = VAR_FLOAT;
15719 if (get_float_arg(argvars, &f) == OK)
15720 rettv->vval.v_float = sin(f);
15721 else
15722 rettv->vval.v_float = 0.0;
15723}
15724#endif
15725
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726static int
15727#ifdef __BORLANDC__
15728 _RTLENTRYF
15729#endif
15730 item_compare __ARGS((const void *s1, const void *s2));
15731static int
15732#ifdef __BORLANDC__
15733 _RTLENTRYF
15734#endif
15735 item_compare2 __ARGS((const void *s1, const void *s2));
15736
15737static int item_compare_ic;
15738static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015739static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015740#define ITEM_COMPARE_FAIL 999
15741
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745 static int
15746#ifdef __BORLANDC__
15747_RTLENTRYF
15748#endif
15749item_compare(s1, s2)
15750 const void *s1;
15751 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 char_u *p1, *p2;
15754 char_u *tofree1, *tofree2;
15755 int res;
15756 char_u numbuf1[NUMBUFLEN];
15757 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015759 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15760 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015761 if (p1 == NULL)
15762 p1 = (char_u *)"";
15763 if (p2 == NULL)
15764 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015765 if (item_compare_ic)
15766 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768 res = STRCMP(p1, p2);
15769 vim_free(tofree1);
15770 vim_free(tofree2);
15771 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772}
15773
15774 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015775#ifdef __BORLANDC__
15776_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778item_compare2(s1, s2)
15779 const void *s1;
15780 const void *s2;
15781{
15782 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015783 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015784 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015787 /* shortcut after failure in previous call; compare all items equal */
15788 if (item_compare_func_err)
15789 return 0;
15790
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015791 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15792 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015793 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15794 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795
15796 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015797 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015798 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015799 clear_tv(&argv[0]);
15800 clear_tv(&argv[1]);
15801
15802 if (res == FAIL)
15803 res = ITEM_COMPARE_FAIL;
15804 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015805 /* return value has wrong type */
15806 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15807 if (item_compare_func_err)
15808 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809 clear_tv(&rettv);
15810 return res;
15811}
15812
15813/*
15814 * "sort({list})" function
15815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015818 typval_T *argvars;
15819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820{
Bram Moolenaar33570922005-01-25 22:26:29 +000015821 list_T *l;
15822 listitem_T *li;
15823 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824 long len;
15825 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 rettv->vval.v_number = 0;
15828 if (argvars[0].v_type != VAR_LIST)
15829 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 else
15831 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015833 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015834 return;
15835 rettv->vval.v_list = l;
15836 rettv->v_type = VAR_LIST;
15837 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 len = list_len(l);
15840 if (len <= 1)
15841 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842
Bram Moolenaar0d660222005-01-07 21:51:51 +000015843 item_compare_ic = FALSE;
15844 item_compare_func = NULL;
15845 if (argvars[1].v_type != VAR_UNKNOWN)
15846 {
15847 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015848 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015849 else
15850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015851 int error = FALSE;
15852
15853 i = get_tv_number_chk(&argvars[1], &error);
15854 if (error)
15855 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015856 if (i == 1)
15857 item_compare_ic = TRUE;
15858 else
15859 item_compare_func = get_tv_string(&argvars[1]);
15860 }
15861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015864 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015865 if (ptrs == NULL)
15866 return;
15867 i = 0;
15868 for (li = l->lv_first; li != NULL; li = li->li_next)
15869 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015871 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015872 /* test the compare function */
15873 if (item_compare_func != NULL
15874 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15875 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015876 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015878 {
15879 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015880 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015881 item_compare_func == NULL ? item_compare : item_compare2);
15882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015883 if (!item_compare_func_err)
15884 {
15885 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015886 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015887 l->lv_len = 0;
15888 for (i = 0; i < len; ++i)
15889 list_append(l, ptrs[i]);
15890 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015891 }
15892
15893 vim_free(ptrs);
15894 }
15895}
15896
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015897/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015898 * "soundfold({word})" function
15899 */
15900 static void
15901f_soundfold(argvars, rettv)
15902 typval_T *argvars;
15903 typval_T *rettv;
15904{
15905 char_u *s;
15906
15907 rettv->v_type = VAR_STRING;
15908 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015909#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015910 rettv->vval.v_string = eval_soundfold(s);
15911#else
15912 rettv->vval.v_string = vim_strsave(s);
15913#endif
15914}
15915
15916/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015917 * "spellbadword()" function
15918 */
15919/* ARGSUSED */
15920 static void
15921f_spellbadword(argvars, rettv)
15922 typval_T *argvars;
15923 typval_T *rettv;
15924{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015925 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015926 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015927 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015928
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015929 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015930 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015931
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015932#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015933 if (argvars[0].v_type == VAR_UNKNOWN)
15934 {
15935 /* Find the start and length of the badly spelled word. */
15936 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15937 if (len != 0)
15938 word = ml_get_cursor();
15939 }
15940 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15941 {
15942 char_u *str = get_tv_string_chk(&argvars[0]);
15943 int capcol = -1;
15944
15945 if (str != NULL)
15946 {
15947 /* Check the argument for spelling. */
15948 while (*str != NUL)
15949 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015950 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015951 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015952 {
15953 word = str;
15954 break;
15955 }
15956 str += len;
15957 }
15958 }
15959 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015960#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015962 list_append_string(rettv->vval.v_list, word, len);
15963 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015964 attr == HLF_SPB ? "bad" :
15965 attr == HLF_SPR ? "rare" :
15966 attr == HLF_SPL ? "local" :
15967 attr == HLF_SPC ? "caps" :
15968 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015969}
15970
15971/*
15972 * "spellsuggest()" function
15973 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015974/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015975 static void
15976f_spellsuggest(argvars, rettv)
15977 typval_T *argvars;
15978 typval_T *rettv;
15979{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015980#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015981 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015982 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015983 int maxcount;
15984 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015985 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015986 listitem_T *li;
15987 int need_capital = FALSE;
15988#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015989
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015990 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015991 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015992
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015993#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015994 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15995 {
15996 str = get_tv_string(&argvars[0]);
15997 if (argvars[1].v_type != VAR_UNKNOWN)
15998 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015999 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016000 if (maxcount <= 0)
16001 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016002 if (argvars[2].v_type != VAR_UNKNOWN)
16003 {
16004 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16005 if (typeerr)
16006 return;
16007 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016008 }
16009 else
16010 maxcount = 25;
16011
Bram Moolenaar4770d092006-01-12 23:22:24 +000016012 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016013
16014 for (i = 0; i < ga.ga_len; ++i)
16015 {
16016 str = ((char_u **)ga.ga_data)[i];
16017
16018 li = listitem_alloc();
16019 if (li == NULL)
16020 vim_free(str);
16021 else
16022 {
16023 li->li_tv.v_type = VAR_STRING;
16024 li->li_tv.v_lock = 0;
16025 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016026 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016027 }
16028 }
16029 ga_clear(&ga);
16030 }
16031#endif
16032}
16033
Bram Moolenaar0d660222005-01-07 21:51:51 +000016034 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016035f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016036 typval_T *argvars;
16037 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016038{
16039 char_u *str;
16040 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016041 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016042 regmatch_T regmatch;
16043 char_u patbuf[NUMBUFLEN];
16044 char_u *save_cpo;
16045 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016046 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016047 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016049
16050 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16051 save_cpo = p_cpo;
16052 p_cpo = (char_u *)"";
16053
16054 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016055 if (argvars[1].v_type != VAR_UNKNOWN)
16056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016057 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16058 if (pat == NULL)
16059 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016061 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016062 }
16063 if (pat == NULL || *pat == NUL)
16064 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016065
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016066 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016068 if (typeerr)
16069 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070
Bram Moolenaar0d660222005-01-07 21:51:51 +000016071 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16072 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016074 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016075 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016076 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016077 if (*str == NUL)
16078 match = FALSE; /* empty item at the end */
16079 else
16080 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016081 if (match)
16082 end = regmatch.startp[0];
16083 else
16084 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016085 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16086 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016088 if (list_append_string(rettv->vval.v_list, str,
16089 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016091 }
16092 if (!match)
16093 break;
16094 /* Advance to just after the match. */
16095 if (regmatch.endp[0] > str)
16096 col = 0;
16097 else
16098 {
16099 /* Don't get stuck at the same match. */
16100#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016101 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016102#else
16103 col = 1;
16104#endif
16105 }
16106 str = regmatch.endp[0];
16107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111
Bram Moolenaar0d660222005-01-07 21:51:51 +000016112 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113}
16114
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016115#ifdef FEAT_FLOAT
16116/*
16117 * "sqrt()" function
16118 */
16119 static void
16120f_sqrt(argvars, rettv)
16121 typval_T *argvars;
16122 typval_T *rettv;
16123{
16124 float_T f;
16125
16126 rettv->v_type = VAR_FLOAT;
16127 if (get_float_arg(argvars, &f) == OK)
16128 rettv->vval.v_float = sqrt(f);
16129 else
16130 rettv->vval.v_float = 0.0;
16131}
16132
16133/*
16134 * "str2float()" function
16135 */
16136 static void
16137f_str2float(argvars, rettv)
16138 typval_T *argvars;
16139 typval_T *rettv;
16140{
16141 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16142
16143 if (*p == '+')
16144 p = skipwhite(p + 1);
16145 (void)string2float(p, &rettv->vval.v_float);
16146 rettv->v_type = VAR_FLOAT;
16147}
16148#endif
16149
Bram Moolenaar2c932302006-03-18 21:42:09 +000016150/*
16151 * "str2nr()" function
16152 */
16153 static void
16154f_str2nr(argvars, rettv)
16155 typval_T *argvars;
16156 typval_T *rettv;
16157{
16158 int base = 10;
16159 char_u *p;
16160 long n;
16161
16162 if (argvars[1].v_type != VAR_UNKNOWN)
16163 {
16164 base = get_tv_number(&argvars[1]);
16165 if (base != 8 && base != 10 && base != 16)
16166 {
16167 EMSG(_(e_invarg));
16168 return;
16169 }
16170 }
16171
16172 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016173 if (*p == '+')
16174 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016175 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16176 rettv->vval.v_number = n;
16177}
16178
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179#ifdef HAVE_STRFTIME
16180/*
16181 * "strftime({format}[, {time}])" function
16182 */
16183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016185 typval_T *argvars;
16186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187{
16188 char_u result_buf[256];
16189 struct tm *curtime;
16190 time_t seconds;
16191 char_u *p;
16192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016193 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016195 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016196 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 seconds = time(NULL);
16198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016199 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 curtime = localtime(&seconds);
16201 /* MSVC returns NULL for an invalid value of seconds. */
16202 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016203 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 else
16205 {
16206# ifdef FEAT_MBYTE
16207 vimconv_T conv;
16208 char_u *enc;
16209
16210 conv.vc_type = CONV_NONE;
16211 enc = enc_locale();
16212 convert_setup(&conv, p_enc, enc);
16213 if (conv.vc_type != CONV_NONE)
16214 p = string_convert(&conv, p, NULL);
16215# endif
16216 if (p != NULL)
16217 (void)strftime((char *)result_buf, sizeof(result_buf),
16218 (char *)p, curtime);
16219 else
16220 result_buf[0] = NUL;
16221
16222# ifdef FEAT_MBYTE
16223 if (conv.vc_type != CONV_NONE)
16224 vim_free(p);
16225 convert_setup(&conv, enc, p_enc);
16226 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016227 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 else
16229# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016230 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231
16232# ifdef FEAT_MBYTE
16233 /* Release conversion descriptors */
16234 convert_setup(&conv, NULL, NULL);
16235 vim_free(enc);
16236# endif
16237 }
16238}
16239#endif
16240
16241/*
16242 * "stridx()" function
16243 */
16244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016245f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016246 typval_T *argvars;
16247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248{
16249 char_u buf[NUMBUFLEN];
16250 char_u *needle;
16251 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016256 needle = get_tv_string_chk(&argvars[1]);
16257 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016258 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 if (needle == NULL || haystack == NULL)
16260 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261
Bram Moolenaar33570922005-01-25 22:26:29 +000016262 if (argvars[2].v_type != VAR_UNKNOWN)
16263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 int error = FALSE;
16265
16266 start_idx = get_tv_number_chk(&argvars[2], &error);
16267 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016268 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016269 if (start_idx >= 0)
16270 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016271 }
16272
16273 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16274 if (pos != NULL)
16275 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276}
16277
16278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016279 * "string()" function
16280 */
16281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016282f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 typval_T *argvars;
16284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016285{
16286 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016287 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016289 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016290 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016291 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016292 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016293 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294}
16295
16296/*
16297 * "strlen()" function
16298 */
16299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 typval_T *argvars;
16302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016304 rettv->vval.v_number = (varnumber_T)(STRLEN(
16305 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306}
16307
16308/*
16309 * "strpart()" function
16310 */
16311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016312f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 typval_T *argvars;
16314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315{
16316 char_u *p;
16317 int n;
16318 int len;
16319 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016322 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 slen = (int)STRLEN(p);
16324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016325 n = get_tv_number_chk(&argvars[1], &error);
16326 if (error)
16327 len = 0;
16328 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016329 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 else
16331 len = slen - n; /* default len: all bytes that are available. */
16332
16333 /*
16334 * Only return the overlap between the specified part and the actual
16335 * string.
16336 */
16337 if (n < 0)
16338 {
16339 len += n;
16340 n = 0;
16341 }
16342 else if (n > slen)
16343 n = slen;
16344 if (len < 0)
16345 len = 0;
16346 else if (n + len > slen)
16347 len = slen - n;
16348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016349 rettv->v_type = VAR_STRING;
16350 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351}
16352
16353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 * "strridx()" function
16355 */
16356 static void
16357f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016358 typval_T *argvars;
16359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016360{
16361 char_u buf[NUMBUFLEN];
16362 char_u *needle;
16363 char_u *haystack;
16364 char_u *rest;
16365 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016366 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 needle = get_tv_string_chk(&argvars[1]);
16369 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016370
16371 rettv->vval.v_number = -1;
16372 if (needle == NULL || haystack == NULL)
16373 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016374
16375 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016376 if (argvars[2].v_type != VAR_UNKNOWN)
16377 {
16378 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016379 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016380 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016381 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016382 }
16383 else
16384 end_idx = haystack_len;
16385
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016387 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016388 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016389 lastmatch = haystack + end_idx;
16390 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016392 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016393 for (rest = haystack; *rest != '\0'; ++rest)
16394 {
16395 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016396 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 break;
16398 lastmatch = rest;
16399 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016400 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401
16402 if (lastmatch == NULL)
16403 rettv->vval.v_number = -1;
16404 else
16405 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16406}
16407
16408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 * "strtrans()" function
16410 */
16411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016413 typval_T *argvars;
16414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016416 rettv->v_type = VAR_STRING;
16417 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418}
16419
16420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421 * "submatch()" function
16422 */
16423 static void
16424f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016425 typval_T *argvars;
16426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427{
16428 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 rettv->vval.v_string =
16430 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431}
16432
16433/*
16434 * "substitute()" function
16435 */
16436 static void
16437f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 typval_T *argvars;
16439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440{
16441 char_u patbuf[NUMBUFLEN];
16442 char_u subbuf[NUMBUFLEN];
16443 char_u flagsbuf[NUMBUFLEN];
16444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445 char_u *str = get_tv_string_chk(&argvars[0]);
16446 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16447 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16448 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16449
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16452 rettv->vval.v_string = NULL;
16453 else
16454 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455}
16456
16457/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016458 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 */
16460/*ARGSUSED*/
16461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016462f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 typval_T *argvars;
16464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465{
16466 int id = 0;
16467#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016468 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 long col;
16470 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016471 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 lnum = get_tv_lnum(argvars); /* -1 on type error */
16474 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16475 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016477 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016478 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016479 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480#endif
16481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016482 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483}
16484
16485/*
16486 * "synIDattr(id, what [, mode])" function
16487 */
16488/*ARGSUSED*/
16489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016490f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016491 typval_T *argvars;
16492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493{
16494 char_u *p = NULL;
16495#ifdef FEAT_SYN_HL
16496 int id;
16497 char_u *what;
16498 char_u *mode;
16499 char_u modebuf[NUMBUFLEN];
16500 int modec;
16501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 id = get_tv_number(&argvars[0]);
16503 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016504 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 modec = TOLOWER_ASC(mode[0]);
16508 if (modec != 't' && modec != 'c'
16509#ifdef FEAT_GUI
16510 && modec != 'g'
16511#endif
16512 )
16513 modec = 0; /* replace invalid with current */
16514 }
16515 else
16516 {
16517#ifdef FEAT_GUI
16518 if (gui.in_use)
16519 modec = 'g';
16520 else
16521#endif
16522 if (t_colors > 1)
16523 modec = 'c';
16524 else
16525 modec = 't';
16526 }
16527
16528
16529 switch (TOLOWER_ASC(what[0]))
16530 {
16531 case 'b':
16532 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16533 p = highlight_color(id, what, modec);
16534 else /* bold */
16535 p = highlight_has_attr(id, HL_BOLD, modec);
16536 break;
16537
16538 case 'f': /* fg[#] */
16539 p = highlight_color(id, what, modec);
16540 break;
16541
16542 case 'i':
16543 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16544 p = highlight_has_attr(id, HL_INVERSE, modec);
16545 else /* italic */
16546 p = highlight_has_attr(id, HL_ITALIC, modec);
16547 break;
16548
16549 case 'n': /* name */
16550 p = get_highlight_name(NULL, id - 1);
16551 break;
16552
16553 case 'r': /* reverse */
16554 p = highlight_has_attr(id, HL_INVERSE, modec);
16555 break;
16556
16557 case 's': /* standout */
16558 p = highlight_has_attr(id, HL_STANDOUT, modec);
16559 break;
16560
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016561 case 'u':
16562 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16563 /* underline */
16564 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16565 else
16566 /* undercurl */
16567 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 break;
16569 }
16570
16571 if (p != NULL)
16572 p = vim_strsave(p);
16573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016574 rettv->v_type = VAR_STRING;
16575 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576}
16577
16578/*
16579 * "synIDtrans(id)" function
16580 */
16581/*ARGSUSED*/
16582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016583f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016584 typval_T *argvars;
16585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586{
16587 int id;
16588
16589#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016590 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591
16592 if (id > 0)
16593 id = syn_get_final_id(id);
16594 else
16595#endif
16596 id = 0;
16597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016598 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599}
16600
16601/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016602 * "synstack(lnum, col)" function
16603 */
16604/*ARGSUSED*/
16605 static void
16606f_synstack(argvars, rettv)
16607 typval_T *argvars;
16608 typval_T *rettv;
16609{
16610#ifdef FEAT_SYN_HL
16611 long lnum;
16612 long col;
16613 int i;
16614 int id;
16615#endif
16616
16617 rettv->v_type = VAR_LIST;
16618 rettv->vval.v_list = NULL;
16619
16620#ifdef FEAT_SYN_HL
16621 lnum = get_tv_lnum(argvars); /* -1 on type error */
16622 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16623
16624 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
16625 && col >= 0 && col < (long)STRLEN(ml_get(lnum))
16626 && rettv_list_alloc(rettv) != FAIL)
16627 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016628 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016629 for (i = 0; ; ++i)
16630 {
16631 id = syn_get_stack_item(i);
16632 if (id < 0)
16633 break;
16634 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16635 break;
16636 }
16637 }
16638#endif
16639}
16640
16641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 * "system()" function
16643 */
16644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016646 typval_T *argvars;
16647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016649 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016651 char_u *infile = NULL;
16652 char_u buf[NUMBUFLEN];
16653 int err = FALSE;
16654 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016656 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016657 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016658
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016659 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016660 {
16661 /*
16662 * Write the string to a temp file, to be used for input of the shell
16663 * command.
16664 */
16665 if ((infile = vim_tempname('i')) == NULL)
16666 {
16667 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016668 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016669 }
16670
16671 fd = mch_fopen((char *)infile, WRITEBIN);
16672 if (fd == NULL)
16673 {
16674 EMSG2(_(e_notopen), infile);
16675 goto done;
16676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016677 p = get_tv_string_buf_chk(&argvars[1], buf);
16678 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016679 {
16680 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016681 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016682 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016683 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16684 err = TRUE;
16685 if (fclose(fd) != 0)
16686 err = TRUE;
16687 if (err)
16688 {
16689 EMSG(_("E677: Error writing temp file"));
16690 goto done;
16691 }
16692 }
16693
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016694 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16695 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016696
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697#ifdef USE_CR
16698 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016699 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 {
16701 char_u *s;
16702
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016703 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 {
16705 if (*s == CAR)
16706 *s = NL;
16707 }
16708 }
16709#else
16710# ifdef USE_CRNL
16711 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016712 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 {
16714 char_u *s, *d;
16715
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016716 d = res;
16717 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 {
16719 if (s[0] == CAR && s[1] == NL)
16720 ++s;
16721 *d++ = *s;
16722 }
16723 *d = NUL;
16724 }
16725# endif
16726#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016727
16728done:
16729 if (infile != NULL)
16730 {
16731 mch_remove(infile);
16732 vim_free(infile);
16733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016734 rettv->v_type = VAR_STRING;
16735 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736}
16737
16738/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016739 * "tabpagebuflist()" function
16740 */
16741/* ARGSUSED */
16742 static void
16743f_tabpagebuflist(argvars, rettv)
16744 typval_T *argvars;
16745 typval_T *rettv;
16746{
16747#ifndef FEAT_WINDOWS
16748 rettv->vval.v_number = 0;
16749#else
16750 tabpage_T *tp;
16751 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016752
16753 if (argvars[0].v_type == VAR_UNKNOWN)
16754 wp = firstwin;
16755 else
16756 {
16757 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16758 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016759 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016760 }
16761 if (wp == NULL)
16762 rettv->vval.v_number = 0;
16763 else
16764 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016765 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016766 rettv->vval.v_number = 0;
16767 else
16768 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016769 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016770 if (list_append_number(rettv->vval.v_list,
16771 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016772 break;
16773 }
16774 }
16775#endif
16776}
16777
16778
16779/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016780 * "tabpagenr()" function
16781 */
16782/* ARGSUSED */
16783 static void
16784f_tabpagenr(argvars, rettv)
16785 typval_T *argvars;
16786 typval_T *rettv;
16787{
16788 int nr = 1;
16789#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016790 char_u *arg;
16791
16792 if (argvars[0].v_type != VAR_UNKNOWN)
16793 {
16794 arg = get_tv_string_chk(&argvars[0]);
16795 nr = 0;
16796 if (arg != NULL)
16797 {
16798 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016799 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016800 else
16801 EMSG2(_(e_invexpr2), arg);
16802 }
16803 }
16804 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016805 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016806#endif
16807 rettv->vval.v_number = nr;
16808}
16809
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016810
16811#ifdef FEAT_WINDOWS
16812static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16813
16814/*
16815 * Common code for tabpagewinnr() and winnr().
16816 */
16817 static int
16818get_winnr(tp, argvar)
16819 tabpage_T *tp;
16820 typval_T *argvar;
16821{
16822 win_T *twin;
16823 int nr = 1;
16824 win_T *wp;
16825 char_u *arg;
16826
16827 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16828 if (argvar->v_type != VAR_UNKNOWN)
16829 {
16830 arg = get_tv_string_chk(argvar);
16831 if (arg == NULL)
16832 nr = 0; /* type error; errmsg already given */
16833 else if (STRCMP(arg, "$") == 0)
16834 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16835 else if (STRCMP(arg, "#") == 0)
16836 {
16837 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16838 if (twin == NULL)
16839 nr = 0;
16840 }
16841 else
16842 {
16843 EMSG2(_(e_invexpr2), arg);
16844 nr = 0;
16845 }
16846 }
16847
16848 if (nr > 0)
16849 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16850 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016851 {
16852 if (wp == NULL)
16853 {
16854 /* didn't find it in this tabpage */
16855 nr = 0;
16856 break;
16857 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016858 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016859 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016860 return nr;
16861}
16862#endif
16863
16864/*
16865 * "tabpagewinnr()" function
16866 */
16867/* ARGSUSED */
16868 static void
16869f_tabpagewinnr(argvars, rettv)
16870 typval_T *argvars;
16871 typval_T *rettv;
16872{
16873 int nr = 1;
16874#ifdef FEAT_WINDOWS
16875 tabpage_T *tp;
16876
16877 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16878 if (tp == NULL)
16879 nr = 0;
16880 else
16881 nr = get_winnr(tp, &argvars[1]);
16882#endif
16883 rettv->vval.v_number = nr;
16884}
16885
16886
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016887/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016888 * "tagfiles()" function
16889 */
16890/*ARGSUSED*/
16891 static void
16892f_tagfiles(argvars, rettv)
16893 typval_T *argvars;
16894 typval_T *rettv;
16895{
16896 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016897 tagname_T tn;
16898 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016900 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016901 {
16902 rettv->vval.v_number = 0;
16903 return;
16904 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016905
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016906 for (first = TRUE; ; first = FALSE)
16907 if (get_tagfname(&tn, first, fname) == FAIL
16908 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016909 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016910 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016911}
16912
16913/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016914 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016915 */
16916 static void
16917f_taglist(argvars, rettv)
16918 typval_T *argvars;
16919 typval_T *rettv;
16920{
16921 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016922
16923 tag_pattern = get_tv_string(&argvars[0]);
16924
16925 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 if (*tag_pattern == NUL)
16927 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016928
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016929 if (rettv_list_alloc(rettv) == OK)
16930 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016931}
16932
16933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 * "tempname()" function
16935 */
16936/*ARGSUSED*/
16937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016938f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016939 typval_T *argvars;
16940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941{
16942 static int x = 'A';
16943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016944 rettv->v_type = VAR_STRING;
16945 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946
16947 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16948 * names. Skip 'I' and 'O', they are used for shell redirection. */
16949 do
16950 {
16951 if (x == 'Z')
16952 x = '0';
16953 else if (x == '9')
16954 x = 'A';
16955 else
16956 {
16957#ifdef EBCDIC
16958 if (x == 'I')
16959 x = 'J';
16960 else if (x == 'R')
16961 x = 'S';
16962 else
16963#endif
16964 ++x;
16965 }
16966 } while (x == 'I' || x == 'O');
16967}
16968
16969/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016970 * "test(list)" function: Just checking the walls...
16971 */
16972/*ARGSUSED*/
16973 static void
16974f_test(argvars, rettv)
16975 typval_T *argvars;
16976 typval_T *rettv;
16977{
16978 /* Used for unit testing. Change the code below to your liking. */
16979#if 0
16980 listitem_T *li;
16981 list_T *l;
16982 char_u *bad, *good;
16983
16984 if (argvars[0].v_type != VAR_LIST)
16985 return;
16986 l = argvars[0].vval.v_list;
16987 if (l == NULL)
16988 return;
16989 li = l->lv_first;
16990 if (li == NULL)
16991 return;
16992 bad = get_tv_string(&li->li_tv);
16993 li = li->li_next;
16994 if (li == NULL)
16995 return;
16996 good = get_tv_string(&li->li_tv);
16997 rettv->vval.v_number = test_edit_score(bad, good);
16998#endif
16999}
17000
17001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 * "tolower(string)" function
17003 */
17004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017006 typval_T *argvars;
17007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008{
17009 char_u *p;
17010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017011 p = vim_strsave(get_tv_string(&argvars[0]));
17012 rettv->v_type = VAR_STRING;
17013 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014
17015 if (p != NULL)
17016 while (*p != NUL)
17017 {
17018#ifdef FEAT_MBYTE
17019 int l;
17020
17021 if (enc_utf8)
17022 {
17023 int c, lc;
17024
17025 c = utf_ptr2char(p);
17026 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017027 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 /* TODO: reallocate string when byte count changes. */
17029 if (utf_char2len(lc) == l)
17030 utf_char2bytes(lc, p);
17031 p += l;
17032 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017033 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 p += l; /* skip multi-byte character */
17035 else
17036#endif
17037 {
17038 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17039 ++p;
17040 }
17041 }
17042}
17043
17044/*
17045 * "toupper(string)" function
17046 */
17047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017048f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017049 typval_T *argvars;
17050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017052 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054}
17055
17056/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017057 * "tr(string, fromstr, tostr)" function
17058 */
17059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017061 typval_T *argvars;
17062 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017063{
17064 char_u *instr;
17065 char_u *fromstr;
17066 char_u *tostr;
17067 char_u *p;
17068#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017069 int inlen;
17070 int fromlen;
17071 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017072 int idx;
17073 char_u *cpstr;
17074 int cplen;
17075 int first = TRUE;
17076#endif
17077 char_u buf[NUMBUFLEN];
17078 char_u buf2[NUMBUFLEN];
17079 garray_T ga;
17080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017081 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17083 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017084
17085 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017086 rettv->v_type = VAR_STRING;
17087 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 if (fromstr == NULL || tostr == NULL)
17089 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017090 ga_init2(&ga, (int)sizeof(char), 80);
17091
17092#ifdef FEAT_MBYTE
17093 if (!has_mbyte)
17094#endif
17095 /* not multi-byte: fromstr and tostr must be the same length */
17096 if (STRLEN(fromstr) != STRLEN(tostr))
17097 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017098#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017099error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017100#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017101 EMSG2(_(e_invarg2), fromstr);
17102 ga_clear(&ga);
17103 return;
17104 }
17105
17106 /* fromstr and tostr have to contain the same number of chars */
17107 while (*instr != NUL)
17108 {
17109#ifdef FEAT_MBYTE
17110 if (has_mbyte)
17111 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017112 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017113 cpstr = instr;
17114 cplen = inlen;
17115 idx = 0;
17116 for (p = fromstr; *p != NUL; p += fromlen)
17117 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017118 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017119 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17120 {
17121 for (p = tostr; *p != NUL; p += tolen)
17122 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017123 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017124 if (idx-- == 0)
17125 {
17126 cplen = tolen;
17127 cpstr = p;
17128 break;
17129 }
17130 }
17131 if (*p == NUL) /* tostr is shorter than fromstr */
17132 goto error;
17133 break;
17134 }
17135 ++idx;
17136 }
17137
17138 if (first && cpstr == instr)
17139 {
17140 /* Check that fromstr and tostr have the same number of
17141 * (multi-byte) characters. Done only once when a character
17142 * of instr doesn't appear in fromstr. */
17143 first = FALSE;
17144 for (p = tostr; *p != NUL; p += tolen)
17145 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017146 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017147 --idx;
17148 }
17149 if (idx != 0)
17150 goto error;
17151 }
17152
17153 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017154 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017155 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017156
17157 instr += inlen;
17158 }
17159 else
17160#endif
17161 {
17162 /* When not using multi-byte chars we can do it faster. */
17163 p = vim_strchr(fromstr, *instr);
17164 if (p != NULL)
17165 ga_append(&ga, tostr[p - fromstr]);
17166 else
17167 ga_append(&ga, *instr);
17168 ++instr;
17169 }
17170 }
17171
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017172 /* add a terminating NUL */
17173 ga_grow(&ga, 1);
17174 ga_append(&ga, NUL);
17175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017177}
17178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017179#ifdef FEAT_FLOAT
17180/*
17181 * "trunc({float})" function
17182 */
17183 static void
17184f_trunc(argvars, rettv)
17185 typval_T *argvars;
17186 typval_T *rettv;
17187{
17188 float_T f;
17189
17190 rettv->v_type = VAR_FLOAT;
17191 if (get_float_arg(argvars, &f) == OK)
17192 /* trunc() is not in C90, use floor() or ceil() instead. */
17193 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17194 else
17195 rettv->vval.v_float = 0.0;
17196}
17197#endif
17198
Bram Moolenaar8299df92004-07-10 09:47:34 +000017199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 * "type(expr)" function
17201 */
17202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017203f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017204 typval_T *argvars;
17205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017207 int n;
17208
17209 switch (argvars[0].v_type)
17210 {
17211 case VAR_NUMBER: n = 0; break;
17212 case VAR_STRING: n = 1; break;
17213 case VAR_FUNC: n = 2; break;
17214 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017215 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017216#ifdef FEAT_FLOAT
17217 case VAR_FLOAT: n = 5; break;
17218#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017219 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17220 }
17221 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222}
17223
17224/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017225 * "values(dict)" function
17226 */
17227 static void
17228f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017229 typval_T *argvars;
17230 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017231{
17232 dict_list(argvars, rettv, 1);
17233}
17234
17235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 * "virtcol(string)" function
17237 */
17238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017240 typval_T *argvars;
17241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242{
17243 colnr_T vcol = 0;
17244 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017245 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017247 fp = var2fpos(&argvars[0], FALSE, &fnum);
17248 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17249 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 {
17251 getvvcol(curwin, fp, NULL, NULL, &vcol);
17252 ++vcol;
17253 }
17254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017255 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256}
17257
17258/*
17259 * "visualmode()" function
17260 */
17261/*ARGSUSED*/
17262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 typval_T *argvars;
17265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266{
17267#ifdef FEAT_VISUAL
17268 char_u str[2];
17269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017270 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 str[0] = curbuf->b_visual_mode_eval;
17272 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017273 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274
17275 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017276 if ((argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number != 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017277 || (argvars[0].v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017278 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 curbuf->b_visual_mode_eval = NUL;
17280#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017281 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282#endif
17283}
17284
17285/*
17286 * "winbufnr(nr)" function
17287 */
17288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017289f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017290 typval_T *argvars;
17291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292{
17293 win_T *wp;
17294
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017295 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300}
17301
17302/*
17303 * "wincol()" function
17304 */
17305/*ARGSUSED*/
17306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017307f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 typval_T *argvars;
17309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310{
17311 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017312 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313}
17314
17315/*
17316 * "winheight(nr)" function
17317 */
17318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017320 typval_T *argvars;
17321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322{
17323 win_T *wp;
17324
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017325 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017327 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330}
17331
17332/*
17333 * "winline()" function
17334 */
17335/*ARGSUSED*/
17336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017337f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017338 typval_T *argvars;
17339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340{
17341 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017342 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343}
17344
17345/*
17346 * "winnr()" function
17347 */
17348/* ARGSUSED */
17349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017350f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017351 typval_T *argvars;
17352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353{
17354 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017355
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017357 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360}
17361
17362/*
17363 * "winrestcmd()" function
17364 */
17365/* ARGSUSED */
17366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017367f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 typval_T *argvars;
17369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370{
17371#ifdef FEAT_WINDOWS
17372 win_T *wp;
17373 int winnr = 1;
17374 garray_T ga;
17375 char_u buf[50];
17376
17377 ga_init2(&ga, (int)sizeof(char), 70);
17378 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17379 {
17380 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17381 ga_concat(&ga, buf);
17382# ifdef FEAT_VERTSPLIT
17383 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17384 ga_concat(&ga, buf);
17385# endif
17386 ++winnr;
17387 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017388 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017392 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017394 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395}
17396
17397/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017398 * "winrestview()" function
17399 */
17400/* ARGSUSED */
17401 static void
17402f_winrestview(argvars, rettv)
17403 typval_T *argvars;
17404 typval_T *rettv;
17405{
17406 dict_T *dict;
17407
17408 if (argvars[0].v_type != VAR_DICT
17409 || (dict = argvars[0].vval.v_dict) == NULL)
17410 EMSG(_(e_invarg));
17411 else
17412 {
17413 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17414 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17415#ifdef FEAT_VIRTUALEDIT
17416 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17417#endif
17418 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017419 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017420
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017421 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017422#ifdef FEAT_DIFF
17423 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17424#endif
17425 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17426 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17427
17428 check_cursor();
17429 changed_cline_bef_curs();
17430 invalidate_botline();
17431 redraw_later(VALID);
17432
17433 if (curwin->w_topline == 0)
17434 curwin->w_topline = 1;
17435 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17436 curwin->w_topline = curbuf->b_ml.ml_line_count;
17437#ifdef FEAT_DIFF
17438 check_topfill(curwin, TRUE);
17439#endif
17440 }
17441}
17442
17443/*
17444 * "winsaveview()" function
17445 */
17446/* ARGSUSED */
17447 static void
17448f_winsaveview(argvars, rettv)
17449 typval_T *argvars;
17450 typval_T *rettv;
17451{
17452 dict_T *dict;
17453
17454 dict = dict_alloc();
17455 if (dict == NULL)
17456 return;
17457 rettv->v_type = VAR_DICT;
17458 rettv->vval.v_dict = dict;
17459 ++dict->dv_refcount;
17460
17461 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17462 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17463#ifdef FEAT_VIRTUALEDIT
17464 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17465#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017466 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017467 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17468
17469 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17470#ifdef FEAT_DIFF
17471 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17472#endif
17473 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17474 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17475}
17476
17477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 * "winwidth(nr)" function
17479 */
17480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017482 typval_T *argvars;
17483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484{
17485 win_T *wp;
17486
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017487 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017489 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 else
17491#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017494 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495#endif
17496}
17497
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017499 * "writefile()" function
17500 */
17501 static void
17502f_writefile(argvars, rettv)
17503 typval_T *argvars;
17504 typval_T *rettv;
17505{
17506 int binary = FALSE;
17507 char_u *fname;
17508 FILE *fd;
17509 listitem_T *li;
17510 char_u *s;
17511 int ret = 0;
17512 int c;
17513
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017514 if (check_restricted() || check_secure())
17515 return;
17516
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017517 if (argvars[0].v_type != VAR_LIST)
17518 {
17519 EMSG2(_(e_listarg), "writefile()");
17520 return;
17521 }
17522 if (argvars[0].vval.v_list == NULL)
17523 return;
17524
17525 if (argvars[2].v_type != VAR_UNKNOWN
17526 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17527 binary = TRUE;
17528
17529 /* Always open the file in binary mode, library functions have a mind of
17530 * their own about CR-LF conversion. */
17531 fname = get_tv_string(&argvars[1]);
17532 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17533 {
17534 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17535 ret = -1;
17536 }
17537 else
17538 {
17539 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17540 li = li->li_next)
17541 {
17542 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17543 {
17544 if (*s == '\n')
17545 c = putc(NUL, fd);
17546 else
17547 c = putc(*s, fd);
17548 if (c == EOF)
17549 {
17550 ret = -1;
17551 break;
17552 }
17553 }
17554 if (!binary || li->li_next != NULL)
17555 if (putc('\n', fd) == EOF)
17556 {
17557 ret = -1;
17558 break;
17559 }
17560 if (ret < 0)
17561 {
17562 EMSG(_(e_write));
17563 break;
17564 }
17565 }
17566 fclose(fd);
17567 }
17568
17569 rettv->vval.v_number = ret;
17570}
17571
17572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017574 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 */
17576 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017577var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017578 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017579 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017580 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017582 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017584 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585
Bram Moolenaara5525202006-03-02 22:52:09 +000017586 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017587 if (varp->v_type == VAR_LIST)
17588 {
17589 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017590 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017591 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017592 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017593
17594 l = varp->vval.v_list;
17595 if (l == NULL)
17596 return NULL;
17597
17598 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017599 pos.lnum = list_find_nr(l, 0L, &error);
17600 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017601 return NULL; /* invalid line number */
17602
17603 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017604 pos.col = list_find_nr(l, 1L, &error);
17605 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017606 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017607 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017608
17609 /* We accept "$" for the column number: last column. */
17610 li = list_find(l, 1L);
17611 if (li != NULL && li->li_tv.v_type == VAR_STRING
17612 && li->li_tv.vval.v_string != NULL
17613 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17614 pos.col = len + 1;
17615
Bram Moolenaara5525202006-03-02 22:52:09 +000017616 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017617 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017618 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017619 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017620
Bram Moolenaara5525202006-03-02 22:52:09 +000017621#ifdef FEAT_VIRTUALEDIT
17622 /* Get the virtual offset. Defaults to zero. */
17623 pos.coladd = list_find_nr(l, 2L, &error);
17624 if (error)
17625 pos.coladd = 0;
17626#endif
17627
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017628 return &pos;
17629 }
17630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017631 name = get_tv_string_chk(varp);
17632 if (name == NULL)
17633 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017634 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017636#ifdef FEAT_VISUAL
17637 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17638 {
17639 if (VIsual_active)
17640 return &VIsual;
17641 return &curwin->w_cursor;
17642 }
17643#endif
17644 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017646 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17648 return NULL;
17649 return pp;
17650 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017651
17652#ifdef FEAT_VIRTUALEDIT
17653 pos.coladd = 0;
17654#endif
17655
Bram Moolenaar477933c2007-07-17 14:32:23 +000017656 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017657 {
17658 pos.col = 0;
17659 if (name[1] == '0') /* "w0": first visible line */
17660 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017661 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017662 pos.lnum = curwin->w_topline;
17663 return &pos;
17664 }
17665 else if (name[1] == '$') /* "w$": last visible line */
17666 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017667 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017668 pos.lnum = curwin->w_botline - 1;
17669 return &pos;
17670 }
17671 }
17672 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017674 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 {
17676 pos.lnum = curbuf->b_ml.ml_line_count;
17677 pos.col = 0;
17678 }
17679 else
17680 {
17681 pos.lnum = curwin->w_cursor.lnum;
17682 pos.col = (colnr_T)STRLEN(ml_get_curline());
17683 }
17684 return &pos;
17685 }
17686 return NULL;
17687}
17688
17689/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017690 * Convert list in "arg" into a position and optional file number.
17691 * When "fnump" is NULL there is no file number, only 3 items.
17692 * Note that the column is passed on as-is, the caller may want to decrement
17693 * it to use 1 for the first column.
17694 * Return FAIL when conversion is not possible, doesn't check the position for
17695 * validity.
17696 */
17697 static int
17698list2fpos(arg, posp, fnump)
17699 typval_T *arg;
17700 pos_T *posp;
17701 int *fnump;
17702{
17703 list_T *l = arg->vval.v_list;
17704 long i = 0;
17705 long n;
17706
Bram Moolenaarbde35262006-07-23 20:12:24 +000017707 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17708 * when "fnump" isn't NULL and "coladd" is optional. */
17709 if (arg->v_type != VAR_LIST
17710 || l == NULL
17711 || l->lv_len < (fnump == NULL ? 2 : 3)
17712 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017713 return FAIL;
17714
17715 if (fnump != NULL)
17716 {
17717 n = list_find_nr(l, i++, NULL); /* fnum */
17718 if (n < 0)
17719 return FAIL;
17720 if (n == 0)
17721 n = curbuf->b_fnum; /* current buffer */
17722 *fnump = n;
17723 }
17724
17725 n = list_find_nr(l, i++, NULL); /* lnum */
17726 if (n < 0)
17727 return FAIL;
17728 posp->lnum = n;
17729
17730 n = list_find_nr(l, i++, NULL); /* col */
17731 if (n < 0)
17732 return FAIL;
17733 posp->col = n;
17734
17735#ifdef FEAT_VIRTUALEDIT
17736 n = list_find_nr(l, i, NULL);
17737 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017738 posp->coladd = 0;
17739 else
17740 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017741#endif
17742
17743 return OK;
17744}
17745
17746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 * Get the length of an environment variable name.
17748 * Advance "arg" to the first character after the name.
17749 * Return 0 for error.
17750 */
17751 static int
17752get_env_len(arg)
17753 char_u **arg;
17754{
17755 char_u *p;
17756 int len;
17757
17758 for (p = *arg; vim_isIDc(*p); ++p)
17759 ;
17760 if (p == *arg) /* no name found */
17761 return 0;
17762
17763 len = (int)(p - *arg);
17764 *arg = p;
17765 return len;
17766}
17767
17768/*
17769 * Get the length of the name of a function or internal variable.
17770 * "arg" is advanced to the first non-white character after the name.
17771 * Return 0 if something is wrong.
17772 */
17773 static int
17774get_id_len(arg)
17775 char_u **arg;
17776{
17777 char_u *p;
17778 int len;
17779
17780 /* Find the end of the name. */
17781 for (p = *arg; eval_isnamec(*p); ++p)
17782 ;
17783 if (p == *arg) /* no name found */
17784 return 0;
17785
17786 len = (int)(p - *arg);
17787 *arg = skipwhite(p);
17788
17789 return len;
17790}
17791
17792/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017793 * Get the length of the name of a variable or function.
17794 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017796 * Return -1 if curly braces expansion failed.
17797 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 * If the name contains 'magic' {}'s, expand them and return the
17799 * expanded name in an allocated string via 'alias' - caller must free.
17800 */
17801 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017802get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 char_u **arg;
17804 char_u **alias;
17805 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017806 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807{
17808 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 char_u *p;
17810 char_u *expr_start;
17811 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812
17813 *alias = NULL; /* default to no alias */
17814
17815 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17816 && (*arg)[2] == (int)KE_SNR)
17817 {
17818 /* hard coded <SNR>, already translated */
17819 *arg += 3;
17820 return get_id_len(arg) + 3;
17821 }
17822 len = eval_fname_script(*arg);
17823 if (len > 0)
17824 {
17825 /* literal "<SID>", "s:" or "<SNR>" */
17826 *arg += len;
17827 }
17828
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017830 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017832 p = find_name_end(*arg, &expr_start, &expr_end,
17833 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 if (expr_start != NULL)
17835 {
17836 char_u *temp_string;
17837
17838 if (!evaluate)
17839 {
17840 len += (int)(p - *arg);
17841 *arg = skipwhite(p);
17842 return len;
17843 }
17844
17845 /*
17846 * Include any <SID> etc in the expanded string:
17847 * Thus the -len here.
17848 */
17849 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17850 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017851 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 *alias = temp_string;
17853 *arg = skipwhite(p);
17854 return (int)STRLEN(temp_string);
17855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856
17857 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017858 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 EMSG2(_(e_invexpr2), *arg);
17860
17861 return len;
17862}
17863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864/*
17865 * Find the end of a variable or function name, taking care of magic braces.
17866 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17867 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017868 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869 * Return a pointer to just after the name. Equal to "arg" if there is no
17870 * valid name.
17871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017873find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 char_u *arg;
17875 char_u **expr_start;
17876 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017877 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017879 int mb_nest = 0;
17880 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 char_u *p;
17882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017883 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017885 *expr_start = NULL;
17886 *expr_end = NULL;
17887 }
17888
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017889 /* Quick check for valid starting character. */
17890 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17891 return arg;
17892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017893 for (p = arg; *p != NUL
17894 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017895 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017896 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017897 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017898 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017899 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017900 if (*p == '\'')
17901 {
17902 /* skip over 'string' to avoid counting [ and ] inside it. */
17903 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17904 ;
17905 if (*p == NUL)
17906 break;
17907 }
17908 else if (*p == '"')
17909 {
17910 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17911 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17912 if (*p == '\\' && p[1] != NUL)
17913 ++p;
17914 if (*p == NUL)
17915 break;
17916 }
17917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017920 if (*p == '[')
17921 ++br_nest;
17922 else if (*p == ']')
17923 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017926 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017928 if (*p == '{')
17929 {
17930 mb_nest++;
17931 if (expr_start != NULL && *expr_start == NULL)
17932 *expr_start = p;
17933 }
17934 else if (*p == '}')
17935 {
17936 mb_nest--;
17937 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17938 *expr_end = p;
17939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941 }
17942
17943 return p;
17944}
17945
17946/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017947 * Expands out the 'magic' {}'s in a variable/function name.
17948 * Note that this can call itself recursively, to deal with
17949 * constructs like foo{bar}{baz}{bam}
17950 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17951 * "in_start" ^
17952 * "expr_start" ^
17953 * "expr_end" ^
17954 * "in_end" ^
17955 *
17956 * Returns a new allocated string, which the caller must free.
17957 * Returns NULL for failure.
17958 */
17959 static char_u *
17960make_expanded_name(in_start, expr_start, expr_end, in_end)
17961 char_u *in_start;
17962 char_u *expr_start;
17963 char_u *expr_end;
17964 char_u *in_end;
17965{
17966 char_u c1;
17967 char_u *retval = NULL;
17968 char_u *temp_result;
17969 char_u *nextcmd = NULL;
17970
17971 if (expr_end == NULL || in_end == NULL)
17972 return NULL;
17973 *expr_start = NUL;
17974 *expr_end = NUL;
17975 c1 = *in_end;
17976 *in_end = NUL;
17977
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017978 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017979 if (temp_result != NULL && nextcmd == NULL)
17980 {
17981 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17982 + (in_end - expr_end) + 1));
17983 if (retval != NULL)
17984 {
17985 STRCPY(retval, in_start);
17986 STRCAT(retval, temp_result);
17987 STRCAT(retval, expr_end + 1);
17988 }
17989 }
17990 vim_free(temp_result);
17991
17992 *in_end = c1; /* put char back for error messages */
17993 *expr_start = '{';
17994 *expr_end = '}';
17995
17996 if (retval != NULL)
17997 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017998 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017999 if (expr_start != NULL)
18000 {
18001 /* Further expansion! */
18002 temp_result = make_expanded_name(retval, expr_start,
18003 expr_end, temp_result);
18004 vim_free(retval);
18005 retval = temp_result;
18006 }
18007 }
18008
18009 return retval;
18010}
18011
18012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018014 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 */
18016 static int
18017eval_isnamec(c)
18018 int c;
18019{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018020 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18021}
18022
18023/*
18024 * Return TRUE if character "c" can be used as the first character in a
18025 * variable or function name (excluding '{' and '}').
18026 */
18027 static int
18028eval_isnamec1(c)
18029 int c;
18030{
18031 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032}
18033
18034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 * Set number v: variable to "val".
18036 */
18037 void
18038set_vim_var_nr(idx, val)
18039 int idx;
18040 long val;
18041{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018042 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043}
18044
18045/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018046 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 */
18048 long
18049get_vim_var_nr(idx)
18050 int idx;
18051{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018052 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053}
18054
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018055#if defined(FEAT_AUTOCMD) || defined(PROTO)
18056/*
18057 * Get string v: variable value. Uses a static buffer, can only be used once.
18058 */
18059 char_u *
18060get_vim_var_str(idx)
18061 int idx;
18062{
18063 return get_tv_string(&vimvars[idx].vv_tv);
18064}
18065#endif
18066
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067/*
18068 * Set v:count, v:count1 and v:prevcount.
18069 */
18070 void
18071set_vcount(count, count1)
18072 long count;
18073 long count1;
18074{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018075 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
18076 vimvars[VV_COUNT].vv_nr = count;
18077 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078}
18079
18080/*
18081 * Set string v: variable to a copy of "val".
18082 */
18083 void
18084set_vim_var_string(idx, val, len)
18085 int idx;
18086 char_u *val;
18087 int len; /* length of "val" to use or -1 (whole string) */
18088{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018089 /* Need to do this (at least) once, since we can't initialize a union.
18090 * Will always be invoked when "v:progname" is set. */
18091 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18092
Bram Moolenaare9a41262005-01-15 22:18:47 +000018093 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018095 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018097 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018099 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100}
18101
18102/*
18103 * Set v:register if needed.
18104 */
18105 void
18106set_reg_var(c)
18107 int c;
18108{
18109 char_u regname;
18110
18111 if (c == 0 || c == ' ')
18112 regname = '"';
18113 else
18114 regname = c;
18115 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018116 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 set_vim_var_string(VV_REG, &regname, 1);
18118}
18119
18120/*
18121 * Get or set v:exception. If "oldval" == NULL, return the current value.
18122 * Otherwise, restore the value to "oldval" and return NULL.
18123 * Must always be called in pairs to save and restore v:exception! Does not
18124 * take care of memory allocations.
18125 */
18126 char_u *
18127v_exception(oldval)
18128 char_u *oldval;
18129{
18130 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018131 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132
Bram Moolenaare9a41262005-01-15 22:18:47 +000018133 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 return NULL;
18135}
18136
18137/*
18138 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18139 * Otherwise, restore the value to "oldval" and return NULL.
18140 * Must always be called in pairs to save and restore v:throwpoint! Does not
18141 * take care of memory allocations.
18142 */
18143 char_u *
18144v_throwpoint(oldval)
18145 char_u *oldval;
18146{
18147 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018148 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149
Bram Moolenaare9a41262005-01-15 22:18:47 +000018150 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 return NULL;
18152}
18153
18154#if defined(FEAT_AUTOCMD) || defined(PROTO)
18155/*
18156 * Set v:cmdarg.
18157 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18158 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18159 * Must always be called in pairs!
18160 */
18161 char_u *
18162set_cmdarg(eap, oldarg)
18163 exarg_T *eap;
18164 char_u *oldarg;
18165{
18166 char_u *oldval;
18167 char_u *newval;
18168 unsigned len;
18169
Bram Moolenaare9a41262005-01-15 22:18:47 +000018170 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018171 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018173 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018174 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018175 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 }
18177
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018178 if (eap->force_bin == FORCE_BIN)
18179 len = 6;
18180 else if (eap->force_bin == FORCE_NOBIN)
18181 len = 8;
18182 else
18183 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018184
18185 if (eap->read_edit)
18186 len += 7;
18187
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018188 if (eap->force_ff != 0)
18189 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18190# ifdef FEAT_MBYTE
18191 if (eap->force_enc != 0)
18192 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018193 if (eap->bad_char != 0)
18194 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018195# endif
18196
18197 newval = alloc(len + 1);
18198 if (newval == NULL)
18199 return NULL;
18200
18201 if (eap->force_bin == FORCE_BIN)
18202 sprintf((char *)newval, " ++bin");
18203 else if (eap->force_bin == FORCE_NOBIN)
18204 sprintf((char *)newval, " ++nobin");
18205 else
18206 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018207
18208 if (eap->read_edit)
18209 STRCAT(newval, " ++edit");
18210
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018211 if (eap->force_ff != 0)
18212 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18213 eap->cmd + eap->force_ff);
18214# ifdef FEAT_MBYTE
18215 if (eap->force_enc != 0)
18216 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18217 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018218 if (eap->bad_char != 0)
18219 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18220 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018221# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018222 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018223 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224}
18225#endif
18226
18227/*
18228 * Get the value of internal variable "name".
18229 * Return OK or FAIL.
18230 */
18231 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018232get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 char_u *name;
18234 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018235 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018236 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237{
18238 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018239 typval_T *tv = NULL;
18240 typval_T atv;
18241 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243
18244 /* truncate the name, so that we can use strcmp() */
18245 cc = name[len];
18246 name[len] = NUL;
18247
18248 /*
18249 * Check for "b:changedtick".
18250 */
18251 if (STRCMP(name, "b:changedtick") == 0)
18252 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018253 atv.v_type = VAR_NUMBER;
18254 atv.vval.v_number = curbuf->b_changedtick;
18255 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256 }
18257
18258 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 * Check for user-defined variables.
18260 */
18261 else
18262 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018263 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018265 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 }
18267
Bram Moolenaare9a41262005-01-15 22:18:47 +000018268 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018270 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018271 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 ret = FAIL;
18273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018274 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018275 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276
18277 name[len] = cc;
18278
18279 return ret;
18280}
18281
18282/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018283 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18284 * Also handle function call with Funcref variable: func(expr)
18285 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18286 */
18287 static int
18288handle_subscript(arg, rettv, evaluate, verbose)
18289 char_u **arg;
18290 typval_T *rettv;
18291 int evaluate; /* do more than finding the end */
18292 int verbose; /* give error messages */
18293{
18294 int ret = OK;
18295 dict_T *selfdict = NULL;
18296 char_u *s;
18297 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018298 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018299
18300 while (ret == OK
18301 && (**arg == '['
18302 || (**arg == '.' && rettv->v_type == VAR_DICT)
18303 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18304 && !vim_iswhite(*(*arg - 1)))
18305 {
18306 if (**arg == '(')
18307 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018308 /* need to copy the funcref so that we can clear rettv */
18309 functv = *rettv;
18310 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018311
18312 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018313 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018314 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018315 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18316 &len, evaluate, selfdict);
18317
18318 /* Clear the funcref afterwards, so that deleting it while
18319 * evaluating the arguments is possible (see test55). */
18320 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018321
18322 /* Stop the expression evaluation when immediately aborting on
18323 * error, or when an interrupt occurred or an exception was thrown
18324 * but not caught. */
18325 if (aborting())
18326 {
18327 if (ret == OK)
18328 clear_tv(rettv);
18329 ret = FAIL;
18330 }
18331 dict_unref(selfdict);
18332 selfdict = NULL;
18333 }
18334 else /* **arg == '[' || **arg == '.' */
18335 {
18336 dict_unref(selfdict);
18337 if (rettv->v_type == VAR_DICT)
18338 {
18339 selfdict = rettv->vval.v_dict;
18340 if (selfdict != NULL)
18341 ++selfdict->dv_refcount;
18342 }
18343 else
18344 selfdict = NULL;
18345 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18346 {
18347 clear_tv(rettv);
18348 ret = FAIL;
18349 }
18350 }
18351 }
18352 dict_unref(selfdict);
18353 return ret;
18354}
18355
18356/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018357 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018358 * value).
18359 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018360 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018361alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018362{
Bram Moolenaar33570922005-01-25 22:26:29 +000018363 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018364}
18365
18366/*
18367 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 * The string "s" must have been allocated, it is consumed.
18369 * Return NULL for out of memory, the variable otherwise.
18370 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018372alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 char_u *s;
18374{
Bram Moolenaar33570922005-01-25 22:26:29 +000018375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018377 rettv = alloc_tv();
18378 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018380 rettv->v_type = VAR_STRING;
18381 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 }
18383 else
18384 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386}
18387
18388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018389 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018391 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018393 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394{
18395 if (varp != NULL)
18396 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018397 switch (varp->v_type)
18398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018399 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018400 func_unref(varp->vval.v_string);
18401 /*FALLTHROUGH*/
18402 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018403 vim_free(varp->vval.v_string);
18404 break;
18405 case VAR_LIST:
18406 list_unref(varp->vval.v_list);
18407 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018408 case VAR_DICT:
18409 dict_unref(varp->vval.v_dict);
18410 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018411 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018412#ifdef FEAT_FLOAT
18413 case VAR_FLOAT:
18414#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018415 case VAR_UNKNOWN:
18416 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018417 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018418 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018419 break;
18420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 vim_free(varp);
18422 }
18423}
18424
18425/*
18426 * Free the memory for a variable value and set the value to NULL or 0.
18427 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018428 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431{
18432 if (varp != NULL)
18433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018434 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018436 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018437 func_unref(varp->vval.v_string);
18438 /*FALLTHROUGH*/
18439 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018440 vim_free(varp->vval.v_string);
18441 varp->vval.v_string = NULL;
18442 break;
18443 case VAR_LIST:
18444 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018445 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018446 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018447 case VAR_DICT:
18448 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018449 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018450 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018451 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018452 varp->vval.v_number = 0;
18453 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018454#ifdef FEAT_FLOAT
18455 case VAR_FLOAT:
18456 varp->vval.v_float = 0.0;
18457 break;
18458#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018459 case VAR_UNKNOWN:
18460 break;
18461 default:
18462 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018464 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 }
18466}
18467
18468/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469 * Set the value of a variable to NULL without freeing items.
18470 */
18471 static void
18472init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018473 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474{
18475 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018476 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018477}
18478
18479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 * Get the number value of a variable.
18481 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018482 * For incompatible types, return 0.
18483 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18484 * caller of incompatible types: it sets *denote to TRUE if "denote"
18485 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 */
18487 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018488get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018489 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018491 int error = FALSE;
18492
18493 return get_tv_number_chk(varp, &error); /* return 0L on error */
18494}
18495
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018496 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018497get_tv_number_chk(varp, denote)
18498 typval_T *varp;
18499 int *denote;
18500{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018501 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018503 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018505 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018506 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018507#ifdef FEAT_FLOAT
18508 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018509 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018510 break;
18511#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018512 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018513 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018514 break;
18515 case VAR_STRING:
18516 if (varp->vval.v_string != NULL)
18517 vim_str2nr(varp->vval.v_string, NULL, NULL,
18518 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018519 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018520 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018521 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018522 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018523 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018524 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018525 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018526 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018527 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018530 if (denote == NULL) /* useful for values that must be unsigned */
18531 n = -1;
18532 else
18533 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018534 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535}
18536
18537/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018538 * Get the lnum from the first argument.
18539 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018540 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 */
18542 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018543get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545{
Bram Moolenaar33570922005-01-25 22:26:29 +000018546 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 linenr_T lnum;
18548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018549 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 if (lnum == 0) /* no valid number, try using line() */
18551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018552 rettv.v_type = VAR_NUMBER;
18553 f_line(argvars, &rettv);
18554 lnum = rettv.vval.v_number;
18555 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 }
18557 return lnum;
18558}
18559
18560/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018561 * Get the lnum from the first argument.
18562 * Also accepts "$", then "buf" is used.
18563 * Returns 0 on error.
18564 */
18565 static linenr_T
18566get_tv_lnum_buf(argvars, buf)
18567 typval_T *argvars;
18568 buf_T *buf;
18569{
18570 if (argvars[0].v_type == VAR_STRING
18571 && argvars[0].vval.v_string != NULL
18572 && argvars[0].vval.v_string[0] == '$'
18573 && buf != NULL)
18574 return buf->b_ml.ml_line_count;
18575 return get_tv_number_chk(&argvars[0], NULL);
18576}
18577
18578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 * Get the string value of a variable.
18580 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018581 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18582 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 * If the String variable has never been set, return an empty string.
18584 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018585 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18586 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 */
18588 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018590 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591{
18592 static char_u mybuf[NUMBUFLEN];
18593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595}
18596
18597 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018599 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018600 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018602 char_u *res = get_tv_string_buf_chk(varp, buf);
18603
18604 return res != NULL ? res : (char_u *)"";
18605}
18606
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018607 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018608get_tv_string_chk(varp)
18609 typval_T *varp;
18610{
18611 static char_u mybuf[NUMBUFLEN];
18612
18613 return get_tv_string_buf_chk(varp, mybuf);
18614}
18615
18616 static char_u *
18617get_tv_string_buf_chk(varp, buf)
18618 typval_T *varp;
18619 char_u *buf;
18620{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018621 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018623 case VAR_NUMBER:
18624 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18625 return buf;
18626 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018627 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018628 break;
18629 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018630 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018631 break;
18632 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018633 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018634 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018635#ifdef FEAT_FLOAT
18636 case VAR_FLOAT:
18637 EMSG(_("E806: using Float as a String"));
18638 break;
18639#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018640 case VAR_STRING:
18641 if (varp->vval.v_string != NULL)
18642 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018643 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018644 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018645 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018648 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649}
18650
18651/*
18652 * Find variable "name" in the list of variables.
18653 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018654 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018655 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018656 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018658 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018659find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018661 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018664 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665
Bram Moolenaara7043832005-01-21 11:56:39 +000018666 ht = find_var_ht(name, &varname);
18667 if (htp != NULL)
18668 *htp = ht;
18669 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018671 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672}
18673
18674/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018675 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018676 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018678 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018679find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018680 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018681 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018682 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018683{
Bram Moolenaar33570922005-01-25 22:26:29 +000018684 hashitem_T *hi;
18685
18686 if (*varname == NUL)
18687 {
18688 /* Must be something like "s:", otherwise "ht" would be NULL. */
18689 switch (varname[-2])
18690 {
18691 case 's': return &SCRIPT_SV(current_SID).sv_var;
18692 case 'g': return &globvars_var;
18693 case 'v': return &vimvars_var;
18694 case 'b': return &curbuf->b_bufvar;
18695 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018696#ifdef FEAT_WINDOWS
18697 case 't': return &curtab->tp_winvar;
18698#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018699 case 'l': return current_funccal == NULL
18700 ? NULL : &current_funccal->l_vars_var;
18701 case 'a': return current_funccal == NULL
18702 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018703 }
18704 return NULL;
18705 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018706
18707 hi = hash_find(ht, varname);
18708 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018709 {
18710 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018711 * worked find the variable again. Don't auto-load a script if it was
18712 * loaded already, otherwise it would be loaded every time when
18713 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018714 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018715 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018716 hi = hash_find(ht, varname);
18717 if (HASHITEM_EMPTY(hi))
18718 return NULL;
18719 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018720 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018721}
18722
18723/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018724 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018725 * Set "varname" to the start of name without ':'.
18726 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018727 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018728find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 char_u *name;
18730 char_u **varname;
18731{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018732 hashitem_T *hi;
18733
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 if (name[1] != ':')
18735 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018736 /* The name must not start with a colon or #. */
18737 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 return NULL;
18739 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018740
18741 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018742 hi = hash_find(&compat_hashtab, name);
18743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018744 return &compat_hashtab;
18745
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018747 return &globvarht; /* global variable */
18748 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 }
18750 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018751 if (*name == 'g') /* global variable */
18752 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018753 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18754 */
18755 if (vim_strchr(name + 2, ':') != NULL
18756 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018757 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018759 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018762#ifdef FEAT_WINDOWS
18763 if (*name == 't') /* tab page variable */
18764 return &curtab->tp_vars.dv_hashtab;
18765#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 if (*name == 'v') /* v: variable */
18767 return &vimvarht;
18768 if (*name == 'a' && current_funccal != NULL) /* function argument */
18769 return &current_funccal->l_avars.dv_hashtab;
18770 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18771 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 if (*name == 's' /* script variable */
18773 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18774 return &SCRIPT_VARS(current_SID);
18775 return NULL;
18776}
18777
18778/*
18779 * Get the string value of a (global/local) variable.
18780 * Returns NULL when it doesn't exist.
18781 */
18782 char_u *
18783get_var_value(name)
18784 char_u *name;
18785{
Bram Moolenaar33570922005-01-25 22:26:29 +000018786 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787
Bram Moolenaara7043832005-01-21 11:56:39 +000018788 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 if (v == NULL)
18790 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792}
18793
18794/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018795 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 * sourcing this script and when executing functions defined in the script.
18797 */
18798 void
18799new_script_vars(id)
18800 scid_T id;
18801{
Bram Moolenaara7043832005-01-21 11:56:39 +000018802 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018803 hashtab_T *ht;
18804 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018805
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18807 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018808 /* Re-allocating ga_data means that an ht_array pointing to
18809 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018811 for (i = 1; i <= ga_scripts.ga_len; ++i)
18812 {
18813 ht = &SCRIPT_VARS(i);
18814 if (ht->ht_mask == HT_INIT_SIZE - 1)
18815 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 sv = &SCRIPT_SV(i);
18817 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018818 }
18819
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 while (ga_scripts.ga_len < id)
18821 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018822 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18823 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 }
18826 }
18827}
18828
18829/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018830 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18831 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 */
18833 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018834init_var_dict(dict, dict_var)
18835 dict_T *dict;
18836 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837{
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 hash_init(&dict->dv_hashtab);
18839 dict->dv_refcount = 99999;
18840 dict_var->di_tv.vval.v_dict = dict;
18841 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018842 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18844 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845}
18846
18847/*
18848 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018849 * Frees all allocated variables and the value they contain.
18850 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 */
18852 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018853vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 hashtab_T *ht;
18855{
18856 vars_clear_ext(ht, TRUE);
18857}
18858
18859/*
18860 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18861 */
18862 static void
18863vars_clear_ext(ht, free_val)
18864 hashtab_T *ht;
18865 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866{
Bram Moolenaara7043832005-01-21 11:56:39 +000018867 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018868 hashitem_T *hi;
18869 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018872 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018873 for (hi = ht->ht_array; todo > 0; ++hi)
18874 {
18875 if (!HASHITEM_EMPTY(hi))
18876 {
18877 --todo;
18878
Bram Moolenaar33570922005-01-25 22:26:29 +000018879 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018880 * ht_array might change then. hash_clear() takes care of it
18881 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018882 v = HI2DI(hi);
18883 if (free_val)
18884 clear_tv(&v->di_tv);
18885 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18886 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018887 }
18888 }
18889 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018890 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891}
18892
Bram Moolenaara7043832005-01-21 11:56:39 +000018893/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018894 * Delete a variable from hashtab "ht" at item "hi".
18895 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018898delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018899 hashtab_T *ht;
18900 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901{
Bram Moolenaar33570922005-01-25 22:26:29 +000018902 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018903
18904 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018905 clear_tv(&di->di_tv);
18906 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907}
18908
18909/*
18910 * List the value of one internal variable.
18911 */
18912 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018913list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018914 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018916 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018918 char_u *tofree;
18919 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018920 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018921
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018922 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018923 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018924 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018925 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926}
18927
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018929list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 char_u *prefix;
18931 char_u *name;
18932 int type;
18933 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018934 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935{
Bram Moolenaar31859182007-08-14 20:41:13 +000018936 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18937 msg_start();
18938 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 if (name != NULL) /* "a:" vars don't have a name stored */
18940 msg_puts(name);
18941 msg_putchar(' ');
18942 msg_advance(22);
18943 if (type == VAR_NUMBER)
18944 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018945 else if (type == VAR_FUNC)
18946 msg_putchar('*');
18947 else if (type == VAR_LIST)
18948 {
18949 msg_putchar('[');
18950 if (*string == '[')
18951 ++string;
18952 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018953 else if (type == VAR_DICT)
18954 {
18955 msg_putchar('{');
18956 if (*string == '{')
18957 ++string;
18958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 else
18960 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018961
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018963
18964 if (type == VAR_FUNC)
18965 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018966 if (*first)
18967 {
18968 msg_clr_eos();
18969 *first = FALSE;
18970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971}
18972
18973/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018974 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 * If the variable already exists, the value is updated.
18976 * Otherwise the variable is created.
18977 */
18978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018979set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018981 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018982 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983{
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018986 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018987 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018989 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018990 {
18991 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18992 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18993 ? name[2] : name[0]))
18994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018995 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018996 return;
18997 }
18998 if (function_exists(name))
18999 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019000 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019001 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019002 return;
19003 }
19004 }
19005
Bram Moolenaara7043832005-01-21 11:56:39 +000019006 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019007 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019008 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019009 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019010 return;
19011 }
19012
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019013 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019016 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019017 if (var_check_ro(v->di_flags, name)
19018 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019019 return;
19020 if (v->di_tv.v_type != tv->v_type
19021 && !((v->di_tv.v_type == VAR_STRING
19022 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019023 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019024 || tv->v_type == VAR_NUMBER))
19025#ifdef FEAT_FLOAT
19026 && !((v->di_tv.v_type == VAR_NUMBER
19027 || v->di_tv.v_type == VAR_FLOAT)
19028 && (tv->v_type == VAR_NUMBER
19029 || tv->v_type == VAR_FLOAT))
19030#endif
19031 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019032 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019033 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019034 return;
19035 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019036
19037 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019038 * Handle setting internal v: variables separately: we don't change
19039 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019040 */
19041 if (ht == &vimvarht)
19042 {
19043 if (v->di_tv.v_type == VAR_STRING)
19044 {
19045 vim_free(v->di_tv.vval.v_string);
19046 if (copy || tv->v_type != VAR_STRING)
19047 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19048 else
19049 {
19050 /* Take over the string to avoid an extra alloc/free. */
19051 v->di_tv.vval.v_string = tv->vval.v_string;
19052 tv->vval.v_string = NULL;
19053 }
19054 }
19055 else if (v->di_tv.v_type != VAR_NUMBER)
19056 EMSG2(_(e_intern2), "set_var()");
19057 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019058 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019060 if (STRCMP(varname, "searchforward") == 0)
19061 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19062 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 return;
19064 }
19065
19066 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 }
19068 else /* add a new variable */
19069 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019070 /* Can't add "v:" variable. */
19071 if (ht == &vimvarht)
19072 {
19073 EMSG2(_(e_illvar), name);
19074 return;
19075 }
19076
Bram Moolenaar92124a32005-06-17 22:03:40 +000019077 /* Make sure the variable name is valid. */
19078 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019079 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19080 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019081 {
19082 EMSG2(_(e_illvar), varname);
19083 return;
19084 }
19085
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019086 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19087 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019088 if (v == NULL)
19089 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019090 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019091 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019093 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 return;
19095 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019096 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019098
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019099 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019100 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019101 else
19102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019103 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019104 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019105 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107}
19108
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019109/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019110 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 * Also give an error message.
19112 */
19113 static int
19114var_check_ro(flags, name)
19115 int flags;
19116 char_u *name;
19117{
19118 if (flags & DI_FLAGS_RO)
19119 {
19120 EMSG2(_(e_readonlyvar), name);
19121 return TRUE;
19122 }
19123 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19124 {
19125 EMSG2(_(e_readonlysbx), name);
19126 return TRUE;
19127 }
19128 return FALSE;
19129}
19130
19131/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019132 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19133 * Also give an error message.
19134 */
19135 static int
19136var_check_fixed(flags, name)
19137 int flags;
19138 char_u *name;
19139{
19140 if (flags & DI_FLAGS_FIX)
19141 {
19142 EMSG2(_("E795: Cannot delete variable %s"), name);
19143 return TRUE;
19144 }
19145 return FALSE;
19146}
19147
19148/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019149 * Return TRUE if typeval "tv" is set to be locked (immutable).
19150 * Also give an error message, using "name".
19151 */
19152 static int
19153tv_check_lock(lock, name)
19154 int lock;
19155 char_u *name;
19156{
19157 if (lock & VAR_LOCKED)
19158 {
19159 EMSG2(_("E741: Value is locked: %s"),
19160 name == NULL ? (char_u *)_("Unknown") : name);
19161 return TRUE;
19162 }
19163 if (lock & VAR_FIXED)
19164 {
19165 EMSG2(_("E742: Cannot change value of %s"),
19166 name == NULL ? (char_u *)_("Unknown") : name);
19167 return TRUE;
19168 }
19169 return FALSE;
19170}
19171
19172/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019173 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019174 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019175 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019179 typval_T *from;
19180 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019182 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019183 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019184 switch (from->v_type)
19185 {
19186 case VAR_NUMBER:
19187 to->vval.v_number = from->vval.v_number;
19188 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019189#ifdef FEAT_FLOAT
19190 case VAR_FLOAT:
19191 to->vval.v_float = from->vval.v_float;
19192 break;
19193#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019194 case VAR_STRING:
19195 case VAR_FUNC:
19196 if (from->vval.v_string == NULL)
19197 to->vval.v_string = NULL;
19198 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019199 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019200 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019201 if (from->v_type == VAR_FUNC)
19202 func_ref(to->vval.v_string);
19203 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019204 break;
19205 case VAR_LIST:
19206 if (from->vval.v_list == NULL)
19207 to->vval.v_list = NULL;
19208 else
19209 {
19210 to->vval.v_list = from->vval.v_list;
19211 ++to->vval.v_list->lv_refcount;
19212 }
19213 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019214 case VAR_DICT:
19215 if (from->vval.v_dict == NULL)
19216 to->vval.v_dict = NULL;
19217 else
19218 {
19219 to->vval.v_dict = from->vval.v_dict;
19220 ++to->vval.v_dict->dv_refcount;
19221 }
19222 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019225 break;
19226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227}
19228
19229/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019230 * Make a copy of an item.
19231 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019232 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19233 * reference to an already copied list/dict can be used.
19234 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019235 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019236 static int
19237item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019238 typval_T *from;
19239 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019240 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019241 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019242{
19243 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019244 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019245
Bram Moolenaar33570922005-01-25 22:26:29 +000019246 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019247 {
19248 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019249 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019250 }
19251 ++recurse;
19252
19253 switch (from->v_type)
19254 {
19255 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019256#ifdef FEAT_FLOAT
19257 case VAR_FLOAT:
19258#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019259 case VAR_STRING:
19260 case VAR_FUNC:
19261 copy_tv(from, to);
19262 break;
19263 case VAR_LIST:
19264 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019265 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019266 if (from->vval.v_list == NULL)
19267 to->vval.v_list = NULL;
19268 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19269 {
19270 /* use the copy made earlier */
19271 to->vval.v_list = from->vval.v_list->lv_copylist;
19272 ++to->vval.v_list->lv_refcount;
19273 }
19274 else
19275 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19276 if (to->vval.v_list == NULL)
19277 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019278 break;
19279 case VAR_DICT:
19280 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019281 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019282 if (from->vval.v_dict == NULL)
19283 to->vval.v_dict = NULL;
19284 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19285 {
19286 /* use the copy made earlier */
19287 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19288 ++to->vval.v_dict->dv_refcount;
19289 }
19290 else
19291 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19292 if (to->vval.v_dict == NULL)
19293 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019294 break;
19295 default:
19296 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019297 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019298 }
19299 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019300 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019301}
19302
19303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 * ":echo expr1 ..." print each argument separated with a space, add a
19305 * newline at the end.
19306 * ":echon expr1 ..." print each argument plain.
19307 */
19308 void
19309ex_echo(eap)
19310 exarg_T *eap;
19311{
19312 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019313 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019314 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 char_u *p;
19316 int needclr = TRUE;
19317 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019318 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319
19320 if (eap->skip)
19321 ++emsg_skip;
19322 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19323 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019324 /* If eval1() causes an error message the text from the command may
19325 * still need to be cleared. E.g., "echo 22,44". */
19326 need_clr_eos = needclr;
19327
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019329 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 {
19331 /*
19332 * Report the invalid expression unless the expression evaluation
19333 * has been cancelled due to an aborting error, an interrupt, or an
19334 * exception.
19335 */
19336 if (!aborting())
19337 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019338 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 break;
19340 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019341 need_clr_eos = FALSE;
19342
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 if (!eap->skip)
19344 {
19345 if (atstart)
19346 {
19347 atstart = FALSE;
19348 /* Call msg_start() after eval1(), evaluating the expression
19349 * may cause a message to appear. */
19350 if (eap->cmdidx == CMD_echo)
19351 msg_start();
19352 }
19353 else if (eap->cmdidx == CMD_echo)
19354 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019355 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019356 if (p != NULL)
19357 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019359 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019361 if (*p != TAB && needclr)
19362 {
19363 /* remove any text still there from the command */
19364 msg_clr_eos();
19365 needclr = FALSE;
19366 }
19367 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 }
19369 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019370 {
19371#ifdef FEAT_MBYTE
19372 if (has_mbyte)
19373 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019374 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019375
19376 (void)msg_outtrans_len_attr(p, i, echo_attr);
19377 p += i - 1;
19378 }
19379 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019381 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019384 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 arg = skipwhite(arg);
19388 }
19389 eap->nextcmd = check_nextcmd(arg);
19390
19391 if (eap->skip)
19392 --emsg_skip;
19393 else
19394 {
19395 /* remove text that may still be there from the command */
19396 if (needclr)
19397 msg_clr_eos();
19398 if (eap->cmdidx == CMD_echo)
19399 msg_end();
19400 }
19401}
19402
19403/*
19404 * ":echohl {name}".
19405 */
19406 void
19407ex_echohl(eap)
19408 exarg_T *eap;
19409{
19410 int id;
19411
19412 id = syn_name2id(eap->arg);
19413 if (id == 0)
19414 echo_attr = 0;
19415 else
19416 echo_attr = syn_id2attr(id);
19417}
19418
19419/*
19420 * ":execute expr1 ..." execute the result of an expression.
19421 * ":echomsg expr1 ..." Print a message
19422 * ":echoerr expr1 ..." Print an error
19423 * Each gets spaces around each argument and a newline at the end for
19424 * echo commands
19425 */
19426 void
19427ex_execute(eap)
19428 exarg_T *eap;
19429{
19430 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019431 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 int ret = OK;
19433 char_u *p;
19434 garray_T ga;
19435 int len;
19436 int save_did_emsg;
19437
19438 ga_init2(&ga, 1, 80);
19439
19440 if (eap->skip)
19441 ++emsg_skip;
19442 while (*arg != NUL && *arg != '|' && *arg != '\n')
19443 {
19444 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 {
19447 /*
19448 * Report the invalid expression unless the expression evaluation
19449 * has been cancelled due to an aborting error, an interrupt, or an
19450 * exception.
19451 */
19452 if (!aborting())
19453 EMSG2(_(e_invexpr2), p);
19454 ret = FAIL;
19455 break;
19456 }
19457
19458 if (!eap->skip)
19459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019460 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 len = (int)STRLEN(p);
19462 if (ga_grow(&ga, len + 2) == FAIL)
19463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019464 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 ret = FAIL;
19466 break;
19467 }
19468 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 ga.ga_len += len;
19472 }
19473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 arg = skipwhite(arg);
19476 }
19477
19478 if (ret != FAIL && ga.ga_data != NULL)
19479 {
19480 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019483 out_flush();
19484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 else if (eap->cmdidx == CMD_echoerr)
19486 {
19487 /* We don't want to abort following commands, restore did_emsg. */
19488 save_did_emsg = did_emsg;
19489 EMSG((char_u *)ga.ga_data);
19490 if (!force_abort)
19491 did_emsg = save_did_emsg;
19492 }
19493 else if (eap->cmdidx == CMD_execute)
19494 do_cmdline((char_u *)ga.ga_data,
19495 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19496 }
19497
19498 ga_clear(&ga);
19499
19500 if (eap->skip)
19501 --emsg_skip;
19502
19503 eap->nextcmd = check_nextcmd(arg);
19504}
19505
19506/*
19507 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19508 * "arg" points to the "&" or '+' when called, to "option" when returning.
19509 * Returns NULL when no option name found. Otherwise pointer to the char
19510 * after the option name.
19511 */
19512 static char_u *
19513find_option_end(arg, opt_flags)
19514 char_u **arg;
19515 int *opt_flags;
19516{
19517 char_u *p = *arg;
19518
19519 ++p;
19520 if (*p == 'g' && p[1] == ':')
19521 {
19522 *opt_flags = OPT_GLOBAL;
19523 p += 2;
19524 }
19525 else if (*p == 'l' && p[1] == ':')
19526 {
19527 *opt_flags = OPT_LOCAL;
19528 p += 2;
19529 }
19530 else
19531 *opt_flags = 0;
19532
19533 if (!ASCII_ISALPHA(*p))
19534 return NULL;
19535 *arg = p;
19536
19537 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19538 p += 4; /* termcap option */
19539 else
19540 while (ASCII_ISALPHA(*p))
19541 ++p;
19542 return p;
19543}
19544
19545/*
19546 * ":function"
19547 */
19548 void
19549ex_function(eap)
19550 exarg_T *eap;
19551{
19552 char_u *theline;
19553 int j;
19554 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 char_u *name = NULL;
19557 char_u *p;
19558 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019559 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 garray_T newargs;
19561 garray_T newlines;
19562 int varargs = FALSE;
19563 int mustend = FALSE;
19564 int flags = 0;
19565 ufunc_T *fp;
19566 int indent;
19567 int nesting;
19568 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019569 dictitem_T *v;
19570 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019571 static int func_nr = 0; /* number for nameless function */
19572 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019573 hashtab_T *ht;
19574 int todo;
19575 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019576 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577
19578 /*
19579 * ":function" without argument: list functions.
19580 */
19581 if (ends_excmd(*eap->arg))
19582 {
19583 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019584 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019585 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019586 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019587 {
19588 if (!HASHITEM_EMPTY(hi))
19589 {
19590 --todo;
19591 fp = HI2UF(hi);
19592 if (!isdigit(*fp->uf_name))
19593 list_func_head(fp, FALSE);
19594 }
19595 }
19596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 eap->nextcmd = check_nextcmd(eap->arg);
19598 return;
19599 }
19600
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019601 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019602 * ":function /pat": list functions matching pattern.
19603 */
19604 if (*eap->arg == '/')
19605 {
19606 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19607 if (!eap->skip)
19608 {
19609 regmatch_T regmatch;
19610
19611 c = *p;
19612 *p = NUL;
19613 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19614 *p = c;
19615 if (regmatch.regprog != NULL)
19616 {
19617 regmatch.rm_ic = p_ic;
19618
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019619 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019620 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19621 {
19622 if (!HASHITEM_EMPTY(hi))
19623 {
19624 --todo;
19625 fp = HI2UF(hi);
19626 if (!isdigit(*fp->uf_name)
19627 && vim_regexec(&regmatch, fp->uf_name, 0))
19628 list_func_head(fp, FALSE);
19629 }
19630 }
19631 }
19632 }
19633 if (*p == '/')
19634 ++p;
19635 eap->nextcmd = check_nextcmd(p);
19636 return;
19637 }
19638
19639 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019640 * Get the function name. There are these situations:
19641 * func normal function name
19642 * "name" == func, "fudi.fd_dict" == NULL
19643 * dict.func new dictionary entry
19644 * "name" == NULL, "fudi.fd_dict" set,
19645 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19646 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019647 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019648 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19649 * dict.func existing dict entry that's not a Funcref
19650 * "name" == NULL, "fudi.fd_dict" set,
19651 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019654 name = trans_function_name(&p, eap->skip, 0, &fudi);
19655 paren = (vim_strchr(p, '(') != NULL);
19656 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 {
19658 /*
19659 * Return on an invalid expression in braces, unless the expression
19660 * evaluation has been cancelled due to an aborting error, an
19661 * interrupt, or an exception.
19662 */
19663 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019665 if (!eap->skip && fudi.fd_newkey != NULL)
19666 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019667 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 else
19671 eap->skip = TRUE;
19672 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019673
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 /* An error in a function call during evaluation of an expression in magic
19675 * braces should not cause the function not to be defined. */
19676 saved_did_emsg = did_emsg;
19677 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678
19679 /*
19680 * ":function func" with only function name: list function.
19681 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019682 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 {
19684 if (!ends_excmd(*skipwhite(p)))
19685 {
19686 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019687 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 }
19689 eap->nextcmd = check_nextcmd(p);
19690 if (eap->nextcmd != NULL)
19691 *p = NUL;
19692 if (!eap->skip && !got_int)
19693 {
19694 fp = find_func(name);
19695 if (fp != NULL)
19696 {
19697 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019698 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019700 if (FUNCLINE(fp, j) == NULL)
19701 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 msg_putchar('\n');
19703 msg_outnum((long)(j + 1));
19704 if (j < 9)
19705 msg_putchar(' ');
19706 if (j < 99)
19707 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019708 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 out_flush(); /* show a line at a time */
19710 ui_breakcheck();
19711 }
19712 if (!got_int)
19713 {
19714 msg_putchar('\n');
19715 msg_puts((char_u *)" endfunction");
19716 }
19717 }
19718 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019719 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019721 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 }
19723
19724 /*
19725 * ":function name(arg1, arg2)" Define function.
19726 */
19727 p = skipwhite(p);
19728 if (*p != '(')
19729 {
19730 if (!eap->skip)
19731 {
19732 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019733 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 }
19735 /* attempt to continue by skipping some text */
19736 if (vim_strchr(p, '(') != NULL)
19737 p = vim_strchr(p, '(');
19738 }
19739 p = skipwhite(p + 1);
19740
19741 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19742 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19743
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019744 if (!eap->skip)
19745 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019746 /* Check the name of the function. Unless it's a dictionary function
19747 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019748 if (name != NULL)
19749 arg = name;
19750 else
19751 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019752 if (arg != NULL && (fudi.fd_di == NULL
19753 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019754 {
19755 if (*arg == K_SPECIAL)
19756 j = 3;
19757 else
19758 j = 0;
19759 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19760 : eval_isnamec(arg[j])))
19761 ++j;
19762 if (arg[j] != NUL)
19763 emsg_funcname(_(e_invarg2), arg);
19764 }
19765 }
19766
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 /*
19768 * Isolate the arguments: "arg1, arg2, ...)"
19769 */
19770 while (*p != ')')
19771 {
19772 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19773 {
19774 varargs = TRUE;
19775 p += 3;
19776 mustend = TRUE;
19777 }
19778 else
19779 {
19780 arg = p;
19781 while (ASCII_ISALNUM(*p) || *p == '_')
19782 ++p;
19783 if (arg == p || isdigit(*arg)
19784 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19785 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19786 {
19787 if (!eap->skip)
19788 EMSG2(_("E125: Illegal argument: %s"), arg);
19789 break;
19790 }
19791 if (ga_grow(&newargs, 1) == FAIL)
19792 goto erret;
19793 c = *p;
19794 *p = NUL;
19795 arg = vim_strsave(arg);
19796 if (arg == NULL)
19797 goto erret;
19798 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19799 *p = c;
19800 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 if (*p == ',')
19802 ++p;
19803 else
19804 mustend = TRUE;
19805 }
19806 p = skipwhite(p);
19807 if (mustend && *p != ')')
19808 {
19809 if (!eap->skip)
19810 EMSG2(_(e_invarg2), eap->arg);
19811 break;
19812 }
19813 }
19814 ++p; /* skip the ')' */
19815
Bram Moolenaare9a41262005-01-15 22:18:47 +000019816 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 for (;;)
19818 {
19819 p = skipwhite(p);
19820 if (STRNCMP(p, "range", 5) == 0)
19821 {
19822 flags |= FC_RANGE;
19823 p += 5;
19824 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019825 else if (STRNCMP(p, "dict", 4) == 0)
19826 {
19827 flags |= FC_DICT;
19828 p += 4;
19829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 else if (STRNCMP(p, "abort", 5) == 0)
19831 {
19832 flags |= FC_ABORT;
19833 p += 5;
19834 }
19835 else
19836 break;
19837 }
19838
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019839 /* When there is a line break use what follows for the function body.
19840 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19841 if (*p == '\n')
19842 line_arg = p + 1;
19843 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 EMSG(_(e_trailing));
19845
19846 /*
19847 * Read the body of the function, until ":endfunction" is found.
19848 */
19849 if (KeyTyped)
19850 {
19851 /* Check if the function already exists, don't let the user type the
19852 * whole function before telling him it doesn't work! For a script we
19853 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019854 if (!eap->skip && !eap->forceit)
19855 {
19856 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19857 EMSG(_(e_funcdict));
19858 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019859 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019862 if (!eap->skip && did_emsg)
19863 goto erret;
19864
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 msg_putchar('\n'); /* don't overwrite the function name */
19866 cmdline_row = msg_row;
19867 }
19868
19869 indent = 2;
19870 nesting = 0;
19871 for (;;)
19872 {
19873 msg_scroll = TRUE;
19874 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019875 sourcing_lnum_off = sourcing_lnum;
19876
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019877 if (line_arg != NULL)
19878 {
19879 /* Use eap->arg, split up in parts by line breaks. */
19880 theline = line_arg;
19881 p = vim_strchr(theline, '\n');
19882 if (p == NULL)
19883 line_arg += STRLEN(line_arg);
19884 else
19885 {
19886 *p = NUL;
19887 line_arg = p + 1;
19888 }
19889 }
19890 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 theline = getcmdline(':', 0L, indent);
19892 else
19893 theline = eap->getline(':', eap->cookie, indent);
19894 if (KeyTyped)
19895 lines_left = Rows - 1;
19896 if (theline == NULL)
19897 {
19898 EMSG(_("E126: Missing :endfunction"));
19899 goto erret;
19900 }
19901
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019902 /* Detect line continuation: sourcing_lnum increased more than one. */
19903 if (sourcing_lnum > sourcing_lnum_off + 1)
19904 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19905 else
19906 sourcing_lnum_off = 0;
19907
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 if (skip_until != NULL)
19909 {
19910 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19911 * don't check for ":endfunc". */
19912 if (STRCMP(theline, skip_until) == 0)
19913 {
19914 vim_free(skip_until);
19915 skip_until = NULL;
19916 }
19917 }
19918 else
19919 {
19920 /* skip ':' and blanks*/
19921 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19922 ;
19923
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019924 /* Check for "endfunction". */
19925 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019927 if (line_arg == NULL)
19928 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 break;
19930 }
19931
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019932 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * at "end". */
19934 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19935 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019936 else if (STRNCMP(p, "if", 2) == 0
19937 || STRNCMP(p, "wh", 2) == 0
19938 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 || STRNCMP(p, "try", 3) == 0)
19940 indent += 2;
19941
19942 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019943 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019945 if (*p == '!')
19946 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 p += eval_fname_script(p);
19948 if (ASCII_ISALPHA(*p))
19949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019950 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 if (*skipwhite(p) == '(')
19952 {
19953 ++nesting;
19954 indent += 2;
19955 }
19956 }
19957 }
19958
19959 /* Check for ":append" or ":insert". */
19960 p = skip_range(p, NULL);
19961 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19962 || (p[0] == 'i'
19963 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19964 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19965 skip_until = vim_strsave((char_u *)".");
19966
19967 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19968 arg = skipwhite(skiptowhite(p));
19969 if (arg[0] == '<' && arg[1] =='<'
19970 && ((p[0] == 'p' && p[1] == 'y'
19971 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19972 || (p[0] == 'p' && p[1] == 'e'
19973 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19974 || (p[0] == 't' && p[1] == 'c'
19975 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19976 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19977 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019978 || (p[0] == 'm' && p[1] == 'z'
19979 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 ))
19981 {
19982 /* ":python <<" continues until a dot, like ":append" */
19983 p = skipwhite(arg + 2);
19984 if (*p == NUL)
19985 skip_until = vim_strsave((char_u *)".");
19986 else
19987 skip_until = vim_strsave(p);
19988 }
19989 }
19990
19991 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019992 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019993 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019994 if (line_arg == NULL)
19995 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019997 }
19998
19999 /* Copy the line to newly allocated memory. get_one_sourceline()
20000 * allocates 250 bytes per line, this saves 80% on average. The cost
20001 * is an extra alloc/free. */
20002 p = vim_strsave(theline);
20003 if (p != NULL)
20004 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020005 if (line_arg == NULL)
20006 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020007 theline = p;
20008 }
20009
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020010 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20011
20012 /* Add NULL lines for continuation lines, so that the line count is
20013 * equal to the index in the growarray. */
20014 while (sourcing_lnum_off-- > 0)
20015 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020016
20017 /* Check for end of eap->arg. */
20018 if (line_arg != NULL && *line_arg == NUL)
20019 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
20021
20022 /* Don't define the function when skipping commands or when an error was
20023 * detected. */
20024 if (eap->skip || did_emsg)
20025 goto erret;
20026
20027 /*
20028 * If there are no errors, add the function
20029 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020030 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020032 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020033 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020034 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020035 emsg_funcname("E707: Function name conflicts with variable: %s",
20036 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020037 goto erret;
20038 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020039
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020040 fp = find_func(name);
20041 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020043 if (!eap->forceit)
20044 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020045 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020046 goto erret;
20047 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020048 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020049 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020050 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020051 name);
20052 goto erret;
20053 }
20054 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020055 ga_clear_strings(&(fp->uf_args));
20056 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020057 vim_free(name);
20058 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 }
20061 else
20062 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020063 char numbuf[20];
20064
20065 fp = NULL;
20066 if (fudi.fd_newkey == NULL && !eap->forceit)
20067 {
20068 EMSG(_(e_funcdict));
20069 goto erret;
20070 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020071 if (fudi.fd_di == NULL)
20072 {
20073 /* Can't add a function to a locked dictionary */
20074 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20075 goto erret;
20076 }
20077 /* Can't change an existing function if it is locked */
20078 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20079 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020080
20081 /* Give the function a sequential number. Can only be used with a
20082 * Funcref! */
20083 vim_free(name);
20084 sprintf(numbuf, "%d", ++func_nr);
20085 name = vim_strsave((char_u *)numbuf);
20086 if (name == NULL)
20087 goto erret;
20088 }
20089
20090 if (fp == NULL)
20091 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020092 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020093 {
20094 int slen, plen;
20095 char_u *scriptname;
20096
20097 /* Check that the autoload name matches the script name. */
20098 j = FAIL;
20099 if (sourcing_name != NULL)
20100 {
20101 scriptname = autoload_name(name);
20102 if (scriptname != NULL)
20103 {
20104 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020105 plen = (int)STRLEN(p);
20106 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020107 if (slen > plen && fnamecmp(p,
20108 sourcing_name + slen - plen) == 0)
20109 j = OK;
20110 vim_free(scriptname);
20111 }
20112 }
20113 if (j == FAIL)
20114 {
20115 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20116 goto erret;
20117 }
20118 }
20119
20120 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 if (fp == NULL)
20122 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020123
20124 if (fudi.fd_dict != NULL)
20125 {
20126 if (fudi.fd_di == NULL)
20127 {
20128 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020129 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020130 if (fudi.fd_di == NULL)
20131 {
20132 vim_free(fp);
20133 goto erret;
20134 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020135 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20136 {
20137 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020138 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020139 goto erret;
20140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020141 }
20142 else
20143 /* overwrite existing dict entry */
20144 clear_tv(&fudi.fd_di->di_tv);
20145 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020146 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020147 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020148 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020149
20150 /* behave like "dict" was used */
20151 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020152 }
20153
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020155 STRCPY(fp->uf_name, name);
20156 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020158 fp->uf_args = newargs;
20159 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020160#ifdef FEAT_PROFILE
20161 fp->uf_tml_count = NULL;
20162 fp->uf_tml_total = NULL;
20163 fp->uf_tml_self = NULL;
20164 fp->uf_profiling = FALSE;
20165 if (prof_def_func())
20166 func_do_profile(fp);
20167#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020168 fp->uf_varargs = varargs;
20169 fp->uf_flags = flags;
20170 fp->uf_calls = 0;
20171 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020172 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173
20174erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 ga_clear_strings(&newargs);
20176 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020177ret_free:
20178 vim_free(skip_until);
20179 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182}
20183
20184/*
20185 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020186 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020188 * flags:
20189 * TFN_INT: internal function name OK
20190 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 * Advances "pp" to just after the function name (if no error).
20192 */
20193 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020194trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 char_u **pp;
20196 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020197 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020200 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 char_u *start;
20202 char_u *end;
20203 int lead;
20204 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020207
20208 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020211
20212 /* Check for hard coded <SNR>: already translated function ID (from a user
20213 * command). */
20214 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20215 && (*pp)[2] == (int)KE_SNR)
20216 {
20217 *pp += 3;
20218 len = get_id_len(pp) + 3;
20219 return vim_strnsave(start, len);
20220 }
20221
20222 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20223 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020225 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020227
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020228 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20229 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020230 if (end == start)
20231 {
20232 if (!skip)
20233 EMSG(_("E129: Function name required"));
20234 goto theend;
20235 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020236 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020237 {
20238 /*
20239 * Report an invalid expression in braces, unless the expression
20240 * evaluation has been cancelled due to an aborting error, an
20241 * interrupt, or an exception.
20242 */
20243 if (!aborting())
20244 {
20245 if (end != NULL)
20246 EMSG2(_(e_invarg2), start);
20247 }
20248 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020249 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020250 goto theend;
20251 }
20252
20253 if (lv.ll_tv != NULL)
20254 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020255 if (fdp != NULL)
20256 {
20257 fdp->fd_dict = lv.ll_dict;
20258 fdp->fd_newkey = lv.ll_newkey;
20259 lv.ll_newkey = NULL;
20260 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020261 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020262 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20263 {
20264 name = vim_strsave(lv.ll_tv->vval.v_string);
20265 *pp = end;
20266 }
20267 else
20268 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020269 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20270 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020271 EMSG(_(e_funcref));
20272 else
20273 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020274 name = NULL;
20275 }
20276 goto theend;
20277 }
20278
20279 if (lv.ll_name == NULL)
20280 {
20281 /* Error found, but continue after the function name. */
20282 *pp = end;
20283 goto theend;
20284 }
20285
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020286 /* Check if the name is a Funcref. If so, use the value. */
20287 if (lv.ll_exp_name != NULL)
20288 {
20289 len = (int)STRLEN(lv.ll_exp_name);
20290 name = deref_func_name(lv.ll_exp_name, &len);
20291 if (name == lv.ll_exp_name)
20292 name = NULL;
20293 }
20294 else
20295 {
20296 len = (int)(end - *pp);
20297 name = deref_func_name(*pp, &len);
20298 if (name == *pp)
20299 name = NULL;
20300 }
20301 if (name != NULL)
20302 {
20303 name = vim_strsave(name);
20304 *pp = end;
20305 goto theend;
20306 }
20307
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020308 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020309 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020310 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020311 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20312 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20313 {
20314 /* When there was "s:" already or the name expanded to get a
20315 * leading "s:" then remove it. */
20316 lv.ll_name += 2;
20317 len -= 2;
20318 lead = 2;
20319 }
20320 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020321 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020322 {
20323 if (lead == 2) /* skip over "s:" */
20324 lv.ll_name += 2;
20325 len = (int)(end - lv.ll_name);
20326 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020327
20328 /*
20329 * Copy the function name to allocated memory.
20330 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20331 * Accept <SNR>123_name() outside a script.
20332 */
20333 if (skip)
20334 lead = 0; /* do nothing */
20335 else if (lead > 0)
20336 {
20337 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020338 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20339 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020340 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020341 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020342 if (current_SID <= 0)
20343 {
20344 EMSG(_(e_usingsid));
20345 goto theend;
20346 }
20347 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20348 lead += (int)STRLEN(sid_buf);
20349 }
20350 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020351 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020352 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020353 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020354 goto theend;
20355 }
20356 name = alloc((unsigned)(len + lead + 1));
20357 if (name != NULL)
20358 {
20359 if (lead > 0)
20360 {
20361 name[0] = K_SPECIAL;
20362 name[1] = KS_EXTRA;
20363 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020364 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020365 STRCPY(name + 3, sid_buf);
20366 }
20367 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20368 name[len + lead] = NUL;
20369 }
20370 *pp = end;
20371
20372theend:
20373 clear_lval(&lv);
20374 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375}
20376
20377/*
20378 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20379 * Return 2 if "p" starts with "s:".
20380 * Return 0 otherwise.
20381 */
20382 static int
20383eval_fname_script(p)
20384 char_u *p;
20385{
20386 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20387 || STRNICMP(p + 1, "SNR>", 4) == 0))
20388 return 5;
20389 if (p[0] == 's' && p[1] == ':')
20390 return 2;
20391 return 0;
20392}
20393
20394/*
20395 * Return TRUE if "p" starts with "<SID>" or "s:".
20396 * Only works if eval_fname_script() returned non-zero for "p"!
20397 */
20398 static int
20399eval_fname_sid(p)
20400 char_u *p;
20401{
20402 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20403}
20404
20405/*
20406 * List the head of the function: "name(arg1, arg2)".
20407 */
20408 static void
20409list_func_head(fp, indent)
20410 ufunc_T *fp;
20411 int indent;
20412{
20413 int j;
20414
20415 msg_start();
20416 if (indent)
20417 MSG_PUTS(" ");
20418 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020419 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 {
20421 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020422 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 }
20424 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020425 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020427 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 {
20429 if (j)
20430 MSG_PUTS(", ");
20431 msg_puts(FUNCARG(fp, j));
20432 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020433 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 {
20435 if (j)
20436 MSG_PUTS(", ");
20437 MSG_PUTS("...");
20438 }
20439 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020440 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020441 if (p_verbose > 0)
20442 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443}
20444
20445/*
20446 * Find a function by name, return pointer to it in ufuncs.
20447 * Return NULL for unknown function.
20448 */
20449 static ufunc_T *
20450find_func(name)
20451 char_u *name;
20452{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020453 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020455 hi = hash_find(&func_hashtab, name);
20456 if (!HASHITEM_EMPTY(hi))
20457 return HI2UF(hi);
20458 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459}
20460
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020461#if defined(EXITFREE) || defined(PROTO)
20462 void
20463free_all_functions()
20464{
20465 hashitem_T *hi;
20466
20467 /* Need to start all over every time, because func_free() may change the
20468 * hash table. */
20469 while (func_hashtab.ht_used > 0)
20470 for (hi = func_hashtab.ht_array; ; ++hi)
20471 if (!HASHITEM_EMPTY(hi))
20472 {
20473 func_free(HI2UF(hi));
20474 break;
20475 }
20476}
20477#endif
20478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020479/*
20480 * Return TRUE if a function "name" exists.
20481 */
20482 static int
20483function_exists(name)
20484 char_u *name;
20485{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020486 char_u *nm = name;
20487 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020488 int n = FALSE;
20489
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020490 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020491 nm = skipwhite(nm);
20492
20493 /* Only accept "funcname", "funcname ", "funcname (..." and
20494 * "funcname(...", not "funcname!...". */
20495 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020497 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020498 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020499 else
20500 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020501 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020502 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020503 return n;
20504}
20505
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020506/*
20507 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020508 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020509 */
20510 static int
20511builtin_function(name)
20512 char_u *name;
20513{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020514 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20515 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020516}
20517
Bram Moolenaar05159a02005-02-26 23:04:13 +000020518#if defined(FEAT_PROFILE) || defined(PROTO)
20519/*
20520 * Start profiling function "fp".
20521 */
20522 static void
20523func_do_profile(fp)
20524 ufunc_T *fp;
20525{
20526 fp->uf_tm_count = 0;
20527 profile_zero(&fp->uf_tm_self);
20528 profile_zero(&fp->uf_tm_total);
20529 if (fp->uf_tml_count == NULL)
20530 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20531 (sizeof(int) * fp->uf_lines.ga_len));
20532 if (fp->uf_tml_total == NULL)
20533 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20534 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20535 if (fp->uf_tml_self == NULL)
20536 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20537 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20538 fp->uf_tml_idx = -1;
20539 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20540 || fp->uf_tml_self == NULL)
20541 return; /* out of memory */
20542
20543 fp->uf_profiling = TRUE;
20544}
20545
20546/*
20547 * Dump the profiling results for all functions in file "fd".
20548 */
20549 void
20550func_dump_profile(fd)
20551 FILE *fd;
20552{
20553 hashitem_T *hi;
20554 int todo;
20555 ufunc_T *fp;
20556 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020557 ufunc_T **sorttab;
20558 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020559
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020560 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000020561 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20562
Bram Moolenaar05159a02005-02-26 23:04:13 +000020563 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20564 {
20565 if (!HASHITEM_EMPTY(hi))
20566 {
20567 --todo;
20568 fp = HI2UF(hi);
20569 if (fp->uf_profiling)
20570 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020571 if (sorttab != NULL)
20572 sorttab[st_len++] = fp;
20573
Bram Moolenaar05159a02005-02-26 23:04:13 +000020574 if (fp->uf_name[0] == K_SPECIAL)
20575 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20576 else
20577 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20578 if (fp->uf_tm_count == 1)
20579 fprintf(fd, "Called 1 time\n");
20580 else
20581 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20582 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20583 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20584 fprintf(fd, "\n");
20585 fprintf(fd, "count total (s) self (s)\n");
20586
20587 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20588 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020589 if (FUNCLINE(fp, i) == NULL)
20590 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020591 prof_func_line(fd, fp->uf_tml_count[i],
20592 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020593 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20594 }
20595 fprintf(fd, "\n");
20596 }
20597 }
20598 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020599
20600 if (sorttab != NULL && st_len > 0)
20601 {
20602 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20603 prof_total_cmp);
20604 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20605 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20606 prof_self_cmp);
20607 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20608 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020609}
Bram Moolenaar73830342005-02-28 22:48:19 +000020610
20611 static void
20612prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20613 FILE *fd;
20614 ufunc_T **sorttab;
20615 int st_len;
20616 char *title;
20617 int prefer_self; /* when equal print only self time */
20618{
20619 int i;
20620 ufunc_T *fp;
20621
20622 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20623 fprintf(fd, "count total (s) self (s) function\n");
20624 for (i = 0; i < 20 && i < st_len; ++i)
20625 {
20626 fp = sorttab[i];
20627 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20628 prefer_self);
20629 if (fp->uf_name[0] == K_SPECIAL)
20630 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20631 else
20632 fprintf(fd, " %s()\n", fp->uf_name);
20633 }
20634 fprintf(fd, "\n");
20635}
20636
20637/*
20638 * Print the count and times for one function or function line.
20639 */
20640 static void
20641prof_func_line(fd, count, total, self, prefer_self)
20642 FILE *fd;
20643 int count;
20644 proftime_T *total;
20645 proftime_T *self;
20646 int prefer_self; /* when equal print only self time */
20647{
20648 if (count > 0)
20649 {
20650 fprintf(fd, "%5d ", count);
20651 if (prefer_self && profile_equal(total, self))
20652 fprintf(fd, " ");
20653 else
20654 fprintf(fd, "%s ", profile_msg(total));
20655 if (!prefer_self && profile_equal(total, self))
20656 fprintf(fd, " ");
20657 else
20658 fprintf(fd, "%s ", profile_msg(self));
20659 }
20660 else
20661 fprintf(fd, " ");
20662}
20663
20664/*
20665 * Compare function for total time sorting.
20666 */
20667 static int
20668#ifdef __BORLANDC__
20669_RTLENTRYF
20670#endif
20671prof_total_cmp(s1, s2)
20672 const void *s1;
20673 const void *s2;
20674{
20675 ufunc_T *p1, *p2;
20676
20677 p1 = *(ufunc_T **)s1;
20678 p2 = *(ufunc_T **)s2;
20679 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20680}
20681
20682/*
20683 * Compare function for self time sorting.
20684 */
20685 static int
20686#ifdef __BORLANDC__
20687_RTLENTRYF
20688#endif
20689prof_self_cmp(s1, s2)
20690 const void *s1;
20691 const void *s2;
20692{
20693 ufunc_T *p1, *p2;
20694
20695 p1 = *(ufunc_T **)s1;
20696 p2 = *(ufunc_T **)s2;
20697 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20698}
20699
Bram Moolenaar05159a02005-02-26 23:04:13 +000020700#endif
20701
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020702/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020703 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020704 * Return TRUE if a package was loaded.
20705 */
20706 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020707script_autoload(name, reload)
20708 char_u *name;
20709 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020710{
20711 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020712 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020713 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020714 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020715
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020716 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020717 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020718 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020719 return FALSE;
20720
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020721 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020722
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020723 /* Find the name in the list of previously loaded package names. Skip
20724 * "autoload/", it's always the same. */
20725 for (i = 0; i < ga_loaded.ga_len; ++i)
20726 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20727 break;
20728 if (!reload && i < ga_loaded.ga_len)
20729 ret = FALSE; /* was loaded already */
20730 else
20731 {
20732 /* Remember the name if it wasn't loaded already. */
20733 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20734 {
20735 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20736 tofree = NULL;
20737 }
20738
20739 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020740 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020741 ret = TRUE;
20742 }
20743
20744 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020745 return ret;
20746}
20747
20748/*
20749 * Return the autoload script name for a function or variable name.
20750 * Returns NULL when out of memory.
20751 */
20752 static char_u *
20753autoload_name(name)
20754 char_u *name;
20755{
20756 char_u *p;
20757 char_u *scriptname;
20758
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020759 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020760 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20761 if (scriptname == NULL)
20762 return FALSE;
20763 STRCPY(scriptname, "autoload/");
20764 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020765 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020766 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020767 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020768 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020769 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020770}
20771
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20773
20774/*
20775 * Function given to ExpandGeneric() to obtain the list of user defined
20776 * function names.
20777 */
20778 char_u *
20779get_user_func_name(xp, idx)
20780 expand_T *xp;
20781 int idx;
20782{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020783 static long_u done;
20784 static hashitem_T *hi;
20785 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786
20787 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020789 done = 0;
20790 hi = func_hashtab.ht_array;
20791 }
20792 if (done < func_hashtab.ht_used)
20793 {
20794 if (done++ > 0)
20795 ++hi;
20796 while (HASHITEM_EMPTY(hi))
20797 ++hi;
20798 fp = HI2UF(hi);
20799
20800 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20801 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802
20803 cat_func_name(IObuff, fp);
20804 if (xp->xp_context != EXPAND_USER_FUNC)
20805 {
20806 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020807 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 STRCAT(IObuff, ")");
20809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 return IObuff;
20811 }
20812 return NULL;
20813}
20814
20815#endif /* FEAT_CMDL_COMPL */
20816
20817/*
20818 * Copy the function name of "fp" to buffer "buf".
20819 * "buf" must be able to hold the function name plus three bytes.
20820 * Takes care of script-local function names.
20821 */
20822 static void
20823cat_func_name(buf, fp)
20824 char_u *buf;
20825 ufunc_T *fp;
20826{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020827 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 {
20829 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020830 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 }
20832 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020833 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834}
20835
20836/*
20837 * ":delfunction {name}"
20838 */
20839 void
20840ex_delfunction(eap)
20841 exarg_T *eap;
20842{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 char_u *p;
20845 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847
20848 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020849 name = trans_function_name(&p, eap->skip, 0, &fudi);
20850 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020852 {
20853 if (fudi.fd_dict != NULL && !eap->skip)
20854 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 if (!ends_excmd(*skipwhite(p)))
20858 {
20859 vim_free(name);
20860 EMSG(_(e_trailing));
20861 return;
20862 }
20863 eap->nextcmd = check_nextcmd(p);
20864 if (eap->nextcmd != NULL)
20865 *p = NUL;
20866
20867 if (!eap->skip)
20868 fp = find_func(name);
20869 vim_free(name);
20870
20871 if (!eap->skip)
20872 {
20873 if (fp == NULL)
20874 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020875 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 return;
20877 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020878 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 {
20880 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20881 return;
20882 }
20883
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020884 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020886 /* Delete the dict item that refers to the function, it will
20887 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020888 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020890 else
20891 func_free(fp);
20892 }
20893}
20894
20895/*
20896 * Free a function and remove it from the list of functions.
20897 */
20898 static void
20899func_free(fp)
20900 ufunc_T *fp;
20901{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020902 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020903
20904 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020905 ga_clear_strings(&(fp->uf_args));
20906 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020907#ifdef FEAT_PROFILE
20908 vim_free(fp->uf_tml_count);
20909 vim_free(fp->uf_tml_total);
20910 vim_free(fp->uf_tml_self);
20911#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020912
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020913 /* remove the function from the function hashtable */
20914 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20915 if (HASHITEM_EMPTY(hi))
20916 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020917 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020918 hash_remove(&func_hashtab, hi);
20919
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020920 vim_free(fp);
20921}
20922
20923/*
20924 * Unreference a Function: decrement the reference count and free it when it
20925 * becomes zero. Only for numbered functions.
20926 */
20927 static void
20928func_unref(name)
20929 char_u *name;
20930{
20931 ufunc_T *fp;
20932
20933 if (name != NULL && isdigit(*name))
20934 {
20935 fp = find_func(name);
20936 if (fp == NULL)
20937 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020938 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 {
20940 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020941 * when "uf_calls" becomes zero. */
20942 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943 func_free(fp);
20944 }
20945 }
20946}
20947
20948/*
20949 * Count a reference to a Function.
20950 */
20951 static void
20952func_ref(name)
20953 char_u *name;
20954{
20955 ufunc_T *fp;
20956
20957 if (name != NULL && isdigit(*name))
20958 {
20959 fp = find_func(name);
20960 if (fp == NULL)
20961 EMSG2(_(e_intern2), "func_ref()");
20962 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020963 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 }
20965}
20966
20967/*
20968 * Call a user function.
20969 */
20970 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020971call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 ufunc_T *fp; /* pointer to function */
20973 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020974 typval_T *argvars; /* arguments */
20975 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 linenr_T firstline; /* first line of range */
20977 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020978 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979{
Bram Moolenaar33570922005-01-25 22:26:29 +000020980 char_u *save_sourcing_name;
20981 linenr_T save_sourcing_lnum;
20982 scid_T save_current_SID;
20983 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020984 int save_did_emsg;
20985 static int depth = 0;
20986 dictitem_T *v;
20987 int fixvar_idx = 0; /* index in fixvar[] */
20988 int i;
20989 int ai;
20990 char_u numbuf[NUMBUFLEN];
20991 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020992#ifdef FEAT_PROFILE
20993 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020994 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996
20997 /* If depth of calling is getting too high, don't execute the function */
20998 if (depth >= p_mfd)
20999 {
21000 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021001 rettv->v_type = VAR_NUMBER;
21002 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 return;
21004 }
21005 ++depth;
21006
21007 line_breakcheck(); /* check for CTRL-C hit */
21008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021009 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000021010 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021012 fc.rettv = rettv;
21013 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 fc.linenr = 0;
21015 fc.returned = FALSE;
21016 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021018 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 fc.dbg_tick = debug_tick;
21020
Bram Moolenaar33570922005-01-25 22:26:29 +000021021 /*
21022 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21023 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21024 * each argument variable and saves a lot of time.
21025 */
21026 /*
21027 * Init l: variables.
21028 */
21029 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021030 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021031 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021032 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21033 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021034 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021035 name = v->di_key;
21036 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021037 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
21038 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
21039 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021040 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021041 v->di_tv.vval.v_dict = selfdict;
21042 ++selfdict->dv_refcount;
21043 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021044
Bram Moolenaar33570922005-01-25 22:26:29 +000021045 /*
21046 * Init a: variables.
21047 * Set a:0 to "argcount".
21048 * Set a:000 to a list with room for the "..." arguments.
21049 */
21050 init_var_dict(&fc.l_avars, &fc.l_avars_var);
21051 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021052 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 v = &fc.fixvar[fixvar_idx++].var;
21054 STRCPY(v->di_key, "000");
21055 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21056 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21057 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021058 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021059 v->di_tv.vval.v_list = &fc.l_varlist;
21060 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
21061 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000021062 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021063
21064 /*
21065 * Set a:firstline to "firstline" and a:lastline to "lastline".
21066 * Set a:name to named arguments.
21067 * Set a:N to the "..." arguments.
21068 */
21069 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
21070 (varnumber_T)firstline);
21071 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
21072 (varnumber_T)lastline);
21073 for (i = 0; i < argcount; ++i)
21074 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021075 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021076 if (ai < 0)
21077 /* named argument a:name */
21078 name = FUNCARG(fp, i);
21079 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021080 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021081 /* "..." argument a:1, a:2, etc. */
21082 sprintf((char *)numbuf, "%d", ai + 1);
21083 name = numbuf;
21084 }
21085 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21086 {
21087 v = &fc.fixvar[fixvar_idx++].var;
21088 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21089 }
21090 else
21091 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021092 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21093 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021094 if (v == NULL)
21095 break;
21096 v->di_flags = DI_FLAGS_RO;
21097 }
21098 STRCPY(v->di_key, name);
21099 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21100
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021101 /* Note: the values are copied directly to avoid alloc/free.
21102 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021103 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021104 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021105
21106 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21107 {
21108 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
21109 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021110 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021111 }
21112 }
21113
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 /* Don't redraw while executing the function. */
21115 ++RedrawingDisabled;
21116 save_sourcing_name = sourcing_name;
21117 save_sourcing_lnum = sourcing_lnum;
21118 sourcing_lnum = 1;
21119 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021120 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 if (sourcing_name != NULL)
21122 {
21123 if (save_sourcing_name != NULL
21124 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21125 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21126 else
21127 STRCPY(sourcing_name, "function ");
21128 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21129
21130 if (p_verbose >= 12)
21131 {
21132 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021133 verbose_enter_scroll();
21134
Bram Moolenaar555b2802005-05-19 21:08:39 +000021135 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 if (p_verbose >= 14)
21137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021139 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021140 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021141 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142
21143 msg_puts((char_u *)"(");
21144 for (i = 0; i < argcount; ++i)
21145 {
21146 if (i > 0)
21147 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021148 if (argvars[i].v_type == VAR_NUMBER)
21149 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 else
21151 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021152 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21153 if (s != NULL)
21154 {
21155 trunc_string(s, buf, MSG_BUF_CLEN);
21156 msg_puts(buf);
21157 vim_free(tofree);
21158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 }
21160 }
21161 msg_puts((char_u *)")");
21162 }
21163 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021164
21165 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 --no_wait_return;
21167 }
21168 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021169#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021170 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021171 {
21172 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21173 func_do_profile(fp);
21174 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021175 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021176 {
21177 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021178 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021179 profile_zero(&fp->uf_tm_children);
21180 }
21181 script_prof_save(&wait_start);
21182 }
21183#endif
21184
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021186 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 save_did_emsg = did_emsg;
21188 did_emsg = FALSE;
21189
21190 /* call do_cmdline() to execute the lines */
21191 do_cmdline(NULL, get_func_line, (void *)&fc,
21192 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21193
21194 --RedrawingDisabled;
21195
21196 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021197 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021199 clear_tv(rettv);
21200 rettv->v_type = VAR_NUMBER;
21201 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 }
21203
Bram Moolenaar05159a02005-02-26 23:04:13 +000021204#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021205 if (do_profiling == PROF_YES && (fp->uf_profiling
21206 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021207 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021208 profile_end(&call_start);
21209 profile_sub_wait(&wait_start, &call_start);
21210 profile_add(&fp->uf_tm_total, &call_start);
21211 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021212 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021213 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021214 profile_add(&fc.caller->func->uf_tm_children, &call_start);
21215 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021216 }
21217 }
21218#endif
21219
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 /* when being verbose, mention the return value */
21221 if (p_verbose >= 12)
21222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021224 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021227 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021228 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021229 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
21230 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021233 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021234 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021235 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021236 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021237
Bram Moolenaar555b2802005-05-19 21:08:39 +000021238 /* The value may be very long. Skip the middle part, so that we
21239 * have some idea how it starts and ends. smsg() would always
21240 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021241 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
21242 if (s != NULL)
21243 {
21244 trunc_string(s, buf, MSG_BUF_CLEN);
21245 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21246 vim_free(tofree);
21247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 }
21249 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021250
21251 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 --no_wait_return;
21253 }
21254
21255 vim_free(sourcing_name);
21256 sourcing_name = save_sourcing_name;
21257 sourcing_lnum = save_sourcing_lnum;
21258 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021259#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021260 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021261 script_prof_restore(&wait_start);
21262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263
21264 if (p_verbose >= 12 && sourcing_name != NULL)
21265 {
21266 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021267 verbose_enter_scroll();
21268
Bram Moolenaar555b2802005-05-19 21:08:39 +000021269 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021271
21272 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 --no_wait_return;
21274 }
21275
21276 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021277 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021279 /* The a: variables typevals were not allocated, only free the allocated
Bram Moolenaar33570922005-01-25 22:26:29 +000021280 * variables. */
21281 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
21282
21283 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 --depth;
21285}
21286
21287/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021288 * Add a number variable "name" to dict "dp" with value "nr".
21289 */
21290 static void
21291add_nr_var(dp, v, name, nr)
21292 dict_T *dp;
21293 dictitem_T *v;
21294 char *name;
21295 varnumber_T nr;
21296{
21297 STRCPY(v->di_key, name);
21298 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21299 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21300 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021301 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021302 v->di_tv.vval.v_number = nr;
21303}
21304
21305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 * ":return [expr]"
21307 */
21308 void
21309ex_return(eap)
21310 exarg_T *eap;
21311{
21312 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021313 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 int returning = FALSE;
21315
21316 if (current_funccal == NULL)
21317 {
21318 EMSG(_("E133: :return not inside a function"));
21319 return;
21320 }
21321
21322 if (eap->skip)
21323 ++emsg_skip;
21324
21325 eap->nextcmd = NULL;
21326 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021327 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 {
21329 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021330 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021332 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 }
21334 /* It's safer to return also on error. */
21335 else if (!eap->skip)
21336 {
21337 /*
21338 * Return unless the expression evaluation has been cancelled due to an
21339 * aborting error, an interrupt, or an exception.
21340 */
21341 if (!aborting())
21342 returning = do_return(eap, FALSE, TRUE, NULL);
21343 }
21344
21345 /* When skipping or the return gets pending, advance to the next command
21346 * in this line (!returning). Otherwise, ignore the rest of the line.
21347 * Following lines will be ignored by get_func_line(). */
21348 if (returning)
21349 eap->nextcmd = NULL;
21350 else if (eap->nextcmd == NULL) /* no argument */
21351 eap->nextcmd = check_nextcmd(arg);
21352
21353 if (eap->skip)
21354 --emsg_skip;
21355}
21356
21357/*
21358 * Return from a function. Possibly makes the return pending. Also called
21359 * for a pending return at the ":endtry" or after returning from an extra
21360 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021361 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 * FALSE when the return gets pending.
21364 */
21365 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021366do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 exarg_T *eap;
21368 int reanimate;
21369 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021370 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371{
21372 int idx;
21373 struct condstack *cstack = eap->cstack;
21374
21375 if (reanimate)
21376 /* Undo the return. */
21377 current_funccal->returned = FALSE;
21378
21379 /*
21380 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21381 * not in its finally clause (which then is to be executed next) is found.
21382 * In this case, make the ":return" pending for execution at the ":endtry".
21383 * Otherwise, return normally.
21384 */
21385 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21386 if (idx >= 0)
21387 {
21388 cstack->cs_pending[idx] = CSTP_RETURN;
21389
21390 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021391 /* A pending return again gets pending. "rettv" points to an
21392 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021394 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 else
21396 {
21397 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021398 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021400 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021402 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 {
21404 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021405 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021406 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 else
21408 EMSG(_(e_outofmem));
21409 }
21410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021411 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412
21413 if (reanimate)
21414 {
21415 /* The pending return value could be overwritten by a ":return"
21416 * without argument in a finally clause; reset the default
21417 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 current_funccal->rettv->v_type = VAR_NUMBER;
21419 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 }
21421 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021422 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 }
21424 else
21425 {
21426 current_funccal->returned = TRUE;
21427
21428 /* If the return is carried out now, store the return value. For
21429 * a return immediately after reanimation, the value is already
21430 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021431 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021433 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021434 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021436 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 }
21438 }
21439
21440 return idx < 0;
21441}
21442
21443/*
21444 * Free the variable with a pending return value.
21445 */
21446 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021447discard_pending_return(rettv)
21448 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449{
Bram Moolenaar33570922005-01-25 22:26:29 +000021450 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451}
21452
21453/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 * is an allocated string. Used by report_pending() for verbose messages.
21456 */
21457 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021458get_return_cmd(rettv)
21459 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021461 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021462 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021463 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021465 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021466 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021467 if (s == NULL)
21468 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021469
21470 STRCPY(IObuff, ":return ");
21471 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21472 if (STRLEN(s) + 8 >= IOSIZE)
21473 STRCPY(IObuff + IOSIZE - 4, "...");
21474 vim_free(tofree);
21475 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476}
21477
21478/*
21479 * Get next function line.
21480 * Called by do_cmdline() to get the next line.
21481 * Returns allocated string, or NULL for end of function.
21482 */
21483/* ARGSUSED */
21484 char_u *
21485get_func_line(c, cookie, indent)
21486 int c; /* not used */
21487 void *cookie;
21488 int indent; /* not used */
21489{
Bram Moolenaar33570922005-01-25 22:26:29 +000021490 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021491 ufunc_T *fp = fcp->func;
21492 char_u *retval;
21493 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494
21495 /* If breakpoints have been added/deleted need to check for it. */
21496 if (fcp->dbg_tick != debug_tick)
21497 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021498 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 sourcing_lnum);
21500 fcp->dbg_tick = debug_tick;
21501 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021502#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021503 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021504 func_line_end(cookie);
21505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506
Bram Moolenaar05159a02005-02-26 23:04:13 +000021507 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021508 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21509 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 retval = NULL;
21511 else
21512 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021513 /* Skip NULL lines (continuation lines). */
21514 while (fcp->linenr < gap->ga_len
21515 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21516 ++fcp->linenr;
21517 if (fcp->linenr >= gap->ga_len)
21518 retval = NULL;
21519 else
21520 {
21521 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21522 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021523#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021524 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021525 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021526#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 }
21529
21530 /* Did we encounter a breakpoint? */
21531 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21532 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021533 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021535 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 sourcing_lnum);
21537 fcp->dbg_tick = debug_tick;
21538 }
21539
21540 return retval;
21541}
21542
Bram Moolenaar05159a02005-02-26 23:04:13 +000021543#if defined(FEAT_PROFILE) || defined(PROTO)
21544/*
21545 * Called when starting to read a function line.
21546 * "sourcing_lnum" must be correct!
21547 * When skipping lines it may not actually be executed, but we won't find out
21548 * until later and we need to store the time now.
21549 */
21550 void
21551func_line_start(cookie)
21552 void *cookie;
21553{
21554 funccall_T *fcp = (funccall_T *)cookie;
21555 ufunc_T *fp = fcp->func;
21556
21557 if (fp->uf_profiling && sourcing_lnum >= 1
21558 && sourcing_lnum <= fp->uf_lines.ga_len)
21559 {
21560 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021561 /* Skip continuation lines. */
21562 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21563 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021564 fp->uf_tml_execed = FALSE;
21565 profile_start(&fp->uf_tml_start);
21566 profile_zero(&fp->uf_tml_children);
21567 profile_get_wait(&fp->uf_tml_wait);
21568 }
21569}
21570
21571/*
21572 * Called when actually executing a function line.
21573 */
21574 void
21575func_line_exec(cookie)
21576 void *cookie;
21577{
21578 funccall_T *fcp = (funccall_T *)cookie;
21579 ufunc_T *fp = fcp->func;
21580
21581 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21582 fp->uf_tml_execed = TRUE;
21583}
21584
21585/*
21586 * Called when done with a function line.
21587 */
21588 void
21589func_line_end(cookie)
21590 void *cookie;
21591{
21592 funccall_T *fcp = (funccall_T *)cookie;
21593 ufunc_T *fp = fcp->func;
21594
21595 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21596 {
21597 if (fp->uf_tml_execed)
21598 {
21599 ++fp->uf_tml_count[fp->uf_tml_idx];
21600 profile_end(&fp->uf_tml_start);
21601 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021602 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021603 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21604 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021605 }
21606 fp->uf_tml_idx = -1;
21607 }
21608}
21609#endif
21610
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611/*
21612 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021613 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 */
21615 int
21616func_has_ended(cookie)
21617 void *cookie;
21618{
Bram Moolenaar33570922005-01-25 22:26:29 +000021619 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620
21621 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21622 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021623 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 || fcp->returned);
21625}
21626
21627/*
21628 * return TRUE if cookie indicates a function which "abort"s on errors.
21629 */
21630 int
21631func_has_abort(cookie)
21632 void *cookie;
21633{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021634 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635}
21636
21637#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21638typedef enum
21639{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021640 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21641 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21642 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643} var_flavour_T;
21644
21645static var_flavour_T var_flavour __ARGS((char_u *varname));
21646
21647 static var_flavour_T
21648var_flavour(varname)
21649 char_u *varname;
21650{
21651 char_u *p = varname;
21652
21653 if (ASCII_ISUPPER(*p))
21654 {
21655 while (*(++p))
21656 if (ASCII_ISLOWER(*p))
21657 return VAR_FLAVOUR_SESSION;
21658 return VAR_FLAVOUR_VIMINFO;
21659 }
21660 else
21661 return VAR_FLAVOUR_DEFAULT;
21662}
21663#endif
21664
21665#if defined(FEAT_VIMINFO) || defined(PROTO)
21666/*
21667 * Restore global vars that start with a capital from the viminfo file
21668 */
21669 int
21670read_viminfo_varlist(virp, writing)
21671 vir_T *virp;
21672 int writing;
21673{
21674 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021675 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021676 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677
21678 if (!writing && (find_viminfo_parameter('!') != NULL))
21679 {
21680 tab = vim_strchr(virp->vir_line + 1, '\t');
21681 if (tab != NULL)
21682 {
21683 *tab++ = '\0'; /* isolate the variable name */
21684 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021685 type = VAR_STRING;
21686#ifdef FEAT_FLOAT
21687 else if (*tab == 'F')
21688 type = VAR_FLOAT;
21689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690
21691 tab = vim_strchr(tab, '\t');
21692 if (tab != NULL)
21693 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021694 tv.v_type = type;
21695 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021696 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021698#ifdef FEAT_FLOAT
21699 else if (type == VAR_FLOAT)
21700 (void)string2float(tab + 1, &tv.vval.v_float);
21701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021703 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021704 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021705 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021706 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 }
21708 }
21709 }
21710
21711 return viminfo_readline(virp);
21712}
21713
21714/*
21715 * Write global vars that start with a capital to the viminfo file
21716 */
21717 void
21718write_viminfo_varlist(fp)
21719 FILE *fp;
21720{
Bram Moolenaar33570922005-01-25 22:26:29 +000021721 hashitem_T *hi;
21722 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021723 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021724 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021725 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021726 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021727 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728
21729 if (find_viminfo_parameter('!') == NULL)
21730 return;
21731
21732 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021733
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021734 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021735 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021739 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 this_var = HI2DI(hi);
21741 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021742 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021744 {
21745 case VAR_STRING: s = "STR"; break;
21746 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021747#ifdef FEAT_FLOAT
21748 case VAR_FLOAT: s = "FLO"; break;
21749#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021750 default: continue;
21751 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021753 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021754 if (p != NULL)
21755 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021756 vim_free(tofree);
21757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 }
21759 }
21760}
21761#endif
21762
21763#if defined(FEAT_SESSION) || defined(PROTO)
21764 int
21765store_session_globals(fd)
21766 FILE *fd;
21767{
Bram Moolenaar33570922005-01-25 22:26:29 +000021768 hashitem_T *hi;
21769 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021770 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 char_u *p, *t;
21772
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021773 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021776 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021778 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021779 this_var = HI2DI(hi);
21780 if ((this_var->di_tv.v_type == VAR_NUMBER
21781 || this_var->di_tv.v_type == VAR_STRING)
21782 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021783 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021784 /* Escape special characters with a backslash. Turn a LF and
21785 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021787 (char_u *)"\\\"\n\r");
21788 if (p == NULL) /* out of memory */
21789 break;
21790 for (t = p; *t != NUL; ++t)
21791 if (*t == '\n')
21792 *t = 'n';
21793 else if (*t == '\r')
21794 *t = 'r';
21795 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021796 this_var->di_key,
21797 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21798 : ' ',
21799 p,
21800 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21801 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021802 || put_eol(fd) == FAIL)
21803 {
21804 vim_free(p);
21805 return FAIL;
21806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 vim_free(p);
21808 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021809#ifdef FEAT_FLOAT
21810 else if (this_var->di_tv.v_type == VAR_FLOAT
21811 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21812 {
21813 float_T f = this_var->di_tv.vval.v_float;
21814 int sign = ' ';
21815
21816 if (f < 0)
21817 {
21818 f = -f;
21819 sign = '-';
21820 }
21821 if ((fprintf(fd, "let %s = %c&%f",
21822 this_var->di_key, sign, f) < 0)
21823 || put_eol(fd) == FAIL)
21824 return FAIL;
21825 }
21826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 }
21828 }
21829 return OK;
21830}
21831#endif
21832
Bram Moolenaar661b1822005-07-28 22:36:45 +000021833/*
21834 * Display script name where an item was last set.
21835 * Should only be invoked when 'verbose' is non-zero.
21836 */
21837 void
21838last_set_msg(scriptID)
21839 scid_T scriptID;
21840{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021841 char_u *p;
21842
Bram Moolenaar661b1822005-07-28 22:36:45 +000021843 if (scriptID != 0)
21844 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021845 p = home_replace_save(NULL, get_scriptname(scriptID));
21846 if (p != NULL)
21847 {
21848 verbose_enter();
21849 MSG_PUTS(_("\n\tLast set from "));
21850 MSG_PUTS(p);
21851 vim_free(p);
21852 verbose_leave();
21853 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021854 }
21855}
21856
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857#endif /* FEAT_EVAL */
21858
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021860#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861
21862#ifdef WIN3264
21863/*
21864 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21865 */
21866static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21867static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
21868static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
21869
21870/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021871 * Get the short path (8.3) for the filename in "fnamep".
21872 * Only works for a valid file name.
21873 * When the path gets longer "fnamep" is changed and the allocated buffer
21874 * is put in "bufp".
21875 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
21876 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 */
21878 static int
21879get_short_pathname(fnamep, bufp, fnamelen)
21880 char_u **fnamep;
21881 char_u **bufp;
21882 int *fnamelen;
21883{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021884 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 char_u *newbuf;
21886
21887 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 l = GetShortPathName(*fnamep, *fnamep, len);
21889 if (l > len - 1)
21890 {
21891 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021892 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 newbuf = vim_strnsave(*fnamep, l);
21894 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021895 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896
21897 vim_free(*bufp);
21898 *fnamep = *bufp = newbuf;
21899
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021900 /* Really should always succeed, as the buffer is big enough. */
21901 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 }
21903
21904 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021905 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906}
21907
21908/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021909 * Get the short path (8.3) for the filename in "fname". The converted
21910 * path is returned in "bufp".
21911 *
21912 * Some of the directories specified in "fname" may not exist. This function
21913 * will shorten the existing directories at the beginning of the path and then
21914 * append the remaining non-existing path.
21915 *
21916 * fname - Pointer to the filename to shorten. On return, contains the
21917 * pointer to the shortened pathname
21918 * bufp - Pointer to an allocated buffer for the filename.
21919 * fnamelen - Length of the filename pointed to by fname
21920 *
21921 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 */
21923 static int
21924shortpath_for_invalid_fname(fname, bufp, fnamelen)
21925 char_u **fname;
21926 char_u **bufp;
21927 int *fnamelen;
21928{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021929 char_u *short_fname, *save_fname, *pbuf_unused;
21930 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021932 int old_len, len;
21933 int new_len, sfx_len;
21934 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935
21936 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021937 old_len = *fnamelen;
21938 save_fname = vim_strnsave(*fname, old_len);
21939 pbuf_unused = NULL;
21940 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021942 endp = save_fname + old_len - 1; /* Find the end of the copy */
21943 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021945 /*
21946 * Try shortening the supplied path till it succeeds by removing one
21947 * directory at a time from the tail of the path.
21948 */
21949 len = 0;
21950 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021952 /* go back one path-separator */
21953 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
21954 --endp;
21955 if (endp <= save_fname)
21956 break; /* processed the complete path */
21957
21958 /*
21959 * Replace the path separator with a NUL and try to shorten the
21960 * resulting path.
21961 */
21962 ch = *endp;
21963 *endp = 0;
21964 short_fname = save_fname;
21965 len = STRLEN(short_fname) + 1;
21966 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
21967 {
21968 retval = FAIL;
21969 goto theend;
21970 }
21971 *endp = ch; /* preserve the string */
21972
21973 if (len > 0)
21974 break; /* successfully shortened the path */
21975
21976 /* failed to shorten the path. Skip the path separator */
21977 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 }
21979
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021980 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021982 /*
21983 * Succeeded in shortening the path. Now concatenate the shortened
21984 * path with the remaining path at the tail.
21985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021987 /* Compute the length of the new path. */
21988 sfx_len = (int)(save_endp - endp) + 1;
21989 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021991 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021993 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021995 /* There is not enough space in the currently allocated string,
21996 * copy it to a buffer big enough. */
21997 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000021999 {
22000 retval = FAIL;
22001 goto theend;
22002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 }
22004 else
22005 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022006 /* Transfer short_fname to the main buffer (it's big enough),
22007 * unless get_short_pathname() did its work in-place. */
22008 *fname = *bufp = save_fname;
22009 if (short_fname != save_fname)
22010 vim_strncpy(save_fname, short_fname, len);
22011 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022013
22014 /* concat the not-shortened part of the path */
22015 vim_strncpy(*fname + len, endp, sfx_len);
22016 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022018
22019theend:
22020 vim_free(pbuf_unused);
22021 vim_free(save_fname);
22022
22023 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024}
22025
22026/*
22027 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022028 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 */
22030 static int
22031shortpath_for_partial(fnamep, bufp, fnamelen)
22032 char_u **fnamep;
22033 char_u **bufp;
22034 int *fnamelen;
22035{
22036 int sepcount, len, tflen;
22037 char_u *p;
22038 char_u *pbuf, *tfname;
22039 int hasTilde;
22040
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022041 /* Count up the path separators from the RHS.. so we know which part
22042 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022044 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 if (vim_ispathsep(*p))
22046 ++sepcount;
22047
22048 /* Need full path first (use expand_env() to remove a "~/") */
22049 hasTilde = (**fnamep == '~');
22050 if (hasTilde)
22051 pbuf = tfname = expand_env_save(*fnamep);
22052 else
22053 pbuf = tfname = FullName_save(*fnamep, FALSE);
22054
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022055 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022057 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059
22060 if (len == 0)
22061 {
22062 /* Don't have a valid filename, so shorten the rest of the
22063 * path if we can. This CAN give us invalid 8.3 filenames, but
22064 * there's not a lot of point in guessing what it might be.
22065 */
22066 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022067 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 }
22070
22071 /* Count the paths backward to find the beginning of the desired string. */
22072 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022073 {
22074#ifdef FEAT_MBYTE
22075 if (has_mbyte)
22076 p -= mb_head_off(tfname, p);
22077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 if (vim_ispathsep(*p))
22079 {
22080 if (sepcount == 0 || (hasTilde && sepcount == 1))
22081 break;
22082 else
22083 sepcount --;
22084 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 if (hasTilde)
22087 {
22088 --p;
22089 if (p >= tfname)
22090 *p = '~';
22091 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 }
22094 else
22095 ++p;
22096
22097 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22098 vim_free(*bufp);
22099 *fnamelen = (int)STRLEN(p);
22100 *bufp = pbuf;
22101 *fnamep = p;
22102
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022103 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104}
22105#endif /* WIN3264 */
22106
22107/*
22108 * Adjust a filename, according to a string of modifiers.
22109 * *fnamep must be NUL terminated when called. When returning, the length is
22110 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022111 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 * When there is an error, *fnamep is set to NULL.
22113 */
22114 int
22115modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22116 char_u *src; /* string with modifiers */
22117 int *usedlen; /* characters after src that are used */
22118 char_u **fnamep; /* file name so far */
22119 char_u **bufp; /* buffer for allocated file name or NULL */
22120 int *fnamelen; /* length of fnamep */
22121{
22122 int valid = 0;
22123 char_u *tail;
22124 char_u *s, *p, *pbuf;
22125 char_u dirname[MAXPATHL];
22126 int c;
22127 int has_fullname = 0;
22128#ifdef WIN3264
22129 int has_shortname = 0;
22130#endif
22131
22132repeat:
22133 /* ":p" - full path/file_name */
22134 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22135 {
22136 has_fullname = 1;
22137
22138 valid |= VALID_PATH;
22139 *usedlen += 2;
22140
22141 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22142 if ((*fnamep)[0] == '~'
22143#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22144 && ((*fnamep)[1] == '/'
22145# ifdef BACKSLASH_IN_FILENAME
22146 || (*fnamep)[1] == '\\'
22147# endif
22148 || (*fnamep)[1] == NUL)
22149
22150#endif
22151 )
22152 {
22153 *fnamep = expand_env_save(*fnamep);
22154 vim_free(*bufp); /* free any allocated file name */
22155 *bufp = *fnamep;
22156 if (*fnamep == NULL)
22157 return -1;
22158 }
22159
22160 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022161 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 {
22163 if (vim_ispathsep(*p)
22164 && p[1] == '.'
22165 && (p[2] == NUL
22166 || vim_ispathsep(p[2])
22167 || (p[2] == '.'
22168 && (p[3] == NUL || vim_ispathsep(p[3])))))
22169 break;
22170 }
22171
22172 /* FullName_save() is slow, don't use it when not needed. */
22173 if (*p != NUL || !vim_isAbsName(*fnamep))
22174 {
22175 *fnamep = FullName_save(*fnamep, *p != NUL);
22176 vim_free(*bufp); /* free any allocated file name */
22177 *bufp = *fnamep;
22178 if (*fnamep == NULL)
22179 return -1;
22180 }
22181
22182 /* Append a path separator to a directory. */
22183 if (mch_isdir(*fnamep))
22184 {
22185 /* Make room for one or two extra characters. */
22186 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22187 vim_free(*bufp); /* free any allocated file name */
22188 *bufp = *fnamep;
22189 if (*fnamep == NULL)
22190 return -1;
22191 add_pathsep(*fnamep);
22192 }
22193 }
22194
22195 /* ":." - path relative to the current directory */
22196 /* ":~" - path relative to the home directory */
22197 /* ":8" - shortname path - postponed till after */
22198 while (src[*usedlen] == ':'
22199 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22200 {
22201 *usedlen += 2;
22202 if (c == '8')
22203 {
22204#ifdef WIN3264
22205 has_shortname = 1; /* Postpone this. */
22206#endif
22207 continue;
22208 }
22209 pbuf = NULL;
22210 /* Need full path first (use expand_env() to remove a "~/") */
22211 if (!has_fullname)
22212 {
22213 if (c == '.' && **fnamep == '~')
22214 p = pbuf = expand_env_save(*fnamep);
22215 else
22216 p = pbuf = FullName_save(*fnamep, FALSE);
22217 }
22218 else
22219 p = *fnamep;
22220
22221 has_fullname = 0;
22222
22223 if (p != NULL)
22224 {
22225 if (c == '.')
22226 {
22227 mch_dirname(dirname, MAXPATHL);
22228 s = shorten_fname(p, dirname);
22229 if (s != NULL)
22230 {
22231 *fnamep = s;
22232 if (pbuf != NULL)
22233 {
22234 vim_free(*bufp); /* free any allocated file name */
22235 *bufp = pbuf;
22236 pbuf = NULL;
22237 }
22238 }
22239 }
22240 else
22241 {
22242 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22243 /* Only replace it when it starts with '~' */
22244 if (*dirname == '~')
22245 {
22246 s = vim_strsave(dirname);
22247 if (s != NULL)
22248 {
22249 *fnamep = s;
22250 vim_free(*bufp);
22251 *bufp = s;
22252 }
22253 }
22254 }
22255 vim_free(pbuf);
22256 }
22257 }
22258
22259 tail = gettail(*fnamep);
22260 *fnamelen = (int)STRLEN(*fnamep);
22261
22262 /* ":h" - head, remove "/file_name", can be repeated */
22263 /* Don't remove the first "/" or "c:\" */
22264 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22265 {
22266 valid |= VALID_HEAD;
22267 *usedlen += 2;
22268 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022269 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022270 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 *fnamelen = (int)(tail - *fnamep);
22272#ifdef VMS
22273 if (*fnamelen > 0)
22274 *fnamelen += 1; /* the path separator is part of the path */
22275#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022276 if (*fnamelen == 0)
22277 {
22278 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22279 p = vim_strsave((char_u *)".");
22280 if (p == NULL)
22281 return -1;
22282 vim_free(*bufp);
22283 *bufp = *fnamep = tail = p;
22284 *fnamelen = 1;
22285 }
22286 else
22287 {
22288 while (tail > s && !after_pathsep(s, tail))
22289 mb_ptr_back(*fnamep, tail);
22290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 }
22292
22293 /* ":8" - shortname */
22294 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22295 {
22296 *usedlen += 2;
22297#ifdef WIN3264
22298 has_shortname = 1;
22299#endif
22300 }
22301
22302#ifdef WIN3264
22303 /* Check shortname after we have done 'heads' and before we do 'tails'
22304 */
22305 if (has_shortname)
22306 {
22307 pbuf = NULL;
22308 /* Copy the string if it is shortened by :h */
22309 if (*fnamelen < (int)STRLEN(*fnamep))
22310 {
22311 p = vim_strnsave(*fnamep, *fnamelen);
22312 if (p == 0)
22313 return -1;
22314 vim_free(*bufp);
22315 *bufp = *fnamep = p;
22316 }
22317
22318 /* Split into two implementations - makes it easier. First is where
22319 * there isn't a full name already, second is where there is.
22320 */
22321 if (!has_fullname && !vim_isAbsName(*fnamep))
22322 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022323 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 return -1;
22325 }
22326 else
22327 {
22328 int l;
22329
22330 /* Simple case, already have the full-name
22331 * Nearly always shorter, so try first time. */
22332 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022333 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 return -1;
22335
22336 if (l == 0)
22337 {
22338 /* Couldn't find the filename.. search the paths.
22339 */
22340 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022341 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 return -1;
22343 }
22344 *fnamelen = l;
22345 }
22346 }
22347#endif /* WIN3264 */
22348
22349 /* ":t" - tail, just the basename */
22350 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22351 {
22352 *usedlen += 2;
22353 *fnamelen -= (int)(tail - *fnamep);
22354 *fnamep = tail;
22355 }
22356
22357 /* ":e" - extension, can be repeated */
22358 /* ":r" - root, without extension, can be repeated */
22359 while (src[*usedlen] == ':'
22360 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22361 {
22362 /* find a '.' in the tail:
22363 * - for second :e: before the current fname
22364 * - otherwise: The last '.'
22365 */
22366 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22367 s = *fnamep - 2;
22368 else
22369 s = *fnamep + *fnamelen - 1;
22370 for ( ; s > tail; --s)
22371 if (s[0] == '.')
22372 break;
22373 if (src[*usedlen + 1] == 'e') /* :e */
22374 {
22375 if (s > tail)
22376 {
22377 *fnamelen += (int)(*fnamep - (s + 1));
22378 *fnamep = s + 1;
22379#ifdef VMS
22380 /* cut version from the extension */
22381 s = *fnamep + *fnamelen - 1;
22382 for ( ; s > *fnamep; --s)
22383 if (s[0] == ';')
22384 break;
22385 if (s > *fnamep)
22386 *fnamelen = s - *fnamep;
22387#endif
22388 }
22389 else if (*fnamep <= tail)
22390 *fnamelen = 0;
22391 }
22392 else /* :r */
22393 {
22394 if (s > tail) /* remove one extension */
22395 *fnamelen = (int)(s - *fnamep);
22396 }
22397 *usedlen += 2;
22398 }
22399
22400 /* ":s?pat?foo?" - substitute */
22401 /* ":gs?pat?foo?" - global substitute */
22402 if (src[*usedlen] == ':'
22403 && (src[*usedlen + 1] == 's'
22404 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22405 {
22406 char_u *str;
22407 char_u *pat;
22408 char_u *sub;
22409 int sep;
22410 char_u *flags;
22411 int didit = FALSE;
22412
22413 flags = (char_u *)"";
22414 s = src + *usedlen + 2;
22415 if (src[*usedlen + 1] == 'g')
22416 {
22417 flags = (char_u *)"g";
22418 ++s;
22419 }
22420
22421 sep = *s++;
22422 if (sep)
22423 {
22424 /* find end of pattern */
22425 p = vim_strchr(s, sep);
22426 if (p != NULL)
22427 {
22428 pat = vim_strnsave(s, (int)(p - s));
22429 if (pat != NULL)
22430 {
22431 s = p + 1;
22432 /* find end of substitution */
22433 p = vim_strchr(s, sep);
22434 if (p != NULL)
22435 {
22436 sub = vim_strnsave(s, (int)(p - s));
22437 str = vim_strnsave(*fnamep, *fnamelen);
22438 if (sub != NULL && str != NULL)
22439 {
22440 *usedlen = (int)(p + 1 - src);
22441 s = do_string_sub(str, pat, sub, flags);
22442 if (s != NULL)
22443 {
22444 *fnamep = s;
22445 *fnamelen = (int)STRLEN(s);
22446 vim_free(*bufp);
22447 *bufp = s;
22448 didit = TRUE;
22449 }
22450 }
22451 vim_free(sub);
22452 vim_free(str);
22453 }
22454 vim_free(pat);
22455 }
22456 }
22457 /* after using ":s", repeat all the modifiers */
22458 if (didit)
22459 goto repeat;
22460 }
22461 }
22462
22463 return valid;
22464}
22465
22466/*
22467 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22468 * "flags" can be "g" to do a global substitute.
22469 * Returns an allocated string, NULL for error.
22470 */
22471 char_u *
22472do_string_sub(str, pat, sub, flags)
22473 char_u *str;
22474 char_u *pat;
22475 char_u *sub;
22476 char_u *flags;
22477{
22478 int sublen;
22479 regmatch_T regmatch;
22480 int i;
22481 int do_all;
22482 char_u *tail;
22483 garray_T ga;
22484 char_u *ret;
22485 char_u *save_cpo;
22486
22487 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22488 save_cpo = p_cpo;
22489 p_cpo = (char_u *)"";
22490
22491 ga_init2(&ga, 1, 200);
22492
22493 do_all = (flags[0] == 'g');
22494
22495 regmatch.rm_ic = p_ic;
22496 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22497 if (regmatch.regprog != NULL)
22498 {
22499 tail = str;
22500 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22501 {
22502 /*
22503 * Get some space for a temporary buffer to do the substitution
22504 * into. It will contain:
22505 * - The text up to where the match is.
22506 * - The substituted text.
22507 * - The text after the match.
22508 */
22509 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22510 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22511 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22512 {
22513 ga_clear(&ga);
22514 break;
22515 }
22516
22517 /* copy the text up to where the match is */
22518 i = (int)(regmatch.startp[0] - tail);
22519 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22520 /* add the substituted text */
22521 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22522 + ga.ga_len + i, TRUE, TRUE, FALSE);
22523 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524 /* avoid getting stuck on a match with an empty string */
22525 if (tail == regmatch.endp[0])
22526 {
22527 if (*tail == NUL)
22528 break;
22529 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22530 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 }
22532 else
22533 {
22534 tail = regmatch.endp[0];
22535 if (*tail == NUL)
22536 break;
22537 }
22538 if (!do_all)
22539 break;
22540 }
22541
22542 if (ga.ga_data != NULL)
22543 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22544
22545 vim_free(regmatch.regprog);
22546 }
22547
22548 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22549 ga_clear(&ga);
22550 p_cpo = save_cpo;
22551
22552 return ret;
22553}
22554
22555#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */