blob: 5573e764d8df73cab81e98ef1c7ce6f07dc49831 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000111
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000112/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000113 * All user-defined global variables are stored in dictionary "globvardict".
114 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000116static dict_T globvardict;
117static dictitem_T globvars_var;
118#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
120/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000121 * Old Vim variables such as "v:version" are also available without the "v:".
122 * Also in functions. We need a special hashtable for them.
123 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000124static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125
126/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000127 * When recursively copying lists and dicts we need to remember which ones we
128 * have done to avoid endless recursiveness. This unique ID is used for that.
129 */
130static int current_copyID = 0;
131
132/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000133 * Array to hold the hashtab with variables local to each sourced script.
134 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000136typedef struct
137{
138 dictitem_T sv_var;
139 dict_T sv_dict;
140} scriptvar_T;
141
142static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
143#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
144#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146static int echo_attr = 0; /* attributes used for ":echo" */
147
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000148/* Values for trans_function_name() argument: */
149#define TFN_INT 1 /* internal function name OK */
150#define TFN_QUIET 2 /* no error messages */
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Structure to hold info for a user function.
154 */
155typedef struct ufunc ufunc_T;
156
157struct ufunc
158{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000159 int uf_varargs; /* variable nr of arguments */
160 int uf_flags;
161 int uf_calls; /* nr of active calls */
162 garray_T uf_args; /* arguments */
163 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000164#ifdef FEAT_PROFILE
165 int uf_profiling; /* TRUE when func is being profiled */
166 /* profiling the function as a whole */
167 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000168 proftime_T uf_tm_total; /* time spent in function + children */
169 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 proftime_T *uf_tml_total; /* time spent in a line + children */
174 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000195 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000197static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000199/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000200static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
201
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000202/* list heads for garbage collection */
203static dict_T *first_dict = NULL; /* list of all dicts */
204static list_T *first_list = NULL; /* list of all lists */
205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000206/* From user function to hashitem and back. */
207static ufunc_T dumuf;
208#define UF2HIKEY(fp) ((fp)->uf_name)
209#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
210#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
211
212#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
213#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar33570922005-01-25 22:26:29 +0000215#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
216#define VAR_SHORT_LEN 20 /* short variable name length */
217#define FIXVAR_CNT 12 /* number of fixed variables */
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000220typedef struct funccall_S funccall_T;
221
222struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 ufunc_T *func; /* function being called */
225 int linenr; /* next line to be executed */
226 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000227 struct /* fixed variables for arguments */
228 {
229 dictitem_T var; /* variable (without room for name) */
230 char_u room[VAR_SHORT_LEN]; /* room for the name */
231 } fixvar[FIXVAR_CNT];
232 dict_T l_vars; /* l: local function variables */
233 dictitem_T l_vars_var; /* variable for l: scope */
234 dict_T l_avars; /* a: argument variables */
235 dictitem_T l_avars_var; /* variable for a: scope */
236 list_T l_varlist; /* list for a:000 */
237 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
238 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 linenr_T breakpoint; /* next line with breakpoint or zero */
240 int dbg_tick; /* debug_tick when breakpoint was set */
241 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000242#ifdef FEAT_PROFILE
243 proftime_T prof_child; /* time spent in a child */
244#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000245 funccall_T *caller; /* calling function or NULL */
246};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000249 * Info used by a ":for" loop.
250 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000251typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000252{
253 int fi_semicolon; /* TRUE if ending in '; var]' */
254 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 listwatch_T fi_lw; /* keep an eye on the item used. */
256 list_T *fi_list; /* list being used */
257} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000260 * Struct used by trans_function_name()
261 */
262typedef struct
263{
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000265 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dictitem_T *fd_di; /* Dictionary item used */
267} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268
Bram Moolenaara7043832005-01-21 11:56:39 +0000269
270/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 * Array to hold the value of v: variables.
272 * The value is in a dictitem, so that it can also be used in the v: scope.
273 * The reason to use this table anyway is for very quick access to the
274 * variables with the VV_ defines.
275 */
276#include "version.h"
277
278/* values for vv_flags: */
279#define VV_COMPAT 1 /* compatible, also used without "v:" */
280#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000281#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
285static struct vimvar
286{
287 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288 dictitem_T vv_di; /* value and name for key */
289 char vv_filler[16]; /* space for LONGEST name below!!! */
290 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
291} vimvars[VV_LEN] =
292{
293 /*
294 * The order here must match the VV_ defines in vim.h!
295 * Initializing a union does not work, leave tv.vval empty to get zero's.
296 */
297 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
298 {VV_NAME("count1", VAR_NUMBER), VV_RO},
299 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
300 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
301 {VV_NAME("warningmsg", VAR_STRING), 0},
302 {VV_NAME("statusmsg", VAR_STRING), 0},
303 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
305 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
307 {VV_NAME("termresponse", VAR_STRING), VV_RO},
308 {VV_NAME("fname", VAR_STRING), VV_RO},
309 {VV_NAME("lang", VAR_STRING), VV_RO},
310 {VV_NAME("lc_time", VAR_STRING), VV_RO},
311 {VV_NAME("ctype", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
313 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
314 {VV_NAME("fname_in", VAR_STRING), VV_RO},
315 {VV_NAME("fname_out", VAR_STRING), VV_RO},
316 {VV_NAME("fname_new", VAR_STRING), VV_RO},
317 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
318 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
319 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
322 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("progname", VAR_STRING), VV_RO},
324 {VV_NAME("servername", VAR_STRING), VV_RO},
325 {VV_NAME("dying", VAR_NUMBER), VV_RO},
326 {VV_NAME("exception", VAR_STRING), VV_RO},
327 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
328 {VV_NAME("register", VAR_STRING), VV_RO},
329 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
330 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000331 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
332 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000333 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000334 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
335 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000336 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000341 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000342 {VV_NAME("swapname", VAR_STRING), VV_RO},
343 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000344 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000346 {VV_NAME("mouse_win", VAR_NUMBER), 0},
347 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
348 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000349 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000350 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000351 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000352};
353
354/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355#define vv_type vv_di.di_tv.v_type
356#define vv_nr vv_di.di_tv.vval.v_number
357#define vv_float vv_di.di_tv.vval.v_float
358#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000359#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000361
362/*
363 * The v: variables are stored in dictionary "vimvardict".
364 * "vimvars_var" is the variable that is used for the "l:" scope.
365 */
366static dict_T vimvardict;
367static dictitem_T vimvars_var;
368#define vimvarht vimvardict.dv_hashtab
369
Bram Moolenaara40058a2005-07-11 22:42:07 +0000370static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
371static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
372#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
373static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
374#endif
375static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
376static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
377static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000378static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
379static void list_glob_vars __ARGS((int *first));
380static void list_buf_vars __ARGS((int *first));
381static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000384#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000385static void list_vim_vars __ARGS((int *first));
386static void list_script_vars __ARGS((int *first));
387static void list_func_vars __ARGS((int *first));
388static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000389static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
390static int check_changedtick __ARGS((char_u *arg));
391static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
392static void clear_lval __ARGS((lval_T *lp));
393static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
394static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
395static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
396static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
397static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
398static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
399static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
400static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
401static void item_lock __ARGS((typval_T *tv, int deep, int lock));
402static int tv_islocked __ARGS((typval_T *tv));
403
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
405static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000410static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
411static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000412
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000413static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000418static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static listitem_T *listitem_alloc __ARGS((void));
420static void listitem_free __ARGS((listitem_T *item));
421static void listitem_remove __ARGS((list_T *l, listitem_T *item));
422static long list_len __ARGS((list_T *l));
423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000427static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static void list_append __ARGS((list_T *l, listitem_T *item));
430static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000431static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
433static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
434static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000435static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *list2string __ARGS((typval_T *tv, int copyID));
438static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000439static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
440static void set_ref_in_list __ARGS((list_T *l, int copyID));
441static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_alloc __ARGS((char_u *key));
445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
447static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int dict_add __ARGS((dict_T *d, dictitem_T *item));
450static long dict_len __ARGS((dict_T *d));
451static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000452static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000454static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
455static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static int string2float __ARGS((char_u *text, float_T *value));
459#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
461static int find_internal_func __ARGS((char_u *name));
462static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
463static 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));
464static 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 +0000465static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000466static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000467
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#ifdef FEAT_FLOAT
469static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
511static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000534static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000543static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000545static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000551static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000552static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000559static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000560static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000561static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000562static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000565static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000573static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000587static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000593static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000605#ifdef FEAT_FLOAT
606static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
607#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000614static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000616static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620#ifdef vim_mkdir
621static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000627#ifdef FEAT_FLOAT
628static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000631static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000632static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000635static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000647#ifdef FEAT_FLOAT
648static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000651static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000660static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000661static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000662static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000663static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000665static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000667static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#ifdef FEAT_FLOAT
670static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
671#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000673static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000674static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#ifdef FEAT_FLOAT
678static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
680#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000681static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682#ifdef HAVE_STRFTIME
683static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
684#endif
685static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000696static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000698static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000699static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000700static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000701static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000702static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000704static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000708#ifdef FEAT_FLOAT
709static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
710#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000721static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000724static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000725
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000726static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000727static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static int get_env_len __ARGS((char_u **arg));
729static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000730static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000731static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
732#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
733#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
734 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000735static 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 +0000736static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000737static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000738static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
739static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static typval_T *alloc_tv __ARGS((void));
741static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void init_tv __ARGS((typval_T *varp));
743static long get_tv_number __ARGS((typval_T *varp));
744static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000745static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static char_u *get_tv_string __ARGS((typval_T *varp));
747static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000748static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000750static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
752static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
753static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000754static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
755static 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 +0000756static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
757static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000758static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000759static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000761static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
763static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
764static int eval_fname_script __ARGS((char_u *p));
765static int eval_fname_sid __ARGS((char_u *p));
766static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static ufunc_T *find_func __ARGS((char_u *name));
768static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000769static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#ifdef FEAT_PROFILE
771static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000772static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
773static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
774static int
775# ifdef __BORLANDC__
776 _RTLENTRYF
777# endif
778 prof_total_cmp __ARGS((const void *s1, const void *s2));
779static int
780# ifdef __BORLANDC__
781 _RTLENTRYF
782# endif
783 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000785static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000786static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000787static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static void func_free __ARGS((ufunc_T *fp));
789static void func_unref __ARGS((char_u *name));
790static void func_ref __ARGS((char_u *name));
791static 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));
792static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000793static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
794static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000795static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000796static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000797static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000798
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799/* Character used as separated in autoload function/variable names. */
800#define AUTOLOAD_CHAR '#'
801
Bram Moolenaar33570922005-01-25 22:26:29 +0000802/*
803 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000804 */
805 void
806eval_init()
807{
Bram Moolenaar33570922005-01-25 22:26:29 +0000808 int i;
809 struct vimvar *p;
810
811 init_var_dict(&globvardict, &globvars_var);
812 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000813 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000814 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000815
816 for (i = 0; i < VV_LEN; ++i)
817 {
818 p = &vimvars[i];
819 STRCPY(p->vv_di.di_key, p->vv_name);
820 if (p->vv_flags & VV_RO)
821 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
822 else if (p->vv_flags & VV_RO_SBX)
823 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
824 else
825 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000826
827 /* add to v: scope dict, unless the value is not always available */
828 if (p->vv_type != VAR_UNKNOWN)
829 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000830 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000831 /* add to compat scope dict */
832 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000833 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000834 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000835}
836
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000837#if defined(EXITFREE) || defined(PROTO)
838 void
839eval_clear()
840{
841 int i;
842 struct vimvar *p;
843
844 for (i = 0; i < VV_LEN; ++i)
845 {
846 p = &vimvars[i];
847 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000848 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000849 vim_free(p->vv_str);
850 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000851 }
852 else if (p->vv_di.di_tv.v_type == VAR_LIST)
853 {
854 list_unref(p->vv_list);
855 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000856 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000857 }
858 hash_clear(&vimvarht);
859 hash_clear(&compat_hashtab);
860
861 /* script-local variables */
862 for (i = 1; i <= ga_scripts.ga_len; ++i)
863 vars_clear(&SCRIPT_VARS(i));
864 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000865 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000866
867 /* global variables */
868 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000869
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000870 /* autoloaded script names */
871 ga_clear_strings(&ga_loaded);
872
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000873 /* unreferenced lists and dicts */
874 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000875
876 /* functions */
877 free_all_functions();
878 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000879}
880#endif
881
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 * Return the name of the executed function.
884 */
885 char_u *
886func_name(cookie)
887 void *cookie;
888{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000889 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890}
891
892/*
893 * Return the address holding the next breakpoint line for a funccall cookie.
894 */
895 linenr_T *
896func_breakpoint(cookie)
897 void *cookie;
898{
Bram Moolenaar33570922005-01-25 22:26:29 +0000899 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900}
901
902/*
903 * Return the address holding the debug tick for a funccall cookie.
904 */
905 int *
906func_dbg_tick(cookie)
907 void *cookie;
908{
Bram Moolenaar33570922005-01-25 22:26:29 +0000909 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910}
911
912/*
913 * Return the nesting level for a funccall cookie.
914 */
915 int
916func_level(cookie)
917 void *cookie;
918{
Bram Moolenaar33570922005-01-25 22:26:29 +0000919 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920}
921
922/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000923funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
925/*
926 * Return TRUE when a function was ended by a ":return" command.
927 */
928 int
929current_func_returned()
930{
931 return current_funccal->returned;
932}
933
934
935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Set an internal variable to a string value. Creates the variable if it does
937 * not already exist.
938 */
939 void
940set_internal_string_var(name, value)
941 char_u *name;
942 char_u *value;
943{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000944 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000945 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 val = vim_strsave(value);
948 if (val != NULL)
949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000950 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000951 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000953 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956 }
957}
958
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000959static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000960static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961static char_u *redir_endp = NULL;
962static char_u *redir_varname = NULL;
963
964/*
965 * Start recording command output to a variable
966 * Returns OK if successfully completed the setup. FAIL otherwise.
967 */
968 int
969var_redir_start(name, append)
970 char_u *name;
971 int append; /* append to an existing variable */
972{
973 int save_emsg;
974 int err;
975 typval_T tv;
976
977 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000978 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000979 {
980 EMSG(_(e_invarg));
981 return FAIL;
982 }
983
984 redir_varname = vim_strsave(name);
985 if (redir_varname == NULL)
986 return FAIL;
987
988 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
989 if (redir_lval == NULL)
990 {
991 var_redir_stop();
992 return FAIL;
993 }
994
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000995 /* The output is stored in growarray "redir_ga" until redirection ends. */
996 ga_init2(&redir_ga, (int)sizeof(char), 500);
997
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000998 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000999 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1000 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1002 {
1003 if (redir_endp != NULL && *redir_endp != NUL)
1004 /* Trailing characters are present after the variable name */
1005 EMSG(_(e_trailing));
1006 else
1007 EMSG(_(e_invarg));
1008 var_redir_stop();
1009 return FAIL;
1010 }
1011
1012 /* check if we can write to the variable: set it to or append an empty
1013 * string */
1014 save_emsg = did_emsg;
1015 did_emsg = FALSE;
1016 tv.v_type = VAR_STRING;
1017 tv.vval.v_string = (char_u *)"";
1018 if (append)
1019 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1020 else
1021 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1022 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001023 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001024 if (err)
1025 {
1026 var_redir_stop();
1027 return FAIL;
1028 }
1029 if (redir_lval->ll_newkey != NULL)
1030 {
1031 /* Dictionary item was created, don't do it again. */
1032 vim_free(redir_lval->ll_newkey);
1033 redir_lval->ll_newkey = NULL;
1034 }
1035
1036 return OK;
1037}
1038
1039/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001040 * Append "value[value_len]" to the variable set by var_redir_start().
1041 * The actual appending is postponed until redirection ends, because the value
1042 * appended may in fact be the string we write to, changing it may cause freed
1043 * memory to be used:
1044 * :redir => foo
1045 * :let foo
1046 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047 */
1048 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001049var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001053 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054
1055 if (redir_lval == NULL)
1056 return;
1057
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001058 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001059 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001061 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001063 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001064 {
1065 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001066 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001067 }
1068 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070}
1071
1072/*
1073 * Stop redirecting command output to a variable.
1074 */
1075 void
1076var_redir_stop()
1077{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001078 typval_T tv;
1079
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 if (redir_lval != NULL)
1081 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001082 /* Append the trailing NUL. */
1083 ga_append(&redir_ga, NUL);
1084
1085 /* Assign the text to the variable. */
1086 tv.v_type = VAR_STRING;
1087 tv.vval.v_string = redir_ga.ga_data;
1088 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1089 vim_free(tv.vval.v_string);
1090
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 clear_lval(redir_lval);
1092 vim_free(redir_lval);
1093 redir_lval = NULL;
1094 }
1095 vim_free(redir_varname);
1096 redir_varname = NULL;
1097}
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099# if defined(FEAT_MBYTE) || defined(PROTO)
1100 int
1101eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1102 char_u *enc_from;
1103 char_u *enc_to;
1104 char_u *fname_from;
1105 char_u *fname_to;
1106{
1107 int err = FALSE;
1108
1109 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1110 set_vim_var_string(VV_CC_TO, enc_to, -1);
1111 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1112 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1113 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1114 err = TRUE;
1115 set_vim_var_string(VV_CC_FROM, NULL, -1);
1116 set_vim_var_string(VV_CC_TO, NULL, -1);
1117 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1118 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1119
1120 if (err)
1121 return FAIL;
1122 return OK;
1123}
1124# endif
1125
1126# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1127 int
1128eval_printexpr(fname, args)
1129 char_u *fname;
1130 char_u *args;
1131{
1132 int err = FALSE;
1133
1134 set_vim_var_string(VV_FNAME_IN, fname, -1);
1135 set_vim_var_string(VV_CMDARG, args, -1);
1136 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1137 err = TRUE;
1138 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1139 set_vim_var_string(VV_CMDARG, NULL, -1);
1140
1141 if (err)
1142 {
1143 mch_remove(fname);
1144 return FAIL;
1145 }
1146 return OK;
1147}
1148# endif
1149
1150# if defined(FEAT_DIFF) || defined(PROTO)
1151 void
1152eval_diff(origfile, newfile, outfile)
1153 char_u *origfile;
1154 char_u *newfile;
1155 char_u *outfile;
1156{
1157 int err = FALSE;
1158
1159 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1160 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1161 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1162 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1163 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1164 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1165 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1166}
1167
1168 void
1169eval_patch(origfile, difffile, outfile)
1170 char_u *origfile;
1171 char_u *difffile;
1172 char_u *outfile;
1173{
1174 int err;
1175
1176 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1177 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1178 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1179 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1180 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1181 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1182 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1183}
1184# endif
1185
1186/*
1187 * Top level evaluation function, returning a boolean.
1188 * Sets "error" to TRUE if there was an error.
1189 * Return TRUE or FALSE.
1190 */
1191 int
1192eval_to_bool(arg, error, nextcmd, skip)
1193 char_u *arg;
1194 int *error;
1195 char_u **nextcmd;
1196 int skip; /* only parse, don't execute */
1197{
Bram Moolenaar33570922005-01-25 22:26:29 +00001198 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 int retval = FALSE;
1200
1201 if (skip)
1202 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 else
1206 {
1207 *error = FALSE;
1208 if (!skip)
1209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001210 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 }
1214 if (skip)
1215 --emsg_skip;
1216
1217 return retval;
1218}
1219
1220/*
1221 * Top level evaluation function, returning a string. If "skip" is TRUE,
1222 * only parsing to "nextcmd" is done, without reporting errors. Return
1223 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1224 */
1225 char_u *
1226eval_to_string_skip(arg, nextcmd, skip)
1227 char_u *arg;
1228 char_u **nextcmd;
1229 int skip; /* only parse, don't execute */
1230{
Bram Moolenaar33570922005-01-25 22:26:29 +00001231 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 char_u *retval;
1233
1234 if (skip)
1235 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001236 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 retval = NULL;
1238 else
1239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 retval = vim_strsave(get_tv_string(&tv));
1241 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243 if (skip)
1244 --emsg_skip;
1245
1246 return retval;
1247}
1248
1249/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001250 * Skip over an expression at "*pp".
1251 * Return FAIL for an error, OK otherwise.
1252 */
1253 int
1254skip_expr(pp)
1255 char_u **pp;
1256{
Bram Moolenaar33570922005-01-25 22:26:29 +00001257 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001258
1259 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001260 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001261}
1262
1263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001265 * When "convert" is TRUE convert a List into a sequence of lines and convert
1266 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * Return pointer to allocated memory, or NULL for failure.
1268 */
1269 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001270eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 char_u *arg;
1272 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001273 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001277 garray_T ga;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001278 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 retval = NULL;
1282 else
1283 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001284 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001285 {
1286 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001287 if (tv.vval.v_list != NULL)
1288 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001289 ga_append(&ga, NUL);
1290 retval = (char_u *)ga.ga_data;
1291 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001292#ifdef FEAT_FLOAT
1293 else if (convert && tv.v_type == VAR_FLOAT)
1294 {
1295 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1296 retval = vim_strsave(numbuf);
1297 }
1298#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001299 else
1300 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 }
1303
1304 return retval;
1305}
1306
1307/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001308 * Call eval_to_string() without using current local variables and using
1309 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 */
1311 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001312eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 char_u *arg;
1314 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001315 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 char_u *retval;
1318 void *save_funccalp;
1319
1320 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001321 if (use_sandbox)
1322 ++sandbox;
1323 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001324 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001325 if (use_sandbox)
1326 --sandbox;
1327 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 restore_funccal(save_funccalp);
1329 return retval;
1330}
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332/*
1333 * Top level evaluation function, returning a number.
1334 * Evaluates "expr" silently.
1335 * Returns -1 for an error.
1336 */
1337 int
1338eval_to_number(expr)
1339 char_u *expr;
1340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001343 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345 ++emsg_off;
1346
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 retval = -1;
1349 else
1350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001351 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354 --emsg_off;
1355
1356 return retval;
1357}
1358
Bram Moolenaara40058a2005-07-11 22:42:07 +00001359/*
1360 * Prepare v: variable "idx" to be used.
1361 * Save the current typeval in "save_tv".
1362 * When not used yet add the variable to the v: hashtable.
1363 */
1364 static void
1365prepare_vimvar(idx, save_tv)
1366 int idx;
1367 typval_T *save_tv;
1368{
1369 *save_tv = vimvars[idx].vv_tv;
1370 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1371 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1372}
1373
1374/*
1375 * Restore v: variable "idx" to typeval "save_tv".
1376 * When no longer defined, remove the variable from the v: hashtable.
1377 */
1378 static void
1379restore_vimvar(idx, save_tv)
1380 int idx;
1381 typval_T *save_tv;
1382{
1383 hashitem_T *hi;
1384
Bram Moolenaara40058a2005-07-11 22:42:07 +00001385 vimvars[idx].vv_tv = *save_tv;
1386 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1387 {
1388 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1389 if (HASHITEM_EMPTY(hi))
1390 EMSG2(_(e_intern2), "restore_vimvar()");
1391 else
1392 hash_remove(&vimvarht, hi);
1393 }
1394}
1395
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001396#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001397/*
1398 * Evaluate an expression to a list with suggestions.
1399 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001400 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001401 */
1402 list_T *
1403eval_spell_expr(badword, expr)
1404 char_u *badword;
1405 char_u *expr;
1406{
1407 typval_T save_val;
1408 typval_T rettv;
1409 list_T *list = NULL;
1410 char_u *p = skipwhite(expr);
1411
1412 /* Set "v:val" to the bad word. */
1413 prepare_vimvar(VV_VAL, &save_val);
1414 vimvars[VV_VAL].vv_type = VAR_STRING;
1415 vimvars[VV_VAL].vv_str = badword;
1416 if (p_verbose == 0)
1417 ++emsg_off;
1418
1419 if (eval1(&p, &rettv, TRUE) == OK)
1420 {
1421 if (rettv.v_type != VAR_LIST)
1422 clear_tv(&rettv);
1423 else
1424 list = rettv.vval.v_list;
1425 }
1426
1427 if (p_verbose == 0)
1428 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001429 restore_vimvar(VV_VAL, &save_val);
1430
1431 return list;
1432}
1433
1434/*
1435 * "list" is supposed to contain two items: a word and a number. Return the
1436 * word in "pp" and the number as the return value.
1437 * Return -1 if anything isn't right.
1438 * Used to get the good word and score from the eval_spell_expr() result.
1439 */
1440 int
1441get_spellword(list, pp)
1442 list_T *list;
1443 char_u **pp;
1444{
1445 listitem_T *li;
1446
1447 li = list->lv_first;
1448 if (li == NULL)
1449 return -1;
1450 *pp = get_tv_string(&li->li_tv);
1451
1452 li = li->li_next;
1453 if (li == NULL)
1454 return -1;
1455 return get_tv_number(&li->li_tv);
1456}
1457#endif
1458
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001459/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001460 * Top level evaluation function.
1461 * Returns an allocated typval_T with the result.
1462 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001463 */
1464 typval_T *
1465eval_expr(arg, nextcmd)
1466 char_u *arg;
1467 char_u **nextcmd;
1468{
1469 typval_T *tv;
1470
1471 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001472 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001473 {
1474 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001475 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001476 }
1477
1478 return tv;
1479}
1480
1481
Bram Moolenaar4f688582007-07-24 12:34:30 +00001482#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1483 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001486 * Uses argv[argc] for the function arguments. Only Number and String
1487 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001488 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001490 static int
1491call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 char_u *func;
1493 int argc;
1494 char_u **argv;
1495 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
Bram Moolenaar33570922005-01-25 22:26:29 +00001498 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 long n;
1500 int len;
1501 int i;
1502 int doesrange;
1503 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001504 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001506 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
1510 for (i = 0; i < argc; i++)
1511 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001512 /* Pass a NULL or empty argument as an empty string */
1513 if (argv[i] == NULL || *argv[i] == NUL)
1514 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001515 argvars[i].v_type = VAR_STRING;
1516 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001517 continue;
1518 }
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /* Recognize a number argument, the others must be strings. */
1521 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1522 if (len != 0 && len == (int)STRLEN(argv[i]))
1523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001524 argvars[i].v_type = VAR_NUMBER;
1525 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
1527 else
1528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001529 argvars[i].v_type = VAR_STRING;
1530 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 }
1533
1534 if (safe)
1535 {
1536 save_funccalp = save_funccal();
1537 ++sandbox;
1538 }
1539
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001540 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1541 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001543 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (safe)
1545 {
1546 --sandbox;
1547 restore_funccal(save_funccalp);
1548 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001549 vim_free(argvars);
1550
1551 if (ret == FAIL)
1552 clear_tv(rettv);
1553
1554 return ret;
1555}
1556
Bram Moolenaar4f688582007-07-24 12:34:30 +00001557# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001559 * Call vimL function "func" and return the result as a string.
1560 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Uses argv[argc] for the function arguments.
1562 */
1563 void *
1564call_func_retstr(func, argc, argv, safe)
1565 char_u *func;
1566 int argc;
1567 char_u **argv;
1568 int safe; /* use the sandbox */
1569{
1570 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001571 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572
1573 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1574 return NULL;
1575
1576 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 return retval;
1579}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001580# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581
Bram Moolenaar4f688582007-07-24 12:34:30 +00001582# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001583/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001584 * Call vimL function "func" and return the result as a number.
1585 * Returns -1 when calling the function fails.
1586 * Uses argv[argc] for the function arguments.
1587 */
1588 long
1589call_func_retnr(func, argc, argv, safe)
1590 char_u *func;
1591 int argc;
1592 char_u **argv;
1593 int safe; /* use the sandbox */
1594{
1595 typval_T rettv;
1596 long retval;
1597
1598 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1599 return -1;
1600
1601 retval = get_tv_number_chk(&rettv, NULL);
1602 clear_tv(&rettv);
1603 return retval;
1604}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001605# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001606
1607/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001608 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001610 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 */
1612 void *
1613call_func_retlist(func, argc, argv, safe)
1614 char_u *func;
1615 int argc;
1616 char_u **argv;
1617 int safe; /* use the sandbox */
1618{
1619 typval_T rettv;
1620
1621 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1622 return NULL;
1623
1624 if (rettv.v_type != VAR_LIST)
1625 {
1626 clear_tv(&rettv);
1627 return NULL;
1628 }
1629
1630 return rettv.vval.v_list;
1631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
1633
Bram Moolenaar4f688582007-07-24 12:34:30 +00001634
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635/*
1636 * Save the current function call pointer, and set it to NULL.
1637 * Used when executing autocommands and for ":source".
1638 */
1639 void *
1640save_funccal()
1641{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001642 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 current_funccal = NULL;
1645 return (void *)fc;
1646}
1647
1648 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001649restore_funccal(vfc)
1650 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001652 funccall_T *fc = (funccall_T *)vfc;
1653
1654 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655}
1656
Bram Moolenaar05159a02005-02-26 23:04:13 +00001657#if defined(FEAT_PROFILE) || defined(PROTO)
1658/*
1659 * Prepare profiling for entering a child or something else that is not
1660 * counted for the script/function itself.
1661 * Should always be called in pair with prof_child_exit().
1662 */
1663 void
1664prof_child_enter(tm)
1665 proftime_T *tm; /* place to store waittime */
1666{
1667 funccall_T *fc = current_funccal;
1668
1669 if (fc != NULL && fc->func->uf_profiling)
1670 profile_start(&fc->prof_child);
1671 script_prof_save(tm);
1672}
1673
1674/*
1675 * Take care of time spent in a child.
1676 * Should always be called after prof_child_enter().
1677 */
1678 void
1679prof_child_exit(tm)
1680 proftime_T *tm; /* where waittime was stored */
1681{
1682 funccall_T *fc = current_funccal;
1683
1684 if (fc != NULL && fc->func->uf_profiling)
1685 {
1686 profile_end(&fc->prof_child);
1687 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1688 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1689 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1690 }
1691 script_prof_restore(tm);
1692}
1693#endif
1694
1695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#ifdef FEAT_FOLDING
1697/*
1698 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1699 * it in "*cp". Doesn't give error messages.
1700 */
1701 int
1702eval_foldexpr(arg, cp)
1703 char_u *arg;
1704 int *cp;
1705{
Bram Moolenaar33570922005-01-25 22:26:29 +00001706 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 int retval;
1708 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001709 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1710 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001713 if (use_sandbox)
1714 ++sandbox;
1715 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 retval = 0;
1719 else
1720 {
1721 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 if (tv.v_type == VAR_NUMBER)
1723 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001724 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 retval = 0;
1726 else
1727 {
1728 /* If the result is a string, check if there is a non-digit before
1729 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (!VIM_ISDIGIT(*s) && *s != '-')
1732 *cp = *s++;
1733 retval = atol((char *)s);
1734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001735 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
1737 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001738 if (use_sandbox)
1739 --sandbox;
1740 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742 return retval;
1743}
1744#endif
1745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001747 * ":let" list all variable values
1748 * ":let var1 var2" list variable values
1749 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001750 * ":let var += expr" assignment command.
1751 * ":let var -= expr" assignment command.
1752 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001753 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 */
1755 void
1756ex_let(eap)
1757 exarg_T *eap;
1758{
1759 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001761 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 int var_count = 0;
1764 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001766 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001767 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaardb552d602006-03-23 22:59:57 +00001769 argend = skip_var_list(arg, &var_count, &semicolon);
1770 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001772 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1773 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001774 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001777 /*
1778 * ":let" without "=": list variables
1779 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 if (*arg == '[')
1781 EMSG(_(e_invarg));
1782 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001784 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001788 list_glob_vars(&first);
1789 list_buf_vars(&first);
1790 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001791#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001792 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001793#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001794 list_script_vars(&first);
1795 list_func_vars(&first);
1796 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 eap->nextcmd = check_nextcmd(arg);
1799 }
1800 else
1801 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001802 op[0] = '=';
1803 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001804 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 {
1806 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1807 op[0] = expr[-1]; /* +=, -= or .= */
1808 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (eap->skip)
1812 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (eap->skip)
1815 {
1816 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 --emsg_skip;
1819 }
1820 else if (i != FAIL)
1821 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826 }
1827}
1828
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829/*
1830 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1831 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001832 * When "nextchars" is not NULL it points to a string with characters that
1833 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1834 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835 * Returns OK or FAIL;
1836 */
1837 static int
1838ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1839 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 int copy; /* copy values from "tv", don't move */
1842 int semicolon; /* from skip_var_list() */
1843 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845{
1846 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 listitem_T *item;
1850 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851
1852 if (*arg != '[')
1853 {
1854 /*
1855 * ":let var = expr" or ":for var in list"
1856 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001857 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 return FAIL;
1859 return OK;
1860 }
1861
1862 /*
1863 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1864 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001865 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001866 {
1867 EMSG(_(e_listreq));
1868 return FAIL;
1869 }
1870
1871 i = list_len(l);
1872 if (semicolon == 0 && var_count < i)
1873 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001874 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 return FAIL;
1876 }
1877 if (var_count - semicolon > i)
1878 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001879 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 return FAIL;
1881 }
1882
1883 item = l->lv_first;
1884 while (*arg != ']')
1885 {
1886 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 item = item->li_next;
1889 if (arg == NULL)
1890 return FAIL;
1891
1892 arg = skipwhite(arg);
1893 if (*arg == ';')
1894 {
1895 /* Put the rest of the list (may be empty) in the var after ';'.
1896 * Create a new list for this. */
1897 l = list_alloc();
1898 if (l == NULL)
1899 return FAIL;
1900 while (item != NULL)
1901 {
1902 list_append_tv(l, &item->li_tv);
1903 item = item->li_next;
1904 }
1905
1906 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001907 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908 ltv.vval.v_list = l;
1909 l->lv_refcount = 1;
1910
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1912 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 clear_tv(&ltv);
1914 if (arg == NULL)
1915 return FAIL;
1916 break;
1917 }
1918 else if (*arg != ',' && *arg != ']')
1919 {
1920 EMSG2(_(e_intern2), "ex_let_vars()");
1921 return FAIL;
1922 }
1923 }
1924
1925 return OK;
1926}
1927
1928/*
1929 * Skip over assignable variable "var" or list of variables "[var, var]".
1930 * Used for ":let varvar = expr" and ":for varvar in expr".
1931 * For "[var, var]" increment "*var_count" for each variable.
1932 * for "[var, var; var]" set "semicolon".
1933 * Return NULL for an error.
1934 */
1935 static char_u *
1936skip_var_list(arg, var_count, semicolon)
1937 char_u *arg;
1938 int *var_count;
1939 int *semicolon;
1940{
1941 char_u *p, *s;
1942
1943 if (*arg == '[')
1944 {
1945 /* "[var, var]": find the matching ']'. */
1946 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001947 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 {
1949 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1950 s = skip_var_one(p);
1951 if (s == p)
1952 {
1953 EMSG2(_(e_invarg2), p);
1954 return NULL;
1955 }
1956 ++*var_count;
1957
1958 p = skipwhite(s);
1959 if (*p == ']')
1960 break;
1961 else if (*p == ';')
1962 {
1963 if (*semicolon == 1)
1964 {
1965 EMSG(_("Double ; in list of variables"));
1966 return NULL;
1967 }
1968 *semicolon = 1;
1969 }
1970 else if (*p != ',')
1971 {
1972 EMSG2(_(e_invarg2), p);
1973 return NULL;
1974 }
1975 }
1976 return p + 1;
1977 }
1978 else
1979 return skip_var_one(arg);
1980}
1981
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001983 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001984 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001985 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 static char_u *
1987skip_var_one(arg)
1988 char_u *arg;
1989{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001990 if (*arg == '@' && arg[1] != NUL)
1991 return arg + 2;
1992 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1993 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994}
1995
Bram Moolenaara7043832005-01-21 11:56:39 +00001996/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001997 * List variables for hashtab "ht" with prefix "prefix".
1998 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001999 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002001list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002002 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002003 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002004 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002005 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002006{
Bram Moolenaar33570922005-01-25 22:26:29 +00002007 hashitem_T *hi;
2008 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002009 int todo;
2010
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002011 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002012 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2013 {
2014 if (!HASHITEM_EMPTY(hi))
2015 {
2016 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002017 di = HI2DI(hi);
2018 if (empty || di->di_tv.v_type != VAR_STRING
2019 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002020 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002021 }
2022 }
2023}
2024
2025/*
2026 * List global variables.
2027 */
2028 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002029list_glob_vars(first)
2030 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002031{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002032 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002033}
2034
2035/*
2036 * List buffer variables.
2037 */
2038 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002039list_buf_vars(first)
2040 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002041{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 char_u numbuf[NUMBUFLEN];
2043
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002044 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2045 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046
2047 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002048 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2049 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002050}
2051
2052/*
2053 * List window variables.
2054 */
2055 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002056list_win_vars(first)
2057 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002058{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002059 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2060 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002061}
2062
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002063#ifdef FEAT_WINDOWS
2064/*
2065 * List tab page variables.
2066 */
2067 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002068list_tab_vars(first)
2069 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002070{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2072 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002073}
2074#endif
2075
Bram Moolenaara7043832005-01-21 11:56:39 +00002076/*
2077 * List Vim variables.
2078 */
2079 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002080list_vim_vars(first)
2081 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084}
2085
2086/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002087 * List script-local variables, if there is a script.
2088 */
2089 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090list_script_vars(first)
2091 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002092{
2093 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2095 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002096}
2097
2098/*
2099 * List function variables, if there is a function.
2100 */
2101 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102list_func_vars(first)
2103 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002104{
2105 if (current_funccal != NULL)
2106 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002108}
2109
2110/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 * List variables in "arg".
2112 */
2113 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 exarg_T *eap;
2116 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118{
2119 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 char_u *name_start;
2123 char_u *arg_subsc;
2124 char_u *tofree;
2125 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126
2127 while (!ends_excmd(*arg) && !got_int)
2128 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002129 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002131 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2133 {
2134 emsg_severe = TRUE;
2135 EMSG(_(e_trailing));
2136 break;
2137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002141 /* get_name_len() takes care of expanding curly braces */
2142 name_start = name = arg;
2143 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2144 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146 /* This is mainly to keep test 49 working: when expanding
2147 * curly braces fails overrule the exception error message. */
2148 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 emsg_severe = TRUE;
2151 EMSG2(_(e_invarg2), arg);
2152 break;
2153 }
2154 error = TRUE;
2155 }
2156 else
2157 {
2158 if (tofree != NULL)
2159 name = tofree;
2160 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 else
2163 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002164 /* handle d.key, l[idx], f(expr) */
2165 arg_subsc = arg;
2166 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002169 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 case 'g': list_glob_vars(first); break;
2175 case 'b': list_buf_vars(first); break;
2176 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002177#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 case 'v': list_vim_vars(first); break;
2181 case 's': list_script_vars(first); break;
2182 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 default:
2184 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002186 }
2187 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 {
2189 char_u numbuf[NUMBUFLEN];
2190 char_u *tf;
2191 int c;
2192 char_u *s;
2193
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002194 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 c = *arg;
2196 *arg = NUL;
2197 list_one_var_a((char_u *)"",
2198 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 tv.v_type,
2200 s == NULL ? (char_u *)"" : s,
2201 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 *arg = c;
2203 vim_free(tf);
2204 }
2205 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002206 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 }
2208 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209
2210 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212
2213 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
2215
2216 return arg;
2217}
2218
2219/*
2220 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2221 * Returns a pointer to the char just after the var name.
2222 * Returns NULL if there is an error.
2223 */
2224 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002225ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002227 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002229 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002230 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231{
2232 int c1;
2233 char_u *name;
2234 char_u *p;
2235 char_u *arg_end = NULL;
2236 int len;
2237 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002238 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239
2240 /*
2241 * ":let $VAR = expr": Set environment variable.
2242 */
2243 if (*arg == '$')
2244 {
2245 /* Find the end of the name. */
2246 ++arg;
2247 name = arg;
2248 len = get_env_len(&arg);
2249 if (len == 0)
2250 EMSG2(_(e_invarg2), name - 1);
2251 else
2252 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002253 if (op != NULL && (*op == '+' || *op == '-'))
2254 EMSG2(_(e_letwrong), op);
2255 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002256 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 EMSG(_(e_letunexp));
2258 else
2259 {
2260 c1 = name[len];
2261 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 p = get_tv_string_chk(tv);
2263 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 {
2265 int mustfree = FALSE;
2266 char_u *s = vim_getenv(name, &mustfree);
2267
2268 if (s != NULL)
2269 {
2270 p = tofree = concat_str(s, p);
2271 if (mustfree)
2272 vim_free(s);
2273 }
2274 }
2275 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002276 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002277 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 if (STRICMP(name, "HOME") == 0)
2279 init_homedir();
2280 else if (didset_vim && STRICMP(name, "VIM") == 0)
2281 didset_vim = FALSE;
2282 else if (didset_vimruntime
2283 && STRICMP(name, "VIMRUNTIME") == 0)
2284 didset_vimruntime = FALSE;
2285 arg_end = arg;
2286 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
2290 }
2291 }
2292
2293 /*
2294 * ":let &option = expr": Set option value.
2295 * ":let &l:option = expr": Set local option value.
2296 * ":let &g:option = expr": Set global option value.
2297 */
2298 else if (*arg == '&')
2299 {
2300 /* Find the end of the name. */
2301 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002302 if (p == NULL || (endchars != NULL
2303 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 EMSG(_(e_letunexp));
2305 else
2306 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 long n;
2308 int opt_type;
2309 long numval;
2310 char_u *stringval = NULL;
2311 char_u *s;
2312
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 c1 = *p;
2314 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315
2316 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002317 s = get_tv_string_chk(tv); /* != NULL if number or string */
2318 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319 {
2320 opt_type = get_option_value(arg, &numval,
2321 &stringval, opt_flags);
2322 if ((opt_type == 1 && *op == '.')
2323 || (opt_type == 0 && *op != '.'))
2324 EMSG2(_(e_letwrong), op);
2325 else
2326 {
2327 if (opt_type == 1) /* number */
2328 {
2329 if (*op == '+')
2330 n = numval + n;
2331 else
2332 n = numval - n;
2333 }
2334 else if (opt_type == 0 && stringval != NULL) /* string */
2335 {
2336 s = concat_str(stringval, s);
2337 vim_free(stringval);
2338 stringval = s;
2339 }
2340 }
2341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 if (s != NULL)
2343 {
2344 set_option_value(arg, n, s, opt_flags);
2345 arg_end = p;
2346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 }
2350 }
2351
2352 /*
2353 * ":let @r = expr": Set register contents.
2354 */
2355 else if (*arg == '@')
2356 {
2357 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 if (op != NULL && (*op == '+' || *op == '-'))
2359 EMSG2(_(e_letwrong), op);
2360 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002361 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 EMSG(_(e_letunexp));
2363 else
2364 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002365 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 char_u *s;
2367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 p = get_tv_string_chk(tv);
2369 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002371 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 if (s != NULL)
2373 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002374 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 vim_free(s);
2376 }
2377 }
2378 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002381 arg_end = arg + 1;
2382 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002383 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 }
2385 }
2386
2387 /*
2388 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002391 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002393 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002395 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2399 EMSG(_(e_letunexp));
2400 else
2401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 arg_end = p;
2404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 }
2408
2409 else
2410 EMSG2(_(e_invarg2), arg);
2411
2412 return arg_end;
2413}
2414
2415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2417 */
2418 static int
2419check_changedtick(arg)
2420 char_u *arg;
2421{
2422 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2423 {
2424 EMSG2(_(e_readonlyvar), arg);
2425 return TRUE;
2426 }
2427 return FALSE;
2428}
2429
2430/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 * Get an lval: variable, Dict item or List item that can be assigned a value
2432 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2433 * "name.key", "name.key[expr]" etc.
2434 * Indexing only works if "name" is an existing List or Dictionary.
2435 * "name" points to the start of the name.
2436 * If "rettv" is not NULL it points to the value to be assigned.
2437 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2438 * wrong; must end in space or cmd separator.
2439 *
2440 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002441 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 */
2444 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002445get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002447 typval_T *rettv;
2448 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 int unlet;
2450 int skip;
2451 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002452 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 char_u *expr_start, *expr_end;
2456 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002457 dictitem_T *v;
2458 typval_T var1;
2459 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002461 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002464 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002467 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468
2469 if (skip)
2470 {
2471 /* When skipping just find the end of the name. */
2472 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002473 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 }
2475
2476 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002477 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (expr_start != NULL)
2479 {
2480 /* Don't expand the name when we already know there is an error. */
2481 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2482 && *p != '[' && *p != '.')
2483 {
2484 EMSG(_(e_trailing));
2485 return NULL;
2486 }
2487
2488 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2489 if (lp->ll_exp_name == NULL)
2490 {
2491 /* Report an invalid expression in braces, unless the
2492 * expression evaluation has been cancelled due to an
2493 * aborting error, an interrupt, or an exception. */
2494 if (!aborting() && !quiet)
2495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002496 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 EMSG2(_(e_invarg2), name);
2498 return NULL;
2499 }
2500 }
2501 lp->ll_name = lp->ll_exp_name;
2502 }
2503 else
2504 lp->ll_name = name;
2505
2506 /* Without [idx] or .key we are done. */
2507 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2508 return p;
2509
2510 cc = *p;
2511 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002512 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (v == NULL && !quiet)
2514 EMSG2(_(e_undefvar), lp->ll_name);
2515 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 if (v == NULL)
2517 return NULL;
2518
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 /*
2520 * Loop until no more [idx] or .key is following.
2521 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2526 && !(lp->ll_tv->v_type == VAR_DICT
2527 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (!quiet)
2530 EMSG(_("E689: Can only index a List or Dictionary"));
2531 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (!quiet)
2536 EMSG(_("E708: [:] must come last"));
2537 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002539
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 len = -1;
2541 if (*p == '.')
2542 {
2543 key = p + 1;
2544 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2545 ;
2546 if (len == 0)
2547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 if (!quiet)
2549 EMSG(_(e_emptykey));
2550 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 }
2552 p = key + len;
2553 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002554 else
2555 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002557 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (*p == ':')
2559 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002560 else
2561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 empty1 = FALSE;
2563 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002565 if (get_tv_string_chk(&var1) == NULL)
2566 {
2567 /* not a number or string */
2568 clear_tv(&var1);
2569 return NULL;
2570 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 }
2572
2573 /* Optionally get the second index [ :expr]. */
2574 if (*p == ':')
2575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002579 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002580 if (!empty1)
2581 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002583 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (rettv != NULL && (rettv->v_type != VAR_LIST
2585 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (!quiet)
2588 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 if (!empty1)
2590 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 }
2593 p = skipwhite(p + 1);
2594 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 else
2597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 if (!empty1)
2602 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002605 if (get_tv_string_chk(&var2) == NULL)
2606 {
2607 /* not a number or string */
2608 if (!empty1)
2609 clear_tv(&var1);
2610 clear_tv(&var2);
2611 return NULL;
2612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002618
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (!quiet)
2622 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 if (!empty1)
2624 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629
2630 /* Skip to past ']'. */
2631 ++p;
2632 }
2633
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 {
2636 if (len == -1)
2637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002639 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 if (*key == NUL)
2641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!quiet)
2643 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
2647 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002648 lp->ll_list = NULL;
2649 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002650 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002653 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002657 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 if (len == -1)
2659 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 }
2662 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (len == -1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 p = NULL;
2670 break;
2671 }
2672 if (len == -1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676 else
2677 {
2678 /*
2679 * Get the number and item for the only or first index of the List.
2680 */
2681 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 else
2684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002685 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 clear_tv(&var1);
2687 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_list = lp->ll_tv->vval.v_list;
2690 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2691 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002693 if (lp->ll_n1 < 0)
2694 {
2695 lp->ll_n1 = 0;
2696 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2697 }
2698 }
2699 if (lp->ll_li == NULL)
2700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705
2706 /*
2707 * May need to find the item or absolute index for the second
2708 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 * When no index given: "lp->ll_empty2" is TRUE.
2710 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002714 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2725 if (lp->ll_n1 < 0)
2726 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2727 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 }
2730
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002733 }
2734
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return p;
2736}
2737
2738/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002739 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 */
2741 static void
2742clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002743 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744{
2745 vim_free(lp->ll_exp_name);
2746 vim_free(lp->ll_newkey);
2747}
2748
2749/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002750 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 */
2754 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002756 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002758 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761{
2762 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002763 listitem_T *ri;
2764 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765
2766 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 cc = *endp;
2771 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 if (op != NULL && *op != '=')
2773 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002776 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002777 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002778 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 {
2780 if (tv_op(&tv, rettv, op) == OK)
2781 set_var(lp->ll_name, &tv, FALSE);
2782 clear_tv(&tv);
2783 }
2784 }
2785 else
2786 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002788 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002790 else if (tv_check_lock(lp->ll_newkey == NULL
2791 ? lp->ll_tv->v_lock
2792 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2793 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 else if (lp->ll_range)
2795 {
2796 /*
2797 * Assign the List values to the list items.
2798 */
2799 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002801 if (op != NULL && *op != '=')
2802 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2803 else
2804 {
2805 clear_tv(&lp->ll_li->li_tv);
2806 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2807 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 ri = ri->li_next;
2809 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2810 break;
2811 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002814 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002815 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 ri = NULL;
2817 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002818 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002819 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_li = lp->ll_li->li_next;
2821 ++lp->ll_n1;
2822 }
2823 if (ri != NULL)
2824 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 else if (lp->ll_empty2
2826 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 : lp->ll_n1 != lp->ll_n2)
2828 EMSG(_("E711: List value has not enough items"));
2829 }
2830 else
2831 {
2832 /*
2833 * Assign to a List or Dictionary item.
2834 */
2835 if (lp->ll_newkey != NULL)
2836 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002837 if (op != NULL && *op != '=')
2838 {
2839 EMSG2(_(e_letwrong), op);
2840 return;
2841 }
2842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002844 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (di == NULL)
2846 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002847 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2848 {
2849 vim_free(di);
2850 return;
2851 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 else if (op != NULL && *op != '=')
2855 {
2856 tv_op(lp->ll_tv, rettv, op);
2857 return;
2858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002859 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 /*
2863 * Assign the value to the variable or list item.
2864 */
2865 if (copy)
2866 copy_tv(rettv, lp->ll_tv);
2867 else
2868 {
2869 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002870 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002872 }
2873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002874}
2875
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2878 * Returns OK or FAIL.
2879 */
2880 static int
2881tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 typval_T *tv1;
2883 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 char_u *op;
2885{
2886 long n;
2887 char_u numbuf[NUMBUFLEN];
2888 char_u *s;
2889
2890 /* Can't do anything with a Funcref or a Dict on the right. */
2891 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2892 {
2893 switch (tv1->v_type)
2894 {
2895 case VAR_DICT:
2896 case VAR_FUNC:
2897 break;
2898
2899 case VAR_LIST:
2900 if (*op != '+' || tv2->v_type != VAR_LIST)
2901 break;
2902 /* List += List */
2903 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2904 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2905 return OK;
2906
2907 case VAR_NUMBER:
2908 case VAR_STRING:
2909 if (tv2->v_type == VAR_LIST)
2910 break;
2911 if (*op == '+' || *op == '-')
2912 {
2913 /* nr += nr or nr -= nr*/
2914 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002915#ifdef FEAT_FLOAT
2916 if (tv2->v_type == VAR_FLOAT)
2917 {
2918 float_T f = n;
2919
2920 if (*op == '+')
2921 f += tv2->vval.v_float;
2922 else
2923 f -= tv2->vval.v_float;
2924 clear_tv(tv1);
2925 tv1->v_type = VAR_FLOAT;
2926 tv1->vval.v_float = f;
2927 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002929#endif
2930 {
2931 if (*op == '+')
2932 n += get_tv_number(tv2);
2933 else
2934 n -= get_tv_number(tv2);
2935 clear_tv(tv1);
2936 tv1->v_type = VAR_NUMBER;
2937 tv1->vval.v_number = n;
2938 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939 }
2940 else
2941 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002942 if (tv2->v_type == VAR_FLOAT)
2943 break;
2944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 /* str .= str */
2946 s = get_tv_string(tv1);
2947 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2948 clear_tv(tv1);
2949 tv1->v_type = VAR_STRING;
2950 tv1->vval.v_string = s;
2951 }
2952 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002953
2954#ifdef FEAT_FLOAT
2955 case VAR_FLOAT:
2956 {
2957 float_T f;
2958
2959 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2960 && tv2->v_type != VAR_NUMBER
2961 && tv2->v_type != VAR_STRING))
2962 break;
2963 if (tv2->v_type == VAR_FLOAT)
2964 f = tv2->vval.v_float;
2965 else
2966 f = get_tv_number(tv2);
2967 if (*op == '+')
2968 tv1->vval.v_float += f;
2969 else
2970 tv1->vval.v_float -= f;
2971 }
2972 return OK;
2973#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002974 }
2975 }
2976
2977 EMSG2(_(e_letwrong), op);
2978 return FAIL;
2979}
2980
2981/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982 * Add a watcher to a list.
2983 */
2984 static void
2985list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 list_T *l;
2987 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002988{
2989 lw->lw_next = l->lv_watch;
2990 l->lv_watch = lw;
2991}
2992
2993/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002994 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002995 * No warning when it isn't found...
2996 */
2997 static void
2998list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 list_T *l;
3000 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001{
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003
3004 lwp = &l->lv_watch;
3005 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3006 {
3007 if (lw == lwrem)
3008 {
3009 *lwp = lw->lw_next;
3010 break;
3011 }
3012 lwp = &lw->lw_next;
3013 }
3014}
3015
3016/*
3017 * Just before removing an item from a list: advance watchers to the next
3018 * item.
3019 */
3020 static void
3021list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003022 list_T *l;
3023 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003024{
Bram Moolenaar33570922005-01-25 22:26:29 +00003025 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003026
3027 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3028 if (lw->lw_item == item)
3029 lw->lw_item = item->li_next;
3030}
3031
3032/*
3033 * Evaluate the expression used in a ":for var in expr" command.
3034 * "arg" points to "var".
3035 * Set "*errp" to TRUE for an error, FALSE otherwise;
3036 * Return a pointer that holds the info. Null when there is an error.
3037 */
3038 void *
3039eval_for_line(arg, errp, nextcmdp, skip)
3040 char_u *arg;
3041 int *errp;
3042 char_u **nextcmdp;
3043 int skip;
3044{
Bram Moolenaar33570922005-01-25 22:26:29 +00003045 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003047 typval_T tv;
3048 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003049
3050 *errp = TRUE; /* default: there is an error */
3051
Bram Moolenaar33570922005-01-25 22:26:29 +00003052 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 if (fi == NULL)
3054 return NULL;
3055
3056 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3057 if (expr == NULL)
3058 return fi;
3059
3060 expr = skipwhite(expr);
3061 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3062 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003063 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064 return fi;
3065 }
3066
3067 if (skip)
3068 ++emsg_skip;
3069 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3070 {
3071 *errp = FALSE;
3072 if (!skip)
3073 {
3074 l = tv.vval.v_list;
3075 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003076 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003077 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003078 clear_tv(&tv);
3079 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003080 else
3081 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003082 /* No need to increment the refcount, it's already set for the
3083 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003084 fi->fi_list = l;
3085 list_add_watch(l, &fi->fi_lw);
3086 fi->fi_lw.lw_item = l->lv_first;
3087 }
3088 }
3089 }
3090 if (skip)
3091 --emsg_skip;
3092
3093 return fi;
3094}
3095
3096/*
3097 * Use the first item in a ":for" list. Advance to the next.
3098 * Assign the values to the variable (list). "arg" points to the first one.
3099 * Return TRUE when a valid item was found, FALSE when at end of list or
3100 * something wrong.
3101 */
3102 int
3103next_for_item(fi_void, arg)
3104 void *fi_void;
3105 char_u *arg;
3106{
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110
3111 item = fi->fi_lw.lw_item;
3112 if (item == NULL)
3113 result = FALSE;
3114 else
3115 {
3116 fi->fi_lw.lw_item = item->li_next;
3117 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3118 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3119 }
3120 return result;
3121}
3122
3123/*
3124 * Free the structure used to store info used by ":for".
3125 */
3126 void
3127free_for_info(fi_void)
3128 void *fi_void;
3129{
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003132 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003133 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003135 list_unref(fi->fi_list);
3136 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137 vim_free(fi);
3138}
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3141
3142 void
3143set_context_for_expression(xp, arg, cmdidx)
3144 expand_T *xp;
3145 char_u *arg;
3146 cmdidx_T cmdidx;
3147{
3148 int got_eq = FALSE;
3149 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003152 if (cmdidx == CMD_let)
3153 {
3154 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003155 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156 {
3157 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003158 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159 {
3160 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 if (vim_iswhite(*p))
3163 break;
3164 }
3165 return;
3166 }
3167 }
3168 else
3169 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3170 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 while ((xp->xp_pattern = vim_strpbrk(arg,
3172 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3173 {
3174 c = *xp->xp_pattern;
3175 if (c == '&')
3176 {
3177 c = xp->xp_pattern[1];
3178 if (c == '&')
3179 {
3180 ++xp->xp_pattern;
3181 xp->xp_context = cmdidx != CMD_let || got_eq
3182 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3183 }
3184 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003187 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3188 xp->xp_pattern += 2;
3189
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 else if (c == '$')
3193 {
3194 /* environment variable */
3195 xp->xp_context = EXPAND_ENV_VARS;
3196 }
3197 else if (c == '=')
3198 {
3199 got_eq = TRUE;
3200 xp->xp_context = EXPAND_EXPRESSION;
3201 }
3202 else if (c == '<'
3203 && xp->xp_context == EXPAND_FUNCTIONS
3204 && vim_strchr(xp->xp_pattern, '(') == NULL)
3205 {
3206 /* Function name can start with "<SNR>" */
3207 break;
3208 }
3209 else if (cmdidx != CMD_let || got_eq)
3210 {
3211 if (c == '"') /* string */
3212 {
3213 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3214 if (c == '\\' && xp->xp_pattern[1] != NUL)
3215 ++xp->xp_pattern;
3216 xp->xp_context = EXPAND_NOTHING;
3217 }
3218 else if (c == '\'') /* literal string */
3219 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003220 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3222 /* skip */ ;
3223 xp->xp_context = EXPAND_NOTHING;
3224 }
3225 else if (c == '|')
3226 {
3227 if (xp->xp_pattern[1] == '|')
3228 {
3229 ++xp->xp_pattern;
3230 xp->xp_context = EXPAND_EXPRESSION;
3231 }
3232 else
3233 xp->xp_context = EXPAND_COMMANDS;
3234 }
3235 else
3236 xp->xp_context = EXPAND_EXPRESSION;
3237 }
3238 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 /* Doesn't look like something valid, expand as an expression
3240 * anyway. */
3241 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 arg = xp->xp_pattern;
3243 if (*arg != NUL)
3244 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3245 /* skip */ ;
3246 }
3247 xp->xp_pattern = arg;
3248}
3249
3250#endif /* FEAT_CMDL_COMPL */
3251
3252/*
3253 * ":1,25call func(arg1, arg2)" function call.
3254 */
3255 void
3256ex_call(eap)
3257 exarg_T *eap;
3258{
3259 char_u *arg = eap->arg;
3260 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003262 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 linenr_T lnum;
3266 int doesrange;
3267 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003268 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003270 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003271 if (fudi.fd_newkey != NULL)
3272 {
3273 /* Still need to give an error message for missing key. */
3274 EMSG2(_(e_dictkey), fudi.fd_newkey);
3275 vim_free(fudi.fd_newkey);
3276 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003277 if (tofree == NULL)
3278 return;
3279
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003280 /* Increase refcount on dictionary, it could get deleted when evaluating
3281 * the arguments. */
3282 if (fudi.fd_dict != NULL)
3283 ++fudi.fd_dict->dv_refcount;
3284
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003285 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003286 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003287 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
Bram Moolenaar532c7802005-01-27 14:44:31 +00003289 /* Skip white space to allow ":call func ()". Not good, but required for
3290 * backward compatibility. */
3291 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003292 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 if (*startarg != '(')
3295 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003296 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 goto end;
3298 }
3299
3300 /*
3301 * When skipping, evaluate the function once, to find the end of the
3302 * arguments.
3303 * When the function takes a range, this is discovered after the first
3304 * call, and the loop is broken.
3305 */
3306 if (eap->skip)
3307 {
3308 ++emsg_skip;
3309 lnum = eap->line2; /* do it once, also with an invalid range */
3310 }
3311 else
3312 lnum = eap->line1;
3313 for ( ; lnum <= eap->line2; ++lnum)
3314 {
3315 if (!eap->skip && eap->addr_count > 0)
3316 {
3317 curwin->w_cursor.lnum = lnum;
3318 curwin->w_cursor.col = 0;
3319 }
3320 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003321 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003322 eap->line1, eap->line2, &doesrange,
3323 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 failed = TRUE;
3326 break;
3327 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003328
3329 /* Handle a function returning a Funcref, Dictionary or List. */
3330 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3331 {
3332 failed = TRUE;
3333 break;
3334 }
3335
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (doesrange || eap->skip)
3338 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003341 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003343 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (aborting())
3345 break;
3346 }
3347 if (eap->skip)
3348 --emsg_skip;
3349
3350 if (!failed)
3351 {
3352 /* Check for trailing illegal characters and a following command. */
3353 if (!ends_excmd(*arg))
3354 {
3355 emsg_severe = TRUE;
3356 EMSG(_(e_trailing));
3357 }
3358 else
3359 eap->nextcmd = check_nextcmd(arg);
3360 }
3361
3362end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003363 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003364 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365}
3366
3367/*
3368 * ":unlet[!] var1 ... " command.
3369 */
3370 void
3371ex_unlet(eap)
3372 exarg_T *eap;
3373{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003374 ex_unletlock(eap, eap->arg, 0);
3375}
3376
3377/*
3378 * ":lockvar" and ":unlockvar" commands
3379 */
3380 void
3381ex_lockvar(eap)
3382 exarg_T *eap;
3383{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003385 int deep = 2;
3386
3387 if (eap->forceit)
3388 deep = -1;
3389 else if (vim_isdigit(*arg))
3390 {
3391 deep = getdigits(&arg);
3392 arg = skipwhite(arg);
3393 }
3394
3395 ex_unletlock(eap, arg, deep);
3396}
3397
3398/*
3399 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3400 */
3401 static void
3402ex_unletlock(eap, argstart, deep)
3403 exarg_T *eap;
3404 char_u *argstart;
3405 int deep;
3406{
3407 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003410 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 do
3413 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003414 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003415 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3416 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003417 if (lv.ll_name == NULL)
3418 error = TRUE; /* error but continue parsing */
3419 if (name_end == NULL || (!vim_iswhite(*name_end)
3420 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003422 if (name_end != NULL)
3423 {
3424 emsg_severe = TRUE;
3425 EMSG(_(e_trailing));
3426 }
3427 if (!(eap->skip || error))
3428 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 break;
3430 }
3431
3432 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003433 {
3434 if (eap->cmdidx == CMD_unlet)
3435 {
3436 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3437 error = TRUE;
3438 }
3439 else
3440 {
3441 if (do_lock_var(&lv, name_end, deep,
3442 eap->cmdidx == CMD_lockvar) == FAIL)
3443 error = TRUE;
3444 }
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003447 if (!eap->skip)
3448 clear_lval(&lv);
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 arg = skipwhite(name_end);
3451 } while (!ends_excmd(*arg));
3452
3453 eap->nextcmd = check_nextcmd(arg);
3454}
3455
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003457do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003459 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003460 int forceit;
3461{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003462 int ret = OK;
3463 int cc;
3464
3465 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003466 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003467 cc = *name_end;
3468 *name_end = NUL;
3469
3470 /* Normal name or expanded name. */
3471 if (check_changedtick(lp->ll_name))
3472 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003473 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003474 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003477 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3478 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 else if (lp->ll_range)
3480 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003481 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003482
3483 /* Delete a range of List items. */
3484 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3485 {
3486 li = lp->ll_li->li_next;
3487 listitem_remove(lp->ll_list, lp->ll_li);
3488 lp->ll_li = li;
3489 ++lp->ll_n1;
3490 }
3491 }
3492 else
3493 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003494 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003495 /* unlet a List item. */
3496 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003497 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003498 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003499 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003500 }
3501
3502 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503}
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505/*
3506 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003507 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
3509 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003512 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
Bram Moolenaar33570922005-01-25 22:26:29 +00003514 hashtab_T *ht;
3515 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003516 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003517 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 ht = find_var_ht(name, &varname);
3520 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003522 hi = hash_find(ht, varname);
3523 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003524 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003525 di = HI2DI(hi);
3526 if (var_check_fixed(di->di_flags, name)
3527 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003528 return FAIL;
3529 delete_var(ht, hi);
3530 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003533 if (forceit)
3534 return OK;
3535 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return FAIL;
3537}
3538
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003539/*
3540 * Lock or unlock variable indicated by "lp".
3541 * "deep" is the levels to go (-1 for unlimited);
3542 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3543 */
3544 static int
3545do_lock_var(lp, name_end, deep, lock)
3546 lval_T *lp;
3547 char_u *name_end;
3548 int deep;
3549 int lock;
3550{
3551 int ret = OK;
3552 int cc;
3553 dictitem_T *di;
3554
3555 if (deep == 0) /* nothing to do */
3556 return OK;
3557
3558 if (lp->ll_tv == NULL)
3559 {
3560 cc = *name_end;
3561 *name_end = NUL;
3562
3563 /* Normal name or expanded name. */
3564 if (check_changedtick(lp->ll_name))
3565 ret = FAIL;
3566 else
3567 {
3568 di = find_var(lp->ll_name, NULL);
3569 if (di == NULL)
3570 ret = FAIL;
3571 else
3572 {
3573 if (lock)
3574 di->di_flags |= DI_FLAGS_LOCK;
3575 else
3576 di->di_flags &= ~DI_FLAGS_LOCK;
3577 item_lock(&di->di_tv, deep, lock);
3578 }
3579 }
3580 *name_end = cc;
3581 }
3582 else if (lp->ll_range)
3583 {
3584 listitem_T *li = lp->ll_li;
3585
3586 /* (un)lock a range of List items. */
3587 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3588 {
3589 item_lock(&li->li_tv, deep, lock);
3590 li = li->li_next;
3591 ++lp->ll_n1;
3592 }
3593 }
3594 else if (lp->ll_list != NULL)
3595 /* (un)lock a List item. */
3596 item_lock(&lp->ll_li->li_tv, deep, lock);
3597 else
3598 /* un(lock) a Dictionary item. */
3599 item_lock(&lp->ll_di->di_tv, deep, lock);
3600
3601 return ret;
3602}
3603
3604/*
3605 * Lock or unlock an item. "deep" is nr of levels to go.
3606 */
3607 static void
3608item_lock(tv, deep, lock)
3609 typval_T *tv;
3610 int deep;
3611 int lock;
3612{
3613 static int recurse = 0;
3614 list_T *l;
3615 listitem_T *li;
3616 dict_T *d;
3617 hashitem_T *hi;
3618 int todo;
3619
3620 if (recurse >= DICT_MAXNEST)
3621 {
3622 EMSG(_("E743: variable nested too deep for (un)lock"));
3623 return;
3624 }
3625 if (deep == 0)
3626 return;
3627 ++recurse;
3628
3629 /* lock/unlock the item itself */
3630 if (lock)
3631 tv->v_lock |= VAR_LOCKED;
3632 else
3633 tv->v_lock &= ~VAR_LOCKED;
3634
3635 switch (tv->v_type)
3636 {
3637 case VAR_LIST:
3638 if ((l = tv->vval.v_list) != NULL)
3639 {
3640 if (lock)
3641 l->lv_lock |= VAR_LOCKED;
3642 else
3643 l->lv_lock &= ~VAR_LOCKED;
3644 if (deep < 0 || deep > 1)
3645 /* recursive: lock/unlock the items the List contains */
3646 for (li = l->lv_first; li != NULL; li = li->li_next)
3647 item_lock(&li->li_tv, deep - 1, lock);
3648 }
3649 break;
3650 case VAR_DICT:
3651 if ((d = tv->vval.v_dict) != NULL)
3652 {
3653 if (lock)
3654 d->dv_lock |= VAR_LOCKED;
3655 else
3656 d->dv_lock &= ~VAR_LOCKED;
3657 if (deep < 0 || deep > 1)
3658 {
3659 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003660 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3662 {
3663 if (!HASHITEM_EMPTY(hi))
3664 {
3665 --todo;
3666 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3667 }
3668 }
3669 }
3670 }
3671 }
3672 --recurse;
3673}
3674
Bram Moolenaara40058a2005-07-11 22:42:07 +00003675/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003676 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3677 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003678 */
3679 static int
3680tv_islocked(tv)
3681 typval_T *tv;
3682{
3683 return (tv->v_lock & VAR_LOCKED)
3684 || (tv->v_type == VAR_LIST
3685 && tv->vval.v_list != NULL
3686 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3687 || (tv->v_type == VAR_DICT
3688 && tv->vval.v_dict != NULL
3689 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3690}
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3693/*
3694 * Delete all "menutrans_" variables.
3695 */
3696 void
3697del_menutrans_vars()
3698{
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003700 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
Bram Moolenaar33570922005-01-25 22:26:29 +00003702 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003703 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003704 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003705 {
3706 if (!HASHITEM_EMPTY(hi))
3707 {
3708 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3710 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003711 }
3712 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003713 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714}
3715#endif
3716
3717#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3718
3719/*
3720 * Local string buffer for the next two functions to store a variable name
3721 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3722 * get_user_var_name().
3723 */
3724
3725static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3726
3727static char_u *varnamebuf = NULL;
3728static int varnamebuflen = 0;
3729
3730/*
3731 * Function to concatenate a prefix and a variable name.
3732 */
3733 static char_u *
3734cat_prefix_varname(prefix, name)
3735 int prefix;
3736 char_u *name;
3737{
3738 int len;
3739
3740 len = (int)STRLEN(name) + 3;
3741 if (len > varnamebuflen)
3742 {
3743 vim_free(varnamebuf);
3744 len += 10; /* some additional space */
3745 varnamebuf = alloc(len);
3746 if (varnamebuf == NULL)
3747 {
3748 varnamebuflen = 0;
3749 return NULL;
3750 }
3751 varnamebuflen = len;
3752 }
3753 *varnamebuf = prefix;
3754 varnamebuf[1] = ':';
3755 STRCPY(varnamebuf + 2, name);
3756 return varnamebuf;
3757}
3758
3759/*
3760 * Function given to ExpandGeneric() to obtain the list of user defined
3761 * (global/buffer/window/built-in) variable names.
3762 */
3763/*ARGSUSED*/
3764 char_u *
3765get_user_var_name(xp, idx)
3766 expand_T *xp;
3767 int idx;
3768{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003769 static long_u gdone;
3770 static long_u bdone;
3771 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003772#ifdef FEAT_WINDOWS
3773 static long_u tdone;
3774#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003775 static int vidx;
3776 static hashitem_T *hi;
3777 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
3779 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003780 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003782#ifdef FEAT_WINDOWS
3783 tdone = 0;
3784#endif
3785 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003786
3787 /* Global variables */
3788 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003790 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003792 else
3793 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003794 while (HASHITEM_EMPTY(hi))
3795 ++hi;
3796 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3797 return cat_prefix_varname('g', hi->hi_key);
3798 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003800
3801 /* b: variables */
3802 ht = &curbuf->b_vars.dv_hashtab;
3803 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003805 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003807 else
3808 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003809 while (HASHITEM_EMPTY(hi))
3810 ++hi;
3811 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003813 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003815 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 return (char_u *)"b:changedtick";
3817 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003818
3819 /* w: variables */
3820 ht = &curwin->w_vars.dv_hashtab;
3821 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003823 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003825 else
3826 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003827 while (HASHITEM_EMPTY(hi))
3828 ++hi;
3829 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003831
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003832#ifdef FEAT_WINDOWS
3833 /* t: variables */
3834 ht = &curtab->tp_vars.dv_hashtab;
3835 if (tdone < ht->ht_used)
3836 {
3837 if (tdone++ == 0)
3838 hi = ht->ht_array;
3839 else
3840 ++hi;
3841 while (HASHITEM_EMPTY(hi))
3842 ++hi;
3843 return cat_prefix_varname('t', hi->hi_key);
3844 }
3845#endif
3846
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 /* v: variables */
3848 if (vidx < VV_LEN)
3849 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851 vim_free(varnamebuf);
3852 varnamebuf = NULL;
3853 varnamebuflen = 0;
3854 return NULL;
3855}
3856
3857#endif /* FEAT_CMDL_COMPL */
3858
3859/*
3860 * types for expressions.
3861 */
3862typedef enum
3863{
3864 TYPE_UNKNOWN = 0
3865 , TYPE_EQUAL /* == */
3866 , TYPE_NEQUAL /* != */
3867 , TYPE_GREATER /* > */
3868 , TYPE_GEQUAL /* >= */
3869 , TYPE_SMALLER /* < */
3870 , TYPE_SEQUAL /* <= */
3871 , TYPE_MATCH /* =~ */
3872 , TYPE_NOMATCH /* !~ */
3873} exptype_T;
3874
3875/*
3876 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3879 */
3880
3881/*
3882 * Handle zero level expression.
3883 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003884 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003885 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 * Return OK or FAIL.
3887 */
3888 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 char_u **nextcmd;
3893 int evaluate;
3894{
3895 int ret;
3896 char_u *p;
3897
3898 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (ret == FAIL || !ends_excmd(*p))
3901 {
3902 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003903 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 /*
3905 * Report the invalid expression unless the expression evaluation has
3906 * been cancelled due to an aborting error, an interrupt, or an
3907 * exception.
3908 */
3909 if (!aborting())
3910 EMSG2(_(e_invexpr2), arg);
3911 ret = FAIL;
3912 }
3913 if (nextcmd != NULL)
3914 *nextcmd = check_nextcmd(p);
3915
3916 return ret;
3917}
3918
3919/*
3920 * Handle top level expression:
3921 * expr1 ? expr0 : expr0
3922 *
3923 * "arg" must point to the first non-white of the expression.
3924 * "arg" is advanced to the next non-white after the recognized expression.
3925 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003926 * Note: "rettv.v_lock" is not set.
3927 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 * Return OK or FAIL.
3929 */
3930 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003931eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 int evaluate;
3935{
3936 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 /*
3940 * Get the first variable.
3941 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003942 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 return FAIL;
3944
3945 if ((*arg)[0] == '?')
3946 {
3947 result = FALSE;
3948 if (evaluate)
3949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003950 int error = FALSE;
3951
3952 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003955 if (error)
3956 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958
3959 /*
3960 * Get the second variable.
3961 */
3962 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return FAIL;
3965
3966 /*
3967 * Check for the ":".
3968 */
3969 if ((*arg)[0] != ':')
3970 {
3971 EMSG(_("E109: Missing ':' after '?'"));
3972 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 return FAIL;
3975 }
3976
3977 /*
3978 * Get the third variable.
3979 */
3980 *arg = skipwhite(*arg + 1);
3981 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3982 {
3983 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003984 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return FAIL;
3986 }
3987 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990
3991 return OK;
3992}
3993
3994/*
3995 * Handle first level expression:
3996 * expr2 || expr2 || expr2 logical OR
3997 *
3998 * "arg" must point to the first non-white of the expression.
3999 * "arg" is advanced to the next non-white after the recognized expression.
4000 *
4001 * Return OK or FAIL.
4002 */
4003 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 int evaluate;
4008{
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 long result;
4011 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014 /*
4015 * Get the first variable.
4016 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004017 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 return FAIL;
4019
4020 /*
4021 * Repeat until there is no following "||".
4022 */
4023 first = TRUE;
4024 result = FALSE;
4025 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4026 {
4027 if (evaluate && first)
4028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004029 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004031 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 if (error)
4033 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 first = FALSE;
4035 }
4036
4037 /*
4038 * Get the second variable.
4039 */
4040 *arg = skipwhite(*arg + 2);
4041 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4042 return FAIL;
4043
4044 /*
4045 * Compute the result.
4046 */
4047 if (evaluate && !result)
4048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004049 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004052 if (error)
4053 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055 if (evaluate)
4056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004057 rettv->v_type = VAR_NUMBER;
4058 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 }
4061
4062 return OK;
4063}
4064
4065/*
4066 * Handle second level expression:
4067 * expr3 && expr3 && expr3 logical AND
4068 *
4069 * "arg" must point to the first non-white of the expression.
4070 * "arg" is advanced to the next non-white after the recognized expression.
4071 *
4072 * Return OK or FAIL.
4073 */
4074 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 int evaluate;
4079{
Bram Moolenaar33570922005-01-25 22:26:29 +00004080 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 long result;
4082 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 /*
4086 * Get the first variable.
4087 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return FAIL;
4090
4091 /*
4092 * Repeat until there is no following "&&".
4093 */
4094 first = TRUE;
4095 result = TRUE;
4096 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4097 {
4098 if (evaluate && first)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103 if (error)
4104 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 first = FALSE;
4106 }
4107
4108 /*
4109 * Get the second variable.
4110 */
4111 *arg = skipwhite(*arg + 2);
4112 if (eval4(arg, &var2, evaluate && result) == FAIL)
4113 return FAIL;
4114
4115 /*
4116 * Compute the result.
4117 */
4118 if (evaluate && result)
4119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004120 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004123 if (error)
4124 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 if (evaluate)
4127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128 rettv->v_type = VAR_NUMBER;
4129 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 }
4132
4133 return OK;
4134}
4135
4136/*
4137 * Handle third level expression:
4138 * var1 == var2
4139 * var1 =~ var2
4140 * var1 != var2
4141 * var1 !~ var2
4142 * var1 > var2
4143 * var1 >= var2
4144 * var1 < var2
4145 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004146 * var1 is var2
4147 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 *
4149 * "arg" must point to the first non-white of the expression.
4150 * "arg" is advanced to the next non-white after the recognized expression.
4151 *
4152 * Return OK or FAIL.
4153 */
4154 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 int evaluate;
4159{
Bram Moolenaar33570922005-01-25 22:26:29 +00004160 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 char_u *p;
4162 int i;
4163 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004164 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int len = 2;
4166 long n1, n2;
4167 char_u *s1, *s2;
4168 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4169 regmatch_T regmatch;
4170 int ic;
4171 char_u *save_cpo;
4172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 p = *arg;
4180 switch (p[0])
4181 {
4182 case '=': if (p[1] == '=')
4183 type = TYPE_EQUAL;
4184 else if (p[1] == '~')
4185 type = TYPE_MATCH;
4186 break;
4187 case '!': if (p[1] == '=')
4188 type = TYPE_NEQUAL;
4189 else if (p[1] == '~')
4190 type = TYPE_NOMATCH;
4191 break;
4192 case '>': if (p[1] != '=')
4193 {
4194 type = TYPE_GREATER;
4195 len = 1;
4196 }
4197 else
4198 type = TYPE_GEQUAL;
4199 break;
4200 case '<': if (p[1] != '=')
4201 {
4202 type = TYPE_SMALLER;
4203 len = 1;
4204 }
4205 else
4206 type = TYPE_SEQUAL;
4207 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004208 case 'i': if (p[1] == 's')
4209 {
4210 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4211 len = 5;
4212 if (!vim_isIDc(p[len]))
4213 {
4214 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4215 type_is = TRUE;
4216 }
4217 }
4218 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220
4221 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004222 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 */
4224 if (type != TYPE_UNKNOWN)
4225 {
4226 /* extra question mark appended: ignore case */
4227 if (p[len] == '?')
4228 {
4229 ic = TRUE;
4230 ++len;
4231 }
4232 /* extra '#' appended: match case */
4233 else if (p[len] == '#')
4234 {
4235 ic = FALSE;
4236 ++len;
4237 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004238 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 else
4240 ic = p_ic;
4241
4242 /*
4243 * Get the second variable.
4244 */
4245 *arg = skipwhite(p + len);
4246 if (eval5(arg, &var2, evaluate) == FAIL)
4247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250 }
4251
4252 if (evaluate)
4253 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004254 if (type_is && rettv->v_type != var2.v_type)
4255 {
4256 /* For "is" a different type always means FALSE, for "notis"
4257 * it means TRUE. */
4258 n1 = (type == TYPE_NEQUAL);
4259 }
4260 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4261 {
4262 if (type_is)
4263 {
4264 n1 = (rettv->v_type == var2.v_type
4265 && rettv->vval.v_list == var2.vval.v_list);
4266 if (type == TYPE_NEQUAL)
4267 n1 = !n1;
4268 }
4269 else if (rettv->v_type != var2.v_type
4270 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4271 {
4272 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004273 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004274 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004275 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004276 clear_tv(rettv);
4277 clear_tv(&var2);
4278 return FAIL;
4279 }
4280 else
4281 {
4282 /* Compare two Lists for being equal or unequal. */
4283 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4284 if (type == TYPE_NEQUAL)
4285 n1 = !n1;
4286 }
4287 }
4288
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004289 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4290 {
4291 if (type_is)
4292 {
4293 n1 = (rettv->v_type == var2.v_type
4294 && rettv->vval.v_dict == var2.vval.v_dict);
4295 if (type == TYPE_NEQUAL)
4296 n1 = !n1;
4297 }
4298 else if (rettv->v_type != var2.v_type
4299 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4300 {
4301 if (rettv->v_type != var2.v_type)
4302 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4303 else
4304 EMSG(_("E736: Invalid operation for Dictionary"));
4305 clear_tv(rettv);
4306 clear_tv(&var2);
4307 return FAIL;
4308 }
4309 else
4310 {
4311 /* Compare two Dictionaries for being equal or unequal. */
4312 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4313 if (type == TYPE_NEQUAL)
4314 n1 = !n1;
4315 }
4316 }
4317
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004318 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4319 {
4320 if (rettv->v_type != var2.v_type
4321 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4322 {
4323 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004324 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004325 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004326 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004327 clear_tv(rettv);
4328 clear_tv(&var2);
4329 return FAIL;
4330 }
4331 else
4332 {
4333 /* Compare two Funcrefs for being equal or unequal. */
4334 if (rettv->vval.v_string == NULL
4335 || var2.vval.v_string == NULL)
4336 n1 = FALSE;
4337 else
4338 n1 = STRCMP(rettv->vval.v_string,
4339 var2.vval.v_string) == 0;
4340 if (type == TYPE_NEQUAL)
4341 n1 = !n1;
4342 }
4343 }
4344
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004345#ifdef FEAT_FLOAT
4346 /*
4347 * If one of the two variables is a float, compare as a float.
4348 * When using "=~" or "!~", always compare as string.
4349 */
4350 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4351 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4352 {
4353 float_T f1, f2;
4354
4355 if (rettv->v_type == VAR_FLOAT)
4356 f1 = rettv->vval.v_float;
4357 else
4358 f1 = get_tv_number(rettv);
4359 if (var2.v_type == VAR_FLOAT)
4360 f2 = var2.vval.v_float;
4361 else
4362 f2 = get_tv_number(&var2);
4363 n1 = FALSE;
4364 switch (type)
4365 {
4366 case TYPE_EQUAL: n1 = (f1 == f2); break;
4367 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4368 case TYPE_GREATER: n1 = (f1 > f2); break;
4369 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4370 case TYPE_SMALLER: n1 = (f1 < f2); break;
4371 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4372 case TYPE_UNKNOWN:
4373 case TYPE_MATCH:
4374 case TYPE_NOMATCH: break; /* avoid gcc warning */
4375 }
4376 }
4377#endif
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /*
4380 * If one of the two variables is a number, compare as a number.
4381 * When using "=~" or "!~", always compare as string.
4382 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 n1 = get_tv_number(rettv);
4387 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 switch (type)
4389 {
4390 case TYPE_EQUAL: n1 = (n1 == n2); break;
4391 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4392 case TYPE_GREATER: n1 = (n1 > n2); break;
4393 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4394 case TYPE_SMALLER: n1 = (n1 < n2); break;
4395 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4396 case TYPE_UNKNOWN:
4397 case TYPE_MATCH:
4398 case TYPE_NOMATCH: break; /* avoid gcc warning */
4399 }
4400 }
4401 else
4402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 s1 = get_tv_string_buf(rettv, buf1);
4404 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4406 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4407 else
4408 i = 0;
4409 n1 = FALSE;
4410 switch (type)
4411 {
4412 case TYPE_EQUAL: n1 = (i == 0); break;
4413 case TYPE_NEQUAL: n1 = (i != 0); break;
4414 case TYPE_GREATER: n1 = (i > 0); break;
4415 case TYPE_GEQUAL: n1 = (i >= 0); break;
4416 case TYPE_SMALLER: n1 = (i < 0); break;
4417 case TYPE_SEQUAL: n1 = (i <= 0); break;
4418
4419 case TYPE_MATCH:
4420 case TYPE_NOMATCH:
4421 /* avoid 'l' flag in 'cpoptions' */
4422 save_cpo = p_cpo;
4423 p_cpo = (char_u *)"";
4424 regmatch.regprog = vim_regcomp(s2,
4425 RE_MAGIC + RE_STRING);
4426 regmatch.rm_ic = ic;
4427 if (regmatch.regprog != NULL)
4428 {
4429 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4430 vim_free(regmatch.regprog);
4431 if (type == TYPE_NOMATCH)
4432 n1 = !n1;
4433 }
4434 p_cpo = save_cpo;
4435 break;
4436
4437 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4438 }
4439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 clear_tv(rettv);
4441 clear_tv(&var2);
4442 rettv->v_type = VAR_NUMBER;
4443 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 }
4446
4447 return OK;
4448}
4449
4450/*
4451 * Handle fourth level expression:
4452 * + number addition
4453 * - number subtraction
4454 * . string concatenation
4455 *
4456 * "arg" must point to the first non-white of the expression.
4457 * "arg" is advanced to the next non-white after the recognized expression.
4458 *
4459 * Return OK or FAIL.
4460 */
4461 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 int evaluate;
4466{
Bram Moolenaar33570922005-01-25 22:26:29 +00004467 typval_T var2;
4468 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int op;
4470 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004471#ifdef FEAT_FLOAT
4472 float_T f1 = 0, f2 = 0;
4473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 char_u *s1, *s2;
4475 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4476 char_u *p;
4477
4478 /*
4479 * Get the first variable.
4480 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004481 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return FAIL;
4483
4484 /*
4485 * Repeat computing, until no '+', '-' or '.' is following.
4486 */
4487 for (;;)
4488 {
4489 op = **arg;
4490 if (op != '+' && op != '-' && op != '.')
4491 break;
4492
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004493 if ((op != '+' || rettv->v_type != VAR_LIST)
4494#ifdef FEAT_FLOAT
4495 && (op == '.' || rettv->v_type != VAR_FLOAT)
4496#endif
4497 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004498 {
4499 /* For "list + ...", an illegal use of the first operand as
4500 * a number cannot be determined before evaluating the 2nd
4501 * operand: if this is also a list, all is ok.
4502 * For "something . ...", "something - ..." or "non-list + ...",
4503 * we know that the first operand needs to be a string or number
4504 * without evaluating the 2nd operand. So check before to avoid
4505 * side effects after an error. */
4506 if (evaluate && get_tv_string_chk(rettv) == NULL)
4507 {
4508 clear_tv(rettv);
4509 return FAIL;
4510 }
4511 }
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /*
4514 * Get the second variable.
4515 */
4516 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004517 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 return FAIL;
4521 }
4522
4523 if (evaluate)
4524 {
4525 /*
4526 * Compute the result.
4527 */
4528 if (op == '.')
4529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004530 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4531 s2 = get_tv_string_buf_chk(&var2, buf2);
4532 if (s2 == NULL) /* type error ? */
4533 {
4534 clear_tv(rettv);
4535 clear_tv(&var2);
4536 return FAIL;
4537 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004538 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004539 clear_tv(rettv);
4540 rettv->v_type = VAR_STRING;
4541 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004543 else if (op == '+' && rettv->v_type == VAR_LIST
4544 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004545 {
4546 /* concatenate Lists */
4547 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4548 &var3) == FAIL)
4549 {
4550 clear_tv(rettv);
4551 clear_tv(&var2);
4552 return FAIL;
4553 }
4554 clear_tv(rettv);
4555 *rettv = var3;
4556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else
4558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004559 int error = FALSE;
4560
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004561#ifdef FEAT_FLOAT
4562 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004563 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004564 f1 = rettv->vval.v_float;
4565 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004566 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004567 else
4568#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004569 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004570 n1 = get_tv_number_chk(rettv, &error);
4571 if (error)
4572 {
4573 /* This can only happen for "list + non-list". For
4574 * "non-list + ..." or "something - ...", we returned
4575 * before evaluating the 2nd operand. */
4576 clear_tv(rettv);
4577 return FAIL;
4578 }
4579#ifdef FEAT_FLOAT
4580 if (var2.v_type == VAR_FLOAT)
4581 f1 = n1;
4582#endif
4583 }
4584#ifdef FEAT_FLOAT
4585 if (var2.v_type == VAR_FLOAT)
4586 {
4587 f2 = var2.vval.v_float;
4588 n2 = 0;
4589 }
4590 else
4591#endif
4592 {
4593 n2 = get_tv_number_chk(&var2, &error);
4594 if (error)
4595 {
4596 clear_tv(rettv);
4597 clear_tv(&var2);
4598 return FAIL;
4599 }
4600#ifdef FEAT_FLOAT
4601 if (rettv->v_type == VAR_FLOAT)
4602 f2 = n2;
4603#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004604 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004606
4607#ifdef FEAT_FLOAT
4608 /* If there is a float on either side the result is a float. */
4609 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4610 {
4611 if (op == '+')
4612 f1 = f1 + f2;
4613 else
4614 f1 = f1 - f2;
4615 rettv->v_type = VAR_FLOAT;
4616 rettv->vval.v_float = f1;
4617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004619#endif
4620 {
4621 if (op == '+')
4622 n1 = n1 + n2;
4623 else
4624 n1 = n1 - n2;
4625 rettv->v_type = VAR_NUMBER;
4626 rettv->vval.v_number = n1;
4627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004629 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631 }
4632 return OK;
4633}
4634
4635/*
4636 * Handle fifth level expression:
4637 * * number multiplication
4638 * / number division
4639 * % number modulo
4640 *
4641 * "arg" must point to the first non-white of the expression.
4642 * "arg" is advanced to the next non-white after the recognized expression.
4643 *
4644 * Return OK or FAIL.
4645 */
4646 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004647eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004651 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
Bram Moolenaar33570922005-01-25 22:26:29 +00004653 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 int op;
4655 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004656#ifdef FEAT_FLOAT
4657 int use_float = FALSE;
4658 float_T f1 = 0, f2;
4659#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004660 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 /*
4663 * Get the first variable.
4664 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004665 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 return FAIL;
4667
4668 /*
4669 * Repeat computing, until no '*', '/' or '%' is following.
4670 */
4671 for (;;)
4672 {
4673 op = **arg;
4674 if (op != '*' && op != '/' && op != '%')
4675 break;
4676
4677 if (evaluate)
4678 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004679#ifdef FEAT_FLOAT
4680 if (rettv->v_type == VAR_FLOAT)
4681 {
4682 f1 = rettv->vval.v_float;
4683 use_float = TRUE;
4684 n1 = 0;
4685 }
4686 else
4687#endif
4688 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 if (error)
4691 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
4693 else
4694 n1 = 0;
4695
4696 /*
4697 * Get the second variable.
4698 */
4699 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004700 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 return FAIL;
4702
4703 if (evaluate)
4704 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705#ifdef FEAT_FLOAT
4706 if (var2.v_type == VAR_FLOAT)
4707 {
4708 if (!use_float)
4709 {
4710 f1 = n1;
4711 use_float = TRUE;
4712 }
4713 f2 = var2.vval.v_float;
4714 n2 = 0;
4715 }
4716 else
4717#endif
4718 {
4719 n2 = get_tv_number_chk(&var2, &error);
4720 clear_tv(&var2);
4721 if (error)
4722 return FAIL;
4723#ifdef FEAT_FLOAT
4724 if (use_float)
4725 f2 = n2;
4726#endif
4727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 /*
4730 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733#ifdef FEAT_FLOAT
4734 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004736 if (op == '*')
4737 f1 = f1 * f2;
4738 else if (op == '/')
4739 {
4740 /* We rely on the floating point library to handle divide
4741 * by zero to result in "inf" and not a crash. */
4742 f1 = f1 / f2;
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004745 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004746 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747 return FAIL;
4748 }
4749 rettv->v_type = VAR_FLOAT;
4750 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 if (op == '*')
4756 n1 = n1 * n2;
4757 else if (op == '/')
4758 {
4759 if (n2 == 0) /* give an error message? */
4760 {
4761 if (n1 == 0)
4762 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4763 else if (n1 < 0)
4764 n1 = -0x7fffffffL;
4765 else
4766 n1 = 0x7fffffffL;
4767 }
4768 else
4769 n1 = n1 / n2;
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772 {
4773 if (n2 == 0) /* give an error message? */
4774 n1 = 0;
4775 else
4776 n1 = n1 % n2;
4777 }
4778 rettv->v_type = VAR_NUMBER;
4779 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 }
4783
4784 return OK;
4785}
4786
4787/*
4788 * Handle sixth level expression:
4789 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004790 * "string" string constant
4791 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 * &option-name option value
4793 * @r register contents
4794 * identifier variable value
4795 * function() function call
4796 * $VAR environment variable
4797 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004798 * [expr, expr] List
4799 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 *
4801 * Also handle:
4802 * ! in front logical NOT
4803 * - in front unary minus
4804 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 * trailing [] subscript in String or List
4806 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 *
4808 * "arg" must point to the first non-white of the expression.
4809 * "arg" is advanced to the next non-white after the recognized expression.
4810 *
4811 * Return OK or FAIL.
4812 */
4813 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004814eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004818 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 long n;
4821 int len;
4822 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 char_u *start_leader, *end_leader;
4824 int ret = OK;
4825 char_u *alias;
4826
4827 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
4833 /*
4834 * Skip '!' and '-' characters. They are handled later.
4835 */
4836 start_leader = *arg;
4837 while (**arg == '!' || **arg == '-' || **arg == '+')
4838 *arg = skipwhite(*arg + 1);
4839 end_leader = *arg;
4840
4841 switch (**arg)
4842 {
4843 /*
4844 * Number constant.
4845 */
4846 case '0':
4847 case '1':
4848 case '2':
4849 case '3':
4850 case '4':
4851 case '5':
4852 case '6':
4853 case '7':
4854 case '8':
4855 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856 {
4857#ifdef FEAT_FLOAT
4858 char_u *p = skipdigits(*arg + 1);
4859 int get_float = FALSE;
4860
4861 /* We accept a float when the format matches
4862 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004863 * strict to avoid backwards compatibility problems.
4864 * Don't look for a float after the "." operator, so that
4865 * ":let vers = 1.2.3" doesn't fail. */
4866 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 get_float = TRUE;
4869 p = skipdigits(p + 2);
4870 if (*p == 'e' || *p == 'E')
4871 {
4872 ++p;
4873 if (*p == '-' || *p == '+')
4874 ++p;
4875 if (!vim_isdigit(*p))
4876 get_float = FALSE;
4877 else
4878 p = skipdigits(p + 1);
4879 }
4880 if (ASCII_ISALPHA(*p) || *p == '.')
4881 get_float = FALSE;
4882 }
4883 if (get_float)
4884 {
4885 float_T f;
4886
4887 *arg += string2float(*arg, &f);
4888 if (evaluate)
4889 {
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f;
4892 }
4893 }
4894 else
4895#endif
4896 {
4897 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4898 *arg += len;
4899 if (evaluate)
4900 {
4901 rettv->v_type = VAR_NUMBER;
4902 rettv->vval.v_number = n;
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
4908 /*
4909 * String constant: "string".
4910 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 break;
4913
4914 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 break;
4919
4920 /*
4921 * List: [expr, expr]
4922 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 break;
4925
4926 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 * Dictionary: {key: val, key: val}
4928 */
4929 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4930 break;
4931
4932 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004933 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004935 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937
4938 /*
4939 * Environment variable: $VAR.
4940 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004941 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 break;
4943
4944 /*
4945 * Register contents: @r.
4946 */
4947 case '@': ++*arg;
4948 if (evaluate)
4949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004951 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 if (**arg != NUL)
4954 ++*arg;
4955 break;
4956
4957 /*
4958 * nested expression: (expression).
4959 */
4960 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (**arg == ')')
4963 ++*arg;
4964 else if (ret == OK)
4965 {
4966 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004967 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 ret = FAIL;
4969 }
4970 break;
4971
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 break;
4974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975
4976 if (ret == NOTDONE)
4977 {
4978 /*
4979 * Must be a variable or function name.
4980 * Can also be a curly-braces kind of name: {expr}.
4981 */
4982 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004983 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 if (alias != NULL)
4985 s = alias;
4986
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004987 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 ret = FAIL;
4989 else
4990 {
4991 if (**arg == '(') /* recursive! */
4992 {
4993 /* If "s" is the name of a variable of type VAR_FUNC
4994 * use its contents. */
4995 s = deref_func_name(s, &len);
4996
4997 /* Invoke the function. */
4998 ret = get_func_tv(s, len, rettv, arg,
4999 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005000 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 /* Stop the expression evaluation when immediately
5002 * aborting on error, or when an interrupt occurred or
5003 * an exception was thrown but not caught. */
5004 if (aborting())
5005 {
5006 if (ret == OK)
5007 clear_tv(rettv);
5008 ret = FAIL;
5009 }
5010 }
5011 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005012 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005013 else
5014 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 }
5016
5017 if (alias != NULL)
5018 vim_free(alias);
5019 }
5020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 *arg = skipwhite(*arg);
5022
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005023 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5024 * expr(expr). */
5025 if (ret == OK)
5026 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028 /*
5029 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5030 */
5031 if (ret == OK && evaluate && end_leader > start_leader)
5032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005033 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 int val = 0;
5035#ifdef FEAT_FLOAT
5036 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005037
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005038 if (rettv->v_type == VAR_FLOAT)
5039 f = rettv->vval.v_float;
5040 else
5041#endif
5042 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005043 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005045 clear_tv(rettv);
5046 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005048 else
5049 {
5050 while (end_leader > start_leader)
5051 {
5052 --end_leader;
5053 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005054 {
5055#ifdef FEAT_FLOAT
5056 if (rettv->v_type == VAR_FLOAT)
5057 f = !f;
5058 else
5059#endif
5060 val = !val;
5061 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005062 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063 {
5064#ifdef FEAT_FLOAT
5065 if (rettv->v_type == VAR_FLOAT)
5066 f = -f;
5067 else
5068#endif
5069 val = -val;
5070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005071 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072#ifdef FEAT_FLOAT
5073 if (rettv->v_type == VAR_FLOAT)
5074 {
5075 clear_tv(rettv);
5076 rettv->vval.v_float = f;
5077 }
5078 else
5079#endif
5080 {
5081 clear_tv(rettv);
5082 rettv->v_type = VAR_NUMBER;
5083 rettv->vval.v_number = val;
5084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087
5088 return ret;
5089}
5090
5091/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005092 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5093 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5095 */
5096 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005097eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005098 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005099 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005100 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005101 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102{
5103 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005104 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005105 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 long len = -1;
5107 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005108 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005111 if (rettv->v_type == VAR_FUNC
5112#ifdef FEAT_FLOAT
5113 || rettv->v_type == VAR_FLOAT
5114#endif
5115 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005116 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005117 if (verbose)
5118 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 return FAIL;
5120 }
5121
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005123 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 /*
5125 * dict.name
5126 */
5127 key = *arg + 1;
5128 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5129 ;
5130 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005133 }
5134 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005135 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 /*
5137 * something[idx]
5138 *
5139 * Get the (first) variable from inside the [].
5140 */
5141 *arg = skipwhite(*arg + 1);
5142 if (**arg == ':')
5143 empty1 = TRUE;
5144 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5145 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005146 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5147 {
5148 /* not a number or string */
5149 clear_tv(&var1);
5150 return FAIL;
5151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152
5153 /*
5154 * Get the second variable from inside the [:].
5155 */
5156 if (**arg == ':')
5157 {
5158 range = TRUE;
5159 *arg = skipwhite(*arg + 1);
5160 if (**arg == ']')
5161 empty2 = TRUE;
5162 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164 if (!empty1)
5165 clear_tv(&var1);
5166 return FAIL;
5167 }
5168 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5169 {
5170 /* not a number or string */
5171 if (!empty1)
5172 clear_tv(&var1);
5173 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 return FAIL;
5175 }
5176 }
5177
5178 /* Check for the ']'. */
5179 if (**arg != ']')
5180 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 if (verbose)
5182 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 clear_tv(&var1);
5184 if (range)
5185 clear_tv(&var2);
5186 return FAIL;
5187 }
5188 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 }
5190
5191 if (evaluate)
5192 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 n1 = 0;
5194 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 n1 = get_tv_number(&var1);
5197 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 }
5199 if (range)
5200 {
5201 if (empty2)
5202 n2 = -1;
5203 else
5204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 n2 = get_tv_number(&var2);
5206 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 }
5208 }
5209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005210 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211 {
5212 case VAR_NUMBER:
5213 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215 len = (long)STRLEN(s);
5216 if (range)
5217 {
5218 /* The resulting variable is a substring. If the indexes
5219 * are out of range the result is empty. */
5220 if (n1 < 0)
5221 {
5222 n1 = len + n1;
5223 if (n1 < 0)
5224 n1 = 0;
5225 }
5226 if (n2 < 0)
5227 n2 = len + n2;
5228 else if (n2 >= len)
5229 n2 = len;
5230 if (n1 >= len || n2 < 0 || n1 > n2)
5231 s = NULL;
5232 else
5233 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5234 }
5235 else
5236 {
5237 /* The resulting variable is a string of a single
5238 * character. If the index is too big or negative the
5239 * result is empty. */
5240 if (n1 >= len || n1 < 0)
5241 s = NULL;
5242 else
5243 s = vim_strnsave(s + n1, 1);
5244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005245 clear_tv(rettv);
5246 rettv->v_type = VAR_STRING;
5247 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 break;
5249
5250 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005251 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 if (n1 < 0)
5253 n1 = len + n1;
5254 if (!empty1 && (n1 < 0 || n1 >= len))
5255 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005256 /* For a range we allow invalid values and return an empty
5257 * list. A list index out of range is an error. */
5258 if (!range)
5259 {
5260 if (verbose)
5261 EMSGN(_(e_listidx), n1);
5262 return FAIL;
5263 }
5264 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 }
5266 if (range)
5267 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 list_T *l;
5269 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
5271 if (n2 < 0)
5272 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005273 else if (n2 >= len)
5274 n2 = len - 1;
5275 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005276 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 l = list_alloc();
5278 if (l == NULL)
5279 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 n1 <= n2; ++n1)
5282 {
5283 if (list_append_tv(l, &item->li_tv) == FAIL)
5284 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005285 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 return FAIL;
5287 }
5288 item = item->li_next;
5289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 clear_tv(rettv);
5291 rettv->v_type = VAR_LIST;
5292 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005293 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 }
5295 else
5296 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005297 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 clear_tv(rettv);
5299 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 }
5301 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302
5303 case VAR_DICT:
5304 if (range)
5305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005306 if (verbose)
5307 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 if (len == -1)
5309 clear_tv(&var1);
5310 return FAIL;
5311 }
5312 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314
5315 if (len == -1)
5316 {
5317 key = get_tv_string(&var1);
5318 if (*key == NUL)
5319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005320 if (verbose)
5321 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 clear_tv(&var1);
5323 return FAIL;
5324 }
5325 }
5326
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005327 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005329 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005330 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 if (len == -1)
5332 clear_tv(&var1);
5333 if (item == NULL)
5334 return FAIL;
5335
5336 copy_tv(&item->di_tv, &var1);
5337 clear_tv(rettv);
5338 *rettv = var1;
5339 }
5340 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 }
5342 }
5343
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 return OK;
5345}
5346
5347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 * Get an option value.
5349 * "arg" points to the '&' or '+' before the option name.
5350 * "arg" is advanced to character after the option name.
5351 * Return OK or FAIL.
5352 */
5353 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005356 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 int evaluate;
5358{
5359 char_u *option_end;
5360 long numval;
5361 char_u *stringval;
5362 int opt_type;
5363 int c;
5364 int working = (**arg == '+'); /* has("+option") */
5365 int ret = OK;
5366 int opt_flags;
5367
5368 /*
5369 * Isolate the option name and find its value.
5370 */
5371 option_end = find_option_end(arg, &opt_flags);
5372 if (option_end == NULL)
5373 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 EMSG2(_("E112: Option name missing: %s"), *arg);
5376 return FAIL;
5377 }
5378
5379 if (!evaluate)
5380 {
5381 *arg = option_end;
5382 return OK;
5383 }
5384
5385 c = *option_end;
5386 *option_end = NUL;
5387 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
5390 if (opt_type == -3) /* invalid name */
5391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005392 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 EMSG2(_("E113: Unknown option: %s"), *arg);
5394 ret = FAIL;
5395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
5398 if (opt_type == -2) /* hidden string option */
5399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005400 rettv->v_type = VAR_STRING;
5401 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 else if (opt_type == -1) /* hidden number option */
5404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 rettv->v_type = VAR_NUMBER;
5406 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408 else if (opt_type == 1) /* number option */
5409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410 rettv->v_type = VAR_NUMBER;
5411 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413 else /* string option */
5414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 rettv->v_type = VAR_STRING;
5416 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 }
5419 else if (working && (opt_type == -2 || opt_type == -1))
5420 ret = FAIL;
5421
5422 *option_end = c; /* put back for error messages */
5423 *arg = option_end;
5424
5425 return ret;
5426}
5427
5428/*
5429 * Allocate a variable for a string constant.
5430 * Return OK or FAIL.
5431 */
5432 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 int evaluate;
5437{
5438 char_u *p;
5439 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 int extra = 0;
5441
5442 /*
5443 * Find the end of the string, skipping backslashed characters.
5444 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005445 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
5447 if (*p == '\\' && p[1] != NUL)
5448 {
5449 ++p;
5450 /* A "\<x>" form occupies at least 4 characters, and produces up
5451 * to 6 characters: reserve space for 2 extra */
5452 if (*p == '<')
5453 extra += 2;
5454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
5456
5457 if (*p != '"')
5458 {
5459 EMSG2(_("E114: Missing quote: %s"), *arg);
5460 return FAIL;
5461 }
5462
5463 /* If only parsing, set *arg and return here */
5464 if (!evaluate)
5465 {
5466 *arg = p + 1;
5467 return OK;
5468 }
5469
5470 /*
5471 * Copy the string into allocated memory, handling backslashed
5472 * characters.
5473 */
5474 name = alloc((unsigned)(p - *arg + extra));
5475 if (name == NULL)
5476 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005477 rettv->v_type = VAR_STRING;
5478 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 if (*p == '\\')
5483 {
5484 switch (*++p)
5485 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486 case 'b': *name++ = BS; ++p; break;
5487 case 'e': *name++ = ESC; ++p; break;
5488 case 'f': *name++ = FF; ++p; break;
5489 case 'n': *name++ = NL; ++p; break;
5490 case 'r': *name++ = CAR; ++p; break;
5491 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 case 'X': /* hex: "\x1", "\x12" */
5494 case 'x':
5495 case 'u': /* Unicode: "\u0023" */
5496 case 'U':
5497 if (vim_isxdigit(p[1]))
5498 {
5499 int n, nr;
5500 int c = toupper(*p);
5501
5502 if (c == 'X')
5503 n = 2;
5504 else
5505 n = 4;
5506 nr = 0;
5507 while (--n >= 0 && vim_isxdigit(p[1]))
5508 {
5509 ++p;
5510 nr = (nr << 4) + hex2nr(*p);
5511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005512 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#ifdef FEAT_MBYTE
5514 /* For "\u" store the number according to
5515 * 'encoding'. */
5516 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 else
5519#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 break;
5523
5524 /* octal: "\1", "\12", "\123" */
5525 case '0':
5526 case '1':
5527 case '2':
5528 case '3':
5529 case '4':
5530 case '5':
5531 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 case '7': *name = *p++ - '0';
5533 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 *name = (*name << 3) + *p++ - '0';
5536 if (*p >= '0' && *p <= '7')
5537 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005539 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 break;
5541
5542 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 if (extra != 0)
5545 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 break;
5548 }
5549 /* FALLTHROUGH */
5550
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 break;
5553 }
5554 }
5555 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005556 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 *arg = p + 1;
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return OK;
5563}
5564
5565/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 * Return OK or FAIL.
5568 */
5569 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 int evaluate;
5574{
5575 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005577 int reduce = 0;
5578
5579 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005581 */
5582 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5583 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005587 break;
5588 ++reduce;
5589 ++p;
5590 }
5591 }
5592
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005596 return FAIL;
5597 }
5598
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 if (!evaluate)
5601 {
5602 *arg = p + 1;
5603 return OK;
5604 }
5605
5606 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005608 */
5609 str = alloc((unsigned)((p - *arg) - reduce));
5610 if (str == NULL)
5611 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 rettv->v_type = VAR_STRING;
5613 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005614
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005617 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005620 break;
5621 ++p;
5622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005626 *arg = p + 1;
5627
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005628 return OK;
5629}
5630
5631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 * Allocate a variable for a List and fill it from "*arg".
5633 * Return OK or FAIL.
5634 */
5635 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005638 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 int evaluate;
5640{
Bram Moolenaar33570922005-01-25 22:26:29 +00005641 list_T *l = NULL;
5642 typval_T tv;
5643 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644
5645 if (evaluate)
5646 {
5647 l = list_alloc();
5648 if (l == NULL)
5649 return FAIL;
5650 }
5651
5652 *arg = skipwhite(*arg + 1);
5653 while (**arg != ']' && **arg != NUL)
5654 {
5655 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5656 goto failret;
5657 if (evaluate)
5658 {
5659 item = listitem_alloc();
5660 if (item != NULL)
5661 {
5662 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005663 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 list_append(l, item);
5665 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005666 else
5667 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005668 }
5669
5670 if (**arg == ']')
5671 break;
5672 if (**arg != ',')
5673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 goto failret;
5676 }
5677 *arg = skipwhite(*arg + 1);
5678 }
5679
5680 if (**arg != ']')
5681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683failret:
5684 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005685 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 return FAIL;
5687 }
5688
5689 *arg = skipwhite(*arg + 1);
5690 if (evaluate)
5691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 rettv->v_type = VAR_LIST;
5693 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 ++l->lv_refcount;
5695 }
5696
5697 return OK;
5698}
5699
5700/*
5701 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005702 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005704 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705list_alloc()
5706{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005707 list_T *l;
5708
5709 l = (list_T *)alloc_clear(sizeof(list_T));
5710 if (l != NULL)
5711 {
5712 /* Prepend the list to the list of lists for garbage collection. */
5713 if (first_list != NULL)
5714 first_list->lv_used_prev = l;
5715 l->lv_used_prev = NULL;
5716 l->lv_used_next = first_list;
5717 first_list = l;
5718 }
5719 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720}
5721
5722/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005723 * Allocate an empty list for a return value.
5724 * Returns OK or FAIL.
5725 */
5726 static int
5727rettv_list_alloc(rettv)
5728 typval_T *rettv;
5729{
5730 list_T *l = list_alloc();
5731
5732 if (l == NULL)
5733 return FAIL;
5734
5735 rettv->vval.v_list = l;
5736 rettv->v_type = VAR_LIST;
5737 ++l->lv_refcount;
5738 return OK;
5739}
5740
5741/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 * Unreference a list: decrement the reference count and free it when it
5743 * becomes zero.
5744 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005745 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005749 if (l != NULL && --l->lv_refcount <= 0)
5750 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751}
5752
5753/*
5754 * Free a list, including all items it points to.
5755 * Ignores the reference count.
5756 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005757 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005758list_free(l, recurse)
5759 list_T *l;
5760 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761{
Bram Moolenaar33570922005-01-25 22:26:29 +00005762 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005764 /* Remove the list from the list of lists for garbage collection. */
5765 if (l->lv_used_prev == NULL)
5766 first_list = l->lv_used_next;
5767 else
5768 l->lv_used_prev->lv_used_next = l->lv_used_next;
5769 if (l->lv_used_next != NULL)
5770 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5771
Bram Moolenaard9fba312005-06-26 22:34:35 +00005772 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005774 /* Remove the item before deleting it. */
5775 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005776 if (recurse || (item->li_tv.v_type != VAR_LIST
5777 && item->li_tv.v_type != VAR_DICT))
5778 clear_tv(&item->li_tv);
5779 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 }
5781 vim_free(l);
5782}
5783
5784/*
5785 * Allocate a list item.
5786 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788listitem_alloc()
5789{
Bram Moolenaar33570922005-01-25 22:26:29 +00005790 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791}
5792
5793/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005794 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 */
5796 static void
5797listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005800 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 vim_free(item);
5802}
5803
5804/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005805 * Remove a list item from a List and free it. Also clears the value.
5806 */
5807 static void
5808listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 list_T *l;
5810 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005811{
5812 list_remove(l, item, item);
5813 listitem_free(item);
5814}
5815
5816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 * Get the number of items in a list.
5818 */
5819 static long
5820list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 if (l == NULL)
5824 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005825 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826}
5827
5828/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005829 * Return TRUE when two lists have exactly the same values.
5830 */
5831 static int
5832list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 list_T *l1;
5834 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005835 int ic; /* ignore case for strings */
5836{
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005838
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005839 if (l1 == NULL || l2 == NULL)
5840 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005841 if (l1 == l2)
5842 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005843 if (list_len(l1) != list_len(l2))
5844 return FALSE;
5845
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005846 for (item1 = l1->lv_first, item2 = l2->lv_first;
5847 item1 != NULL && item2 != NULL;
5848 item1 = item1->li_next, item2 = item2->li_next)
5849 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5850 return FALSE;
5851 return item1 == NULL && item2 == NULL;
5852}
5853
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005854#if defined(FEAT_PYTHON) || defined(PROTO)
5855/*
5856 * Return the dictitem that an entry in a hashtable points to.
5857 */
5858 dictitem_T *
5859dict_lookup(hi)
5860 hashitem_T *hi;
5861{
5862 return HI2DI(hi);
5863}
5864#endif
5865
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005867 * Return TRUE when two dictionaries have exactly the same key/values.
5868 */
5869 static int
5870dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 dict_T *d1;
5872 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005873 int ic; /* ignore case for strings */
5874{
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 hashitem_T *hi;
5876 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005877 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005878
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005879 if (d1 == NULL || d2 == NULL)
5880 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005881 if (d1 == d2)
5882 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005883 if (dict_len(d1) != dict_len(d2))
5884 return FALSE;
5885
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005886 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005887 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005888 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005889 if (!HASHITEM_EMPTY(hi))
5890 {
5891 item2 = dict_find(d2, hi->hi_key, -1);
5892 if (item2 == NULL)
5893 return FALSE;
5894 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5895 return FALSE;
5896 --todo;
5897 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005898 }
5899 return TRUE;
5900}
5901
5902/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005904 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005905 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906 */
5907 static int
5908tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 typval_T *tv1;
5910 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005911 int ic; /* ignore case */
5912{
5913 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005914 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005915 static int recursive = 0; /* cach recursive loops */
5916 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005917
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005918 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005919 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005920 /* Catch lists and dicts that have an endless loop by limiting
5921 * recursiveness to 1000. We guess they are equal then. */
5922 if (recursive >= 1000)
5923 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005924
5925 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005927 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005928 ++recursive;
5929 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5930 --recursive;
5931 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005932
5933 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005934 ++recursive;
5935 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5936 --recursive;
5937 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005938
5939 case VAR_FUNC:
5940 return (tv1->vval.v_string != NULL
5941 && tv2->vval.v_string != NULL
5942 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5943
5944 case VAR_NUMBER:
5945 return tv1->vval.v_number == tv2->vval.v_number;
5946
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005947#ifdef FEAT_FLOAT
5948 case VAR_FLOAT:
5949 return tv1->vval.v_float == tv2->vval.v_float;
5950#endif
5951
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005952 case VAR_STRING:
5953 s1 = get_tv_string_buf(tv1, buf1);
5954 s2 = get_tv_string_buf(tv2, buf2);
5955 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005956 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005957
5958 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005959 return TRUE;
5960}
5961
5962/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 * Locate item with index "n" in list "l" and return it.
5964 * A negative index is counted from the end; -1 is the last item.
5965 * Returns NULL when "n" is out of range.
5966 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005967 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 long n;
5971{
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 long idx;
5974
5975 if (l == NULL)
5976 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005977
5978 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005980 n = l->lv_len + n;
5981
5982 /* Check for index out of range. */
5983 if (n < 0 || n >= l->lv_len)
5984 return NULL;
5985
5986 /* When there is a cached index may start search from there. */
5987 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005989 if (n < l->lv_idx / 2)
5990 {
5991 /* closest to the start of the list */
5992 item = l->lv_first;
5993 idx = 0;
5994 }
5995 else if (n > (l->lv_idx + l->lv_len) / 2)
5996 {
5997 /* closest to the end of the list */
5998 item = l->lv_last;
5999 idx = l->lv_len - 1;
6000 }
6001 else
6002 {
6003 /* closest to the cached index */
6004 item = l->lv_idx_item;
6005 idx = l->lv_idx;
6006 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 }
6008 else
6009 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006010 if (n < l->lv_len / 2)
6011 {
6012 /* closest to the start of the list */
6013 item = l->lv_first;
6014 idx = 0;
6015 }
6016 else
6017 {
6018 /* closest to the end of the list */
6019 item = l->lv_last;
6020 idx = l->lv_len - 1;
6021 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006023
6024 while (n > idx)
6025 {
6026 /* search forward */
6027 item = item->li_next;
6028 ++idx;
6029 }
6030 while (n < idx)
6031 {
6032 /* search backward */
6033 item = item->li_prev;
6034 --idx;
6035 }
6036
6037 /* cache the used index */
6038 l->lv_idx = idx;
6039 l->lv_idx_item = item;
6040
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 return item;
6042}
6043
6044/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006045 * Get list item "l[idx]" as a number.
6046 */
6047 static long
6048list_find_nr(l, idx, errorp)
6049 list_T *l;
6050 long idx;
6051 int *errorp; /* set to TRUE when something wrong */
6052{
6053 listitem_T *li;
6054
6055 li = list_find(l, idx);
6056 if (li == NULL)
6057 {
6058 if (errorp != NULL)
6059 *errorp = TRUE;
6060 return -1L;
6061 }
6062 return get_tv_number_chk(&li->li_tv, errorp);
6063}
6064
6065/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006066 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6067 */
6068 char_u *
6069list_find_str(l, idx)
6070 list_T *l;
6071 long idx;
6072{
6073 listitem_T *li;
6074
6075 li = list_find(l, idx - 1);
6076 if (li == NULL)
6077 {
6078 EMSGN(_(e_listidx), idx);
6079 return NULL;
6080 }
6081 return get_tv_string(&li->li_tv);
6082}
6083
6084/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006085 * Locate "item" list "l" and return its index.
6086 * Returns -1 when "item" is not in the list.
6087 */
6088 static long
6089list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 list_T *l;
6091 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006092{
6093 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006094 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006095
6096 if (l == NULL)
6097 return -1;
6098 idx = 0;
6099 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6100 ++idx;
6101 if (li == NULL)
6102 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006103 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006104}
6105
6106/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 * Append item "item" to the end of list "l".
6108 */
6109 static void
6110list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 list_T *l;
6112 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113{
6114 if (l->lv_last == NULL)
6115 {
6116 /* empty list */
6117 l->lv_first = item;
6118 l->lv_last = item;
6119 item->li_prev = NULL;
6120 }
6121 else
6122 {
6123 l->lv_last->li_next = item;
6124 item->li_prev = l->lv_last;
6125 l->lv_last = item;
6126 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006127 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128 item->li_next = NULL;
6129}
6130
6131/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134 */
6135 static int
6136list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 list_T *l;
6138 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006140 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141
Bram Moolenaar05159a02005-02-26 23:04:13 +00006142 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006144 copy_tv(tv, &li->li_tv);
6145 list_append(l, li);
6146 return OK;
6147}
6148
6149/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006150 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006151 * Return FAIL when out of memory.
6152 */
6153 int
6154list_append_dict(list, dict)
6155 list_T *list;
6156 dict_T *dict;
6157{
6158 listitem_T *li = listitem_alloc();
6159
6160 if (li == NULL)
6161 return FAIL;
6162 li->li_tv.v_type = VAR_DICT;
6163 li->li_tv.v_lock = 0;
6164 li->li_tv.vval.v_dict = dict;
6165 list_append(list, li);
6166 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006167 return OK;
6168}
6169
6170/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006171 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006172 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006173 * Returns FAIL when out of memory.
6174 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006175 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006176list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006177 list_T *l;
6178 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006179 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006180{
6181 listitem_T *li = listitem_alloc();
6182
6183 if (li == NULL)
6184 return FAIL;
6185 list_append(l, li);
6186 li->li_tv.v_type = VAR_STRING;
6187 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006188 if (str == NULL)
6189 li->li_tv.vval.v_string = NULL;
6190 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006191 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006192 return FAIL;
6193 return OK;
6194}
6195
6196/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006197 * Append "n" to list "l".
6198 * Returns FAIL when out of memory.
6199 */
6200 static int
6201list_append_number(l, n)
6202 list_T *l;
6203 varnumber_T n;
6204{
6205 listitem_T *li;
6206
6207 li = listitem_alloc();
6208 if (li == NULL)
6209 return FAIL;
6210 li->li_tv.v_type = VAR_NUMBER;
6211 li->li_tv.v_lock = 0;
6212 li->li_tv.vval.v_number = n;
6213 list_append(l, li);
6214 return OK;
6215}
6216
6217/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219 * If "item" is NULL append at the end.
6220 * Return FAIL when out of memory.
6221 */
6222 static int
6223list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006224 list_T *l;
6225 typval_T *tv;
6226 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227{
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006229
6230 if (ni == NULL)
6231 return FAIL;
6232 copy_tv(tv, &ni->li_tv);
6233 if (item == NULL)
6234 /* Append new item at end of list. */
6235 list_append(l, ni);
6236 else
6237 {
6238 /* Insert new item before existing item. */
6239 ni->li_prev = item->li_prev;
6240 ni->li_next = item;
6241 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006242 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006244 ++l->lv_idx;
6245 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006247 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006249 l->lv_idx_item = NULL;
6250 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006251 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006252 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 }
6254 return OK;
6255}
6256
6257/*
6258 * Extend "l1" with "l2".
6259 * If "bef" is NULL append at the end, otherwise insert before this item.
6260 * Returns FAIL when out of memory.
6261 */
6262 static int
6263list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *l1;
6265 list_T *l2;
6266 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006267{
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006269 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006270
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006271 /* We also quit the loop when we have inserted the original item count of
6272 * the list, avoid a hang when we extend a list with itself. */
6273 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006274 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6275 return FAIL;
6276 return OK;
6277}
6278
6279/*
6280 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6281 * Return FAIL when out of memory.
6282 */
6283 static int
6284list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l1;
6286 list_T *l2;
6287 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288{
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006291 if (l1 == NULL || l2 == NULL)
6292 return FAIL;
6293
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006294 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006295 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006296 if (l == NULL)
6297 return FAIL;
6298 tv->v_type = VAR_LIST;
6299 tv->vval.v_list = l;
6300
6301 /* append all items from the second list */
6302 return list_extend(l, l2, NULL);
6303}
6304
6305/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006308 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 * Returns NULL when out of memory.
6310 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006312list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006315 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316{
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 list_T *copy;
6318 listitem_T *item;
6319 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320
6321 if (orig == NULL)
6322 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323
6324 copy = list_alloc();
6325 if (copy != NULL)
6326 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006327 if (copyID != 0)
6328 {
6329 /* Do this before adding the items, because one of the items may
6330 * refer back to this list. */
6331 orig->lv_copyID = copyID;
6332 orig->lv_copylist = copy;
6333 }
6334 for (item = orig->lv_first; item != NULL && !got_int;
6335 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336 {
6337 ni = listitem_alloc();
6338 if (ni == NULL)
6339 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006340 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006341 {
6342 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6343 {
6344 vim_free(ni);
6345 break;
6346 }
6347 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006349 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 list_append(copy, ni);
6351 }
6352 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006353 if (item != NULL)
6354 {
6355 list_unref(copy);
6356 copy = NULL;
6357 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006358 }
6359
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360 return copy;
6361}
6362
6363/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006364 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006365 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006367 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006368list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 list_T *l;
6370 listitem_T *item;
6371 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006372{
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375 /* notify watchers */
6376 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006378 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379 list_fix_watch(l, ip);
6380 if (ip == item2)
6381 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383
6384 if (item2->li_next == NULL)
6385 l->lv_last = item->li_prev;
6386 else
6387 item2->li_next->li_prev = item->li_prev;
6388 if (item->li_prev == NULL)
6389 l->lv_first = item2->li_next;
6390 else
6391 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006392 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393}
6394
6395/*
6396 * Return an allocated string with the string representation of a list.
6397 * May return NULL.
6398 */
6399 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006400list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006402 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
6404 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405
6406 if (tv->vval.v_list == NULL)
6407 return NULL;
6408 ga_init2(&ga, (int)sizeof(char), 80);
6409 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006410 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006411 {
6412 vim_free(ga.ga_data);
6413 return NULL;
6414 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 ga_append(&ga, ']');
6416 ga_append(&ga, NUL);
6417 return (char_u *)ga.ga_data;
6418}
6419
6420/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006421 * Join list "l" into a string in "*gap", using separator "sep".
6422 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006423 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006424 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006425 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006426list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006427 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006429 char_u *sep;
6430 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006431 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006432{
6433 int first = TRUE;
6434 char_u *tofree;
6435 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006437 char_u *s;
6438
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006439 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006440 {
6441 if (first)
6442 first = FALSE;
6443 else
6444 ga_concat(gap, sep);
6445
6446 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006447 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006448 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006449 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006450 if (s != NULL)
6451 ga_concat(gap, s);
6452 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006453 if (s == NULL)
6454 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006455 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006456 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006457}
6458
6459/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006460 * Garbage collection for lists and dictionaries.
6461 *
6462 * We use reference counts to be able to free most items right away when they
6463 * are no longer used. But for composite items it's possible that it becomes
6464 * unused while the reference count is > 0: When there is a recursive
6465 * reference. Example:
6466 * :let l = [1, 2, 3]
6467 * :let d = {9: l}
6468 * :let l[1] = d
6469 *
6470 * Since this is quite unusual we handle this with garbage collection: every
6471 * once in a while find out which lists and dicts are not referenced from any
6472 * variable.
6473 *
6474 * Here is a good reference text about garbage collection (refers to Python
6475 * but it applies to all reference-counting mechanisms):
6476 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006477 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006478
6479/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006480 * Do garbage collection for lists and dicts.
6481 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006482 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006483 int
6484garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006485{
6486 dict_T *dd;
6487 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006488 int copyID = ++current_copyID;
6489 buf_T *buf;
6490 win_T *wp;
6491 int i;
6492 funccall_T *fc;
6493 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006494#ifdef FEAT_WINDOWS
6495 tabpage_T *tp;
6496#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006497
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006498 /* Only do this once. */
6499 want_garbage_collect = FALSE;
6500 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006501 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006502
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006503 /*
6504 * 1. Go through all accessible variables and mark all lists and dicts
6505 * with copyID.
6506 */
6507 /* script-local variables */
6508 for (i = 1; i <= ga_scripts.ga_len; ++i)
6509 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6510
6511 /* buffer-local variables */
6512 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6513 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6514
6515 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006516 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006517 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6518
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006519#ifdef FEAT_WINDOWS
6520 /* tabpage-local variables */
6521 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6522 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6523#endif
6524
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006525 /* global variables */
6526 set_ref_in_ht(&globvarht, copyID);
6527
6528 /* function-local variables */
6529 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6530 {
6531 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6532 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6533 }
6534
Bram Moolenaard812df62008-11-09 12:46:09 +00006535 /* v: vars */
6536 set_ref_in_ht(&vimvarht, copyID);
6537
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006538 /*
6539 * 2. Go through the list of dicts and free items without the copyID.
6540 */
6541 for (dd = first_dict; dd != NULL; )
6542 if (dd->dv_copyID != copyID)
6543 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006544 /* Free the Dictionary and ordinary items it contains, but don't
6545 * recurse into Lists and Dictionaries, they will be in the list
6546 * of dicts or list of lists. */
6547 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006548 did_free = TRUE;
6549
6550 /* restart, next dict may also have been freed */
6551 dd = first_dict;
6552 }
6553 else
6554 dd = dd->dv_used_next;
6555
6556 /*
6557 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006558 * But don't free a list that has a watcher (used in a for loop), these
6559 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006560 */
6561 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006562 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006563 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006564 /* Free the List and ordinary items it contains, but don't recurse
6565 * into Lists and Dictionaries, they will be in the list of dicts
6566 * or list of lists. */
6567 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006568 did_free = TRUE;
6569
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006570 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006571 ll = first_list;
6572 }
6573 else
6574 ll = ll->lv_used_next;
6575
6576 return did_free;
6577}
6578
6579/*
6580 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6581 */
6582 static void
6583set_ref_in_ht(ht, copyID)
6584 hashtab_T *ht;
6585 int copyID;
6586{
6587 int todo;
6588 hashitem_T *hi;
6589
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006590 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006591 for (hi = ht->ht_array; todo > 0; ++hi)
6592 if (!HASHITEM_EMPTY(hi))
6593 {
6594 --todo;
6595 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6596 }
6597}
6598
6599/*
6600 * Mark all lists and dicts referenced through list "l" with "copyID".
6601 */
6602 static void
6603set_ref_in_list(l, copyID)
6604 list_T *l;
6605 int copyID;
6606{
6607 listitem_T *li;
6608
6609 for (li = l->lv_first; li != NULL; li = li->li_next)
6610 set_ref_in_item(&li->li_tv, copyID);
6611}
6612
6613/*
6614 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6615 */
6616 static void
6617set_ref_in_item(tv, copyID)
6618 typval_T *tv;
6619 int copyID;
6620{
6621 dict_T *dd;
6622 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006623
6624 switch (tv->v_type)
6625 {
6626 case VAR_DICT:
6627 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006628 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006629 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006630 /* Didn't see this dict yet. */
6631 dd->dv_copyID = copyID;
6632 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006633 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006634 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006635
6636 case VAR_LIST:
6637 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006638 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006639 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006640 /* Didn't see this list yet. */
6641 ll->lv_copyID = copyID;
6642 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006643 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006644 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006645 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006646 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006647}
6648
6649/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006650 * Allocate an empty header for a dictionary.
6651 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006652 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653dict_alloc()
6654{
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006656
Bram Moolenaar33570922005-01-25 22:26:29 +00006657 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006658 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006659 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006660 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006661 if (first_dict != NULL)
6662 first_dict->dv_used_prev = d;
6663 d->dv_used_next = first_dict;
6664 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006665 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006666
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006668 d->dv_lock = 0;
6669 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006671 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006672 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006673}
6674
6675/*
6676 * Unreference a Dictionary: decrement the reference count and free it when it
6677 * becomes zero.
6678 */
6679 static void
6680dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006683 if (d != NULL && --d->dv_refcount <= 0)
6684 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685}
6686
6687/*
6688 * Free a Dictionary, including all items it contains.
6689 * Ignores the reference count.
6690 */
6691 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006692dict_free(d, recurse)
6693 dict_T *d;
6694 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006695{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006696 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006697 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006698 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006700 /* Remove the dict from the list of dicts for garbage collection. */
6701 if (d->dv_used_prev == NULL)
6702 first_dict = d->dv_used_next;
6703 else
6704 d->dv_used_prev->dv_used_next = d->dv_used_next;
6705 if (d->dv_used_next != NULL)
6706 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6707
6708 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006709 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006710 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006711 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006713 if (!HASHITEM_EMPTY(hi))
6714 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006715 /* Remove the item before deleting it, just in case there is
6716 * something recursive causing trouble. */
6717 di = HI2DI(hi);
6718 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006719 if (recurse || (di->di_tv.v_type != VAR_LIST
6720 && di->di_tv.v_type != VAR_DICT))
6721 clear_tv(&di->di_tv);
6722 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006723 --todo;
6724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006725 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006726 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006727 vim_free(d);
6728}
6729
6730/*
6731 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006732 * The "key" is copied to the new item.
6733 * Note that the value of the item "di_tv" still needs to be initialized!
6734 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006735 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006736 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737dictitem_alloc(key)
6738 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739{
Bram Moolenaar33570922005-01-25 22:26:29 +00006740 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006741
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006742 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006743 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006745 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006746 di->di_flags = 0;
6747 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006748 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749}
6750
6751/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006752 * Make a copy of a Dictionary item.
6753 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006754 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006755dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006757{
Bram Moolenaar33570922005-01-25 22:26:29 +00006758 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006759
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006760 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6761 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006762 if (di != NULL)
6763 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006764 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006765 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006766 copy_tv(&org->di_tv, &di->di_tv);
6767 }
6768 return di;
6769}
6770
6771/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006772 * Remove item "item" from Dictionary "dict" and free it.
6773 */
6774 static void
6775dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006776 dict_T *dict;
6777 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006778{
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006780
Bram Moolenaar33570922005-01-25 22:26:29 +00006781 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006782 if (HASHITEM_EMPTY(hi))
6783 EMSG2(_(e_intern2), "dictitem_remove()");
6784 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006785 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006786 dictitem_free(item);
6787}
6788
6789/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006790 * Free a dict item. Also clears the value.
6791 */
6792 static void
6793dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006796 clear_tv(&item->di_tv);
6797 vim_free(item);
6798}
6799
6800/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006801 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6802 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006803 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006804 * Returns NULL when out of memory.
6805 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006807dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006808 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006810 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006811{
Bram Moolenaar33570922005-01-25 22:26:29 +00006812 dict_T *copy;
6813 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006814 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006815 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816
6817 if (orig == NULL)
6818 return NULL;
6819
6820 copy = dict_alloc();
6821 if (copy != NULL)
6822 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006823 if (copyID != 0)
6824 {
6825 orig->dv_copyID = copyID;
6826 orig->dv_copydict = copy;
6827 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006828 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006829 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006830 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006831 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006833 --todo;
6834
6835 di = dictitem_alloc(hi->hi_key);
6836 if (di == NULL)
6837 break;
6838 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006839 {
6840 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6841 copyID) == FAIL)
6842 {
6843 vim_free(di);
6844 break;
6845 }
6846 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006847 else
6848 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6849 if (dict_add(copy, di) == FAIL)
6850 {
6851 dictitem_free(di);
6852 break;
6853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006855 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006856
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006858 if (todo > 0)
6859 {
6860 dict_unref(copy);
6861 copy = NULL;
6862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006863 }
6864
6865 return copy;
6866}
6867
6868/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006869 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006870 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006871 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006872 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 dict_T *d;
6875 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006876{
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006878}
6879
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006881 * Add a number or string entry to dictionary "d".
6882 * When "str" is NULL use number "nr", otherwise use "str".
6883 * Returns FAIL when out of memory and when key already exists.
6884 */
6885 int
6886dict_add_nr_str(d, key, nr, str)
6887 dict_T *d;
6888 char *key;
6889 long nr;
6890 char_u *str;
6891{
6892 dictitem_T *item;
6893
6894 item = dictitem_alloc((char_u *)key);
6895 if (item == NULL)
6896 return FAIL;
6897 item->di_tv.v_lock = 0;
6898 if (str == NULL)
6899 {
6900 item->di_tv.v_type = VAR_NUMBER;
6901 item->di_tv.vval.v_number = nr;
6902 }
6903 else
6904 {
6905 item->di_tv.v_type = VAR_STRING;
6906 item->di_tv.vval.v_string = vim_strsave(str);
6907 }
6908 if (dict_add(d, item) == FAIL)
6909 {
6910 dictitem_free(item);
6911 return FAIL;
6912 }
6913 return OK;
6914}
6915
6916/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917 * Get the number of items in a Dictionary.
6918 */
6919 static long
6920dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006922{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 if (d == NULL)
6924 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006925 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006926}
6927
6928/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006929 * Find item "key[len]" in Dictionary "d".
6930 * If "len" is negative use strlen(key).
6931 * Returns NULL when not found.
6932 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006933 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006934dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936 char_u *key;
6937 int len;
6938{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006939#define AKEYLEN 200
6940 char_u buf[AKEYLEN];
6941 char_u *akey;
6942 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006945 if (len < 0)
6946 akey = key;
6947 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006949 tofree = akey = vim_strnsave(key, len);
6950 if (akey == NULL)
6951 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006953 else
6954 {
6955 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006956 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006957 akey = buf;
6958 }
6959
Bram Moolenaar33570922005-01-25 22:26:29 +00006960 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006961 vim_free(tofree);
6962 if (HASHITEM_EMPTY(hi))
6963 return NULL;
6964 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006965}
6966
6967/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006968 * Get a string item from a dictionary.
6969 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006970 * Returns NULL if the entry doesn't exist or out of memory.
6971 */
6972 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006973get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006974 dict_T *d;
6975 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006976 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006977{
6978 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006979 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006980
6981 di = dict_find(d, key, -1);
6982 if (di == NULL)
6983 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006984 s = get_tv_string(&di->di_tv);
6985 if (save && s != NULL)
6986 s = vim_strsave(s);
6987 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006988}
6989
6990/*
6991 * Get a number item from a dictionary.
6992 * Returns 0 if the entry doesn't exist or out of memory.
6993 */
6994 long
6995get_dict_number(d, key)
6996 dict_T *d;
6997 char_u *key;
6998{
6999 dictitem_T *di;
7000
7001 di = dict_find(d, key, -1);
7002 if (di == NULL)
7003 return 0;
7004 return get_tv_number(&di->di_tv);
7005}
7006
7007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008 * Return an allocated string with the string representation of a Dictionary.
7009 * May return NULL.
7010 */
7011 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007012dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007014 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015{
7016 garray_T ga;
7017 int first = TRUE;
7018 char_u *tofree;
7019 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007023 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007025 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026 return NULL;
7027 ga_init2(&ga, (int)sizeof(char), 80);
7028 ga_append(&ga, '{');
7029
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007030 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007031 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007035 --todo;
7036
7037 if (first)
7038 first = FALSE;
7039 else
7040 ga_concat(&ga, (char_u *)", ");
7041
7042 tofree = string_quote(hi->hi_key, FALSE);
7043 if (tofree != NULL)
7044 {
7045 ga_concat(&ga, tofree);
7046 vim_free(tofree);
7047 }
7048 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007049 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 if (s != NULL)
7051 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007053 if (s == NULL)
7054 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007057 if (todo > 0)
7058 {
7059 vim_free(ga.ga_data);
7060 return NULL;
7061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062
7063 ga_append(&ga, '}');
7064 ga_append(&ga, NUL);
7065 return (char_u *)ga.ga_data;
7066}
7067
7068/*
7069 * Allocate a variable for a Dictionary and fill it from "*arg".
7070 * Return OK or FAIL. Returns NOTDONE for {expr}.
7071 */
7072 static int
7073get_dict_tv(arg, rettv, evaluate)
7074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007075 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 int evaluate;
7077{
Bram Moolenaar33570922005-01-25 22:26:29 +00007078 dict_T *d = NULL;
7079 typval_T tvkey;
7080 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007081 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007082 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007085
7086 /*
7087 * First check if it's not a curly-braces thing: {expr}.
7088 * Must do this without evaluating, otherwise a function may be called
7089 * twice. Unfortunately this means we need to call eval1() twice for the
7090 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007091 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093 if (*start != '}')
7094 {
7095 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7096 return FAIL;
7097 if (*start == '}')
7098 return NOTDONE;
7099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100
7101 if (evaluate)
7102 {
7103 d = dict_alloc();
7104 if (d == NULL)
7105 return FAIL;
7106 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007107 tvkey.v_type = VAR_UNKNOWN;
7108 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109
7110 *arg = skipwhite(*arg + 1);
7111 while (**arg != '}' && **arg != NUL)
7112 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114 goto failret;
7115 if (**arg != ':')
7116 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007117 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119 goto failret;
7120 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007121 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007123 key = get_tv_string_buf_chk(&tvkey, buf);
7124 if (key == NULL || *key == NUL)
7125 {
7126 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7127 if (key != NULL)
7128 EMSG(_(e_emptykey));
7129 clear_tv(&tvkey);
7130 goto failret;
7131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133
7134 *arg = skipwhite(*arg + 1);
7135 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7136 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007137 if (evaluate)
7138 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 goto failret;
7140 }
7141 if (evaluate)
7142 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007143 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 if (item != NULL)
7145 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007146 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148 clear_tv(&tv);
7149 goto failret;
7150 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 item = dictitem_alloc(key);
7152 clear_tv(&tvkey);
7153 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007156 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 if (dict_add(d, item) == FAIL)
7158 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159 }
7160 }
7161
7162 if (**arg == '}')
7163 break;
7164 if (**arg != ',')
7165 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007166 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167 goto failret;
7168 }
7169 *arg = skipwhite(*arg + 1);
7170 }
7171
7172 if (**arg != '}')
7173 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007174 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175failret:
7176 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007177 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178 return FAIL;
7179 }
7180
7181 *arg = skipwhite(*arg + 1);
7182 if (evaluate)
7183 {
7184 rettv->v_type = VAR_DICT;
7185 rettv->vval.v_dict = d;
7186 ++d->dv_refcount;
7187 }
7188
7189 return OK;
7190}
7191
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007193 * Return a string with the string representation of a variable.
7194 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007195 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007196 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007197 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007198 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007199 */
7200 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007201echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007203 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007204 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007205 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207 static int recurse = 0;
7208 char_u *r = NULL;
7209
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007212 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 *tofree = NULL;
7214 return NULL;
7215 }
7216 ++recurse;
7217
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007218 switch (tv->v_type)
7219 {
7220 case VAR_FUNC:
7221 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222 r = tv->vval.v_string;
7223 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007224
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007225 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007226 if (tv->vval.v_list == NULL)
7227 {
7228 *tofree = NULL;
7229 r = NULL;
7230 }
7231 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7232 {
7233 *tofree = NULL;
7234 r = (char_u *)"[...]";
7235 }
7236 else
7237 {
7238 tv->vval.v_list->lv_copyID = copyID;
7239 *tofree = list2string(tv, copyID);
7240 r = *tofree;
7241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007243
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007245 if (tv->vval.v_dict == NULL)
7246 {
7247 *tofree = NULL;
7248 r = NULL;
7249 }
7250 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7251 {
7252 *tofree = NULL;
7253 r = (char_u *)"{...}";
7254 }
7255 else
7256 {
7257 tv->vval.v_dict->dv_copyID = copyID;
7258 *tofree = dict2string(tv, copyID);
7259 r = *tofree;
7260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007261 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007262
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007263 case VAR_STRING:
7264 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007265 *tofree = NULL;
7266 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007267 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007269#ifdef FEAT_FLOAT
7270 case VAR_FLOAT:
7271 *tofree = NULL;
7272 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7273 r = numbuf;
7274 break;
7275#endif
7276
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007278 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281
7282 --recurse;
7283 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007284}
7285
7286/*
7287 * Return a string with the string representation of a variable.
7288 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7289 * "numbuf" is used for a number.
7290 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007291 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007292 */
7293 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007294tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007296 char_u **tofree;
7297 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007298 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007299{
7300 switch (tv->v_type)
7301 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007302 case VAR_FUNC:
7303 *tofree = string_quote(tv->vval.v_string, TRUE);
7304 return *tofree;
7305 case VAR_STRING:
7306 *tofree = string_quote(tv->vval.v_string, FALSE);
7307 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007308#ifdef FEAT_FLOAT
7309 case VAR_FLOAT:
7310 *tofree = NULL;
7311 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7312 return numbuf;
7313#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007315 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007318 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007320 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007321 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007322}
7323
7324/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 * Return string "str" in ' quotes, doubling ' characters.
7326 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007328 */
7329 static char_u *
7330string_quote(str, function)
7331 char_u *str;
7332 int function;
7333{
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007335 char_u *p, *r, *s;
7336
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 len = (function ? 13 : 3);
7338 if (str != NULL)
7339 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007340 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 for (p = str; *p != NUL; mb_ptr_adv(p))
7342 if (*p == '\'')
7343 ++len;
7344 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007345 s = r = alloc(len);
7346 if (r != NULL)
7347 {
7348 if (function)
7349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007351 r += 10;
7352 }
7353 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 if (str != NULL)
7356 for (p = str; *p != NUL; )
7357 {
7358 if (*p == '\'')
7359 *r++ = '\'';
7360 MB_COPY_CHAR(p, r);
7361 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007363 if (function)
7364 *r++ = ')';
7365 *r++ = NUL;
7366 }
7367 return s;
7368}
7369
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007370#ifdef FEAT_FLOAT
7371/*
7372 * Convert the string "text" to a floating point number.
7373 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7374 * this always uses a decimal point.
7375 * Returns the length of the text that was consumed.
7376 */
7377 static int
7378string2float(text, value)
7379 char_u *text;
7380 float_T *value; /* result stored here */
7381{
7382 char *s = (char *)text;
7383 float_T f;
7384
7385 f = strtod(s, &s);
7386 *value = f;
7387 return (int)((char_u *)s - text);
7388}
7389#endif
7390
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 * Get the value of an environment variable.
7393 * "arg" is pointing to the '$'. It is advanced to after the name.
7394 * If the environment variable was not set, silently assume it is empty.
7395 * Always return OK.
7396 */
7397 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007398get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 int evaluate;
7402{
7403 char_u *string = NULL;
7404 int len;
7405 int cc;
7406 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007407 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
7409 ++*arg;
7410 name = *arg;
7411 len = get_env_len(arg);
7412 if (evaluate)
7413 {
7414 if (len != 0)
7415 {
7416 cc = name[len];
7417 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007418 /* first try vim_getenv(), fast for normal environment vars */
7419 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007421 {
7422 if (!mustfree)
7423 string = vim_strsave(string);
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 else
7426 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007427 if (mustfree)
7428 vim_free(string);
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 /* next try expanding things like $VIM and ${HOME} */
7431 string = expand_env_save(name - 1);
7432 if (string != NULL && *string == '$')
7433 {
7434 vim_free(string);
7435 string = NULL;
7436 }
7437 }
7438 name[len] = cc;
7439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 rettv->v_type = VAR_STRING;
7441 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443
7444 return OK;
7445}
7446
7447/*
7448 * Array with names and number of arguments of all internal functions
7449 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7450 */
7451static struct fst
7452{
7453 char *f_name; /* function name */
7454 char f_min_argc; /* minimal number of arguments */
7455 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007457 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458} functions[] =
7459{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007460#ifdef FEAT_FLOAT
7461 {"abs", 1, 1, f_abs},
7462#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007463 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {"append", 2, 2, f_append},
7465 {"argc", 0, 0, f_argc},
7466 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007467 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007468#ifdef FEAT_FLOAT
7469 {"atan", 1, 1, f_atan},
7470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007472 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {"bufexists", 1, 1, f_bufexists},
7474 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7475 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7476 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7477 {"buflisted", 1, 1, f_buflisted},
7478 {"bufloaded", 1, 1, f_bufloaded},
7479 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007480 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {"bufwinnr", 1, 1, f_bufwinnr},
7482 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007483 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007484 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007485#ifdef FEAT_FLOAT
7486 {"ceil", 1, 1, f_ceil},
7487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007488 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {"char2nr", 1, 1, f_char2nr},
7490 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007491 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007494 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007495 {"complete_add", 1, 1, f_complete_add},
7496 {"complete_check", 0, 0, f_complete_check},
7497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007499 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007500#ifdef FEAT_FLOAT
7501 {"cos", 1, 1, f_cos},
7502#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007503 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007505 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007506 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {"delete", 1, 1, f_delete},
7508 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007509 {"diff_filler", 1, 1, f_diff_filler},
7510 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007511 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007513 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 {"eventhandler", 0, 0, f_eventhandler},
7515 {"executable", 1, 1, f_executable},
7516 {"exists", 1, 1, f_exists},
7517 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007518 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007519 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7521 {"filereadable", 1, 1, f_filereadable},
7522 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007523 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007524 {"finddir", 1, 3, f_finddir},
7525 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007526#ifdef FEAT_FLOAT
7527 {"float2nr", 1, 1, f_float2nr},
7528 {"floor", 1, 1, f_floor},
7529#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007530 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {"fnamemodify", 2, 2, f_fnamemodify},
7532 {"foldclosed", 1, 1, f_foldclosed},
7533 {"foldclosedend", 1, 1, f_foldclosedend},
7534 {"foldlevel", 1, 1, f_foldlevel},
7535 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007536 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007538 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007539 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007540 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007541 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {"getbufvar", 2, 2, f_getbufvar},
7543 {"getchar", 0, 1, f_getchar},
7544 {"getcharmod", 0, 0, f_getcharmod},
7545 {"getcmdline", 0, 0, f_getcmdline},
7546 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007547 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007549 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007550 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {"getfsize", 1, 1, f_getfsize},
7552 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007553 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007554 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007555 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007556 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007557 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007558 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007559 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007560 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007562 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {"getwinposx", 0, 0, f_getwinposx},
7564 {"getwinposy", 0, 0, f_getwinposy},
7565 {"getwinvar", 2, 2, f_getwinvar},
7566 {"glob", 1, 1, f_glob},
7567 {"globpath", 2, 2, f_globpath},
7568 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007569 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007570 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007571 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7573 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7574 {"histadd", 2, 2, f_histadd},
7575 {"histdel", 1, 2, f_histdel},
7576 {"histget", 1, 2, f_histget},
7577 {"histnr", 1, 1, f_histnr},
7578 {"hlID", 1, 1, f_hlID},
7579 {"hlexists", 1, 1, f_hlexists},
7580 {"hostname", 0, 0, f_hostname},
7581 {"iconv", 3, 3, f_iconv},
7582 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007583 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007584 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007586 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {"inputrestore", 0, 0, f_inputrestore},
7588 {"inputsave", 0, 0, f_inputsave},
7589 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007590 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007592 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007594 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007597 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {"libcall", 3, 3, f_libcall},
7599 {"libcallnr", 3, 3, f_libcallnr},
7600 {"line", 1, 1, f_line},
7601 {"line2byte", 1, 1, f_line2byte},
7602 {"lispindent", 1, 1, f_lispindent},
7603 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007604#ifdef FEAT_FLOAT
7605 {"log10", 1, 1, f_log10},
7606#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007607 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007608 {"maparg", 1, 3, f_maparg},
7609 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007610 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007611 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007612 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007613 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007614 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007615 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007616 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007617 {"max", 1, 1, f_max},
7618 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007619#ifdef vim_mkdir
7620 {"mkdir", 1, 3, f_mkdir},
7621#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007622 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {"nextnonblank", 1, 1, f_nextnonblank},
7624 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007625 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007626#ifdef FEAT_FLOAT
7627 {"pow", 2, 2, f_pow},
7628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007630 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007631 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007633 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007634 {"reltime", 0, 2, f_reltime},
7635 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {"remote_expr", 2, 3, f_remote_expr},
7637 {"remote_foreground", 1, 1, f_remote_foreground},
7638 {"remote_peek", 1, 2, f_remote_peek},
7639 {"remote_read", 1, 1, f_remote_read},
7640 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007641 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007643 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007646#ifdef FEAT_FLOAT
7647 {"round", 1, 1, f_round},
7648#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007649 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007650 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007651 {"searchpair", 3, 7, f_searchpair},
7652 {"searchpairpos", 3, 7, f_searchpairpos},
7653 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {"server2client", 2, 2, f_server2client},
7655 {"serverlist", 0, 0, f_serverlist},
7656 {"setbufvar", 3, 3, f_setbufvar},
7657 {"setcmdpos", 1, 1, f_setcmdpos},
7658 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007659 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007660 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007661 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007662 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007664 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007666 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007668#ifdef FEAT_FLOAT
7669 {"sin", 1, 1, f_sin},
7670#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007671 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007672 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007673 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007674 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007675 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007676#ifdef FEAT_FLOAT
7677 {"sqrt", 1, 1, f_sqrt},
7678 {"str2float", 1, 1, f_str2float},
7679#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007680 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681#ifdef HAVE_STRFTIME
7682 {"strftime", 1, 2, f_strftime},
7683#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007684 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007685 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {"strlen", 1, 1, f_strlen},
7687 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007688 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {"strtrans", 1, 1, f_strtrans},
7690 {"submatch", 1, 1, f_submatch},
7691 {"substitute", 4, 4, f_substitute},
7692 {"synID", 3, 3, f_synID},
7693 {"synIDattr", 2, 3, f_synIDattr},
7694 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007695 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007696 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007697 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007698 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007699 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007700 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007701 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007703 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {"tolower", 1, 1, f_tolower},
7705 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007706 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007707#ifdef FEAT_FLOAT
7708 {"trunc", 1, 1, f_trunc},
7709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"virtcol", 1, 1, f_virtcol},
7713 {"visualmode", 0, 1, f_visualmode},
7714 {"winbufnr", 1, 1, f_winbufnr},
7715 {"wincol", 0, 0, f_wincol},
7716 {"winheight", 1, 1, f_winheight},
7717 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007718 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007720 {"winrestview", 1, 1, f_winrestview},
7721 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007723 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724};
7725
7726#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7727
7728/*
7729 * Function given to ExpandGeneric() to obtain the list of internal
7730 * or user defined function names.
7731 */
7732 char_u *
7733get_function_name(xp, idx)
7734 expand_T *xp;
7735 int idx;
7736{
7737 static int intidx = -1;
7738 char_u *name;
7739
7740 if (idx == 0)
7741 intidx = -1;
7742 if (intidx < 0)
7743 {
7744 name = get_user_func_name(xp, idx);
7745 if (name != NULL)
7746 return name;
7747 }
7748 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7749 {
7750 STRCPY(IObuff, functions[intidx].f_name);
7751 STRCAT(IObuff, "(");
7752 if (functions[intidx].f_max_argc == 0)
7753 STRCAT(IObuff, ")");
7754 return IObuff;
7755 }
7756
7757 return NULL;
7758}
7759
7760/*
7761 * Function given to ExpandGeneric() to obtain the list of internal or
7762 * user defined variable or function names.
7763 */
7764/*ARGSUSED*/
7765 char_u *
7766get_expr_name(xp, idx)
7767 expand_T *xp;
7768 int idx;
7769{
7770 static int intidx = -1;
7771 char_u *name;
7772
7773 if (idx == 0)
7774 intidx = -1;
7775 if (intidx < 0)
7776 {
7777 name = get_function_name(xp, idx);
7778 if (name != NULL)
7779 return name;
7780 }
7781 return get_user_var_name(xp, ++intidx);
7782}
7783
7784#endif /* FEAT_CMDL_COMPL */
7785
7786/*
7787 * Find internal function in table above.
7788 * Return index, or -1 if not found
7789 */
7790 static int
7791find_internal_func(name)
7792 char_u *name; /* name of the function */
7793{
7794 int first = 0;
7795 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7796 int cmp;
7797 int x;
7798
7799 /*
7800 * Find the function name in the table. Binary search.
7801 */
7802 while (first <= last)
7803 {
7804 x = first + ((unsigned)(last - first) >> 1);
7805 cmp = STRCMP(name, functions[x].f_name);
7806 if (cmp < 0)
7807 last = x - 1;
7808 else if (cmp > 0)
7809 first = x + 1;
7810 else
7811 return x;
7812 }
7813 return -1;
7814}
7815
7816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007817 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7818 * name it contains, otherwise return "name".
7819 */
7820 static char_u *
7821deref_func_name(name, lenp)
7822 char_u *name;
7823 int *lenp;
7824{
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826 int cc;
7827
7828 cc = name[*lenp];
7829 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007830 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007831 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 {
7836 *lenp = 0;
7837 return (char_u *)""; /* just in case */
7838 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007839 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 }
7842
7843 return name;
7844}
7845
7846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 * Allocate a variable for the result of a function.
7848 * Return OK or FAIL.
7849 */
7850 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7852 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 char_u *name; /* name of the function */
7854 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 char_u **arg; /* argument, pointing to the '(' */
7857 linenr_T firstline; /* first line of range */
7858 linenr_T lastline; /* last line of range */
7859 int *doesrange; /* return: function handled range */
7860 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 char_u *argp;
7864 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007865 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 int argcount = 0; /* number of arguments found */
7867
7868 /*
7869 * Get the arguments.
7870 */
7871 argp = *arg;
7872 while (argcount < MAX_FUNC_ARGS)
7873 {
7874 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7875 if (*argp == ')' || *argp == ',' || *argp == NUL)
7876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7878 {
7879 ret = FAIL;
7880 break;
7881 }
7882 ++argcount;
7883 if (*argp != ',')
7884 break;
7885 }
7886 if (*argp == ')')
7887 ++argp;
7888 else
7889 ret = FAIL;
7890
7891 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 {
7896 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007897 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007898 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007899 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
7902 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
7905 *arg = skipwhite(argp);
7906 return ret;
7907}
7908
7909
7910/*
7911 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007912 * Return OK when the function can't be called, FAIL otherwise.
7913 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 */
7915 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 char_u *name; /* name of the function */
7919 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007922 typval_T *argvars; /* vars for arguments, must have "argcount"
7923 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 linenr_T firstline; /* first line of range */
7925 linenr_T lastline; /* last line of range */
7926 int *doesrange; /* return: function handled range */
7927 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931#define ERROR_UNKNOWN 0
7932#define ERROR_TOOMANY 1
7933#define ERROR_TOOFEW 2
7934#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935#define ERROR_DICT 4
7936#define ERROR_NONE 5
7937#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 int error = ERROR_NONE;
7939 int i;
7940 int llen;
7941 ufunc_T *fp;
7942 int cc;
7943#define FLEN_FIXED 40
7944 char_u fname_buf[FLEN_FIXED + 1];
7945 char_u *fname;
7946
7947 /*
7948 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7949 * Change <SNR>123_name() to K_SNR 123_name().
7950 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7951 */
7952 cc = name[len];
7953 name[len] = NUL;
7954 llen = eval_fname_script(name);
7955 if (llen > 0)
7956 {
7957 fname_buf[0] = K_SPECIAL;
7958 fname_buf[1] = KS_EXTRA;
7959 fname_buf[2] = (int)KE_SNR;
7960 i = 3;
7961 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7962 {
7963 if (current_SID <= 0)
7964 error = ERROR_SCRIPT;
7965 else
7966 {
7967 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7968 i = (int)STRLEN(fname_buf);
7969 }
7970 }
7971 if (i + STRLEN(name + llen) < FLEN_FIXED)
7972 {
7973 STRCPY(fname_buf + i, name + llen);
7974 fname = fname_buf;
7975 }
7976 else
7977 {
7978 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7979 if (fname == NULL)
7980 error = ERROR_OTHER;
7981 else
7982 {
7983 mch_memmove(fname, fname_buf, (size_t)i);
7984 STRCPY(fname + i, name + llen);
7985 }
7986 }
7987 }
7988 else
7989 fname = name;
7990
7991 *doesrange = FALSE;
7992
7993
7994 /* execute the function if no errors detected and executing */
7995 if (evaluate && error == ERROR_NONE)
7996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 error = ERROR_UNKNOWN;
7999
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008000 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
8002 /*
8003 * User defined function.
8004 */
8005 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008006
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008008 /* Trigger FuncUndefined event, may load the function. */
8009 if (fp == NULL
8010 && apply_autocmds(EVENT_FUNCUNDEFINED,
8011 fname, fname, TRUE, NULL)
8012 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008014 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 fp = find_func(fname);
8016 }
8017#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008018 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008019 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008020 {
8021 /* loaded a package, search for the function again */
8022 fp = find_func(fname);
8023 }
8024
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (fp != NULL)
8026 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008027 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008029 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008031 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008033 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008034 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 else
8036 {
8037 /*
8038 * Call the user function.
8039 * Save and restore search patterns, script variables and
8040 * redo buffer.
8041 */
8042 save_search_patterns();
8043 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008044 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008046 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008047 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8048 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8049 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008050 /* Function was unreferenced while being used, free it
8051 * now. */
8052 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 restoreRedobuff();
8054 restore_search_patterns();
8055 error = ERROR_NONE;
8056 }
8057 }
8058 }
8059 else
8060 {
8061 /*
8062 * Find the function name in the table, call its implementation.
8063 */
8064 i = find_internal_func(fname);
8065 if (i >= 0)
8066 {
8067 if (argcount < functions[i].f_min_argc)
8068 error = ERROR_TOOFEW;
8069 else if (argcount > functions[i].f_max_argc)
8070 error = ERROR_TOOMANY;
8071 else
8072 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008073 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 error = ERROR_NONE;
8076 }
8077 }
8078 }
8079 /*
8080 * The function call (or "FuncUndefined" autocommand sequence) might
8081 * have been aborted by an error, an interrupt, or an explicitly thrown
8082 * exception that has not been caught so far. This situation can be
8083 * tested for by calling aborting(). For an error in an internal
8084 * function or for the "E132" error in call_user_func(), however, the
8085 * throw point at which the "force_abort" flag (temporarily reset by
8086 * emsg()) is normally updated has not been reached yet. We need to
8087 * update that flag first to make aborting() reliable.
8088 */
8089 update_force_abort();
8090 }
8091 if (error == ERROR_NONE)
8092 ret = OK;
8093
8094 /*
8095 * Report an error unless the argument evaluation or function call has been
8096 * cancelled due to an aborting error, an interrupt, or an exception.
8097 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008098 if (!aborting())
8099 {
8100 switch (error)
8101 {
8102 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008103 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008104 break;
8105 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008106 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008107 break;
8108 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008109 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008110 name);
8111 break;
8112 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008113 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008114 name);
8115 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008116 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008117 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008118 name);
8119 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008120 }
8121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122
8123 name[len] = cc;
8124 if (fname != name && fname != fname_buf)
8125 vim_free(fname);
8126
8127 return ret;
8128}
8129
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008130/*
8131 * Give an error message with a function name. Handle <SNR> things.
8132 */
8133 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008134emsg_funcname(ermsg, name)
8135 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008136 char_u *name;
8137{
8138 char_u *p;
8139
8140 if (*name == K_SPECIAL)
8141 p = concat_str((char_u *)"<SNR>", name + 3);
8142 else
8143 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008144 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008145 if (p != name)
8146 vim_free(p);
8147}
8148
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008149/*
8150 * Return TRUE for a non-zero Number and a non-empty String.
8151 */
8152 static int
8153non_zero_arg(argvars)
8154 typval_T *argvars;
8155{
8156 return ((argvars[0].v_type == VAR_NUMBER
8157 && argvars[0].vval.v_number != 0)
8158 || (argvars[0].v_type == VAR_STRING
8159 && argvars[0].vval.v_string != NULL
8160 && *argvars[0].vval.v_string != NUL));
8161}
8162
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163/*********************************************
8164 * Implementation of the built-in functions
8165 */
8166
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167#ifdef FEAT_FLOAT
8168/*
8169 * "abs(expr)" function
8170 */
8171 static void
8172f_abs(argvars, rettv)
8173 typval_T *argvars;
8174 typval_T *rettv;
8175{
8176 if (argvars[0].v_type == VAR_FLOAT)
8177 {
8178 rettv->v_type = VAR_FLOAT;
8179 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8180 }
8181 else
8182 {
8183 varnumber_T n;
8184 int error = FALSE;
8185
8186 n = get_tv_number_chk(&argvars[0], &error);
8187 if (error)
8188 rettv->vval.v_number = -1;
8189 else if (n > 0)
8190 rettv->vval.v_number = n;
8191 else
8192 rettv->vval.v_number = -n;
8193 }
8194}
8195#endif
8196
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008198 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 */
8200 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 typval_T *argvars;
8203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008207 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008210 if ((l = argvars[0].vval.v_list) != NULL
8211 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8212 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008213 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008214 }
8215 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008216 EMSG(_(e_listreq));
8217}
8218
8219/*
8220 * "append(lnum, string/list)" function
8221 */
8222 static void
8223f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 typval_T *argvars;
8225 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008226{
8227 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008228 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 list_T *l = NULL;
8230 listitem_T *li = NULL;
8231 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008232 long added = 0;
8233
Bram Moolenaar0d660222005-01-07 21:51:51 +00008234 lnum = get_tv_lnum(argvars);
8235 if (lnum >= 0
8236 && lnum <= curbuf->b_ml.ml_line_count
8237 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008238 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008239 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008241 l = argvars[1].vval.v_list;
8242 if (l == NULL)
8243 return;
8244 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008246 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00008247 for (;;)
8248 {
8249 if (l == NULL)
8250 tv = &argvars[1]; /* append a string */
8251 else if (li == NULL)
8252 break; /* end of list */
8253 else
8254 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 line = get_tv_string_chk(tv);
8256 if (line == NULL) /* type error */
8257 {
8258 rettv->vval.v_number = 1; /* Failed */
8259 break;
8260 }
8261 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008262 ++added;
8263 if (l == NULL)
8264 break;
8265 li = li->li_next;
8266 }
8267
8268 appended_lines_mark(lnum, added);
8269 if (curwin->w_cursor.lnum > lnum)
8270 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008272 else
8273 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274}
8275
8276/*
8277 * "argc()" function
8278 */
8279/* ARGSUSED */
8280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008281f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 typval_T *argvars;
8283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286}
8287
8288/*
8289 * "argidx()" function
8290 */
8291/* ARGSUSED */
8292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 typval_T *argvars;
8295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298}
8299
8300/*
8301 * "argv(nr)" function
8302 */
8303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 typval_T *argvars;
8306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307{
8308 int idx;
8309
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008310 if (argvars[0].v_type != VAR_UNKNOWN)
8311 {
8312 idx = get_tv_number_chk(&argvars[0], NULL);
8313 if (idx >= 0 && idx < ARGCOUNT)
8314 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8315 else
8316 rettv->vval.v_string = NULL;
8317 rettv->v_type = VAR_STRING;
8318 }
8319 else if (rettv_list_alloc(rettv) == OK)
8320 for (idx = 0; idx < ARGCOUNT; ++idx)
8321 list_append_string(rettv->vval.v_list,
8322 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323}
8324
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008325#ifdef FEAT_FLOAT
8326static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8327
8328/*
8329 * Get the float value of "argvars[0]" into "f".
8330 * Returns FAIL when the argument is not a Number or Float.
8331 */
8332 static int
8333get_float_arg(argvars, f)
8334 typval_T *argvars;
8335 float_T *f;
8336{
8337 if (argvars[0].v_type == VAR_FLOAT)
8338 {
8339 *f = argvars[0].vval.v_float;
8340 return OK;
8341 }
8342 if (argvars[0].v_type == VAR_NUMBER)
8343 {
8344 *f = (float_T)argvars[0].vval.v_number;
8345 return OK;
8346 }
8347 EMSG(_("E808: Number or Float required"));
8348 return FAIL;
8349}
8350
8351/*
8352 * "atan()" function
8353 */
8354 static void
8355f_atan(argvars, rettv)
8356 typval_T *argvars;
8357 typval_T *rettv;
8358{
8359 float_T f;
8360
8361 rettv->v_type = VAR_FLOAT;
8362 if (get_float_arg(argvars, &f) == OK)
8363 rettv->vval.v_float = atan(f);
8364 else
8365 rettv->vval.v_float = 0.0;
8366}
8367#endif
8368
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369/*
8370 * "browse(save, title, initdir, default)" function
8371 */
8372/* ARGSUSED */
8373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008374f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 typval_T *argvars;
8376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377{
8378#ifdef FEAT_BROWSE
8379 int save;
8380 char_u *title;
8381 char_u *initdir;
8382 char_u *defname;
8383 char_u buf[NUMBUFLEN];
8384 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008387 save = get_tv_number_chk(&argvars[0], &error);
8388 title = get_tv_string_chk(&argvars[1]);
8389 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8390 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008392 if (error || title == NULL || initdir == NULL || defname == NULL)
8393 rettv->vval.v_string = NULL;
8394 else
8395 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008396 do_browse(save ? BROWSE_SAVE : 0,
8397 title, defname, NULL, initdir, NULL, curbuf);
8398#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008400#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008402}
8403
8404/*
8405 * "browsedir(title, initdir)" function
8406 */
8407/* ARGSUSED */
8408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008409f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 typval_T *argvars;
8411 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008412{
8413#ifdef FEAT_BROWSE
8414 char_u *title;
8415 char_u *initdir;
8416 char_u buf[NUMBUFLEN];
8417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008418 title = get_tv_string_chk(&argvars[0]);
8419 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008421 if (title == NULL || initdir == NULL)
8422 rettv->vval.v_string = NULL;
8423 else
8424 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008425 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430}
8431
Bram Moolenaar33570922005-01-25 22:26:29 +00008432static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008433
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434/*
8435 * Find a buffer by number or exact name.
8436 */
8437 static buf_T *
8438find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008443 if (avar->v_type == VAR_NUMBER)
8444 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008445 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008447 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008448 if (buf == NULL)
8449 {
8450 /* No full path name match, try a match with a URL or a "nofile"
8451 * buffer, these don't use the full path. */
8452 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8453 if (buf->b_fname != NULL
8454 && (path_with_url(buf->b_fname)
8455#ifdef FEAT_QUICKFIX
8456 || bt_nofile(buf)
8457#endif
8458 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008459 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008460 break;
8461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 }
8463 return buf;
8464}
8465
8466/*
8467 * "bufexists(expr)" function
8468 */
8469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008470f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 typval_T *argvars;
8472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475}
8476
8477/*
8478 * "buflisted(expr)" function
8479 */
8480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 buf_T *buf;
8486
8487 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489}
8490
8491/*
8492 * "bufloaded(expr)" function
8493 */
8494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008495f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 typval_T *argvars;
8497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
8499 buf_T *buf;
8500
8501 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503}
8504
Bram Moolenaar33570922005-01-25 22:26:29 +00008505static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008506
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507/*
8508 * Get buffer by number or pattern.
8509 */
8510 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008512 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 int save_magic;
8516 char_u *save_cpo;
8517 buf_T *buf;
8518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 if (tv->v_type == VAR_NUMBER)
8520 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008521 if (tv->v_type != VAR_STRING)
8522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (name == NULL || *name == NUL)
8524 return curbuf;
8525 if (name[0] == '$' && name[1] == NUL)
8526 return lastbuf;
8527
8528 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8529 save_magic = p_magic;
8530 p_magic = TRUE;
8531 save_cpo = p_cpo;
8532 p_cpo = (char_u *)"";
8533
8534 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8535 TRUE, FALSE));
8536
8537 p_magic = save_magic;
8538 p_cpo = save_cpo;
8539
8540 /* If not found, try expanding the name, like done for bufexists(). */
8541 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008542 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543
8544 return buf;
8545}
8546
8547/*
8548 * "bufname(expr)" function
8549 */
8550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008552 typval_T *argvars;
8553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
8555 buf_T *buf;
8556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008557 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559 buf = get_buf_tv(&argvars[0]);
8560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 --emsg_off;
8566}
8567
8568/*
8569 * "bufnr(expr)" function
8570 */
8571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 typval_T *argvars;
8574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008577 int error = FALSE;
8578 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008580 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008583 --emsg_off;
8584
8585 /* If the buffer isn't found and the second argument is not zero create a
8586 * new buffer. */
8587 if (buf == NULL
8588 && argvars[1].v_type != VAR_UNKNOWN
8589 && get_tv_number_chk(&argvars[1], &error) != 0
8590 && !error
8591 && (name = get_tv_string_chk(&argvars[0])) != NULL
8592 && !error)
8593 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599}
8600
8601/*
8602 * "bufwinnr(nr)" function
8603 */
8604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008605f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *argvars;
8607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609#ifdef FEAT_WINDOWS
8610 win_T *wp;
8611 int winnr = 0;
8612#endif
8613 buf_T *buf;
8614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008615 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618#ifdef FEAT_WINDOWS
8619 for (wp = firstwin; wp; wp = wp->w_next)
8620 {
8621 ++winnr;
8622 if (wp->w_buffer == buf)
8623 break;
8624 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628#endif
8629 --emsg_off;
8630}
8631
8632/*
8633 * "byte2line(byte)" function
8634 */
8635/*ARGSUSED*/
8636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 typval_T *argvars;
8639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#else
8644 long boff = 0;
8645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008648 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 (linenr_T)0, &boff);
8652#endif
8653}
8654
8655/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008656 * "byteidx()" function
8657 */
8658/*ARGSUSED*/
8659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 typval_T *argvars;
8662 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008663{
8664#ifdef FEAT_MBYTE
8665 char_u *t;
8666#endif
8667 char_u *str;
8668 long idx;
8669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008670 str = get_tv_string_chk(&argvars[0]);
8671 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008673 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008674 return;
8675
8676#ifdef FEAT_MBYTE
8677 t = str;
8678 for ( ; idx > 0; idx--)
8679 {
8680 if (*t == NUL) /* EOL reached */
8681 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008682 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008683 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008684 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008685#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008687 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008688#endif
8689}
8690
8691/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008692 * "call(func, arglist)" function
8693 */
8694 static void
8695f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *argvars;
8697 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008698{
8699 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008700 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008701 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008703 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008705
8706 rettv->vval.v_number = 0;
8707 if (argvars[1].v_type != VAR_LIST)
8708 {
8709 EMSG(_(e_listreq));
8710 return;
8711 }
8712 if (argvars[1].vval.v_list == NULL)
8713 return;
8714
8715 if (argvars[0].v_type == VAR_FUNC)
8716 func = argvars[0].vval.v_string;
8717 else
8718 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 if (*func == NUL)
8720 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008721
Bram Moolenaare9a41262005-01-15 22:18:47 +00008722 if (argvars[2].v_type != VAR_UNKNOWN)
8723 {
8724 if (argvars[2].v_type != VAR_DICT)
8725 {
8726 EMSG(_(e_dictreq));
8727 return;
8728 }
8729 selfdict = argvars[2].vval.v_dict;
8730 }
8731
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008732 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8733 item = item->li_next)
8734 {
8735 if (argc == MAX_FUNC_ARGS)
8736 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008737 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008738 break;
8739 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008740 /* Make a copy of each argument. This is needed to be able to set
8741 * v_lock to VAR_FIXED in the copy without changing the original list.
8742 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008743 copy_tv(&item->li_tv, &argv[argc++]);
8744 }
8745
8746 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008747 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8749 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008750
8751 /* Free the arguments. */
8752 while (argc > 0)
8753 clear_tv(&argv[--argc]);
8754}
8755
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008756#ifdef FEAT_FLOAT
8757/*
8758 * "ceil({float})" function
8759 */
8760 static void
8761f_ceil(argvars, rettv)
8762 typval_T *argvars;
8763 typval_T *rettv;
8764{
8765 float_T f;
8766
8767 rettv->v_type = VAR_FLOAT;
8768 if (get_float_arg(argvars, &f) == OK)
8769 rettv->vval.v_float = ceil(f);
8770 else
8771 rettv->vval.v_float = 0.0;
8772}
8773#endif
8774
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008775/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008776 * "changenr()" function
8777 */
8778/*ARGSUSED*/
8779 static void
8780f_changenr(argvars, rettv)
8781 typval_T *argvars;
8782 typval_T *rettv;
8783{
8784 rettv->vval.v_number = curbuf->b_u_seq_cur;
8785}
8786
8787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 * "char2nr(string)" function
8789 */
8790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *argvars;
8793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795#ifdef FEAT_MBYTE
8796 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008797 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 else
8799#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801}
8802
8803/*
8804 * "cindent(lnum)" function
8805 */
8806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 typval_T *argvars;
8809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
8811#ifdef FEAT_CINDENT
8812 pos_T pos;
8813 linenr_T lnum;
8814
8815 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8818 {
8819 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 curwin->w_cursor = pos;
8822 }
8823 else
8824#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008829 * "clearmatches()" function
8830 */
8831/*ARGSUSED*/
8832 static void
8833f_clearmatches(argvars, rettv)
8834 typval_T *argvars;
8835 typval_T *rettv;
8836{
8837#ifdef FEAT_SEARCH_EXTRA
8838 clear_matches(curwin);
8839#endif
8840}
8841
8842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 * "col(string)" function
8844 */
8845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *argvars;
8848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 colnr_T col = 0;
8851 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008852 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008854 fp = var2fpos(&argvars[0], FALSE, &fnum);
8855 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 {
8857 if (fp->col == MAXCOL)
8858 {
8859 /* '> can be MAXCOL, get the length of the line then */
8860 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008861 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 else
8863 col = MAXCOL;
8864 }
8865 else
8866 {
8867 col = fp->col + 1;
8868#ifdef FEAT_VIRTUALEDIT
8869 /* col(".") when the cursor is on the NUL at the end of the line
8870 * because of "coladd" can be seen as an extra column. */
8871 if (virtual_active() && fp == &curwin->w_cursor)
8872 {
8873 char_u *p = ml_get_cursor();
8874
8875 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8876 curwin->w_virtcol - curwin->w_cursor.coladd))
8877 {
8878# ifdef FEAT_MBYTE
8879 int l;
8880
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008881 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 col += l;
8883# else
8884 if (*p != NUL && p[1] == NUL)
8885 ++col;
8886# endif
8887 }
8888 }
8889#endif
8890 }
8891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893}
8894
Bram Moolenaar572cb562005-08-05 21:35:02 +00008895#if defined(FEAT_INS_EXPAND)
8896/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008897 * "complete()" function
8898 */
8899/*ARGSUSED*/
8900 static void
8901f_complete(argvars, rettv)
8902 typval_T *argvars;
8903 typval_T *rettv;
8904{
8905 int startcol;
8906
8907 if ((State & INSERT) == 0)
8908 {
8909 EMSG(_("E785: complete() can only be used in Insert mode"));
8910 return;
8911 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008912
8913 /* Check for undo allowed here, because if something was already inserted
8914 * the line was already saved for undo and this check isn't done. */
8915 if (!undo_allowed())
8916 return;
8917
Bram Moolenaarade00832006-03-10 21:46:58 +00008918 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8919 {
8920 EMSG(_(e_invarg));
8921 return;
8922 }
8923
8924 startcol = get_tv_number_chk(&argvars[0], NULL);
8925 if (startcol <= 0)
8926 return;
8927
8928 set_completion(startcol - 1, argvars[1].vval.v_list);
8929}
8930
8931/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008932 * "complete_add()" function
8933 */
8934/*ARGSUSED*/
8935 static void
8936f_complete_add(argvars, rettv)
8937 typval_T *argvars;
8938 typval_T *rettv;
8939{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008940 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008941}
8942
8943/*
8944 * "complete_check()" function
8945 */
8946/*ARGSUSED*/
8947 static void
8948f_complete_check(argvars, rettv)
8949 typval_T *argvars;
8950 typval_T *rettv;
8951{
8952 int saved = RedrawingDisabled;
8953
8954 RedrawingDisabled = 0;
8955 ins_compl_check_keys(0);
8956 rettv->vval.v_number = compl_interrupted;
8957 RedrawingDisabled = saved;
8958}
8959#endif
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961/*
8962 * "confirm(message, buttons[, default [, type]])" function
8963 */
8964/*ARGSUSED*/
8965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008967 typval_T *argvars;
8968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8971 char_u *message;
8972 char_u *buttons = NULL;
8973 char_u buf[NUMBUFLEN];
8974 char_u buf2[NUMBUFLEN];
8975 int def = 1;
8976 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977 char_u *typestr;
8978 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 message = get_tv_string_chk(&argvars[0]);
8981 if (message == NULL)
8982 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8986 if (buttons == NULL)
8987 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008991 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008993 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8994 if (typestr == NULL)
8995 error = TRUE;
8996 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 switch (TOUPPER_ASC(*typestr))
8999 {
9000 case 'E': type = VIM_ERROR; break;
9001 case 'Q': type = VIM_QUESTION; break;
9002 case 'I': type = VIM_INFO; break;
9003 case 'W': type = VIM_WARNING; break;
9004 case 'G': type = VIM_GENERIC; break;
9005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 }
9007 }
9008 }
9009 }
9010
9011 if (buttons == NULL || *buttons == NUL)
9012 buttons = (char_u *)_("&Ok");
9013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 if (error)
9015 rettv->vval.v_number = 0;
9016 else
9017 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 def, NULL);
9019#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021#endif
9022}
9023
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009024/*
9025 * "copy()" function
9026 */
9027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009031{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009032 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009035#ifdef FEAT_FLOAT
9036/*
9037 * "cos()" function
9038 */
9039 static void
9040f_cos(argvars, rettv)
9041 typval_T *argvars;
9042 typval_T *rettv;
9043{
9044 float_T f;
9045
9046 rettv->v_type = VAR_FLOAT;
9047 if (get_float_arg(argvars, &f) == OK)
9048 rettv->vval.v_float = cos(f);
9049 else
9050 rettv->vval.v_float = 0.0;
9051}
9052#endif
9053
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009055 * "count()" function
9056 */
9057 static void
9058f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009061{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009062 long n = 0;
9063 int ic = FALSE;
9064
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009066 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 listitem_T *li;
9068 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009069 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009070
Bram Moolenaare9a41262005-01-15 22:18:47 +00009071 if ((l = argvars[0].vval.v_list) != NULL)
9072 {
9073 li = l->lv_first;
9074 if (argvars[2].v_type != VAR_UNKNOWN)
9075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009076 int error = FALSE;
9077
9078 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 if (argvars[3].v_type != VAR_UNKNOWN)
9080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081 idx = get_tv_number_chk(&argvars[3], &error);
9082 if (!error)
9083 {
9084 li = list_find(l, idx);
9085 if (li == NULL)
9086 EMSGN(_(e_listidx), idx);
9087 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 if (error)
9090 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009091 }
9092
9093 for ( ; li != NULL; li = li->li_next)
9094 if (tv_equal(&li->li_tv, &argvars[1], ic))
9095 ++n;
9096 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009098 else if (argvars[0].v_type == VAR_DICT)
9099 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 int todo;
9101 dict_T *d;
9102 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009103
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009104 if ((d = argvars[0].vval.v_dict) != NULL)
9105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 int error = FALSE;
9107
Bram Moolenaare9a41262005-01-15 22:18:47 +00009108 if (argvars[2].v_type != VAR_UNKNOWN)
9109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009111 if (argvars[3].v_type != VAR_UNKNOWN)
9112 EMSG(_(e_invarg));
9113 }
9114
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009115 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009117 {
9118 if (!HASHITEM_EMPTY(hi))
9119 {
9120 --todo;
9121 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9122 ++n;
9123 }
9124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009125 }
9126 }
9127 else
9128 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009129 rettv->vval.v_number = n;
9130}
9131
9132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9134 *
9135 * Checks the existence of a cscope connection.
9136 */
9137/*ARGSUSED*/
9138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009140 typval_T *argvars;
9141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142{
9143#ifdef FEAT_CSCOPE
9144 int num = 0;
9145 char_u *dbpath = NULL;
9146 char_u *prepend = NULL;
9147 char_u buf[NUMBUFLEN];
9148
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009149 if (argvars[0].v_type != VAR_UNKNOWN
9150 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 num = (int)get_tv_number(&argvars[0]);
9153 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009154 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 }
9157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161#endif
9162}
9163
9164/*
9165 * "cursor(lnum, col)" function
9166 *
9167 * Moves the cursor to the specified line and column
9168 */
9169/*ARGSUSED*/
9170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009172 typval_T *argvars;
9173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009176#ifdef FEAT_VIRTUALEDIT
9177 long coladd = 0;
9178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179
Bram Moolenaara5525202006-03-02 22:52:09 +00009180 if (argvars[1].v_type == VAR_UNKNOWN)
9181 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009182 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009183
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009184 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009185 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009186 line = pos.lnum;
9187 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009188#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009189 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009190#endif
9191 }
9192 else
9193 {
9194 line = get_tv_lnum(argvars);
9195 col = get_tv_number_chk(&argvars[1], NULL);
9196#ifdef FEAT_VIRTUALEDIT
9197 if (argvars[2].v_type != VAR_UNKNOWN)
9198 coladd = get_tv_number_chk(&argvars[2], NULL);
9199#endif
9200 }
9201 if (line < 0 || col < 0
9202#ifdef FEAT_VIRTUALEDIT
9203 || coladd < 0
9204#endif
9205 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 if (line > 0)
9208 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 if (col > 0)
9210 curwin->w_cursor.col = col - 1;
9211#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009212 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213#endif
9214
9215 /* Make sure the cursor is in a valid position. */
9216 check_cursor();
9217#ifdef FEAT_MBYTE
9218 /* Correct cursor for multi-byte character. */
9219 if (has_mbyte)
9220 mb_adjust_cursor();
9221#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009222
9223 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224}
9225
9226/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009227 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 */
9229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009231 typval_T *argvars;
9232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009234 int noref = 0;
9235
9236 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009238 if (noref < 0 || noref > 1)
9239 EMSG(_(e_invarg));
9240 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00009241 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242}
9243
9244/*
9245 * "delete()" function
9246 */
9247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009248f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009249 typval_T *argvars;
9250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251{
9252 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256}
9257
9258/*
9259 * "did_filetype()" function
9260 */
9261/*ARGSUSED*/
9262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009263f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009264 typval_T *argvars;
9265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266{
9267#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009268 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009270 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271#endif
9272}
9273
9274/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009275 * "diff_filler()" function
9276 */
9277/*ARGSUSED*/
9278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009280 typval_T *argvars;
9281 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009282{
9283#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009285#endif
9286}
9287
9288/*
9289 * "diff_hlID()" function
9290 */
9291/*ARGSUSED*/
9292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009294 typval_T *argvars;
9295 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009296{
9297#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009299 static linenr_T prev_lnum = 0;
9300 static int changedtick = 0;
9301 static int fnum = 0;
9302 static int change_start = 0;
9303 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009304 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009305 int filler_lines;
9306 int col;
9307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 if (lnum < 0) /* ignore type error in {lnum} arg */
9309 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009310 if (lnum != prev_lnum
9311 || changedtick != curbuf->b_changedtick
9312 || fnum != curbuf->b_fnum)
9313 {
9314 /* New line, buffer, change: need to get the values. */
9315 filler_lines = diff_check(curwin, lnum);
9316 if (filler_lines < 0)
9317 {
9318 if (filler_lines == -1)
9319 {
9320 change_start = MAXCOL;
9321 change_end = -1;
9322 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9323 hlID = HLF_ADD; /* added line */
9324 else
9325 hlID = HLF_CHD; /* changed line */
9326 }
9327 else
9328 hlID = HLF_ADD; /* added line */
9329 }
9330 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009331 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009332 prev_lnum = lnum;
9333 changedtick = curbuf->b_changedtick;
9334 fnum = curbuf->b_fnum;
9335 }
9336
9337 if (hlID == HLF_CHD || hlID == HLF_TXD)
9338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009340 if (col >= change_start && col <= change_end)
9341 hlID = HLF_TXD; /* changed text */
9342 else
9343 hlID = HLF_CHD; /* changed line */
9344 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009345 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009346#endif
9347}
9348
9349/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009350 * "empty({expr})" function
9351 */
9352 static void
9353f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009354 typval_T *argvars;
9355 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009356{
9357 int n;
9358
9359 switch (argvars[0].v_type)
9360 {
9361 case VAR_STRING:
9362 case VAR_FUNC:
9363 n = argvars[0].vval.v_string == NULL
9364 || *argvars[0].vval.v_string == NUL;
9365 break;
9366 case VAR_NUMBER:
9367 n = argvars[0].vval.v_number == 0;
9368 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009369#ifdef FEAT_FLOAT
9370 case VAR_FLOAT:
9371 n = argvars[0].vval.v_float == 0.0;
9372 break;
9373#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009374 case VAR_LIST:
9375 n = argvars[0].vval.v_list == NULL
9376 || argvars[0].vval.v_list->lv_first == NULL;
9377 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378 case VAR_DICT:
9379 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009380 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009381 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009382 default:
9383 EMSG2(_(e_intern2), "f_empty()");
9384 n = 0;
9385 }
9386
9387 rettv->vval.v_number = n;
9388}
9389
9390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 * "escape({string}, {chars})" function
9392 */
9393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 char_u buf[NUMBUFLEN];
9399
Bram Moolenaar758711c2005-02-02 23:11:38 +00009400 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9401 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403}
9404
9405/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009406 * "eval()" function
9407 */
9408/*ARGSUSED*/
9409 static void
9410f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009411 typval_T *argvars;
9412 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009413{
9414 char_u *s;
9415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009416 s = get_tv_string_chk(&argvars[0]);
9417 if (s != NULL)
9418 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9421 {
9422 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009423 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009424 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009425 else if (*s != NUL)
9426 EMSG(_(e_trailing));
9427}
9428
9429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 * "eventhandler()" function
9431 */
9432/*ARGSUSED*/
9433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009435 typval_T *argvars;
9436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
9441/*
9442 * "executable()" function
9443 */
9444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450}
9451
9452/*
9453 * "exists()" function
9454 */
9455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 typval_T *argvars;
9458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 char_u *p;
9461 char_u *name;
9462 int n = FALSE;
9463 int len = 0;
9464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 if (*p == '$') /* environment variable */
9467 {
9468 /* first try "normal" environment variables (fast) */
9469 if (mch_getenv(p + 1) != NULL)
9470 n = TRUE;
9471 else
9472 {
9473 /* try expanding things like $VIM and ${HOME} */
9474 p = expand_env_save(p);
9475 if (p != NULL && *p != '$')
9476 n = TRUE;
9477 vim_free(p);
9478 }
9479 }
9480 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009483 if (*skipwhite(p) != NUL)
9484 n = FALSE; /* trailing garbage */
9485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 else if (*p == '*') /* internal or user defined function */
9487 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009488 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 }
9490 else if (*p == ':')
9491 {
9492 n = cmd_exists(p + 1);
9493 }
9494 else if (*p == '#')
9495 {
9496#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009497 if (p[1] == '#')
9498 n = autocmd_supported(p + 2);
9499 else
9500 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#endif
9502 }
9503 else /* internal variable */
9504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009505 char_u *tofree;
9506 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009508 /* get_name_len() takes care of expanding curly braces */
9509 name = p;
9510 len = get_name_len(&p, &tofree, TRUE, FALSE);
9511 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009513 if (tofree != NULL)
9514 name = tofree;
9515 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9516 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009518 /* handle d.key, l[idx], f(expr) */
9519 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9520 if (n)
9521 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 }
9523 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009524 if (*p != NUL)
9525 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009527 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 }
9529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
9533/*
9534 * "expand()" function
9535 */
9536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009538 typval_T *argvars;
9539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540{
9541 char_u *s;
9542 int len;
9543 char_u *errormsg;
9544 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9545 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548 rettv->v_type = VAR_STRING;
9549 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 if (*s == '%' || *s == '#' || *s == '<')
9551 {
9552 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009553 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 --emsg_off;
9555 }
9556 else
9557 {
9558 /* When the optional second argument is non-zero, don't remove matches
9559 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 if (argvars[1].v_type != VAR_UNKNOWN
9561 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 if (!error)
9564 {
9565 ExpandInit(&xpc);
9566 xpc.xp_context = EXPAND_FILES;
9567 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009568 }
9569 else
9570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 }
9572}
9573
9574/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009575 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009576 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009577 */
9578 static void
9579f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009580 typval_T *argvars;
9581 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009582{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009583 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009584 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 list_T *l1, *l2;
9587 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009588 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009590
Bram Moolenaare9a41262005-01-15 22:18:47 +00009591 l1 = argvars[0].vval.v_list;
9592 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009593 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9594 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009595 {
9596 if (argvars[2].v_type != VAR_UNKNOWN)
9597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 before = get_tv_number_chk(&argvars[2], &error);
9599 if (error)
9600 return; /* type error; errmsg already given */
9601
Bram Moolenaar758711c2005-02-02 23:11:38 +00009602 if (before == l1->lv_len)
9603 item = NULL;
9604 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009606 item = list_find(l1, before);
9607 if (item == NULL)
9608 {
9609 EMSGN(_(e_listidx), before);
9610 return;
9611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 }
9613 }
9614 else
9615 item = NULL;
9616 list_extend(l1, l2, item);
9617
Bram Moolenaare9a41262005-01-15 22:18:47 +00009618 copy_tv(&argvars[0], rettv);
9619 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9622 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009623 dict_T *d1, *d2;
9624 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 char_u *action;
9626 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009628 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629
9630 d1 = argvars[0].vval.v_dict;
9631 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009632 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9633 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009634 {
9635 /* Check the third argument. */
9636 if (argvars[2].v_type != VAR_UNKNOWN)
9637 {
9638 static char *(av[]) = {"keep", "force", "error"};
9639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 action = get_tv_string_chk(&argvars[2]);
9641 if (action == NULL)
9642 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 for (i = 0; i < 3; ++i)
9644 if (STRCMP(action, av[i]) == 0)
9645 break;
9646 if (i == 3)
9647 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009648 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 return;
9650 }
9651 }
9652 else
9653 action = (char_u *)"force";
9654
9655 /* Go over all entries in the second dict and add them to the
9656 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009657 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009660 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009662 --todo;
9663 di1 = dict_find(d1, hi2->hi_key, -1);
9664 if (di1 == NULL)
9665 {
9666 di1 = dictitem_copy(HI2DI(hi2));
9667 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9668 dictitem_free(di1);
9669 }
9670 else if (*action == 'e')
9671 {
9672 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9673 break;
9674 }
9675 else if (*action == 'f')
9676 {
9677 clear_tv(&di1->di_tv);
9678 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009680 }
9681 }
9682
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 copy_tv(&argvars[0], rettv);
9684 }
9685 }
9686 else
9687 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009688}
9689
9690/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009691 * "feedkeys()" function
9692 */
9693/*ARGSUSED*/
9694 static void
9695f_feedkeys(argvars, rettv)
9696 typval_T *argvars;
9697 typval_T *rettv;
9698{
9699 int remap = TRUE;
9700 char_u *keys, *flags;
9701 char_u nbuf[NUMBUFLEN];
9702 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009703 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009704
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009705 /* This is not allowed in the sandbox. If the commands would still be
9706 * executed in the sandbox it would be OK, but it probably happens later,
9707 * when "sandbox" is no longer set. */
9708 if (check_secure())
9709 return;
9710
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009711 rettv->vval.v_number = 0;
9712 keys = get_tv_string(&argvars[0]);
9713 if (*keys != NUL)
9714 {
9715 if (argvars[1].v_type != VAR_UNKNOWN)
9716 {
9717 flags = get_tv_string_buf(&argvars[1], nbuf);
9718 for ( ; *flags != NUL; ++flags)
9719 {
9720 switch (*flags)
9721 {
9722 case 'n': remap = FALSE; break;
9723 case 'm': remap = TRUE; break;
9724 case 't': typed = TRUE; break;
9725 }
9726 }
9727 }
9728
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009729 /* Need to escape K_SPECIAL and CSI before putting the string in the
9730 * typeahead buffer. */
9731 keys_esc = vim_strsave_escape_csi(keys);
9732 if (keys_esc != NULL)
9733 {
9734 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009735 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009736 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009737 if (vgetc_busy)
9738 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009739 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009740 }
9741}
9742
9743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 * "filereadable()" function
9745 */
9746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009748 typval_T *argvars;
9749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009751 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 char_u *p;
9753 int n;
9754
Bram Moolenaarc236c162008-07-13 17:41:49 +00009755#ifndef O_NONBLOCK
9756# define O_NONBLOCK 0
9757#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009759 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9760 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 {
9762 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009763 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
9765 else
9766 n = FALSE;
9767
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009772 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 * rights to write into.
9774 */
9775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 typval_T *argvars;
9778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009780 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781}
9782
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009783static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009784
9785 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009786findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009787 typval_T *argvars;
9788 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009789 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009790{
9791#ifdef FEAT_SEARCHPATH
9792 char_u *fname;
9793 char_u *fresult = NULL;
9794 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9795 char_u *p;
9796 char_u pathbuf[NUMBUFLEN];
9797 int count = 1;
9798 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009799 int error = FALSE;
9800#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009801
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009802 rettv->vval.v_string = NULL;
9803 rettv->v_type = VAR_STRING;
9804
9805#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009806 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009808 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009810 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9811 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009812 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 else
9814 {
9815 if (*p != NUL)
9816 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009819 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009820 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009821 }
9822
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009823 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9824 error = TRUE;
9825
9826 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009828 do
9829 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009830 if (rettv->v_type == VAR_STRING)
9831 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 fresult = find_file_in_path_option(first ? fname : NULL,
9833 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009834 0, first, path,
9835 find_what,
9836 curbuf->b_ffname,
9837 find_what == FINDFILE_DIR
9838 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009840
9841 if (fresult != NULL && rettv->v_type == VAR_LIST)
9842 list_append_string(rettv->vval.v_list, fresult, -1);
9843
9844 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009845 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009846
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009847 if (rettv->v_type == VAR_STRING)
9848 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009849#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009850}
9851
Bram Moolenaar33570922005-01-25 22:26:29 +00009852static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9853static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009854
9855/*
9856 * Implementation of map() and filter().
9857 */
9858 static void
9859filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009860 typval_T *argvars;
9861 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009862 int map;
9863{
9864 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009865 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009866 listitem_T *li, *nli;
9867 list_T *l = NULL;
9868 dictitem_T *di;
9869 hashtab_T *ht;
9870 hashitem_T *hi;
9871 dict_T *d = NULL;
9872 typval_T save_val;
9873 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009874 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009875 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009876 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009877 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009878
9879 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009880 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009881 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009882 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009883 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009884 return;
9885 }
9886 else if (argvars[0].v_type == VAR_DICT)
9887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009888 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009889 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009890 return;
9891 }
9892 else
9893 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009894 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009895 return;
9896 }
9897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 expr = get_tv_string_buf_chk(&argvars[1], buf);
9899 /* On type errors, the preceding call has already displayed an error
9900 * message. Avoid a misleading error message for an empty string that
9901 * was not passed as argument. */
9902 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 prepare_vimvar(VV_VAL, &save_val);
9905 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009906
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009907 /* We reset "did_emsg" to be able to detect whether an error
9908 * occurred during evaluation of the expression. */
9909 save_did_emsg = did_emsg;
9910 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009912 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 prepare_vimvar(VV_KEY, &save_key);
9915 vimvars[VV_KEY].vv_type = VAR_STRING;
9916
9917 ht = &d->dv_hashtab;
9918 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009919 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 if (!HASHITEM_EMPTY(hi))
9923 {
9924 --todo;
9925 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009926 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 break;
9928 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009929 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009930 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 break;
9932 if (!map && rem)
9933 dictitem_remove(d, di);
9934 clear_tv(&vimvars[VV_KEY].vv_tv);
9935 }
9936 }
9937 hash_unlock(ht);
9938
9939 restore_vimvar(VV_KEY, &save_key);
9940 }
9941 else
9942 {
9943 for (li = l->lv_first; li != NULL; li = nli)
9944 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009945 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009946 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009948 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009949 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009950 break;
9951 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009953 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009956 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009957
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009958 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960
9961 copy_tv(&argvars[0], rettv);
9962}
9963
9964 static int
9965filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 char_u *expr;
9968 int map;
9969 int *remp;
9970{
Bram Moolenaar33570922005-01-25 22:26:29 +00009971 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009972 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009973 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 s = expr;
9977 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009978 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009979 if (*s != NUL) /* check for trailing chars after expr */
9980 {
9981 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009982 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 }
9984 if (map)
9985 {
9986 /* map(): replace the list item value */
9987 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009988 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009989 *tv = rettv;
9990 }
9991 else
9992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993 int error = FALSE;
9994
Bram Moolenaare9a41262005-01-15 22:18:47 +00009995 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009998 /* On type error, nothing has been removed; return FAIL to stop the
9999 * loop. The error message was given by get_tv_number_chk(). */
10000 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010001 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010002 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010003 retval = OK;
10004theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010006 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010007}
10008
10009/*
10010 * "filter()" function
10011 */
10012 static void
10013f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010014 typval_T *argvars;
10015 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010016{
10017 filter_map(argvars, rettv, FALSE);
10018}
10019
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010021 * "finddir({fname}[, {path}[, {count}]])" function
10022 */
10023 static void
10024f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010025 typval_T *argvars;
10026 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010027{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010028 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010029}
10030
10031/*
10032 * "findfile({fname}[, {path}[, {count}]])" function
10033 */
10034 static void
10035f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 typval_T *argvars;
10037 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010038{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010039 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010040}
10041
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010042#ifdef FEAT_FLOAT
10043/*
10044 * "float2nr({float})" function
10045 */
10046 static void
10047f_float2nr(argvars, rettv)
10048 typval_T *argvars;
10049 typval_T *rettv;
10050{
10051 float_T f;
10052
10053 if (get_float_arg(argvars, &f) == OK)
10054 {
10055 if (f < -0x7fffffff)
10056 rettv->vval.v_number = -0x7fffffff;
10057 else if (f > 0x7fffffff)
10058 rettv->vval.v_number = 0x7fffffff;
10059 else
10060 rettv->vval.v_number = (varnumber_T)f;
10061 }
10062 else
10063 rettv->vval.v_number = 0;
10064}
10065
10066/*
10067 * "floor({float})" function
10068 */
10069 static void
10070f_floor(argvars, rettv)
10071 typval_T *argvars;
10072 typval_T *rettv;
10073{
10074 float_T f;
10075
10076 rettv->v_type = VAR_FLOAT;
10077 if (get_float_arg(argvars, &f) == OK)
10078 rettv->vval.v_float = floor(f);
10079 else
10080 rettv->vval.v_float = 0.0;
10081}
10082#endif
10083
Bram Moolenaar0d660222005-01-07 21:51:51 +000010084/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010085 * "fnameescape({string})" function
10086 */
10087 static void
10088f_fnameescape(argvars, rettv)
10089 typval_T *argvars;
10090 typval_T *rettv;
10091{
10092 rettv->vval.v_string = vim_strsave_fnameescape(
10093 get_tv_string(&argvars[0]), FALSE);
10094 rettv->v_type = VAR_STRING;
10095}
10096
10097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 * "fnamemodify({fname}, {mods})" function
10099 */
10100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010102 typval_T *argvars;
10103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
10105 char_u *fname;
10106 char_u *mods;
10107 int usedlen = 0;
10108 int len;
10109 char_u *fbuf = NULL;
10110 char_u buf[NUMBUFLEN];
10111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010112 fname = get_tv_string_chk(&argvars[0]);
10113 mods = get_tv_string_buf_chk(&argvars[1], buf);
10114 if (fname == NULL || mods == NULL)
10115 fname = NULL;
10116 else
10117 {
10118 len = (int)STRLEN(fname);
10119 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 vim_free(fbuf);
10128}
10129
Bram Moolenaar33570922005-01-25 22:26:29 +000010130static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
10132/*
10133 * "foldclosed()" function
10134 */
10135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010137 typval_T *argvars;
10138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 int end;
10140{
10141#ifdef FEAT_FOLDING
10142 linenr_T lnum;
10143 linenr_T first, last;
10144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10147 {
10148 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10149 {
10150 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 return;
10155 }
10156 }
10157#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159}
10160
10161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010162 * "foldclosed()" function
10163 */
10164 static void
10165f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010166 typval_T *argvars;
10167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010168{
10169 foldclosed_both(argvars, rettv, FALSE);
10170}
10171
10172/*
10173 * "foldclosedend()" function
10174 */
10175 static void
10176f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010177 typval_T *argvars;
10178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010179{
10180 foldclosed_both(argvars, rettv, TRUE);
10181}
10182
10183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 * "foldlevel()" function
10185 */
10186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 typval_T *argvars;
10189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191#ifdef FEAT_FOLDING
10192 linenr_T lnum;
10193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010196 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 else
10198#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010199 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200}
10201
10202/*
10203 * "foldtext()" function
10204 */
10205/*ARGSUSED*/
10206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010208 typval_T *argvars;
10209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211#ifdef FEAT_FOLDING
10212 linenr_T lnum;
10213 char_u *s;
10214 char_u *r;
10215 int len;
10216 char *txt;
10217#endif
10218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010219 rettv->v_type = VAR_STRING;
10220 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10223 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10224 <= curbuf->b_ml.ml_line_count
10225 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 {
10227 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010228 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10229 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 {
10231 if (!linewhite(lnum))
10232 break;
10233 ++lnum;
10234 }
10235
10236 /* Find interesting text in this line. */
10237 s = skipwhite(ml_get(lnum));
10238 /* skip C comment-start */
10239 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010242 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010244 {
10245 s = skipwhite(ml_get(lnum + 1));
10246 if (*s == '*')
10247 s = skipwhite(s + 1);
10248 }
10249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 txt = _("+-%s%3ld lines: ");
10251 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 + 20 /* for %3ld */
10254 + STRLEN(s))); /* concatenated */
10255 if (r != NULL)
10256 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10258 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10259 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 len = (int)STRLEN(r);
10261 STRCAT(r, s);
10262 /* remove 'foldmarker' and 'commentstring' */
10263 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 }
10266 }
10267#endif
10268}
10269
10270/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010271 * "foldtextresult(lnum)" function
10272 */
10273/*ARGSUSED*/
10274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010276 typval_T *argvars;
10277 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010278{
10279#ifdef FEAT_FOLDING
10280 linenr_T lnum;
10281 char_u *text;
10282 char_u buf[51];
10283 foldinfo_T foldinfo;
10284 int fold_count;
10285#endif
10286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287 rettv->v_type = VAR_STRING;
10288 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010289#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 /* treat illegal types and illegal string values for {lnum} the same */
10292 if (lnum < 0)
10293 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010294 fold_count = foldedCount(curwin, lnum, &foldinfo);
10295 if (fold_count > 0)
10296 {
10297 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10298 &foldinfo, buf);
10299 if (text == buf)
10300 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010302 }
10303#endif
10304}
10305
10306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 * "foreground()" function
10308 */
10309/*ARGSUSED*/
10310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010312 typval_T *argvars;
10313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316#ifdef FEAT_GUI
10317 if (gui.in_use)
10318 gui_mch_set_foreground();
10319#else
10320# ifdef WIN32
10321 win32_set_foreground();
10322# endif
10323#endif
10324}
10325
10326/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010327 * "function()" function
10328 */
10329/*ARGSUSED*/
10330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010334{
10335 char_u *s;
10336
Bram Moolenaara7043832005-01-21 11:56:39 +000010337 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010339 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010340 EMSG2(_(e_invarg2), s);
10341 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010342 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010343 else
10344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345 rettv->vval.v_string = vim_strsave(s);
10346 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010347 }
10348}
10349
10350/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010351 * "garbagecollect()" function
10352 */
10353/*ARGSUSED*/
10354 static void
10355f_garbagecollect(argvars, rettv)
10356 typval_T *argvars;
10357 typval_T *rettv;
10358{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010359 /* This is postponed until we are back at the toplevel, because we may be
10360 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10361 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010362
10363 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10364 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010365}
10366
10367/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010368 * "get()" function
10369 */
10370 static void
10371f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010372 typval_T *argvars;
10373 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010374{
Bram Moolenaar33570922005-01-25 22:26:29 +000010375 listitem_T *li;
10376 list_T *l;
10377 dictitem_T *di;
10378 dict_T *d;
10379 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010380
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010382 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 int error = FALSE;
10386
10387 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10388 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010389 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010390 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010392 else if (argvars[0].v_type == VAR_DICT)
10393 {
10394 if ((d = argvars[0].vval.v_dict) != NULL)
10395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010396 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010397 if (di != NULL)
10398 tv = &di->di_tv;
10399 }
10400 }
10401 else
10402 EMSG2(_(e_listdictarg), "get()");
10403
10404 if (tv == NULL)
10405 {
10406 if (argvars[2].v_type == VAR_UNKNOWN)
10407 rettv->vval.v_number = 0;
10408 else
10409 copy_tv(&argvars[2], rettv);
10410 }
10411 else
10412 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010413}
10414
Bram Moolenaar342337a2005-07-21 21:11:17 +000010415static 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 +000010416
10417/*
10418 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010419 * Return a range (from start to end) of lines in rettv from the specified
10420 * buffer.
10421 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010422 */
10423 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010424get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010425 buf_T *buf;
10426 linenr_T start;
10427 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010428 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010429 typval_T *rettv;
10430{
10431 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010432
Bram Moolenaar342337a2005-07-21 21:11:17 +000010433 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010434 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010435 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010436 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010437 }
10438 else
10439 rettv->vval.v_number = 0;
10440
10441 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10442 return;
10443
10444 if (!retlist)
10445 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010446 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10447 p = ml_get_buf(buf, start, FALSE);
10448 else
10449 p = (char_u *)"";
10450
10451 rettv->v_type = VAR_STRING;
10452 rettv->vval.v_string = vim_strsave(p);
10453 }
10454 else
10455 {
10456 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010457 return;
10458
10459 if (start < 1)
10460 start = 1;
10461 if (end > buf->b_ml.ml_line_count)
10462 end = buf->b_ml.ml_line_count;
10463 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010464 if (list_append_string(rettv->vval.v_list,
10465 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010466 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010467 }
10468}
10469
10470/*
10471 * "getbufline()" function
10472 */
10473 static void
10474f_getbufline(argvars, rettv)
10475 typval_T *argvars;
10476 typval_T *rettv;
10477{
10478 linenr_T lnum;
10479 linenr_T end;
10480 buf_T *buf;
10481
10482 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10483 ++emsg_off;
10484 buf = get_buf_tv(&argvars[0]);
10485 --emsg_off;
10486
Bram Moolenaar661b1822005-07-28 22:36:45 +000010487 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010488 if (argvars[2].v_type == VAR_UNKNOWN)
10489 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010490 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010491 end = get_tv_lnum_buf(&argvars[2], buf);
10492
Bram Moolenaar342337a2005-07-21 21:11:17 +000010493 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010494}
10495
Bram Moolenaar0d660222005-01-07 21:51:51 +000010496/*
10497 * "getbufvar()" function
10498 */
10499 static void
10500f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010501 typval_T *argvars;
10502 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010503{
10504 buf_T *buf;
10505 buf_T *save_curbuf;
10506 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10510 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010511 ++emsg_off;
10512 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010513
10514 rettv->v_type = VAR_STRING;
10515 rettv->vval.v_string = NULL;
10516
10517 if (buf != NULL && varname != NULL)
10518 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010519 /* set curbuf to be our buf, temporarily */
10520 save_curbuf = curbuf;
10521 curbuf = buf;
10522
Bram Moolenaar0d660222005-01-07 21:51:51 +000010523 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010524 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010525 else
10526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 if (*varname == NUL)
10528 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10529 * scope prefix before the NUL byte is required by
10530 * find_var_in_ht(). */
10531 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010532 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010533 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010534 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010535 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010536 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010537
10538 /* restore previous notion of curbuf */
10539 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010540 }
10541
10542 --emsg_off;
10543}
10544
10545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 * "getchar()" function
10547 */
10548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010550 typval_T *argvars;
10551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010556 /* Position the cursor. Needed after a message that ends in a space. */
10557 windgoto(msg_row, msg_col);
10558
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 ++no_mapping;
10560 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010561 for (;;)
10562 {
10563 if (argvars[0].v_type == VAR_UNKNOWN)
10564 /* getchar(): blocking wait. */
10565 n = safe_vgetc();
10566 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10567 /* getchar(1): only check if char avail */
10568 n = vpeekc();
10569 else if (error || vpeekc() == NUL)
10570 /* illegal argument or getchar(0) and no char avail: return zero */
10571 n = 0;
10572 else
10573 /* getchar(0) and char avail: return char */
10574 n = safe_vgetc();
10575 if (n == K_IGNORE)
10576 continue;
10577 break;
10578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 --no_mapping;
10580 --allow_keys;
10581
Bram Moolenaar219b8702006-11-01 14:32:36 +000010582 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10583 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10584 vimvars[VV_MOUSE_COL].vv_nr = 0;
10585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010586 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 if (IS_SPECIAL(n) || mod_mask != 0)
10588 {
10589 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10590 int i = 0;
10591
10592 /* Turn a special key into three bytes, plus modifier. */
10593 if (mod_mask != 0)
10594 {
10595 temp[i++] = K_SPECIAL;
10596 temp[i++] = KS_MODIFIER;
10597 temp[i++] = mod_mask;
10598 }
10599 if (IS_SPECIAL(n))
10600 {
10601 temp[i++] = K_SPECIAL;
10602 temp[i++] = K_SECOND(n);
10603 temp[i++] = K_THIRD(n);
10604 }
10605#ifdef FEAT_MBYTE
10606 else if (has_mbyte)
10607 i += (*mb_char2bytes)(n, temp + i);
10608#endif
10609 else
10610 temp[i++] = n;
10611 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612 rettv->v_type = VAR_STRING;
10613 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010614
10615#ifdef FEAT_MOUSE
10616 if (n == K_LEFTMOUSE
10617 || n == K_LEFTMOUSE_NM
10618 || n == K_LEFTDRAG
10619 || n == K_LEFTRELEASE
10620 || n == K_LEFTRELEASE_NM
10621 || n == K_MIDDLEMOUSE
10622 || n == K_MIDDLEDRAG
10623 || n == K_MIDDLERELEASE
10624 || n == K_RIGHTMOUSE
10625 || n == K_RIGHTDRAG
10626 || n == K_RIGHTRELEASE
10627 || n == K_X1MOUSE
10628 || n == K_X1DRAG
10629 || n == K_X1RELEASE
10630 || n == K_X2MOUSE
10631 || n == K_X2DRAG
10632 || n == K_X2RELEASE
10633 || n == K_MOUSEDOWN
10634 || n == K_MOUSEUP)
10635 {
10636 int row = mouse_row;
10637 int col = mouse_col;
10638 win_T *win;
10639 linenr_T lnum;
10640# ifdef FEAT_WINDOWS
10641 win_T *wp;
10642# endif
10643 int n = 1;
10644
10645 if (row >= 0 && col >= 0)
10646 {
10647 /* Find the window at the mouse coordinates and compute the
10648 * text position. */
10649 win = mouse_find_win(&row, &col);
10650 (void)mouse_comp_pos(win, &row, &col, &lnum);
10651# ifdef FEAT_WINDOWS
10652 for (wp = firstwin; wp != win; wp = wp->w_next)
10653 ++n;
10654# endif
10655 vimvars[VV_MOUSE_WIN].vv_nr = n;
10656 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10657 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10658 }
10659 }
10660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 }
10662}
10663
10664/*
10665 * "getcharmod()" function
10666 */
10667/*ARGSUSED*/
10668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010670 typval_T *argvars;
10671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674}
10675
10676/*
10677 * "getcmdline()" function
10678 */
10679/*ARGSUSED*/
10680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010682 typval_T *argvars;
10683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 rettv->v_type = VAR_STRING;
10686 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687}
10688
10689/*
10690 * "getcmdpos()" function
10691 */
10692/*ARGSUSED*/
10693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010695 typval_T *argvars;
10696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699}
10700
10701/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010702 * "getcmdtype()" function
10703 */
10704/*ARGSUSED*/
10705 static void
10706f_getcmdtype(argvars, rettv)
10707 typval_T *argvars;
10708 typval_T *rettv;
10709{
10710 rettv->v_type = VAR_STRING;
10711 rettv->vval.v_string = alloc(2);
10712 if (rettv->vval.v_string != NULL)
10713 {
10714 rettv->vval.v_string[0] = get_cmdline_type();
10715 rettv->vval.v_string[1] = NUL;
10716 }
10717}
10718
10719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 * "getcwd()" function
10721 */
10722/*ARGSUSED*/
10723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T *argvars;
10726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727{
10728 char_u cwd[MAXPATHL];
10729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 else
10734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010737 if (rettv->vval.v_string != NULL)
10738 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739#endif
10740 }
10741}
10742
10743/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010744 * "getfontname()" function
10745 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010746/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010749 typval_T *argvars;
10750 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->v_type = VAR_STRING;
10753 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010754#ifdef FEAT_GUI
10755 if (gui.in_use)
10756 {
10757 GuiFont font;
10758 char_u *name = NULL;
10759
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010760 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010761 {
10762 /* Get the "Normal" font. Either the name saved by
10763 * hl_set_font_name() or from the font ID. */
10764 font = gui.norm_font;
10765 name = hl_get_font_name();
10766 }
10767 else
10768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010770 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10771 return;
10772 font = gui_mch_get_font(name, FALSE);
10773 if (font == NOFONT)
10774 return; /* Invalid font name, return empty string. */
10775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010777 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010778 gui_mch_free_font(font);
10779 }
10780#endif
10781}
10782
10783/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010784 * "getfperm({fname})" function
10785 */
10786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010790{
10791 char_u *fname;
10792 struct stat st;
10793 char_u *perm = NULL;
10794 char_u flags[] = "rwx";
10795 int i;
10796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010800 if (mch_stat((char *)fname, &st) >= 0)
10801 {
10802 perm = vim_strsave((char_u *)"---------");
10803 if (perm != NULL)
10804 {
10805 for (i = 0; i < 9; i++)
10806 {
10807 if (st.st_mode & (1 << (8 - i)))
10808 perm[i] = flags[i % 3];
10809 }
10810 }
10811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010812 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010813}
10814
10815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 * "getfsize({fname})" function
10817 */
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
10823 char_u *fname;
10824 struct stat st;
10825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829
10830 if (mch_stat((char *)fname, &st) >= 0)
10831 {
10832 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010837
10838 /* non-perfect check for overflow */
10839 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10840 rettv->vval.v_number = -2;
10841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 }
10843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845}
10846
10847/*
10848 * "getftime({fname})" function
10849 */
10850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010852 typval_T *argvars;
10853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
10855 char_u *fname;
10856 struct stat st;
10857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859
10860 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864}
10865
10866/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010867 * "getftype({fname})" function
10868 */
10869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 typval_T *argvars;
10872 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010873{
10874 char_u *fname;
10875 struct stat st;
10876 char_u *type = NULL;
10877 char *t;
10878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010882 if (mch_lstat((char *)fname, &st) >= 0)
10883 {
10884#ifdef S_ISREG
10885 if (S_ISREG(st.st_mode))
10886 t = "file";
10887 else if (S_ISDIR(st.st_mode))
10888 t = "dir";
10889# ifdef S_ISLNK
10890 else if (S_ISLNK(st.st_mode))
10891 t = "link";
10892# endif
10893# ifdef S_ISBLK
10894 else if (S_ISBLK(st.st_mode))
10895 t = "bdev";
10896# endif
10897# ifdef S_ISCHR
10898 else if (S_ISCHR(st.st_mode))
10899 t = "cdev";
10900# endif
10901# ifdef S_ISFIFO
10902 else if (S_ISFIFO(st.st_mode))
10903 t = "fifo";
10904# endif
10905# ifdef S_ISSOCK
10906 else if (S_ISSOCK(st.st_mode))
10907 t = "fifo";
10908# endif
10909 else
10910 t = "other";
10911#else
10912# ifdef S_IFMT
10913 switch (st.st_mode & S_IFMT)
10914 {
10915 case S_IFREG: t = "file"; break;
10916 case S_IFDIR: t = "dir"; break;
10917# ifdef S_IFLNK
10918 case S_IFLNK: t = "link"; break;
10919# endif
10920# ifdef S_IFBLK
10921 case S_IFBLK: t = "bdev"; break;
10922# endif
10923# ifdef S_IFCHR
10924 case S_IFCHR: t = "cdev"; break;
10925# endif
10926# ifdef S_IFIFO
10927 case S_IFIFO: t = "fifo"; break;
10928# endif
10929# ifdef S_IFSOCK
10930 case S_IFSOCK: t = "socket"; break;
10931# endif
10932 default: t = "other";
10933 }
10934# else
10935 if (mch_isdir(fname))
10936 t = "dir";
10937 else
10938 t = "file";
10939# endif
10940#endif
10941 type = vim_strsave((char_u *)t);
10942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010944}
10945
10946/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010947 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010948 */
10949 static void
10950f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010951 typval_T *argvars;
10952 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010953{
10954 linenr_T lnum;
10955 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010956 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010957
10958 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010959 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010960 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010961 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010962 retlist = FALSE;
10963 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010964 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010965 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010966 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010967 retlist = TRUE;
10968 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010969
Bram Moolenaar342337a2005-07-21 21:11:17 +000010970 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010971}
10972
10973/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010974 * "getmatches()" function
10975 */
10976/*ARGSUSED*/
10977 static void
10978f_getmatches(argvars, rettv)
10979 typval_T *argvars;
10980 typval_T *rettv;
10981{
10982#ifdef FEAT_SEARCH_EXTRA
10983 dict_T *dict;
10984 matchitem_T *cur = curwin->w_match_head;
10985
10986 rettv->vval.v_number = 0;
10987
10988 if (rettv_list_alloc(rettv) == OK)
10989 {
10990 while (cur != NULL)
10991 {
10992 dict = dict_alloc();
10993 if (dict == NULL)
10994 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010995 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10996 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10997 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10998 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10999 list_append_dict(rettv->vval.v_list, dict);
11000 cur = cur->next;
11001 }
11002 }
11003#endif
11004}
11005
11006/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011007 * "getpid()" function
11008 */
11009/*ARGSUSED*/
11010 static void
11011f_getpid(argvars, rettv)
11012 typval_T *argvars;
11013 typval_T *rettv;
11014{
11015 rettv->vval.v_number = mch_get_pid();
11016}
11017
11018/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011019 * "getpos(string)" function
11020 */
11021 static void
11022f_getpos(argvars, rettv)
11023 typval_T *argvars;
11024 typval_T *rettv;
11025{
11026 pos_T *fp;
11027 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011028 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011029
11030 if (rettv_list_alloc(rettv) == OK)
11031 {
11032 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011033 fp = var2fpos(&argvars[0], TRUE, &fnum);
11034 if (fnum != -1)
11035 list_append_number(l, (varnumber_T)fnum);
11036 else
11037 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011038 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11039 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011040 list_append_number(l, (fp != NULL)
11041 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011042 : (varnumber_T)0);
11043 list_append_number(l,
11044#ifdef FEAT_VIRTUALEDIT
11045 (fp != NULL) ? (varnumber_T)fp->coladd :
11046#endif
11047 (varnumber_T)0);
11048 }
11049 else
11050 rettv->vval.v_number = FALSE;
11051}
11052
11053/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011054 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011055 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000011056/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000011057 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011058f_getqflist(argvars, rettv)
11059 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011060 typval_T *rettv;
11061{
11062#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011063 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011064#endif
11065
Bram Moolenaar77f66d62007-02-20 02:16:18 +000011066 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011067#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011068 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011069 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011070 wp = NULL;
11071 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11072 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011073 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011074 if (wp == NULL)
11075 return;
11076 }
11077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011078 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011079 }
11080#endif
11081}
11082
11083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 * "getreg()" function
11085 */
11086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011087f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011088 typval_T *argvars;
11089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090{
11091 char_u *strregname;
11092 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011093 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011096 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098 strregname = get_tv_string_chk(&argvars[0]);
11099 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011100 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011104 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 regname = (strregname == NULL ? '"' : *strregname);
11106 if (regname == 0)
11107 regname = '"';
11108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 rettv->vval.v_string = error ? NULL :
11111 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112}
11113
11114/*
11115 * "getregtype()" function
11116 */
11117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 typval_T *argvars;
11120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121{
11122 char_u *strregname;
11123 int regname;
11124 char_u buf[NUMBUFLEN + 2];
11125 long reglen = 0;
11126
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011127 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 {
11129 strregname = get_tv_string_chk(&argvars[0]);
11130 if (strregname == NULL) /* type error; errmsg already given */
11131 {
11132 rettv->v_type = VAR_STRING;
11133 rettv->vval.v_string = NULL;
11134 return;
11135 }
11136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 else
11138 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
11141 regname = (strregname == NULL ? '"' : *strregname);
11142 if (regname == 0)
11143 regname = '"';
11144
11145 buf[0] = NUL;
11146 buf[1] = NUL;
11147 switch (get_reg_type(regname, &reglen))
11148 {
11149 case MLINE: buf[0] = 'V'; break;
11150 case MCHAR: buf[0] = 'v'; break;
11151#ifdef FEAT_VISUAL
11152 case MBLOCK:
11153 buf[0] = Ctrl_V;
11154 sprintf((char *)buf + 1, "%ld", reglen + 1);
11155 break;
11156#endif
11157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->v_type = VAR_STRING;
11159 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160}
11161
11162/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011163 * "gettabwinvar()" function
11164 */
11165 static void
11166f_gettabwinvar(argvars, rettv)
11167 typval_T *argvars;
11168 typval_T *rettv;
11169{
11170 getwinvar(argvars, rettv, 1);
11171}
11172
11173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 * "getwinposx()" function
11175 */
11176/*ARGSUSED*/
11177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011179 typval_T *argvars;
11180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183#ifdef FEAT_GUI
11184 if (gui.in_use)
11185 {
11186 int x, y;
11187
11188 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 }
11191#endif
11192}
11193
11194/*
11195 * "getwinposy()" function
11196 */
11197/*ARGSUSED*/
11198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011199f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011200 typval_T *argvars;
11201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204#ifdef FEAT_GUI
11205 if (gui.in_use)
11206 {
11207 int x, y;
11208
11209 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 }
11212#endif
11213}
11214
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011215/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011216 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011217 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011218 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011219find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011220 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011221 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011222{
11223#ifdef FEAT_WINDOWS
11224 win_T *wp;
11225#endif
11226 int nr;
11227
11228 nr = get_tv_number_chk(vp, NULL);
11229
11230#ifdef FEAT_WINDOWS
11231 if (nr < 0)
11232 return NULL;
11233 if (nr == 0)
11234 return curwin;
11235
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011236 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11237 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011238 if (--nr <= 0)
11239 break;
11240 return wp;
11241#else
11242 if (nr == 0 || nr == 1)
11243 return curwin;
11244 return NULL;
11245#endif
11246}
11247
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248/*
11249 * "getwinvar()" function
11250 */
11251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011253 typval_T *argvars;
11254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011256 getwinvar(argvars, rettv, 0);
11257}
11258
11259/*
11260 * getwinvar() and gettabwinvar()
11261 */
11262 static void
11263getwinvar(argvars, rettv, off)
11264 typval_T *argvars;
11265 typval_T *rettv;
11266 int off; /* 1 for gettabwinvar() */
11267{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 win_T *win, *oldcurwin;
11269 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011271 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011273#ifdef FEAT_WINDOWS
11274 if (off == 1)
11275 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11276 else
11277 tp = curtab;
11278#endif
11279 win = find_win_by_nr(&argvars[off], tp);
11280 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->v_type = VAR_STRING;
11284 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285
11286 if (win != NULL && varname != NULL)
11287 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011288 /* Set curwin to be our win, temporarily. Also set curbuf, so
11289 * that we can get buffer-local options. */
11290 oldcurwin = curwin;
11291 curwin = win;
11292 curbuf = win->w_buffer;
11293
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 else
11297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 if (*varname == NUL)
11299 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11300 * scope prefix before the NUL byte is required by
11301 * find_var_in_ht(). */
11302 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011304 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011306 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011308
11309 /* restore previous notion of curwin */
11310 curwin = oldcurwin;
11311 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 }
11313
11314 --emsg_off;
11315}
11316
11317/*
11318 * "glob()" function
11319 */
11320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324{
11325 expand_T xpc;
11326
11327 ExpandInit(&xpc);
11328 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->v_type = VAR_STRING;
11330 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332}
11333
11334/*
11335 * "globpath()" function
11336 */
11337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011339 typval_T *argvars;
11340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341{
11342 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011346 if (file == NULL)
11347 rettv->vval.v_string = NULL;
11348 else
11349 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350}
11351
11352/*
11353 * "has()" function
11354 */
11355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011357 typval_T *argvars;
11358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359{
11360 int i;
11361 char_u *name;
11362 int n = FALSE;
11363 static char *(has_list[]) =
11364 {
11365#ifdef AMIGA
11366 "amiga",
11367# ifdef FEAT_ARP
11368 "arp",
11369# endif
11370#endif
11371#ifdef __BEOS__
11372 "beos",
11373#endif
11374#ifdef MSDOS
11375# ifdef DJGPP
11376 "dos32",
11377# else
11378 "dos16",
11379# endif
11380#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011381#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 "mac",
11383#endif
11384#if defined(MACOS_X_UNIX)
11385 "macunix",
11386#endif
11387#ifdef OS2
11388 "os2",
11389#endif
11390#ifdef __QNX__
11391 "qnx",
11392#endif
11393#ifdef RISCOS
11394 "riscos",
11395#endif
11396#ifdef UNIX
11397 "unix",
11398#endif
11399#ifdef VMS
11400 "vms",
11401#endif
11402#ifdef WIN16
11403 "win16",
11404#endif
11405#ifdef WIN32
11406 "win32",
11407#endif
11408#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11409 "win32unix",
11410#endif
11411#ifdef WIN64
11412 "win64",
11413#endif
11414#ifdef EBCDIC
11415 "ebcdic",
11416#endif
11417#ifndef CASE_INSENSITIVE_FILENAME
11418 "fname_case",
11419#endif
11420#ifdef FEAT_ARABIC
11421 "arabic",
11422#endif
11423#ifdef FEAT_AUTOCMD
11424 "autocmd",
11425#endif
11426#ifdef FEAT_BEVAL
11427 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011428# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11429 "balloon_multiline",
11430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431#endif
11432#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11433 "builtin_terms",
11434# ifdef ALL_BUILTIN_TCAPS
11435 "all_builtin_terms",
11436# endif
11437#endif
11438#ifdef FEAT_BYTEOFF
11439 "byte_offset",
11440#endif
11441#ifdef FEAT_CINDENT
11442 "cindent",
11443#endif
11444#ifdef FEAT_CLIENTSERVER
11445 "clientserver",
11446#endif
11447#ifdef FEAT_CLIPBOARD
11448 "clipboard",
11449#endif
11450#ifdef FEAT_CMDL_COMPL
11451 "cmdline_compl",
11452#endif
11453#ifdef FEAT_CMDHIST
11454 "cmdline_hist",
11455#endif
11456#ifdef FEAT_COMMENTS
11457 "comments",
11458#endif
11459#ifdef FEAT_CRYPT
11460 "cryptv",
11461#endif
11462#ifdef FEAT_CSCOPE
11463 "cscope",
11464#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011465#ifdef CURSOR_SHAPE
11466 "cursorshape",
11467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468#ifdef DEBUG
11469 "debug",
11470#endif
11471#ifdef FEAT_CON_DIALOG
11472 "dialog_con",
11473#endif
11474#ifdef FEAT_GUI_DIALOG
11475 "dialog_gui",
11476#endif
11477#ifdef FEAT_DIFF
11478 "diff",
11479#endif
11480#ifdef FEAT_DIGRAPHS
11481 "digraphs",
11482#endif
11483#ifdef FEAT_DND
11484 "dnd",
11485#endif
11486#ifdef FEAT_EMACS_TAGS
11487 "emacs_tags",
11488#endif
11489 "eval", /* always present, of course! */
11490#ifdef FEAT_EX_EXTRA
11491 "ex_extra",
11492#endif
11493#ifdef FEAT_SEARCH_EXTRA
11494 "extra_search",
11495#endif
11496#ifdef FEAT_FKMAP
11497 "farsi",
11498#endif
11499#ifdef FEAT_SEARCHPATH
11500 "file_in_path",
11501#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011502#if defined(UNIX) && !defined(USE_SYSTEM)
11503 "filterpipe",
11504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505#ifdef FEAT_FIND_ID
11506 "find_in_path",
11507#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011508#ifdef FEAT_FLOAT
11509 "float",
11510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511#ifdef FEAT_FOLDING
11512 "folding",
11513#endif
11514#ifdef FEAT_FOOTER
11515 "footer",
11516#endif
11517#if !defined(USE_SYSTEM) && defined(UNIX)
11518 "fork",
11519#endif
11520#ifdef FEAT_GETTEXT
11521 "gettext",
11522#endif
11523#ifdef FEAT_GUI
11524 "gui",
11525#endif
11526#ifdef FEAT_GUI_ATHENA
11527# ifdef FEAT_GUI_NEXTAW
11528 "gui_neXtaw",
11529# else
11530 "gui_athena",
11531# endif
11532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533#ifdef FEAT_GUI_GTK
11534 "gui_gtk",
11535# ifdef HAVE_GTK2
11536 "gui_gtk2",
11537# endif
11538#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011539#ifdef FEAT_GUI_GNOME
11540 "gui_gnome",
11541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542#ifdef FEAT_GUI_MAC
11543 "gui_mac",
11544#endif
11545#ifdef FEAT_GUI_MOTIF
11546 "gui_motif",
11547#endif
11548#ifdef FEAT_GUI_PHOTON
11549 "gui_photon",
11550#endif
11551#ifdef FEAT_GUI_W16
11552 "gui_win16",
11553#endif
11554#ifdef FEAT_GUI_W32
11555 "gui_win32",
11556#endif
11557#ifdef FEAT_HANGULIN
11558 "hangul_input",
11559#endif
11560#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11561 "iconv",
11562#endif
11563#ifdef FEAT_INS_EXPAND
11564 "insert_expand",
11565#endif
11566#ifdef FEAT_JUMPLIST
11567 "jumplist",
11568#endif
11569#ifdef FEAT_KEYMAP
11570 "keymap",
11571#endif
11572#ifdef FEAT_LANGMAP
11573 "langmap",
11574#endif
11575#ifdef FEAT_LIBCALL
11576 "libcall",
11577#endif
11578#ifdef FEAT_LINEBREAK
11579 "linebreak",
11580#endif
11581#ifdef FEAT_LISP
11582 "lispindent",
11583#endif
11584#ifdef FEAT_LISTCMDS
11585 "listcmds",
11586#endif
11587#ifdef FEAT_LOCALMAP
11588 "localmap",
11589#endif
11590#ifdef FEAT_MENU
11591 "menu",
11592#endif
11593#ifdef FEAT_SESSION
11594 "mksession",
11595#endif
11596#ifdef FEAT_MODIFY_FNAME
11597 "modify_fname",
11598#endif
11599#ifdef FEAT_MOUSE
11600 "mouse",
11601#endif
11602#ifdef FEAT_MOUSESHAPE
11603 "mouseshape",
11604#endif
11605#if defined(UNIX) || defined(VMS)
11606# ifdef FEAT_MOUSE_DEC
11607 "mouse_dec",
11608# endif
11609# ifdef FEAT_MOUSE_GPM
11610 "mouse_gpm",
11611# endif
11612# ifdef FEAT_MOUSE_JSB
11613 "mouse_jsbterm",
11614# endif
11615# ifdef FEAT_MOUSE_NET
11616 "mouse_netterm",
11617# endif
11618# ifdef FEAT_MOUSE_PTERM
11619 "mouse_pterm",
11620# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011621# ifdef FEAT_SYSMOUSE
11622 "mouse_sysmouse",
11623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624# ifdef FEAT_MOUSE_XTERM
11625 "mouse_xterm",
11626# endif
11627#endif
11628#ifdef FEAT_MBYTE
11629 "multi_byte",
11630#endif
11631#ifdef FEAT_MBYTE_IME
11632 "multi_byte_ime",
11633#endif
11634#ifdef FEAT_MULTI_LANG
11635 "multi_lang",
11636#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011637#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011638#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011639 "mzscheme",
11640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642#ifdef FEAT_OLE
11643 "ole",
11644#endif
11645#ifdef FEAT_OSFILETYPE
11646 "osfiletype",
11647#endif
11648#ifdef FEAT_PATH_EXTRA
11649 "path_extra",
11650#endif
11651#ifdef FEAT_PERL
11652#ifndef DYNAMIC_PERL
11653 "perl",
11654#endif
11655#endif
11656#ifdef FEAT_PYTHON
11657#ifndef DYNAMIC_PYTHON
11658 "python",
11659#endif
11660#endif
11661#ifdef FEAT_POSTSCRIPT
11662 "postscript",
11663#endif
11664#ifdef FEAT_PRINTER
11665 "printer",
11666#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011667#ifdef FEAT_PROFILE
11668 "profile",
11669#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011670#ifdef FEAT_RELTIME
11671 "reltime",
11672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673#ifdef FEAT_QUICKFIX
11674 "quickfix",
11675#endif
11676#ifdef FEAT_RIGHTLEFT
11677 "rightleft",
11678#endif
11679#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11680 "ruby",
11681#endif
11682#ifdef FEAT_SCROLLBIND
11683 "scrollbind",
11684#endif
11685#ifdef FEAT_CMDL_INFO
11686 "showcmd",
11687 "cmdline_info",
11688#endif
11689#ifdef FEAT_SIGNS
11690 "signs",
11691#endif
11692#ifdef FEAT_SMARTINDENT
11693 "smartindent",
11694#endif
11695#ifdef FEAT_SNIFF
11696 "sniff",
11697#endif
11698#ifdef FEAT_STL_OPT
11699 "statusline",
11700#endif
11701#ifdef FEAT_SUN_WORKSHOP
11702 "sun_workshop",
11703#endif
11704#ifdef FEAT_NETBEANS_INTG
11705 "netbeans_intg",
11706#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011707#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011708 "spell",
11709#endif
11710#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 "syntax",
11712#endif
11713#if defined(USE_SYSTEM) || !defined(UNIX)
11714 "system",
11715#endif
11716#ifdef FEAT_TAG_BINS
11717 "tag_binary",
11718#endif
11719#ifdef FEAT_TAG_OLDSTATIC
11720 "tag_old_static",
11721#endif
11722#ifdef FEAT_TAG_ANYWHITE
11723 "tag_any_white",
11724#endif
11725#ifdef FEAT_TCL
11726# ifndef DYNAMIC_TCL
11727 "tcl",
11728# endif
11729#endif
11730#ifdef TERMINFO
11731 "terminfo",
11732#endif
11733#ifdef FEAT_TERMRESPONSE
11734 "termresponse",
11735#endif
11736#ifdef FEAT_TEXTOBJ
11737 "textobjects",
11738#endif
11739#ifdef HAVE_TGETENT
11740 "tgetent",
11741#endif
11742#ifdef FEAT_TITLE
11743 "title",
11744#endif
11745#ifdef FEAT_TOOLBAR
11746 "toolbar",
11747#endif
11748#ifdef FEAT_USR_CMDS
11749 "user-commands", /* was accidentally included in 5.4 */
11750 "user_commands",
11751#endif
11752#ifdef FEAT_VIMINFO
11753 "viminfo",
11754#endif
11755#ifdef FEAT_VERTSPLIT
11756 "vertsplit",
11757#endif
11758#ifdef FEAT_VIRTUALEDIT
11759 "virtualedit",
11760#endif
11761#ifdef FEAT_VISUAL
11762 "visual",
11763#endif
11764#ifdef FEAT_VISUALEXTRA
11765 "visualextra",
11766#endif
11767#ifdef FEAT_VREPLACE
11768 "vreplace",
11769#endif
11770#ifdef FEAT_WILDIGN
11771 "wildignore",
11772#endif
11773#ifdef FEAT_WILDMENU
11774 "wildmenu",
11775#endif
11776#ifdef FEAT_WINDOWS
11777 "windows",
11778#endif
11779#ifdef FEAT_WAK
11780 "winaltkeys",
11781#endif
11782#ifdef FEAT_WRITEBACKUP
11783 "writebackup",
11784#endif
11785#ifdef FEAT_XIM
11786 "xim",
11787#endif
11788#ifdef FEAT_XFONTSET
11789 "xfontset",
11790#endif
11791#ifdef USE_XSMP
11792 "xsmp",
11793#endif
11794#ifdef USE_XSMP_INTERACT
11795 "xsmp_interact",
11796#endif
11797#ifdef FEAT_XCLIPBOARD
11798 "xterm_clipboard",
11799#endif
11800#ifdef FEAT_XTERM_SAVE
11801 "xterm_save",
11802#endif
11803#if defined(UNIX) && defined(FEAT_X11)
11804 "X11",
11805#endif
11806 NULL
11807 };
11808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 for (i = 0; has_list[i] != NULL; ++i)
11811 if (STRICMP(name, has_list[i]) == 0)
11812 {
11813 n = TRUE;
11814 break;
11815 }
11816
11817 if (n == FALSE)
11818 {
11819 if (STRNICMP(name, "patch", 5) == 0)
11820 n = has_patch(atoi((char *)name + 5));
11821 else if (STRICMP(name, "vim_starting") == 0)
11822 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011823#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11824 else if (STRICMP(name, "balloon_multiline") == 0)
11825 n = multiline_balloon_available();
11826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827#ifdef DYNAMIC_TCL
11828 else if (STRICMP(name, "tcl") == 0)
11829 n = tcl_enabled(FALSE);
11830#endif
11831#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11832 else if (STRICMP(name, "iconv") == 0)
11833 n = iconv_enabled(FALSE);
11834#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011835#ifdef DYNAMIC_MZSCHEME
11836 else if (STRICMP(name, "mzscheme") == 0)
11837 n = mzscheme_enabled(FALSE);
11838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839#ifdef DYNAMIC_RUBY
11840 else if (STRICMP(name, "ruby") == 0)
11841 n = ruby_enabled(FALSE);
11842#endif
11843#ifdef DYNAMIC_PYTHON
11844 else if (STRICMP(name, "python") == 0)
11845 n = python_enabled(FALSE);
11846#endif
11847#ifdef DYNAMIC_PERL
11848 else if (STRICMP(name, "perl") == 0)
11849 n = perl_enabled(FALSE);
11850#endif
11851#ifdef FEAT_GUI
11852 else if (STRICMP(name, "gui_running") == 0)
11853 n = (gui.in_use || gui.starting);
11854# ifdef FEAT_GUI_W32
11855 else if (STRICMP(name, "gui_win32s") == 0)
11856 n = gui_is_win32s();
11857# endif
11858# ifdef FEAT_BROWSE
11859 else if (STRICMP(name, "browse") == 0)
11860 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11861# endif
11862#endif
11863#ifdef FEAT_SYN_HL
11864 else if (STRICMP(name, "syntax_items") == 0)
11865 n = syntax_present(curbuf);
11866#endif
11867#if defined(WIN3264)
11868 else if (STRICMP(name, "win95") == 0)
11869 n = mch_windows95();
11870#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011871#ifdef FEAT_NETBEANS_INTG
11872 else if (STRICMP(name, "netbeans_enabled") == 0)
11873 n = usingNetbeans;
11874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 }
11876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878}
11879
11880/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011881 * "has_key()" function
11882 */
11883 static void
11884f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 typval_T *argvars;
11886 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011887{
11888 rettv->vval.v_number = 0;
11889 if (argvars[0].v_type != VAR_DICT)
11890 {
11891 EMSG(_(e_dictreq));
11892 return;
11893 }
11894 if (argvars[0].vval.v_dict == NULL)
11895 return;
11896
11897 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011898 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011899}
11900
11901/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011902 * "haslocaldir()" function
11903 */
11904/*ARGSUSED*/
11905 static void
11906f_haslocaldir(argvars, rettv)
11907 typval_T *argvars;
11908 typval_T *rettv;
11909{
11910 rettv->vval.v_number = (curwin->w_localdir != NULL);
11911}
11912
11913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 * "hasmapto()" function
11915 */
11916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *argvars;
11919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920{
11921 char_u *name;
11922 char_u *mode;
11923 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011924 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011927 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 mode = (char_u *)"nvo";
11929 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011932 if (argvars[2].v_type != VAR_UNKNOWN)
11933 abbr = get_tv_number(&argvars[2]);
11934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935
Bram Moolenaar2c932302006-03-18 21:42:09 +000011936 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940}
11941
11942/*
11943 * "histadd()" function
11944 */
11945/*ARGSUSED*/
11946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *argvars;
11949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950{
11951#ifdef FEAT_CMDHIST
11952 int histype;
11953 char_u *str;
11954 char_u buf[NUMBUFLEN];
11955#endif
11956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011957 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 if (check_restricted() || check_secure())
11959 return;
11960#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011961 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11962 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 if (histype >= 0)
11964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 if (*str != NUL)
11967 {
11968 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 return;
11971 }
11972 }
11973#endif
11974}
11975
11976/*
11977 * "histdel()" function
11978 */
11979/*ARGSUSED*/
11980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 typval_T *argvars;
11983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
11985#ifdef FEAT_CMDHIST
11986 int n;
11987 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11991 if (str == NULL)
11992 n = 0;
11993 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011996 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011998 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 else
12001 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 get_tv_string_buf(&argvars[1], buf));
12004 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012006 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007#endif
12008}
12009
12010/*
12011 * "histget()" function
12012 */
12013/*ARGSUSED*/
12014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012016 typval_T *argvars;
12017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018{
12019#ifdef FEAT_CMDHIST
12020 int type;
12021 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012022 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12025 if (str == NULL)
12026 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 {
12029 type = get_histtype(str);
12030 if (argvars[1].v_type == VAR_UNKNOWN)
12031 idx = get_history_idx(type);
12032 else
12033 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12034 /* -1 on type error */
12035 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041}
12042
12043/*
12044 * "histnr()" function
12045 */
12046/*ARGSUSED*/
12047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012049 typval_T *argvars;
12050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052 int i;
12053
12054#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012055 char_u *history = get_tv_string_chk(&argvars[0]);
12056
12057 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 if (i >= HIST_CMD && i < HIST_COUNT)
12059 i = get_history_idx(i);
12060 else
12061#endif
12062 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 * "highlightID(name)" function
12068 */
12069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012071 typval_T *argvars;
12072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075}
12076
12077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012078 * "highlight_exists()" function
12079 */
12080 static void
12081f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012082 typval_T *argvars;
12083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012084{
12085 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12086}
12087
12088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 * "hostname()" function
12090 */
12091/*ARGSUSED*/
12092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
12097 char_u hostname[256];
12098
12099 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->v_type = VAR_STRING;
12101 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102}
12103
12104/*
12105 * iconv() function
12106 */
12107/*ARGSUSED*/
12108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012110 typval_T *argvars;
12111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112{
12113#ifdef FEAT_MBYTE
12114 char_u buf1[NUMBUFLEN];
12115 char_u buf2[NUMBUFLEN];
12116 char_u *from, *to, *str;
12117 vimconv_T vimconv;
12118#endif
12119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->v_type = VAR_STRING;
12121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122
12123#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012124 str = get_tv_string(&argvars[0]);
12125 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12126 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 vimconv.vc_type = CONV_NONE;
12128 convert_setup(&vimconv, from, to);
12129
12130 /* If the encodings are equal, no conversion needed. */
12131 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135
12136 convert_setup(&vimconv, NULL, NULL);
12137 vim_free(from);
12138 vim_free(to);
12139#endif
12140}
12141
12142/*
12143 * "indent()" function
12144 */
12145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012147 typval_T *argvars;
12148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149{
12150 linenr_T lnum;
12151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157}
12158
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012159/*
12160 * "index()" function
12161 */
12162 static void
12163f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012164 typval_T *argvars;
12165 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012166{
Bram Moolenaar33570922005-01-25 22:26:29 +000012167 list_T *l;
12168 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012169 long idx = 0;
12170 int ic = FALSE;
12171
12172 rettv->vval.v_number = -1;
12173 if (argvars[0].v_type != VAR_LIST)
12174 {
12175 EMSG(_(e_listreq));
12176 return;
12177 }
12178 l = argvars[0].vval.v_list;
12179 if (l != NULL)
12180 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012181 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012182 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 int error = FALSE;
12185
Bram Moolenaar758711c2005-02-02 23:11:38 +000012186 /* Start at specified item. Use the cached index that list_find()
12187 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012189 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012190 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012191 ic = get_tv_number_chk(&argvars[3], &error);
12192 if (error)
12193 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012194 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012195
Bram Moolenaar758711c2005-02-02 23:11:38 +000012196 for ( ; item != NULL; item = item->li_next, ++idx)
12197 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012198 {
12199 rettv->vval.v_number = idx;
12200 break;
12201 }
12202 }
12203}
12204
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205static int inputsecret_flag = 0;
12206
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012207static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12208
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012210 * This function is used by f_input() and f_inputdialog() functions. The third
12211 * argument to f_input() specifies the type of completion to use at the
12212 * prompt. The third argument to f_inputdialog() specifies the value to return
12213 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 */
12215 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012216get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012217 typval_T *argvars;
12218 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012219 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012221 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 char_u *p = NULL;
12223 int c;
12224 char_u buf[NUMBUFLEN];
12225 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012226 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012227 int xp_type = EXPAND_NOTHING;
12228 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012231 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232
12233#ifdef NO_CONSOLE_INPUT
12234 /* While starting up, there is no place to enter text. */
12235 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237#endif
12238
12239 cmd_silent = FALSE; /* Want to see the prompt. */
12240 if (prompt != NULL)
12241 {
12242 /* Only the part of the message after the last NL is considered as
12243 * prompt for the command line */
12244 p = vim_strrchr(prompt, '\n');
12245 if (p == NULL)
12246 p = prompt;
12247 else
12248 {
12249 ++p;
12250 c = *p;
12251 *p = NUL;
12252 msg_start();
12253 msg_clr_eos();
12254 msg_puts_attr(prompt, echo_attr);
12255 msg_didout = FALSE;
12256 msg_starthere();
12257 *p = c;
12258 }
12259 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012261 if (argvars[1].v_type != VAR_UNKNOWN)
12262 {
12263 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12264 if (defstr != NULL)
12265 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012267 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012268 {
12269 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012270 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012271 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012272
Bram Moolenaar4463f292005-09-25 22:20:24 +000012273 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012274
Bram Moolenaar4463f292005-09-25 22:20:24 +000012275 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12276 if (xp_name == NULL)
12277 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012279 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012280
Bram Moolenaar4463f292005-09-25 22:20:24 +000012281 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12282 &xp_arg) == FAIL)
12283 return;
12284 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012285 }
12286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012287 if (defstr != NULL)
12288 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012289 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12290 xp_type, xp_arg);
12291
12292 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012294 /* since the user typed this, no need to wait for return */
12295 need_wait_return = FALSE;
12296 msg_didout = FALSE;
12297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 cmd_silent = cmd_silent_save;
12299}
12300
12301/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012302 * "input()" function
12303 * Also handles inputsecret() when inputsecret is set.
12304 */
12305 static void
12306f_input(argvars, rettv)
12307 typval_T *argvars;
12308 typval_T *rettv;
12309{
12310 get_user_input(argvars, rettv, FALSE);
12311}
12312
12313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 * "inputdialog()" function
12315 */
12316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 typval_T *argvars;
12319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
12321#if defined(FEAT_GUI_TEXTDIALOG)
12322 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12323 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12324 {
12325 char_u *message;
12326 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012327 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012329 message = get_tv_string_chk(&argvars[0]);
12330 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012331 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012332 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 else
12334 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012335 if (message != NULL && defstr != NULL
12336 && do_dialog(VIM_QUESTION, NULL, message,
12337 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339 else
12340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 if (message != NULL && defstr != NULL
12342 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012343 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->vval.v_string = vim_strsave(
12345 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 }
12351 else
12352#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012353 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354}
12355
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012356/*
12357 * "inputlist()" function
12358 */
12359 static void
12360f_inputlist(argvars, rettv)
12361 typval_T *argvars;
12362 typval_T *rettv;
12363{
12364 listitem_T *li;
12365 int selected;
12366 int mouse_used;
12367
12368 rettv->vval.v_number = 0;
12369#ifdef NO_CONSOLE_INPUT
12370 /* While starting up, there is no place to enter text. */
12371 if (no_console_input())
12372 return;
12373#endif
12374 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12375 {
12376 EMSG2(_(e_listarg), "inputlist()");
12377 return;
12378 }
12379
12380 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012381 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012382 lines_left = Rows; /* avoid more prompt */
12383 msg_scroll = TRUE;
12384 msg_clr_eos();
12385
12386 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12387 {
12388 msg_puts(get_tv_string(&li->li_tv));
12389 msg_putchar('\n');
12390 }
12391
12392 /* Ask for choice. */
12393 selected = prompt_for_number(&mouse_used);
12394 if (mouse_used)
12395 selected -= lines_left;
12396
12397 rettv->vval.v_number = selected;
12398}
12399
12400
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12402
12403/*
12404 * "inputrestore()" function
12405 */
12406/*ARGSUSED*/
12407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012408f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012409 typval_T *argvars;
12410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411{
12412 if (ga_userinput.ga_len > 0)
12413 {
12414 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12416 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 }
12419 else if (p_verbose > 1)
12420 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012421 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 }
12424}
12425
12426/*
12427 * "inputsave()" function
12428 */
12429/*ARGSUSED*/
12430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *argvars;
12433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012435 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 if (ga_grow(&ga_userinput, 1) == OK)
12437 {
12438 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12439 + ga_userinput.ga_len);
12440 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 }
12443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445}
12446
12447/*
12448 * "inputsecret()" function
12449 */
12450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012452 typval_T *argvars;
12453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454{
12455 ++cmdline_star;
12456 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 --cmdline_star;
12459 --inputsecret_flag;
12460}
12461
12462/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012463 * "insert()" function
12464 */
12465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 typval_T *argvars;
12468 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012469{
12470 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012471 listitem_T *item;
12472 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012473 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012474
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012475 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012477 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012478 else if ((l = argvars[0].vval.v_list) != NULL
12479 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012480 {
12481 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012482 before = get_tv_number_chk(&argvars[2], &error);
12483 if (error)
12484 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012485
Bram Moolenaar758711c2005-02-02 23:11:38 +000012486 if (before == l->lv_len)
12487 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012488 else
12489 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012490 item = list_find(l, before);
12491 if (item == NULL)
12492 {
12493 EMSGN(_(e_listidx), before);
12494 l = NULL;
12495 }
12496 }
12497 if (l != NULL)
12498 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012499 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012500 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012501 }
12502 }
12503}
12504
12505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 * "isdirectory()" function
12507 */
12508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 typval_T *argvars;
12511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514}
12515
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012516/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012517 * "islocked()" function
12518 */
12519 static void
12520f_islocked(argvars, rettv)
12521 typval_T *argvars;
12522 typval_T *rettv;
12523{
12524 lval_T lv;
12525 char_u *end;
12526 dictitem_T *di;
12527
12528 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012529 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12530 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012531 if (end != NULL && lv.ll_name != NULL)
12532 {
12533 if (*end != NUL)
12534 EMSG(_(e_trailing));
12535 else
12536 {
12537 if (lv.ll_tv == NULL)
12538 {
12539 if (check_changedtick(lv.ll_name))
12540 rettv->vval.v_number = 1; /* always locked */
12541 else
12542 {
12543 di = find_var(lv.ll_name, NULL);
12544 if (di != NULL)
12545 {
12546 /* Consider a variable locked when:
12547 * 1. the variable itself is locked
12548 * 2. the value of the variable is locked.
12549 * 3. the List or Dict value is locked.
12550 */
12551 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12552 || tv_islocked(&di->di_tv));
12553 }
12554 }
12555 }
12556 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012557 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012558 else if (lv.ll_newkey != NULL)
12559 EMSG2(_(e_dictkey), lv.ll_newkey);
12560 else if (lv.ll_list != NULL)
12561 /* List item. */
12562 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12563 else
12564 /* Dictionary item. */
12565 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12566 }
12567 }
12568
12569 clear_lval(&lv);
12570}
12571
Bram Moolenaar33570922005-01-25 22:26:29 +000012572static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012573
12574/*
12575 * Turn a dict into a list:
12576 * "what" == 0: list of keys
12577 * "what" == 1: list of values
12578 * "what" == 2: list of items
12579 */
12580 static void
12581dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 typval_T *argvars;
12583 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012584 int what;
12585{
Bram Moolenaar33570922005-01-25 22:26:29 +000012586 list_T *l2;
12587 dictitem_T *di;
12588 hashitem_T *hi;
12589 listitem_T *li;
12590 listitem_T *li2;
12591 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012592 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012593
12594 rettv->vval.v_number = 0;
12595 if (argvars[0].v_type != VAR_DICT)
12596 {
12597 EMSG(_(e_dictreq));
12598 return;
12599 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012600 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012601 return;
12602
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012603 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012604 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012605
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012606 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012607 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012609 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012611 --todo;
12612 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012613
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012614 li = listitem_alloc();
12615 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012616 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012617 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012618
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012619 if (what == 0)
12620 {
12621 /* keys() */
12622 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012623 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012624 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12625 }
12626 else if (what == 1)
12627 {
12628 /* values() */
12629 copy_tv(&di->di_tv, &li->li_tv);
12630 }
12631 else
12632 {
12633 /* items() */
12634 l2 = list_alloc();
12635 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012636 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012637 li->li_tv.vval.v_list = l2;
12638 if (l2 == NULL)
12639 break;
12640 ++l2->lv_refcount;
12641
12642 li2 = listitem_alloc();
12643 if (li2 == NULL)
12644 break;
12645 list_append(l2, li2);
12646 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012647 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012648 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12649
12650 li2 = listitem_alloc();
12651 if (li2 == NULL)
12652 break;
12653 list_append(l2, li2);
12654 copy_tv(&di->di_tv, &li2->li_tv);
12655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012656 }
12657 }
12658}
12659
12660/*
12661 * "items(dict)" function
12662 */
12663 static void
12664f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012665 typval_T *argvars;
12666 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012667{
12668 dict_list(argvars, rettv, 2);
12669}
12670
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012672 * "join()" function
12673 */
12674 static void
12675f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012678{
12679 garray_T ga;
12680 char_u *sep;
12681
12682 rettv->vval.v_number = 0;
12683 if (argvars[0].v_type != VAR_LIST)
12684 {
12685 EMSG(_(e_listreq));
12686 return;
12687 }
12688 if (argvars[0].vval.v_list == NULL)
12689 return;
12690 if (argvars[1].v_type == VAR_UNKNOWN)
12691 sep = (char_u *)" ";
12692 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012694
12695 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012696
12697 if (sep != NULL)
12698 {
12699 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012700 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 ga_append(&ga, NUL);
12702 rettv->vval.v_string = (char_u *)ga.ga_data;
12703 }
12704 else
12705 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012706}
12707
12708/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012709 * "keys()" function
12710 */
12711 static void
12712f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 typval_T *argvars;
12714 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012715{
12716 dict_list(argvars, rettv, 0);
12717}
12718
12719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 * "last_buffer_nr()" function.
12721 */
12722/*ARGSUSED*/
12723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 typval_T *argvars;
12726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727{
12728 int n = 0;
12729 buf_T *buf;
12730
12731 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12732 if (n < buf->b_fnum)
12733 n = buf->b_fnum;
12734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012736}
12737
12738/*
12739 * "len()" function
12740 */
12741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012743 typval_T *argvars;
12744 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012745{
12746 switch (argvars[0].v_type)
12747 {
12748 case VAR_STRING:
12749 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 rettv->vval.v_number = (varnumber_T)STRLEN(
12751 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012752 break;
12753 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012755 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012756 case VAR_DICT:
12757 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12758 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012759 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012760 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012761 break;
12762 }
12763}
12764
Bram Moolenaar33570922005-01-25 22:26:29 +000012765static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012766
12767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012769 typval_T *argvars;
12770 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012771 int type;
12772{
12773#ifdef FEAT_LIBCALL
12774 char_u *string_in;
12775 char_u **string_result;
12776 int nr_result;
12777#endif
12778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012780 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012782 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012784
12785 if (check_restricted() || check_secure())
12786 return;
12787
12788#ifdef FEAT_LIBCALL
12789 /* The first two args must be strings, otherwise its meaningless */
12790 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12791 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012792 string_in = NULL;
12793 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012794 string_in = argvars[2].vval.v_string;
12795 if (type == VAR_NUMBER)
12796 string_result = NULL;
12797 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012799 if (mch_libcall(argvars[0].vval.v_string,
12800 argvars[1].vval.v_string,
12801 string_in,
12802 argvars[2].vval.v_number,
12803 string_result,
12804 &nr_result) == OK
12805 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012807 }
12808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809}
12810
12811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012812 * "libcall()" function
12813 */
12814 static void
12815f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 typval_T *argvars;
12817 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012818{
12819 libcall_common(argvars, rettv, VAR_STRING);
12820}
12821
12822/*
12823 * "libcallnr()" function
12824 */
12825 static void
12826f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 typval_T *argvars;
12828 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012829{
12830 libcall_common(argvars, rettv, VAR_NUMBER);
12831}
12832
12833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 * "line(string)" function
12835 */
12836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *argvars;
12839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840{
12841 linenr_T lnum = 0;
12842 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012843 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012845 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 if (fp != NULL)
12847 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849}
12850
12851/*
12852 * "line2byte(lnum)" function
12853 */
12854/*ARGSUSED*/
12855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012857 typval_T *argvars;
12858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859{
12860#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862#else
12863 linenr_T lnum;
12864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12870 if (rettv->vval.v_number >= 0)
12871 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872#endif
12873}
12874
12875/*
12876 * "lispindent(lnum)" function
12877 */
12878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *argvars;
12881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882{
12883#ifdef FEAT_LISP
12884 pos_T pos;
12885 linenr_T lnum;
12886
12887 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12890 {
12891 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 curwin->w_cursor = pos;
12894 }
12895 else
12896#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898}
12899
12900/*
12901 * "localtime()" function
12902 */
12903/*ARGSUSED*/
12904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012906 typval_T *argvars;
12907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910}
12911
Bram Moolenaar33570922005-01-25 22:26:29 +000012912static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913
12914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 int exact;
12919{
12920 char_u *keys;
12921 char_u *which;
12922 char_u buf[NUMBUFLEN];
12923 char_u *keys_buf = NULL;
12924 char_u *rhs;
12925 int mode;
12926 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012927 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928
12929 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 rettv->v_type = VAR_STRING;
12931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 if (*keys == NUL)
12935 return;
12936
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012937 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012939 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012940 if (argvars[2].v_type != VAR_UNKNOWN)
12941 abbr = get_tv_number(&argvars[2]);
12942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 else
12944 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012945 if (which == NULL)
12946 return;
12947
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 mode = get_map_mode(&which, 0);
12949
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012950 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012951 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 vim_free(keys_buf);
12953 if (rhs != NULL)
12954 {
12955 ga_init(&ga);
12956 ga.ga_itemsize = 1;
12957 ga.ga_growsize = 40;
12958
12959 while (*rhs != NUL)
12960 ga_concat(&ga, str2special(&rhs, FALSE));
12961
12962 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 }
12965}
12966
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012967#ifdef FEAT_FLOAT
12968/*
12969 * "log10()" function
12970 */
12971 static void
12972f_log10(argvars, rettv)
12973 typval_T *argvars;
12974 typval_T *rettv;
12975{
12976 float_T f;
12977
12978 rettv->v_type = VAR_FLOAT;
12979 if (get_float_arg(argvars, &f) == OK)
12980 rettv->vval.v_float = log10(f);
12981 else
12982 rettv->vval.v_float = 0.0;
12983}
12984#endif
12985
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012987 * "map()" function
12988 */
12989 static void
12990f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012991 typval_T *argvars;
12992 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012993{
12994 filter_map(argvars, rettv, TRUE);
12995}
12996
12997/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012998 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 */
13000 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013002 typval_T *argvars;
13003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013005 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006}
13007
13008/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013009 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 */
13011 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013012f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013013 typval_T *argvars;
13014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013016 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017}
13018
Bram Moolenaar33570922005-01-25 22:26:29 +000013019static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020
13021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 typval_T *argvars;
13024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 int type;
13026{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013027 char_u *str = NULL;
13028 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 char_u *pat;
13030 regmatch_T regmatch;
13031 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013032 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 char_u *save_cpo;
13034 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013035 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013036 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013037 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013038 list_T *l = NULL;
13039 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013040 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013041 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042
13043 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13044 save_cpo = p_cpo;
13045 p_cpo = (char_u *)"";
13046
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013047 rettv->vval.v_number = -1;
13048 if (type == 3)
13049 {
13050 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013051 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013052 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013053 }
13054 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->v_type = VAR_STRING;
13057 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 if (argvars[0].v_type == VAR_LIST)
13061 {
13062 if ((l = argvars[0].vval.v_list) == NULL)
13063 goto theend;
13064 li = l->lv_first;
13065 }
13066 else
13067 expr = str = get_tv_string(&argvars[0]);
13068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013069 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13070 if (pat == NULL)
13071 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013073 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013075 int error = FALSE;
13076
13077 start = get_tv_number_chk(&argvars[2], &error);
13078 if (error)
13079 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013080 if (l != NULL)
13081 {
13082 li = list_find(l, start);
13083 if (li == NULL)
13084 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013085 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013086 }
13087 else
13088 {
13089 if (start < 0)
13090 start = 0;
13091 if (start > (long)STRLEN(str))
13092 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013093 /* When "count" argument is there ignore matches before "start",
13094 * otherwise skip part of the string. Differs when pattern is "^"
13095 * or "\<". */
13096 if (argvars[3].v_type != VAR_UNKNOWN)
13097 startcol = start;
13098 else
13099 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013100 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013101
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013102 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 nth = get_tv_number_chk(&argvars[3], &error);
13104 if (error)
13105 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 }
13107
13108 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13109 if (regmatch.regprog != NULL)
13110 {
13111 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013112
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013113 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013114 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013115 if (l != NULL)
13116 {
13117 if (li == NULL)
13118 {
13119 match = FALSE;
13120 break;
13121 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013122 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013123 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013124 if (str == NULL)
13125 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013126 }
13127
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013128 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013129
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013130 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013131 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013132 if (l == NULL && !match)
13133 break;
13134
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013135 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013136 if (l != NULL)
13137 {
13138 li = li->li_next;
13139 ++idx;
13140 }
13141 else
13142 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013143#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013144 startcol = (colnr_T)(regmatch.startp[0]
13145 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013146#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013147 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013148#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013149 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013150 }
13151
13152 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013154 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013155 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013156 int i;
13157
13158 /* return list with matched string and submatches */
13159 for (i = 0; i < NSUBEXP; ++i)
13160 {
13161 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013162 {
13163 if (list_append_string(rettv->vval.v_list,
13164 (char_u *)"", 0) == FAIL)
13165 break;
13166 }
13167 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013168 regmatch.startp[i],
13169 (int)(regmatch.endp[i] - regmatch.startp[i]))
13170 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013171 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013172 }
13173 }
13174 else if (type == 2)
13175 {
13176 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013177 if (l != NULL)
13178 copy_tv(&li->li_tv, rettv);
13179 else
13180 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013182 }
13183 else if (l != NULL)
13184 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 else
13186 {
13187 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 (varnumber_T)(regmatch.startp[0] - str);
13190 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013193 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 }
13195 }
13196 vim_free(regmatch.regprog);
13197 }
13198
13199theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013200 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 p_cpo = save_cpo;
13202}
13203
13204/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013205 * "match()" function
13206 */
13207 static void
13208f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013209 typval_T *argvars;
13210 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013211{
13212 find_some_match(argvars, rettv, 1);
13213}
13214
13215/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013216 * "matchadd()" function
13217 */
13218 static void
13219f_matchadd(argvars, rettv)
13220 typval_T *argvars;
13221 typval_T *rettv;
13222{
13223#ifdef FEAT_SEARCH_EXTRA
13224 char_u buf[NUMBUFLEN];
13225 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13226 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13227 int prio = 10; /* default priority */
13228 int id = -1;
13229 int error = FALSE;
13230
13231 rettv->vval.v_number = -1;
13232
13233 if (grp == NULL || pat == NULL)
13234 return;
13235 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013236 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013237 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013238 if (argvars[3].v_type != VAR_UNKNOWN)
13239 id = get_tv_number_chk(&argvars[3], &error);
13240 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013241 if (error == TRUE)
13242 return;
13243 if (id >= 1 && id <= 3)
13244 {
13245 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13246 return;
13247 }
13248
13249 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13250#endif
13251}
13252
13253/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013254 * "matcharg()" function
13255 */
13256 static void
13257f_matcharg(argvars, rettv)
13258 typval_T *argvars;
13259 typval_T *rettv;
13260{
13261 if (rettv_list_alloc(rettv) == OK)
13262 {
13263#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013264 int id = get_tv_number(&argvars[0]);
13265 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013266
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013267 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013268 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013269 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13270 {
13271 list_append_string(rettv->vval.v_list,
13272 syn_id2name(m->hlg_id), -1);
13273 list_append_string(rettv->vval.v_list, m->pattern, -1);
13274 }
13275 else
13276 {
13277 list_append_string(rettv->vval.v_list, NUL, -1);
13278 list_append_string(rettv->vval.v_list, NUL, -1);
13279 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013280 }
13281#endif
13282 }
13283}
13284
13285/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013286 * "matchdelete()" function
13287 */
13288 static void
13289f_matchdelete(argvars, rettv)
13290 typval_T *argvars;
13291 typval_T *rettv;
13292{
13293#ifdef FEAT_SEARCH_EXTRA
13294 rettv->vval.v_number = match_delete(curwin,
13295 (int)get_tv_number(&argvars[0]), TRUE);
13296#endif
13297}
13298
13299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013300 * "matchend()" function
13301 */
13302 static void
13303f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013304 typval_T *argvars;
13305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013306{
13307 find_some_match(argvars, rettv, 0);
13308}
13309
13310/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013311 * "matchlist()" function
13312 */
13313 static void
13314f_matchlist(argvars, rettv)
13315 typval_T *argvars;
13316 typval_T *rettv;
13317{
13318 find_some_match(argvars, rettv, 3);
13319}
13320
13321/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322 * "matchstr()" function
13323 */
13324 static void
13325f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 typval_T *argvars;
13327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013328{
13329 find_some_match(argvars, rettv, 2);
13330}
13331
Bram Moolenaar33570922005-01-25 22:26:29 +000013332static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013333
13334 static void
13335max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013336 typval_T *argvars;
13337 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013338 int domax;
13339{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013340 long n = 0;
13341 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013342 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013343
13344 if (argvars[0].v_type == VAR_LIST)
13345 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 list_T *l;
13347 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013348
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013349 l = argvars[0].vval.v_list;
13350 if (l != NULL)
13351 {
13352 li = l->lv_first;
13353 if (li != NULL)
13354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013355 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013356 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013357 {
13358 li = li->li_next;
13359 if (li == NULL)
13360 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013361 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013362 if (domax ? i > n : i < n)
13363 n = i;
13364 }
13365 }
13366 }
13367 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013368 else if (argvars[0].v_type == VAR_DICT)
13369 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013371 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013372 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013373 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013374
13375 d = argvars[0].vval.v_dict;
13376 if (d != NULL)
13377 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013378 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013380 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013381 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013382 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013383 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013385 if (first)
13386 {
13387 n = i;
13388 first = FALSE;
13389 }
13390 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013391 n = i;
13392 }
13393 }
13394 }
13395 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013396 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013397 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013398 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013399}
13400
13401/*
13402 * "max()" function
13403 */
13404 static void
13405f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *argvars;
13407 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013408{
13409 max_min(argvars, rettv, TRUE);
13410}
13411
13412/*
13413 * "min()" function
13414 */
13415 static void
13416f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 typval_T *argvars;
13418 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013419{
13420 max_min(argvars, rettv, FALSE);
13421}
13422
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013423static int mkdir_recurse __ARGS((char_u *dir, int prot));
13424
13425/*
13426 * Create the directory in which "dir" is located, and higher levels when
13427 * needed.
13428 */
13429 static int
13430mkdir_recurse(dir, prot)
13431 char_u *dir;
13432 int prot;
13433{
13434 char_u *p;
13435 char_u *updir;
13436 int r = FAIL;
13437
13438 /* Get end of directory name in "dir".
13439 * We're done when it's "/" or "c:/". */
13440 p = gettail_sep(dir);
13441 if (p <= get_past_head(dir))
13442 return OK;
13443
13444 /* If the directory exists we're done. Otherwise: create it.*/
13445 updir = vim_strnsave(dir, (int)(p - dir));
13446 if (updir == NULL)
13447 return FAIL;
13448 if (mch_isdir(updir))
13449 r = OK;
13450 else if (mkdir_recurse(updir, prot) == OK)
13451 r = vim_mkdir_emsg(updir, prot);
13452 vim_free(updir);
13453 return r;
13454}
13455
13456#ifdef vim_mkdir
13457/*
13458 * "mkdir()" function
13459 */
13460 static void
13461f_mkdir(argvars, rettv)
13462 typval_T *argvars;
13463 typval_T *rettv;
13464{
13465 char_u *dir;
13466 char_u buf[NUMBUFLEN];
13467 int prot = 0755;
13468
13469 rettv->vval.v_number = FAIL;
13470 if (check_restricted() || check_secure())
13471 return;
13472
13473 dir = get_tv_string_buf(&argvars[0], buf);
13474 if (argvars[1].v_type != VAR_UNKNOWN)
13475 {
13476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 prot = get_tv_number_chk(&argvars[2], NULL);
13478 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013479 mkdir_recurse(dir, prot);
13480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013481 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013482}
13483#endif
13484
Bram Moolenaar0d660222005-01-07 21:51:51 +000013485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 * "mode()" function
13487 */
13488/*ARGSUSED*/
13489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013491 typval_T *argvars;
13492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013494 char_u buf[3];
13495
13496 buf[1] = NUL;
13497 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498
13499#ifdef FEAT_VISUAL
13500 if (VIsual_active)
13501 {
13502 if (VIsual_select)
13503 buf[0] = VIsual_mode + 's' - 'v';
13504 else
13505 buf[0] = VIsual_mode;
13506 }
13507 else
13508#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013509 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13510 || State == CONFIRM)
13511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013513 if (State == ASKMORE)
13514 buf[1] = 'm';
13515 else if (State == CONFIRM)
13516 buf[1] = '?';
13517 }
13518 else if (State == EXTERNCMD)
13519 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 else if (State & INSERT)
13521 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013522#ifdef FEAT_VREPLACE
13523 if (State & VREPLACE_FLAG)
13524 {
13525 buf[0] = 'R';
13526 buf[1] = 'v';
13527 }
13528 else
13529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 if (State & REPLACE_FLAG)
13531 buf[0] = 'R';
13532 else
13533 buf[0] = 'i';
13534 }
13535 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013538 if (exmode_active)
13539 buf[1] = 'v';
13540 }
13541 else if (exmode_active)
13542 {
13543 buf[0] = 'c';
13544 buf[1] = 'e';
13545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013549 if (finish_op)
13550 buf[1] = 'o';
13551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013553 /* Clear out the minor mode when the argument is not a non-zero number or
13554 * non-empty string. */
13555 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013556 buf[1] = NUL;
13557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558 rettv->vval.v_string = vim_strsave(buf);
13559 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560}
13561
13562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013563 * "nextnonblank()" function
13564 */
13565 static void
13566f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013569{
13570 linenr_T lnum;
13571
13572 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013575 {
13576 lnum = 0;
13577 break;
13578 }
13579 if (*skipwhite(ml_get(lnum)) != NUL)
13580 break;
13581 }
13582 rettv->vval.v_number = lnum;
13583}
13584
13585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 * "nr2char()" function
13587 */
13588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 typval_T *argvars;
13591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592{
13593 char_u buf[NUMBUFLEN];
13594
13595#ifdef FEAT_MBYTE
13596 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 else
13599#endif
13600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 buf[1] = NUL;
13603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->v_type = VAR_STRING;
13605 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013606}
13607
13608/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013609 * "pathshorten()" function
13610 */
13611 static void
13612f_pathshorten(argvars, rettv)
13613 typval_T *argvars;
13614 typval_T *rettv;
13615{
13616 char_u *p;
13617
13618 rettv->v_type = VAR_STRING;
13619 p = get_tv_string_chk(&argvars[0]);
13620 if (p == NULL)
13621 rettv->vval.v_string = NULL;
13622 else
13623 {
13624 p = vim_strsave(p);
13625 rettv->vval.v_string = p;
13626 if (p != NULL)
13627 shorten_dir(p);
13628 }
13629}
13630
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013631#ifdef FEAT_FLOAT
13632/*
13633 * "pow()" function
13634 */
13635 static void
13636f_pow(argvars, rettv)
13637 typval_T *argvars;
13638 typval_T *rettv;
13639{
13640 float_T fx, fy;
13641
13642 rettv->v_type = VAR_FLOAT;
13643 if (get_float_arg(argvars, &fx) == OK
13644 && get_float_arg(&argvars[1], &fy) == OK)
13645 rettv->vval.v_float = pow(fx, fy);
13646 else
13647 rettv->vval.v_float = 0.0;
13648}
13649#endif
13650
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013651/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013652 * "prevnonblank()" function
13653 */
13654 static void
13655f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013656 typval_T *argvars;
13657 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013658{
13659 linenr_T lnum;
13660
13661 lnum = get_tv_lnum(argvars);
13662 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13663 lnum = 0;
13664 else
13665 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13666 --lnum;
13667 rettv->vval.v_number = lnum;
13668}
13669
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013670#ifdef HAVE_STDARG_H
13671/* This dummy va_list is here because:
13672 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13673 * - locally in the function results in a "used before set" warning
13674 * - using va_start() to initialize it gives "function with fixed args" error */
13675static va_list ap;
13676#endif
13677
Bram Moolenaar8c711452005-01-14 21:53:12 +000013678/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013679 * "printf()" function
13680 */
13681 static void
13682f_printf(argvars, rettv)
13683 typval_T *argvars;
13684 typval_T *rettv;
13685{
13686 rettv->v_type = VAR_STRING;
13687 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013688#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013689 {
13690 char_u buf[NUMBUFLEN];
13691 int len;
13692 char_u *s;
13693 int saved_did_emsg = did_emsg;
13694 char *fmt;
13695
13696 /* Get the required length, allocate the buffer and do it for real. */
13697 did_emsg = FALSE;
13698 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013699 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013700 if (!did_emsg)
13701 {
13702 s = alloc(len + 1);
13703 if (s != NULL)
13704 {
13705 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013706 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013707 }
13708 }
13709 did_emsg |= saved_did_emsg;
13710 }
13711#endif
13712}
13713
13714/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013715 * "pumvisible()" function
13716 */
13717/*ARGSUSED*/
13718 static void
13719f_pumvisible(argvars, rettv)
13720 typval_T *argvars;
13721 typval_T *rettv;
13722{
13723 rettv->vval.v_number = 0;
13724#ifdef FEAT_INS_EXPAND
13725 if (pum_visible())
13726 rettv->vval.v_number = 1;
13727#endif
13728}
13729
13730/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013731 * "range()" function
13732 */
13733 static void
13734f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013735 typval_T *argvars;
13736 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013737{
13738 long start;
13739 long end;
13740 long stride = 1;
13741 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013742 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013745 if (argvars[1].v_type == VAR_UNKNOWN)
13746 {
13747 end = start - 1;
13748 start = 0;
13749 }
13750 else
13751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013753 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013754 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013755 }
13756
13757 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013758 if (error)
13759 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013760 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013761 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013762 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013763 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013764 else
13765 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013766 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013767 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013768 if (list_append_number(rettv->vval.v_list,
13769 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013770 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013771 }
13772}
13773
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013774/*
13775 * "readfile()" function
13776 */
13777 static void
13778f_readfile(argvars, rettv)
13779 typval_T *argvars;
13780 typval_T *rettv;
13781{
13782 int binary = FALSE;
13783 char_u *fname;
13784 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013785 listitem_T *li;
13786#define FREAD_SIZE 200 /* optimized for text lines */
13787 char_u buf[FREAD_SIZE];
13788 int readlen; /* size of last fread() */
13789 int buflen; /* nr of valid chars in buf[] */
13790 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13791 int tolist; /* first byte in buf[] still to be put in list */
13792 int chop; /* how many CR to chop off */
13793 char_u *prev = NULL; /* previously read bytes, if any */
13794 int prevlen = 0; /* length of "prev" if not NULL */
13795 char_u *s;
13796 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013797 long maxline = MAXLNUM;
13798 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013799
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013800 if (argvars[1].v_type != VAR_UNKNOWN)
13801 {
13802 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13803 binary = TRUE;
13804 if (argvars[2].v_type != VAR_UNKNOWN)
13805 maxline = get_tv_number(&argvars[2]);
13806 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013807
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013808 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013809 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013810
13811 /* Always open the file in binary mode, library functions have a mind of
13812 * their own about CR-LF conversion. */
13813 fname = get_tv_string(&argvars[0]);
13814 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13815 {
13816 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13817 return;
13818 }
13819
13820 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013821 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013822 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013823 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013824 buflen = filtd + readlen;
13825 tolist = 0;
13826 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13827 {
13828 if (buf[filtd] == '\n' || readlen <= 0)
13829 {
13830 /* Only when in binary mode add an empty list item when the
13831 * last line ends in a '\n'. */
13832 if (!binary && readlen == 0 && filtd == 0)
13833 break;
13834
13835 /* Found end-of-line or end-of-file: add a text line to the
13836 * list. */
13837 chop = 0;
13838 if (!binary)
13839 while (filtd - chop - 1 >= tolist
13840 && buf[filtd - chop - 1] == '\r')
13841 ++chop;
13842 len = filtd - tolist - chop;
13843 if (prev == NULL)
13844 s = vim_strnsave(buf + tolist, len);
13845 else
13846 {
13847 s = alloc((unsigned)(prevlen + len + 1));
13848 if (s != NULL)
13849 {
13850 mch_memmove(s, prev, prevlen);
13851 vim_free(prev);
13852 prev = NULL;
13853 mch_memmove(s + prevlen, buf + tolist, len);
13854 s[prevlen + len] = NUL;
13855 }
13856 }
13857 tolist = filtd + 1;
13858
13859 li = listitem_alloc();
13860 if (li == NULL)
13861 {
13862 vim_free(s);
13863 break;
13864 }
13865 li->li_tv.v_type = VAR_STRING;
13866 li->li_tv.v_lock = 0;
13867 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013868 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013869
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013870 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013871 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013872 if (readlen <= 0)
13873 break;
13874 }
13875 else if (buf[filtd] == NUL)
13876 buf[filtd] = '\n';
13877 }
13878 if (readlen <= 0)
13879 break;
13880
13881 if (tolist == 0)
13882 {
13883 /* "buf" is full, need to move text to an allocated buffer */
13884 if (prev == NULL)
13885 {
13886 prev = vim_strnsave(buf, buflen);
13887 prevlen = buflen;
13888 }
13889 else
13890 {
13891 s = alloc((unsigned)(prevlen + buflen));
13892 if (s != NULL)
13893 {
13894 mch_memmove(s, prev, prevlen);
13895 mch_memmove(s + prevlen, buf, buflen);
13896 vim_free(prev);
13897 prev = s;
13898 prevlen += buflen;
13899 }
13900 }
13901 filtd = 0;
13902 }
13903 else
13904 {
13905 mch_memmove(buf, buf + tolist, buflen - tolist);
13906 filtd -= tolist;
13907 }
13908 }
13909
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013910 /*
13911 * For a negative line count use only the lines at the end of the file,
13912 * free the rest.
13913 */
13914 if (maxline < 0)
13915 while (cnt > -maxline)
13916 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013917 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013918 --cnt;
13919 }
13920
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013921 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013922 fclose(fd);
13923}
13924
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013925#if defined(FEAT_RELTIME)
13926static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13927
13928/*
13929 * Convert a List to proftime_T.
13930 * Return FAIL when there is something wrong.
13931 */
13932 static int
13933list2proftime(arg, tm)
13934 typval_T *arg;
13935 proftime_T *tm;
13936{
13937 long n1, n2;
13938 int error = FALSE;
13939
13940 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13941 || arg->vval.v_list->lv_len != 2)
13942 return FAIL;
13943 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13944 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13945# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013946 tm->HighPart = n1;
13947 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013948# else
13949 tm->tv_sec = n1;
13950 tm->tv_usec = n2;
13951# endif
13952 return error ? FAIL : OK;
13953}
13954#endif /* FEAT_RELTIME */
13955
13956/*
13957 * "reltime()" function
13958 */
13959 static void
13960f_reltime(argvars, rettv)
13961 typval_T *argvars;
13962 typval_T *rettv;
13963{
13964#ifdef FEAT_RELTIME
13965 proftime_T res;
13966 proftime_T start;
13967
13968 if (argvars[0].v_type == VAR_UNKNOWN)
13969 {
13970 /* No arguments: get current time. */
13971 profile_start(&res);
13972 }
13973 else if (argvars[1].v_type == VAR_UNKNOWN)
13974 {
13975 if (list2proftime(&argvars[0], &res) == FAIL)
13976 return;
13977 profile_end(&res);
13978 }
13979 else
13980 {
13981 /* Two arguments: compute the difference. */
13982 if (list2proftime(&argvars[0], &start) == FAIL
13983 || list2proftime(&argvars[1], &res) == FAIL)
13984 return;
13985 profile_sub(&res, &start);
13986 }
13987
13988 if (rettv_list_alloc(rettv) == OK)
13989 {
13990 long n1, n2;
13991
13992# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013993 n1 = res.HighPart;
13994 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013995# else
13996 n1 = res.tv_sec;
13997 n2 = res.tv_usec;
13998# endif
13999 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14000 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14001 }
14002#endif
14003}
14004
14005/*
14006 * "reltimestr()" function
14007 */
14008 static void
14009f_reltimestr(argvars, rettv)
14010 typval_T *argvars;
14011 typval_T *rettv;
14012{
14013#ifdef FEAT_RELTIME
14014 proftime_T tm;
14015#endif
14016
14017 rettv->v_type = VAR_STRING;
14018 rettv->vval.v_string = NULL;
14019#ifdef FEAT_RELTIME
14020 if (list2proftime(&argvars[0], &tm) == OK)
14021 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14022#endif
14023}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014024
Bram Moolenaar0d660222005-01-07 21:51:51 +000014025#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14026static void make_connection __ARGS((void));
14027static int check_connection __ARGS((void));
14028
14029 static void
14030make_connection()
14031{
14032 if (X_DISPLAY == NULL
14033# ifdef FEAT_GUI
14034 && !gui.in_use
14035# endif
14036 )
14037 {
14038 x_force_connect = TRUE;
14039 setup_term_clip();
14040 x_force_connect = FALSE;
14041 }
14042}
14043
14044 static int
14045check_connection()
14046{
14047 make_connection();
14048 if (X_DISPLAY == NULL)
14049 {
14050 EMSG(_("E240: No connection to Vim server"));
14051 return FAIL;
14052 }
14053 return OK;
14054}
14055#endif
14056
14057#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014058static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059
14060 static void
14061remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014062 typval_T *argvars;
14063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014064 int expr;
14065{
14066 char_u *server_name;
14067 char_u *keys;
14068 char_u *r = NULL;
14069 char_u buf[NUMBUFLEN];
14070# ifdef WIN32
14071 HWND w;
14072# else
14073 Window w;
14074# endif
14075
14076 if (check_restricted() || check_secure())
14077 return;
14078
14079# ifdef FEAT_X11
14080 if (check_connection() == FAIL)
14081 return;
14082# endif
14083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 server_name = get_tv_string_chk(&argvars[0]);
14085 if (server_name == NULL)
14086 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014087 keys = get_tv_string_buf(&argvars[1], buf);
14088# ifdef WIN32
14089 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14090# else
14091 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14092 < 0)
14093# endif
14094 {
14095 if (r != NULL)
14096 EMSG(r); /* sending worked but evaluation failed */
14097 else
14098 EMSG2(_("E241: Unable to send to %s"), server_name);
14099 return;
14100 }
14101
14102 rettv->vval.v_string = r;
14103
14104 if (argvars[2].v_type != VAR_UNKNOWN)
14105 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014106 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014107 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014109
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014110 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 v.di_tv.v_type = VAR_STRING;
14112 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 idvar = get_tv_string_chk(&argvars[2]);
14114 if (idvar != NULL)
14115 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014117 }
14118}
14119#endif
14120
14121/*
14122 * "remote_expr()" function
14123 */
14124/*ARGSUSED*/
14125 static void
14126f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014127 typval_T *argvars;
14128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129{
14130 rettv->v_type = VAR_STRING;
14131 rettv->vval.v_string = NULL;
14132#ifdef FEAT_CLIENTSERVER
14133 remote_common(argvars, rettv, TRUE);
14134#endif
14135}
14136
14137/*
14138 * "remote_foreground()" function
14139 */
14140/*ARGSUSED*/
14141 static void
14142f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *argvars;
14144 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014145{
14146 rettv->vval.v_number = 0;
14147#ifdef FEAT_CLIENTSERVER
14148# ifdef WIN32
14149 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150 {
14151 char_u *server_name = get_tv_string_chk(&argvars[0]);
14152
14153 if (server_name != NULL)
14154 serverForeground(server_name);
14155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156# else
14157 /* Send a foreground() expression to the server. */
14158 argvars[1].v_type = VAR_STRING;
14159 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14160 argvars[2].v_type = VAR_UNKNOWN;
14161 remote_common(argvars, rettv, TRUE);
14162 vim_free(argvars[1].vval.v_string);
14163# endif
14164#endif
14165}
14166
14167/*ARGSUSED*/
14168 static void
14169f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 typval_T *argvars;
14171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172{
14173#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014174 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014175 char_u *s = NULL;
14176# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014177 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014178# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014180
14181 if (check_restricted() || check_secure())
14182 {
14183 rettv->vval.v_number = -1;
14184 return;
14185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 serverid = get_tv_string_chk(&argvars[0]);
14187 if (serverid == NULL)
14188 {
14189 rettv->vval.v_number = -1;
14190 return; /* type error; errmsg already given */
14191 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014192# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014193 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194 if (n == 0)
14195 rettv->vval.v_number = -1;
14196 else
14197 {
14198 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14199 rettv->vval.v_number = (s != NULL);
14200 }
14201# else
14202 rettv->vval.v_number = 0;
14203 if (check_connection() == FAIL)
14204 return;
14205
14206 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208# endif
14209
14210 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 char_u *retvar;
14213
Bram Moolenaar33570922005-01-25 22:26:29 +000014214 v.di_tv.v_type = VAR_STRING;
14215 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 retvar = get_tv_string_chk(&argvars[1]);
14217 if (retvar != NULL)
14218 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014220 }
14221#else
14222 rettv->vval.v_number = -1;
14223#endif
14224}
14225
14226/*ARGSUSED*/
14227 static void
14228f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *argvars;
14230 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014231{
14232 char_u *r = NULL;
14233
14234#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 char_u *serverid = get_tv_string_chk(&argvars[0]);
14236
14237 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014238 {
14239# ifdef WIN32
14240 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014241 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014242
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014243 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 if (n != 0)
14245 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14246 if (r == NULL)
14247# else
14248 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250# endif
14251 EMSG(_("E277: Unable to read a server reply"));
14252 }
14253#endif
14254 rettv->v_type = VAR_STRING;
14255 rettv->vval.v_string = r;
14256}
14257
14258/*
14259 * "remote_send()" function
14260 */
14261/*ARGSUSED*/
14262 static void
14263f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014266{
14267 rettv->v_type = VAR_STRING;
14268 rettv->vval.v_string = NULL;
14269#ifdef FEAT_CLIENTSERVER
14270 remote_common(argvars, rettv, FALSE);
14271#endif
14272}
14273
14274/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014275 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014276 */
14277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014279 typval_T *argvars;
14280 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014281{
Bram Moolenaar33570922005-01-25 22:26:29 +000014282 list_T *l;
14283 listitem_T *item, *item2;
14284 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014285 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014286 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014287 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014288 dict_T *d;
14289 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014290
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014291 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014292 if (argvars[0].v_type == VAR_DICT)
14293 {
14294 if (argvars[2].v_type != VAR_UNKNOWN)
14295 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014296 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014297 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 key = get_tv_string_chk(&argvars[1]);
14300 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 di = dict_find(d, key, -1);
14303 if (di == NULL)
14304 EMSG2(_(e_dictkey), key);
14305 else
14306 {
14307 *rettv = di->di_tv;
14308 init_tv(&di->di_tv);
14309 dictitem_remove(d, di);
14310 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014312 }
14313 }
14314 else if (argvars[0].v_type != VAR_LIST)
14315 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014316 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014317 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014319 int error = FALSE;
14320
14321 idx = get_tv_number_chk(&argvars[1], &error);
14322 if (error)
14323 ; /* type error: do nothing, errmsg already given */
14324 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014325 EMSGN(_(e_listidx), idx);
14326 else
14327 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014328 if (argvars[2].v_type == VAR_UNKNOWN)
14329 {
14330 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014331 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014332 *rettv = item->li_tv;
14333 vim_free(item);
14334 }
14335 else
14336 {
14337 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 end = get_tv_number_chk(&argvars[2], &error);
14339 if (error)
14340 ; /* type error: do nothing */
14341 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014342 EMSGN(_(e_listidx), end);
14343 else
14344 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014345 int cnt = 0;
14346
14347 for (li = item; li != NULL; li = li->li_next)
14348 {
14349 ++cnt;
14350 if (li == item2)
14351 break;
14352 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014353 if (li == NULL) /* didn't find "item2" after "item" */
14354 EMSG(_(e_invrange));
14355 else
14356 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014357 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014358 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014359 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014360 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014361 l->lv_first = item;
14362 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014363 item->li_prev = NULL;
14364 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014365 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014366 }
14367 }
14368 }
14369 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014370 }
14371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372}
14373
14374/*
14375 * "rename({from}, {to})" function
14376 */
14377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014379 typval_T *argvars;
14380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381{
14382 char_u buf[NUMBUFLEN];
14383
14384 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14388 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389}
14390
14391/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014392 * "repeat()" function
14393 */
14394/*ARGSUSED*/
14395 static void
14396f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *argvars;
14398 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014399{
14400 char_u *p;
14401 int n;
14402 int slen;
14403 int len;
14404 char_u *r;
14405 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014406
14407 n = get_tv_number(&argvars[1]);
14408 if (argvars[0].v_type == VAR_LIST)
14409 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014410 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014411 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014412 if (list_extend(rettv->vval.v_list,
14413 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014414 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014415 }
14416 else
14417 {
14418 p = get_tv_string(&argvars[0]);
14419 rettv->v_type = VAR_STRING;
14420 rettv->vval.v_string = NULL;
14421
14422 slen = (int)STRLEN(p);
14423 len = slen * n;
14424 if (len <= 0)
14425 return;
14426
14427 r = alloc(len + 1);
14428 if (r != NULL)
14429 {
14430 for (i = 0; i < n; i++)
14431 mch_memmove(r + i * slen, p, (size_t)slen);
14432 r[len] = NUL;
14433 }
14434
14435 rettv->vval.v_string = r;
14436 }
14437}
14438
14439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 * "resolve()" function
14441 */
14442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014443f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014444 typval_T *argvars;
14445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446{
14447 char_u *p;
14448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450#ifdef FEAT_SHORTCUT
14451 {
14452 char_u *v = NULL;
14453
14454 v = mch_resolve_shortcut(p);
14455 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 }
14460#else
14461# ifdef HAVE_READLINK
14462 {
14463 char_u buf[MAXPATHL + 1];
14464 char_u *cpy;
14465 int len;
14466 char_u *remain = NULL;
14467 char_u *q;
14468 int is_relative_to_current = FALSE;
14469 int has_trailing_pathsep = FALSE;
14470 int limit = 100;
14471
14472 p = vim_strsave(p);
14473
14474 if (p[0] == '.' && (vim_ispathsep(p[1])
14475 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14476 is_relative_to_current = TRUE;
14477
14478 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014479 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 has_trailing_pathsep = TRUE;
14481
14482 q = getnextcomp(p);
14483 if (*q != NUL)
14484 {
14485 /* Separate the first path component in "p", and keep the
14486 * remainder (beginning with the path separator). */
14487 remain = vim_strsave(q - 1);
14488 q[-1] = NUL;
14489 }
14490
14491 for (;;)
14492 {
14493 for (;;)
14494 {
14495 len = readlink((char *)p, (char *)buf, MAXPATHL);
14496 if (len <= 0)
14497 break;
14498 buf[len] = NUL;
14499
14500 if (limit-- == 0)
14501 {
14502 vim_free(p);
14503 vim_free(remain);
14504 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 goto fail;
14507 }
14508
14509 /* Ensure that the result will have a trailing path separator
14510 * if the argument has one. */
14511 if (remain == NULL && has_trailing_pathsep)
14512 add_pathsep(buf);
14513
14514 /* Separate the first path component in the link value and
14515 * concatenate the remainders. */
14516 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14517 if (*q != NUL)
14518 {
14519 if (remain == NULL)
14520 remain = vim_strsave(q - 1);
14521 else
14522 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014523 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 if (cpy != NULL)
14525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 vim_free(remain);
14527 remain = cpy;
14528 }
14529 }
14530 q[-1] = NUL;
14531 }
14532
14533 q = gettail(p);
14534 if (q > p && *q == NUL)
14535 {
14536 /* Ignore trailing path separator. */
14537 q[-1] = NUL;
14538 q = gettail(p);
14539 }
14540 if (q > p && !mch_isFullName(buf))
14541 {
14542 /* symlink is relative to directory of argument */
14543 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14544 if (cpy != NULL)
14545 {
14546 STRCPY(cpy, p);
14547 STRCPY(gettail(cpy), buf);
14548 vim_free(p);
14549 p = cpy;
14550 }
14551 }
14552 else
14553 {
14554 vim_free(p);
14555 p = vim_strsave(buf);
14556 }
14557 }
14558
14559 if (remain == NULL)
14560 break;
14561
14562 /* Append the first path component of "remain" to "p". */
14563 q = getnextcomp(remain + 1);
14564 len = q - remain - (*q != NUL);
14565 cpy = vim_strnsave(p, STRLEN(p) + len);
14566 if (cpy != NULL)
14567 {
14568 STRNCAT(cpy, remain, len);
14569 vim_free(p);
14570 p = cpy;
14571 }
14572 /* Shorten "remain". */
14573 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014574 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 else
14576 {
14577 vim_free(remain);
14578 remain = NULL;
14579 }
14580 }
14581
14582 /* If the result is a relative path name, make it explicitly relative to
14583 * the current directory if and only if the argument had this form. */
14584 if (!vim_ispathsep(*p))
14585 {
14586 if (is_relative_to_current
14587 && *p != NUL
14588 && !(p[0] == '.'
14589 && (p[1] == NUL
14590 || vim_ispathsep(p[1])
14591 || (p[1] == '.'
14592 && (p[2] == NUL
14593 || vim_ispathsep(p[2]))))))
14594 {
14595 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014596 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 if (cpy != NULL)
14598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 vim_free(p);
14600 p = cpy;
14601 }
14602 }
14603 else if (!is_relative_to_current)
14604 {
14605 /* Strip leading "./". */
14606 q = p;
14607 while (q[0] == '.' && vim_ispathsep(q[1]))
14608 q += 2;
14609 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014610 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611 }
14612 }
14613
14614 /* Ensure that the result will have no trailing path separator
14615 * if the argument had none. But keep "/" or "//". */
14616 if (!has_trailing_pathsep)
14617 {
14618 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014619 if (after_pathsep(p, q))
14620 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 }
14622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 }
14625# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627# endif
14628#endif
14629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631
14632#ifdef HAVE_READLINK
14633fail:
14634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636}
14637
14638/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 */
14641 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014643 typval_T *argvars;
14644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645{
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 list_T *l;
14647 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 rettv->vval.v_number = 0;
14650 if (argvars[0].v_type != VAR_LIST)
14651 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014652 else if ((l = argvars[0].vval.v_list) != NULL
14653 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654 {
14655 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014656 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014657 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014658 while (li != NULL)
14659 {
14660 ni = li->li_prev;
14661 list_append(l, li);
14662 li = ni;
14663 }
14664 rettv->vval.v_list = l;
14665 rettv->v_type = VAR_LIST;
14666 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014667 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669}
14670
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014671#define SP_NOMOVE 0x01 /* don't move cursor */
14672#define SP_REPEAT 0x02 /* repeat to find outer pair */
14673#define SP_RETCOUNT 0x04 /* return matchcount */
14674#define SP_SETPCMARK 0x08 /* set previous context mark */
14675#define SP_START 0x10 /* accept match at start position */
14676#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14677#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014678
Bram Moolenaar33570922005-01-25 22:26:29 +000014679static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014680
14681/*
14682 * Get flags for a search function.
14683 * Possibly sets "p_ws".
14684 * Returns BACKWARD, FORWARD or zero (for an error).
14685 */
14686 static int
14687get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689 int *flagsp;
14690{
14691 int dir = FORWARD;
14692 char_u *flags;
14693 char_u nbuf[NUMBUFLEN];
14694 int mask;
14695
14696 if (varp->v_type != VAR_UNKNOWN)
14697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 flags = get_tv_string_buf_chk(varp, nbuf);
14699 if (flags == NULL)
14700 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701 while (*flags != NUL)
14702 {
14703 switch (*flags)
14704 {
14705 case 'b': dir = BACKWARD; break;
14706 case 'w': p_ws = TRUE; break;
14707 case 'W': p_ws = FALSE; break;
14708 default: mask = 0;
14709 if (flagsp != NULL)
14710 switch (*flags)
14711 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014712 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014713 case 'e': mask = SP_END; break;
14714 case 'm': mask = SP_RETCOUNT; break;
14715 case 'n': mask = SP_NOMOVE; break;
14716 case 'p': mask = SP_SUBPAT; break;
14717 case 'r': mask = SP_REPEAT; break;
14718 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719 }
14720 if (mask == 0)
14721 {
14722 EMSG2(_(e_invarg2), flags);
14723 dir = 0;
14724 }
14725 else
14726 *flagsp |= mask;
14727 }
14728 if (dir == 0)
14729 break;
14730 ++flags;
14731 }
14732 }
14733 return dir;
14734}
14735
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014737 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014739 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014740search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014742 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014743 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014745 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 char_u *pat;
14747 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014748 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 int save_p_ws = p_ws;
14750 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014751 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014752 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014753 proftime_T tm;
14754#ifdef FEAT_RELTIME
14755 long time_limit = 0;
14756#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014757 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014758 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014761 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014762 if (dir == 0)
14763 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014764 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014765 if (flags & SP_START)
14766 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014767 if (flags & SP_END)
14768 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014769
Bram Moolenaar76929292008-01-06 19:07:36 +000014770 /* Optional arguments: line number to stop searching and timeout. */
14771 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014772 {
14773 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14774 if (lnum_stop < 0)
14775 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014776#ifdef FEAT_RELTIME
14777 if (argvars[3].v_type != VAR_UNKNOWN)
14778 {
14779 time_limit = get_tv_number_chk(&argvars[3], NULL);
14780 if (time_limit < 0)
14781 goto theend;
14782 }
14783#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014784 }
14785
Bram Moolenaar76929292008-01-06 19:07:36 +000014786#ifdef FEAT_RELTIME
14787 /* Set the time limit, if there is one. */
14788 profile_setlimit(time_limit, &tm);
14789#endif
14790
Bram Moolenaar231334e2005-07-25 20:46:57 +000014791 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014792 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014793 * Check to make sure only those flags are set.
14794 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14795 * flags cannot be set. Check for that condition also.
14796 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014797 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014798 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014800 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014801 goto theend;
14802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014804 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014805 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014806 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014807 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014809 if (flags & SP_SUBPAT)
14810 retval = subpatnum;
14811 else
14812 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014813 if (flags & SP_SETPCMARK)
14814 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014816 if (match_pos != NULL)
14817 {
14818 /* Store the match cursor position */
14819 match_pos->lnum = pos.lnum;
14820 match_pos->col = pos.col + 1;
14821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822 /* "/$" will put the cursor after the end of the line, may need to
14823 * correct that here */
14824 check_cursor();
14825 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014826
14827 /* If 'n' flag is used: restore cursor position. */
14828 if (flags & SP_NOMOVE)
14829 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014830 else
14831 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014832theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014834
14835 return retval;
14836}
14837
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014838#ifdef FEAT_FLOAT
14839/*
14840 * "round({float})" function
14841 */
14842 static void
14843f_round(argvars, rettv)
14844 typval_T *argvars;
14845 typval_T *rettv;
14846{
14847 float_T f;
14848
14849 rettv->v_type = VAR_FLOAT;
14850 if (get_float_arg(argvars, &f) == OK)
14851 /* round() is not in C90, use ceil() or floor() instead. */
14852 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14853 else
14854 rettv->vval.v_float = 0.0;
14855}
14856#endif
14857
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014858/*
14859 * "search()" function
14860 */
14861 static void
14862f_search(argvars, rettv)
14863 typval_T *argvars;
14864 typval_T *rettv;
14865{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014866 int flags = 0;
14867
14868 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869}
14870
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014872 * "searchdecl()" function
14873 */
14874 static void
14875f_searchdecl(argvars, rettv)
14876 typval_T *argvars;
14877 typval_T *rettv;
14878{
14879 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014880 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014881 int error = FALSE;
14882 char_u *name;
14883
14884 rettv->vval.v_number = 1; /* default: FAIL */
14885
14886 name = get_tv_string_chk(&argvars[0]);
14887 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014888 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014889 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014890 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14891 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14892 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014893 if (!error && name != NULL)
14894 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014895 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014896}
14897
14898/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014899 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014901 static int
14902searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014903 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014904 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905{
14906 char_u *spat, *mpat, *epat;
14907 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 int dir;
14910 int flags = 0;
14911 char_u nbuf1[NUMBUFLEN];
14912 char_u nbuf2[NUMBUFLEN];
14913 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014914 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014915 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014916 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014919 spat = get_tv_string_chk(&argvars[0]);
14920 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14921 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14922 if (spat == NULL || mpat == NULL || epat == NULL)
14923 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 /* Handle the optional fourth argument: flags */
14926 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014927 if (dir == 0)
14928 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014929
14930 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014931 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14932 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014933 if ((flags & (SP_END | SP_SUBPAT)) != 0
14934 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014935 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014936 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014937 goto theend;
14938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014940 /* Using 'r' implies 'W', otherwise it doesn't work. */
14941 if (flags & SP_REPEAT)
14942 p_ws = FALSE;
14943
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014944 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014945 if (argvars[3].v_type == VAR_UNKNOWN
14946 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947 skip = (char_u *)"";
14948 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014950 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014951 if (argvars[5].v_type != VAR_UNKNOWN)
14952 {
14953 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14954 if (lnum_stop < 0)
14955 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014956#ifdef FEAT_RELTIME
14957 if (argvars[6].v_type != VAR_UNKNOWN)
14958 {
14959 time_limit = get_tv_number_chk(&argvars[6], NULL);
14960 if (time_limit < 0)
14961 goto theend;
14962 }
14963#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014964 }
14965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014966 if (skip == NULL)
14967 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014969 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014970 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014971
14972theend:
14973 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014974
14975 return retval;
14976}
14977
14978/*
14979 * "searchpair()" function
14980 */
14981 static void
14982f_searchpair(argvars, rettv)
14983 typval_T *argvars;
14984 typval_T *rettv;
14985{
14986 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14987}
14988
14989/*
14990 * "searchpairpos()" function
14991 */
14992 static void
14993f_searchpairpos(argvars, rettv)
14994 typval_T *argvars;
14995 typval_T *rettv;
14996{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014997 pos_T match_pos;
14998 int lnum = 0;
14999 int col = 0;
15000
15001 rettv->vval.v_number = 0;
15002
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015003 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015004 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015005
15006 if (searchpair_cmn(argvars, &match_pos) > 0)
15007 {
15008 lnum = match_pos.lnum;
15009 col = match_pos.col;
15010 }
15011
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015012 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15013 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015014}
15015
15016/*
15017 * Search for a start/middle/end thing.
15018 * Used by searchpair(), see its documentation for the details.
15019 * Returns 0 or -1 for no match,
15020 */
15021 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015022do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15023 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015024 char_u *spat; /* start pattern */
15025 char_u *mpat; /* middle pattern */
15026 char_u *epat; /* end pattern */
15027 int dir; /* BACKWARD or FORWARD */
15028 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015029 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015030 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015031 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015032 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015033{
15034 char_u *save_cpo;
15035 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15036 long retval = 0;
15037 pos_T pos;
15038 pos_T firstpos;
15039 pos_T foundpos;
15040 pos_T save_cursor;
15041 pos_T save_pos;
15042 int n;
15043 int r;
15044 int nest = 1;
15045 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015046 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015047 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015048
15049 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15050 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015051 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015052
Bram Moolenaar76929292008-01-06 19:07:36 +000015053#ifdef FEAT_RELTIME
15054 /* Set the time limit, if there is one. */
15055 profile_setlimit(time_limit, &tm);
15056#endif
15057
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015058 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15059 * start/middle/end (pat3, for the top pair). */
15060 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15061 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15062 if (pat2 == NULL || pat3 == NULL)
15063 goto theend;
15064 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15065 if (*mpat == NUL)
15066 STRCPY(pat3, pat2);
15067 else
15068 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15069 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015070 if (flags & SP_START)
15071 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015072
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 save_cursor = curwin->w_cursor;
15074 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015075 clearpos(&firstpos);
15076 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 pat = pat3;
15078 for (;;)
15079 {
15080 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015081 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15083 /* didn't find it or found the first match again: FAIL */
15084 break;
15085
15086 if (firstpos.lnum == 0)
15087 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015088 if (equalpos(pos, foundpos))
15089 {
15090 /* Found the same position again. Can happen with a pattern that
15091 * has "\zs" at the end and searching backwards. Advance one
15092 * character and try again. */
15093 if (dir == BACKWARD)
15094 decl(&pos);
15095 else
15096 incl(&pos);
15097 }
15098 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015100 /* clear the start flag to avoid getting stuck here */
15101 options &= ~SEARCH_START;
15102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 /* If the skip pattern matches, ignore this match. */
15104 if (*skip != NUL)
15105 {
15106 save_pos = curwin->w_cursor;
15107 curwin->w_cursor = pos;
15108 r = eval_to_bool(skip, &err, NULL, FALSE);
15109 curwin->w_cursor = save_pos;
15110 if (err)
15111 {
15112 /* Evaluating {skip} caused an error, break here. */
15113 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015114 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 break;
15116 }
15117 if (r)
15118 continue;
15119 }
15120
15121 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15122 {
15123 /* Found end when searching backwards or start when searching
15124 * forward: nested pair. */
15125 ++nest;
15126 pat = pat2; /* nested, don't search for middle */
15127 }
15128 else
15129 {
15130 /* Found end when searching forward or start when searching
15131 * backward: end of (nested) pair; or found middle in outer pair. */
15132 if (--nest == 1)
15133 pat = pat3; /* outer level, search for middle */
15134 }
15135
15136 if (nest == 0)
15137 {
15138 /* Found the match: return matchcount or line number. */
15139 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015140 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015142 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015143 if (flags & SP_SETPCMARK)
15144 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 curwin->w_cursor = pos;
15146 if (!(flags & SP_REPEAT))
15147 break;
15148 nest = 1; /* search for next unmatched */
15149 }
15150 }
15151
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015152 if (match_pos != NULL)
15153 {
15154 /* Store the match cursor position */
15155 match_pos->lnum = curwin->w_cursor.lnum;
15156 match_pos->col = curwin->w_cursor.col + 1;
15157 }
15158
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015160 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 curwin->w_cursor = save_cursor;
15162
15163theend:
15164 vim_free(pat2);
15165 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015166 if (p_cpo == empty_option)
15167 p_cpo = save_cpo;
15168 else
15169 /* Darn, evaluating the {skip} expression changed the value. */
15170 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015171
15172 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173}
15174
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015175/*
15176 * "searchpos()" function
15177 */
15178 static void
15179f_searchpos(argvars, rettv)
15180 typval_T *argvars;
15181 typval_T *rettv;
15182{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015183 pos_T match_pos;
15184 int lnum = 0;
15185 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015186 int n;
15187 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015188
15189 rettv->vval.v_number = 0;
15190
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015191 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015192 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015193
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015194 n = search_cmn(argvars, &match_pos, &flags);
15195 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015196 {
15197 lnum = match_pos.lnum;
15198 col = match_pos.col;
15199 }
15200
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015201 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15202 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015203 if (flags & SP_SUBPAT)
15204 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015205}
15206
15207
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208/*ARGSUSED*/
15209 static void
15210f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015211 typval_T *argvars;
15212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015213{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015214#ifdef FEAT_CLIENTSERVER
15215 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 char_u *server = get_tv_string_chk(&argvars[0]);
15217 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 if (server == NULL || reply == NULL)
15221 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222 if (check_restricted() || check_secure())
15223 return;
15224# ifdef FEAT_X11
15225 if (check_connection() == FAIL)
15226 return;
15227# endif
15228
15229 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231 EMSG(_("E258: Unable to send to client"));
15232 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015234 rettv->vval.v_number = 0;
15235#else
15236 rettv->vval.v_number = -1;
15237#endif
15238}
15239
15240/*ARGSUSED*/
15241 static void
15242f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015243 typval_T *argvars;
15244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245{
15246 char_u *r = NULL;
15247
15248#ifdef FEAT_CLIENTSERVER
15249# ifdef WIN32
15250 r = serverGetVimNames();
15251# else
15252 make_connection();
15253 if (X_DISPLAY != NULL)
15254 r = serverGetVimNames(X_DISPLAY);
15255# endif
15256#endif
15257 rettv->v_type = VAR_STRING;
15258 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259}
15260
15261/*
15262 * "setbufvar()" function
15263 */
15264/*ARGSUSED*/
15265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015266f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015267 typval_T *argvars;
15268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269{
15270 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 char_u nbuf[NUMBUFLEN];
15275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015276 rettv->vval.v_number = 0;
15277
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 if (check_restricted() || check_secure())
15279 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15281 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 varp = &argvars[2];
15284
15285 if (buf != NULL && varname != NULL && varp != NULL)
15286 {
15287 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289
15290 if (*varname == '&')
15291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015292 long numval;
15293 char_u *strval;
15294 int error = FALSE;
15295
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015297 numval = get_tv_number_chk(varp, &error);
15298 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015299 if (!error && strval != NULL)
15300 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 }
15302 else
15303 {
15304 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15305 if (bufvarname != NULL)
15306 {
15307 STRCPY(bufvarname, "b:");
15308 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015309 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 vim_free(bufvarname);
15311 }
15312 }
15313
15314 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317}
15318
15319/*
15320 * "setcmdpos()" function
15321 */
15322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015323f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015324 typval_T *argvars;
15325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015327 int pos = (int)get_tv_number(&argvars[0]) - 1;
15328
15329 if (pos >= 0)
15330 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331}
15332
15333/*
15334 * "setline()" function
15335 */
15336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015338 typval_T *argvars;
15339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340{
15341 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015342 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015343 list_T *l = NULL;
15344 listitem_T *li = NULL;
15345 long added = 0;
15346 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015348 lnum = get_tv_lnum(&argvars[0]);
15349 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015351 l = argvars[1].vval.v_list;
15352 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015354 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015355 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015356
15357 rettv->vval.v_number = 0; /* OK */
15358 for (;;)
15359 {
15360 if (l != NULL)
15361 {
15362 /* list argument, get next string */
15363 if (li == NULL)
15364 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015365 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015366 li = li->li_next;
15367 }
15368
15369 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015370 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015371 break;
15372 if (lnum <= curbuf->b_ml.ml_line_count)
15373 {
15374 /* existing line, replace it */
15375 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15376 {
15377 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015378 if (lnum == curwin->w_cursor.lnum)
15379 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015380 rettv->vval.v_number = 0; /* OK */
15381 }
15382 }
15383 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15384 {
15385 /* lnum is one past the last line, append the line */
15386 ++added;
15387 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15388 rettv->vval.v_number = 0; /* OK */
15389 }
15390
15391 if (l == NULL) /* only one string argument */
15392 break;
15393 ++lnum;
15394 }
15395
15396 if (added > 0)
15397 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398}
15399
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015400static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15401
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015403 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015404 */
15405/*ARGSUSED*/
15406 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015407set_qf_ll_list(wp, list_arg, action_arg, rettv)
15408 win_T *wp;
15409 typval_T *list_arg;
15410 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015411 typval_T *rettv;
15412{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015413#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015414 char_u *act;
15415 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015416#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015417
Bram Moolenaar2641f772005-03-25 21:58:17 +000015418 rettv->vval.v_number = -1;
15419
15420#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015421 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015422 EMSG(_(e_listreq));
15423 else
15424 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015425 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015426
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015427 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015428 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015429 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015430 if (act == NULL)
15431 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015432 if (*act == 'a' || *act == 'r')
15433 action = *act;
15434 }
15435
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015436 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015437 rettv->vval.v_number = 0;
15438 }
15439#endif
15440}
15441
15442/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015443 * "setloclist()" function
15444 */
15445/*ARGSUSED*/
15446 static void
15447f_setloclist(argvars, rettv)
15448 typval_T *argvars;
15449 typval_T *rettv;
15450{
15451 win_T *win;
15452
15453 rettv->vval.v_number = -1;
15454
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015455 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015456 if (win != NULL)
15457 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15458}
15459
15460/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015461 * "setmatches()" function
15462 */
15463 static void
15464f_setmatches(argvars, rettv)
15465 typval_T *argvars;
15466 typval_T *rettv;
15467{
15468#ifdef FEAT_SEARCH_EXTRA
15469 list_T *l;
15470 listitem_T *li;
15471 dict_T *d;
15472
15473 rettv->vval.v_number = -1;
15474 if (argvars[0].v_type != VAR_LIST)
15475 {
15476 EMSG(_(e_listreq));
15477 return;
15478 }
15479 if ((l = argvars[0].vval.v_list) != NULL)
15480 {
15481
15482 /* To some extent make sure that we are dealing with a list from
15483 * "getmatches()". */
15484 li = l->lv_first;
15485 while (li != NULL)
15486 {
15487 if (li->li_tv.v_type != VAR_DICT
15488 || (d = li->li_tv.vval.v_dict) == NULL)
15489 {
15490 EMSG(_(e_invarg));
15491 return;
15492 }
15493 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15494 && dict_find(d, (char_u *)"pattern", -1) != NULL
15495 && dict_find(d, (char_u *)"priority", -1) != NULL
15496 && dict_find(d, (char_u *)"id", -1) != NULL))
15497 {
15498 EMSG(_(e_invarg));
15499 return;
15500 }
15501 li = li->li_next;
15502 }
15503
15504 clear_matches(curwin);
15505 li = l->lv_first;
15506 while (li != NULL)
15507 {
15508 d = li->li_tv.vval.v_dict;
15509 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15510 get_dict_string(d, (char_u *)"pattern", FALSE),
15511 (int)get_dict_number(d, (char_u *)"priority"),
15512 (int)get_dict_number(d, (char_u *)"id"));
15513 li = li->li_next;
15514 }
15515 rettv->vval.v_number = 0;
15516 }
15517#endif
15518}
15519
15520/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015521 * "setpos()" function
15522 */
15523/*ARGSUSED*/
15524 static void
15525f_setpos(argvars, rettv)
15526 typval_T *argvars;
15527 typval_T *rettv;
15528{
15529 pos_T pos;
15530 int fnum;
15531 char_u *name;
15532
Bram Moolenaar08250432008-02-13 11:42:46 +000015533 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015534 name = get_tv_string_chk(argvars);
15535 if (name != NULL)
15536 {
15537 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15538 {
15539 --pos.col;
Bram Moolenaar08250432008-02-13 11:42:46 +000015540 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015541 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015542 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015543 if (fnum == curbuf->b_fnum)
15544 {
15545 curwin->w_cursor = pos;
15546 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015547 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015548 }
15549 else
15550 EMSG(_(e_invarg));
15551 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015552 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15553 {
15554 /* set mark */
15555 if (setmark_pos(name[1], &pos, fnum) == OK)
15556 rettv->vval.v_number = 0;
15557 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015558 else
15559 EMSG(_(e_invarg));
15560 }
15561 }
15562}
15563
15564/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015565 * "setqflist()" function
15566 */
15567/*ARGSUSED*/
15568 static void
15569f_setqflist(argvars, rettv)
15570 typval_T *argvars;
15571 typval_T *rettv;
15572{
15573 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15574}
15575
15576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 * "setreg()" function
15578 */
15579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015581 typval_T *argvars;
15582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583{
15584 int regname;
15585 char_u *strregname;
15586 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015587 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 int append;
15589 char_u yank_type;
15590 long block_len;
15591
15592 block_len = -1;
15593 yank_type = MAUTO;
15594 append = FALSE;
15595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015596 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015597 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015599 if (strregname == NULL)
15600 return; /* type error; errmsg already given */
15601 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 if (regname == 0 || regname == '@')
15603 regname = '"';
15604 else if (regname == '=')
15605 return;
15606
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015607 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 stropt = get_tv_string_chk(&argvars[2]);
15610 if (stropt == NULL)
15611 return; /* type error */
15612 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 switch (*stropt)
15614 {
15615 case 'a': case 'A': /* append */
15616 append = TRUE;
15617 break;
15618 case 'v': case 'c': /* character-wise selection */
15619 yank_type = MCHAR;
15620 break;
15621 case 'V': case 'l': /* line-wise selection */
15622 yank_type = MLINE;
15623 break;
15624#ifdef FEAT_VISUAL
15625 case 'b': case Ctrl_V: /* block-wise selection */
15626 yank_type = MBLOCK;
15627 if (VIM_ISDIGIT(stropt[1]))
15628 {
15629 ++stropt;
15630 block_len = getdigits(&stropt) - 1;
15631 --stropt;
15632 }
15633 break;
15634#endif
15635 }
15636 }
15637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015638 strval = get_tv_string_chk(&argvars[1]);
15639 if (strval != NULL)
15640 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643}
15644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015645/*
15646 * "settabwinvar()" function
15647 */
15648 static void
15649f_settabwinvar(argvars, rettv)
15650 typval_T *argvars;
15651 typval_T *rettv;
15652{
15653 setwinvar(argvars, rettv, 1);
15654}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655
15656/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015657 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 typval_T *argvars;
15662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015664 setwinvar(argvars, rettv, 0);
15665}
15666
15667/*
15668 * "setwinvar()" and "settabwinvar()" functions
15669 */
15670 static void
15671setwinvar(argvars, rettv, off)
15672 typval_T *argvars;
15673 typval_T *rettv;
15674 int off;
15675{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 win_T *win;
15677#ifdef FEAT_WINDOWS
15678 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015679 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680#endif
15681 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015682 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015684 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015686 rettv->vval.v_number = 0;
15687
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 if (check_restricted() || check_secure())
15689 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015690
15691#ifdef FEAT_WINDOWS
15692 if (off == 1)
15693 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15694 else
15695 tp = curtab;
15696#endif
15697 win = find_win_by_nr(&argvars[off], tp);
15698 varname = get_tv_string_chk(&argvars[off + 1]);
15699 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700
15701 if (win != NULL && varname != NULL && varp != NULL)
15702 {
15703#ifdef FEAT_WINDOWS
15704 /* set curwin to be our win, temporarily */
15705 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015706 save_curtab = curtab;
15707 goto_tabpage_tp(tp);
15708 if (!win_valid(win))
15709 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 curwin = win;
15711 curbuf = curwin->w_buffer;
15712#endif
15713
15714 if (*varname == '&')
15715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015716 long numval;
15717 char_u *strval;
15718 int error = FALSE;
15719
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 numval = get_tv_number_chk(varp, &error);
15722 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015723 if (!error && strval != NULL)
15724 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 }
15726 else
15727 {
15728 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15729 if (winvarname != NULL)
15730 {
15731 STRCPY(winvarname, "w:");
15732 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015733 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 vim_free(winvarname);
15735 }
15736 }
15737
15738#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015739 /* Restore current tabpage and window, if still valid (autocomands can
15740 * make them invalid). */
15741 if (valid_tabpage(save_curtab))
15742 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 if (win_valid(save_curwin))
15744 {
15745 curwin = save_curwin;
15746 curbuf = curwin->w_buffer;
15747 }
15748#endif
15749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750}
15751
15752/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015753 * "shellescape({string})" function
15754 */
15755 static void
15756f_shellescape(argvars, rettv)
15757 typval_T *argvars;
15758 typval_T *rettv;
15759{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015760 rettv->vval.v_string = vim_strsave_shellescape(
15761 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015762 rettv->v_type = VAR_STRING;
15763}
15764
15765/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015766 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 */
15768 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 typval_T *argvars;
15771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774
Bram Moolenaar0d660222005-01-07 21:51:51 +000015775 p = get_tv_string(&argvars[0]);
15776 rettv->vval.v_string = vim_strsave(p);
15777 simplify_filename(rettv->vval.v_string); /* simplify in place */
15778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779}
15780
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015781#ifdef FEAT_FLOAT
15782/*
15783 * "sin()" function
15784 */
15785 static void
15786f_sin(argvars, rettv)
15787 typval_T *argvars;
15788 typval_T *rettv;
15789{
15790 float_T f;
15791
15792 rettv->v_type = VAR_FLOAT;
15793 if (get_float_arg(argvars, &f) == OK)
15794 rettv->vval.v_float = sin(f);
15795 else
15796 rettv->vval.v_float = 0.0;
15797}
15798#endif
15799
Bram Moolenaar0d660222005-01-07 21:51:51 +000015800static int
15801#ifdef __BORLANDC__
15802 _RTLENTRYF
15803#endif
15804 item_compare __ARGS((const void *s1, const void *s2));
15805static int
15806#ifdef __BORLANDC__
15807 _RTLENTRYF
15808#endif
15809 item_compare2 __ARGS((const void *s1, const void *s2));
15810
15811static int item_compare_ic;
15812static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015814#define ITEM_COMPARE_FAIL 999
15815
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819 static int
15820#ifdef __BORLANDC__
15821_RTLENTRYF
15822#endif
15823item_compare(s1, s2)
15824 const void *s1;
15825 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 char_u *p1, *p2;
15828 char_u *tofree1, *tofree2;
15829 int res;
15830 char_u numbuf1[NUMBUFLEN];
15831 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015833 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15834 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015835 if (p1 == NULL)
15836 p1 = (char_u *)"";
15837 if (p2 == NULL)
15838 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 if (item_compare_ic)
15840 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015842 res = STRCMP(p1, p2);
15843 vim_free(tofree1);
15844 vim_free(tofree2);
15845 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846}
15847
15848 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015849#ifdef __BORLANDC__
15850_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015852item_compare2(s1, s2)
15853 const void *s1;
15854 const void *s2;
15855{
15856 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015857 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015858 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015861 /* shortcut after failure in previous call; compare all items equal */
15862 if (item_compare_func_err)
15863 return 0;
15864
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015865 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15866 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015867 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15868 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015869
15870 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015871 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015872 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015873 clear_tv(&argv[0]);
15874 clear_tv(&argv[1]);
15875
15876 if (res == FAIL)
15877 res = ITEM_COMPARE_FAIL;
15878 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15880 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015881 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015882 clear_tv(&rettv);
15883 return res;
15884}
15885
15886/*
15887 * "sort({list})" function
15888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015890f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015891 typval_T *argvars;
15892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893{
Bram Moolenaar33570922005-01-25 22:26:29 +000015894 list_T *l;
15895 listitem_T *li;
15896 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015897 long len;
15898 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899
Bram Moolenaar0d660222005-01-07 21:51:51 +000015900 rettv->vval.v_number = 0;
15901 if (argvars[0].v_type != VAR_LIST)
15902 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 else
15904 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015905 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015906 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015907 return;
15908 rettv->vval.v_list = l;
15909 rettv->v_type = VAR_LIST;
15910 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912 len = list_len(l);
15913 if (len <= 1)
15914 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916 item_compare_ic = FALSE;
15917 item_compare_func = NULL;
15918 if (argvars[1].v_type != VAR_UNKNOWN)
15919 {
15920 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015921 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 else
15923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 int error = FALSE;
15925
15926 i = get_tv_number_chk(&argvars[1], &error);
15927 if (error)
15928 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015929 if (i == 1)
15930 item_compare_ic = TRUE;
15931 else
15932 item_compare_func = get_tv_string(&argvars[1]);
15933 }
15934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015937 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015938 if (ptrs == NULL)
15939 return;
15940 i = 0;
15941 for (li = l->lv_first; li != NULL; li = li->li_next)
15942 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015944 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945 /* test the compare function */
15946 if (item_compare_func != NULL
15947 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15948 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015949 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015951 {
15952 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015954 item_compare_func == NULL ? item_compare : item_compare2);
15955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015956 if (!item_compare_func_err)
15957 {
15958 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015959 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015960 l->lv_len = 0;
15961 for (i = 0; i < len; ++i)
15962 list_append(l, ptrs[i]);
15963 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015964 }
15965
15966 vim_free(ptrs);
15967 }
15968}
15969
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015970/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015971 * "soundfold({word})" function
15972 */
15973 static void
15974f_soundfold(argvars, rettv)
15975 typval_T *argvars;
15976 typval_T *rettv;
15977{
15978 char_u *s;
15979
15980 rettv->v_type = VAR_STRING;
15981 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015982#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015983 rettv->vval.v_string = eval_soundfold(s);
15984#else
15985 rettv->vval.v_string = vim_strsave(s);
15986#endif
15987}
15988
15989/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015990 * "spellbadword()" function
15991 */
15992/* ARGSUSED */
15993 static void
15994f_spellbadword(argvars, rettv)
15995 typval_T *argvars;
15996 typval_T *rettv;
15997{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015998 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015999 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016000 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016001
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016002 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016003 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016004
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016005#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016006 if (argvars[0].v_type == VAR_UNKNOWN)
16007 {
16008 /* Find the start and length of the badly spelled word. */
16009 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16010 if (len != 0)
16011 word = ml_get_cursor();
16012 }
16013 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16014 {
16015 char_u *str = get_tv_string_chk(&argvars[0]);
16016 int capcol = -1;
16017
16018 if (str != NULL)
16019 {
16020 /* Check the argument for spelling. */
16021 while (*str != NUL)
16022 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016023 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016024 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016025 {
16026 word = str;
16027 break;
16028 }
16029 str += len;
16030 }
16031 }
16032 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016033#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016034
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016035 list_append_string(rettv->vval.v_list, word, len);
16036 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016037 attr == HLF_SPB ? "bad" :
16038 attr == HLF_SPR ? "rare" :
16039 attr == HLF_SPL ? "local" :
16040 attr == HLF_SPC ? "caps" :
16041 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016042}
16043
16044/*
16045 * "spellsuggest()" function
16046 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016047/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016048 static void
16049f_spellsuggest(argvars, rettv)
16050 typval_T *argvars;
16051 typval_T *rettv;
16052{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016053#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016054 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016055 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016056 int maxcount;
16057 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016058 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016059 listitem_T *li;
16060 int need_capital = FALSE;
16061#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016062
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016063 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016064 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016065
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016066#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016067 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16068 {
16069 str = get_tv_string(&argvars[0]);
16070 if (argvars[1].v_type != VAR_UNKNOWN)
16071 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016072 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016073 if (maxcount <= 0)
16074 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016075 if (argvars[2].v_type != VAR_UNKNOWN)
16076 {
16077 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16078 if (typeerr)
16079 return;
16080 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016081 }
16082 else
16083 maxcount = 25;
16084
Bram Moolenaar4770d092006-01-12 23:22:24 +000016085 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016086
16087 for (i = 0; i < ga.ga_len; ++i)
16088 {
16089 str = ((char_u **)ga.ga_data)[i];
16090
16091 li = listitem_alloc();
16092 if (li == NULL)
16093 vim_free(str);
16094 else
16095 {
16096 li->li_tv.v_type = VAR_STRING;
16097 li->li_tv.v_lock = 0;
16098 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016099 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016100 }
16101 }
16102 ga_clear(&ga);
16103 }
16104#endif
16105}
16106
Bram Moolenaar0d660222005-01-07 21:51:51 +000016107 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016108f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 typval_T *argvars;
16110 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111{
16112 char_u *str;
16113 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016114 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016115 regmatch_T regmatch;
16116 char_u patbuf[NUMBUFLEN];
16117 char_u *save_cpo;
16118 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016119 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016120 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122
16123 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16124 save_cpo = p_cpo;
16125 p_cpo = (char_u *)"";
16126
16127 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016128 if (argvars[1].v_type != VAR_UNKNOWN)
16129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016130 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16131 if (pat == NULL)
16132 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016134 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016135 }
16136 if (pat == NULL || *pat == NUL)
16137 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016138
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016139 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 if (typeerr)
16142 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16145 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016147 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016148 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016150 if (*str == NUL)
16151 match = FALSE; /* empty item at the end */
16152 else
16153 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016154 if (match)
16155 end = regmatch.startp[0];
16156 else
16157 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016158 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16159 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016161 if (list_append_string(rettv->vval.v_list, str,
16162 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 }
16165 if (!match)
16166 break;
16167 /* Advance to just after the match. */
16168 if (regmatch.endp[0] > str)
16169 col = 0;
16170 else
16171 {
16172 /* Don't get stuck at the same match. */
16173#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016174 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175#else
16176 col = 1;
16177#endif
16178 }
16179 str = regmatch.endp[0];
16180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181
Bram Moolenaar0d660222005-01-07 21:51:51 +000016182 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186}
16187
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016188#ifdef FEAT_FLOAT
16189/*
16190 * "sqrt()" function
16191 */
16192 static void
16193f_sqrt(argvars, rettv)
16194 typval_T *argvars;
16195 typval_T *rettv;
16196{
16197 float_T f;
16198
16199 rettv->v_type = VAR_FLOAT;
16200 if (get_float_arg(argvars, &f) == OK)
16201 rettv->vval.v_float = sqrt(f);
16202 else
16203 rettv->vval.v_float = 0.0;
16204}
16205
16206/*
16207 * "str2float()" function
16208 */
16209 static void
16210f_str2float(argvars, rettv)
16211 typval_T *argvars;
16212 typval_T *rettv;
16213{
16214 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16215
16216 if (*p == '+')
16217 p = skipwhite(p + 1);
16218 (void)string2float(p, &rettv->vval.v_float);
16219 rettv->v_type = VAR_FLOAT;
16220}
16221#endif
16222
Bram Moolenaar2c932302006-03-18 21:42:09 +000016223/*
16224 * "str2nr()" function
16225 */
16226 static void
16227f_str2nr(argvars, rettv)
16228 typval_T *argvars;
16229 typval_T *rettv;
16230{
16231 int base = 10;
16232 char_u *p;
16233 long n;
16234
16235 if (argvars[1].v_type != VAR_UNKNOWN)
16236 {
16237 base = get_tv_number(&argvars[1]);
16238 if (base != 8 && base != 10 && base != 16)
16239 {
16240 EMSG(_(e_invarg));
16241 return;
16242 }
16243 }
16244
16245 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016246 if (*p == '+')
16247 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016248 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16249 rettv->vval.v_number = n;
16250}
16251
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252#ifdef HAVE_STRFTIME
16253/*
16254 * "strftime({format}[, {time}])" function
16255 */
16256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016258 typval_T *argvars;
16259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260{
16261 char_u result_buf[256];
16262 struct tm *curtime;
16263 time_t seconds;
16264 char_u *p;
16265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016266 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016269 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 seconds = time(NULL);
16271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 curtime = localtime(&seconds);
16274 /* MSVC returns NULL for an invalid value of seconds. */
16275 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016276 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 else
16278 {
16279# ifdef FEAT_MBYTE
16280 vimconv_T conv;
16281 char_u *enc;
16282
16283 conv.vc_type = CONV_NONE;
16284 enc = enc_locale();
16285 convert_setup(&conv, p_enc, enc);
16286 if (conv.vc_type != CONV_NONE)
16287 p = string_convert(&conv, p, NULL);
16288# endif
16289 if (p != NULL)
16290 (void)strftime((char *)result_buf, sizeof(result_buf),
16291 (char *)p, curtime);
16292 else
16293 result_buf[0] = NUL;
16294
16295# ifdef FEAT_MBYTE
16296 if (conv.vc_type != CONV_NONE)
16297 vim_free(p);
16298 convert_setup(&conv, enc, p_enc);
16299 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 else
16302# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016303 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304
16305# ifdef FEAT_MBYTE
16306 /* Release conversion descriptors */
16307 convert_setup(&conv, NULL, NULL);
16308 vim_free(enc);
16309# endif
16310 }
16311}
16312#endif
16313
16314/*
16315 * "stridx()" function
16316 */
16317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 typval_T *argvars;
16320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321{
16322 char_u buf[NUMBUFLEN];
16323 char_u *needle;
16324 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016325 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016329 needle = get_tv_string_chk(&argvars[1]);
16330 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016331 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016332 if (needle == NULL || haystack == NULL)
16333 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334
Bram Moolenaar33570922005-01-25 22:26:29 +000016335 if (argvars[2].v_type != VAR_UNKNOWN)
16336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016337 int error = FALSE;
16338
16339 start_idx = get_tv_number_chk(&argvars[2], &error);
16340 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016341 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016342 if (start_idx >= 0)
16343 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016344 }
16345
16346 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16347 if (pos != NULL)
16348 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349}
16350
16351/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016352 * "string()" function
16353 */
16354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 typval_T *argvars;
16357 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016358{
16359 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016360 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016362 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016363 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016364 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016365 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367}
16368
16369/*
16370 * "strlen()" function
16371 */
16372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016373f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 typval_T *argvars;
16375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016377 rettv->vval.v_number = (varnumber_T)(STRLEN(
16378 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379}
16380
16381/*
16382 * "strpart()" function
16383 */
16384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016385f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016386 typval_T *argvars;
16387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388{
16389 char_u *p;
16390 int n;
16391 int len;
16392 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016393 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016395 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 slen = (int)STRLEN(p);
16397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016398 n = get_tv_number_chk(&argvars[1], &error);
16399 if (error)
16400 len = 0;
16401 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016402 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 else
16404 len = slen - n; /* default len: all bytes that are available. */
16405
16406 /*
16407 * Only return the overlap between the specified part and the actual
16408 * string.
16409 */
16410 if (n < 0)
16411 {
16412 len += n;
16413 n = 0;
16414 }
16415 else if (n > slen)
16416 n = slen;
16417 if (len < 0)
16418 len = 0;
16419 else if (n + len > slen)
16420 len = slen - n;
16421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016422 rettv->v_type = VAR_STRING;
16423 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424}
16425
16426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427 * "strridx()" function
16428 */
16429 static void
16430f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 typval_T *argvars;
16432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433{
16434 char_u buf[NUMBUFLEN];
16435 char_u *needle;
16436 char_u *haystack;
16437 char_u *rest;
16438 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016439 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 needle = get_tv_string_chk(&argvars[1]);
16442 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443
16444 rettv->vval.v_number = -1;
16445 if (needle == NULL || haystack == NULL)
16446 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016447
16448 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016449 if (argvars[2].v_type != VAR_UNKNOWN)
16450 {
16451 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016452 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016453 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016454 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016455 }
16456 else
16457 end_idx = haystack_len;
16458
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016460 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016462 lastmatch = haystack + end_idx;
16463 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016465 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016466 for (rest = haystack; *rest != '\0'; ++rest)
16467 {
16468 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016469 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470 break;
16471 lastmatch = rest;
16472 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016473 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474
16475 if (lastmatch == NULL)
16476 rettv->vval.v_number = -1;
16477 else
16478 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16479}
16480
16481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 * "strtrans()" function
16483 */
16484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016485f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 typval_T *argvars;
16487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489 rettv->v_type = VAR_STRING;
16490 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491}
16492
16493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016494 * "submatch()" function
16495 */
16496 static void
16497f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016498 typval_T *argvars;
16499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500{
16501 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 rettv->vval.v_string =
16503 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016504}
16505
16506/*
16507 * "substitute()" function
16508 */
16509 static void
16510f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016511 typval_T *argvars;
16512 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513{
16514 char_u patbuf[NUMBUFLEN];
16515 char_u subbuf[NUMBUFLEN];
16516 char_u flagsbuf[NUMBUFLEN];
16517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 char_u *str = get_tv_string_chk(&argvars[0]);
16519 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16520 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16521 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16522
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016524 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16525 rettv->vval.v_string = NULL;
16526 else
16527 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528}
16529
16530/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016531 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 */
16533/*ARGSUSED*/
16534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016535f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016536 typval_T *argvars;
16537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538{
16539 int id = 0;
16540#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016541 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 long col;
16543 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016544 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016546 lnum = get_tv_lnum(argvars); /* -1 on type error */
16547 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16548 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016551 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016552 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553#endif
16554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016555 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556}
16557
16558/*
16559 * "synIDattr(id, what [, mode])" function
16560 */
16561/*ARGSUSED*/
16562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016563f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016564 typval_T *argvars;
16565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566{
16567 char_u *p = NULL;
16568#ifdef FEAT_SYN_HL
16569 int id;
16570 char_u *what;
16571 char_u *mode;
16572 char_u modebuf[NUMBUFLEN];
16573 int modec;
16574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016575 id = get_tv_number(&argvars[0]);
16576 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016577 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 modec = TOLOWER_ASC(mode[0]);
16581 if (modec != 't' && modec != 'c'
16582#ifdef FEAT_GUI
16583 && modec != 'g'
16584#endif
16585 )
16586 modec = 0; /* replace invalid with current */
16587 }
16588 else
16589 {
16590#ifdef FEAT_GUI
16591 if (gui.in_use)
16592 modec = 'g';
16593 else
16594#endif
16595 if (t_colors > 1)
16596 modec = 'c';
16597 else
16598 modec = 't';
16599 }
16600
16601
16602 switch (TOLOWER_ASC(what[0]))
16603 {
16604 case 'b':
16605 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16606 p = highlight_color(id, what, modec);
16607 else /* bold */
16608 p = highlight_has_attr(id, HL_BOLD, modec);
16609 break;
16610
16611 case 'f': /* fg[#] */
16612 p = highlight_color(id, what, modec);
16613 break;
16614
16615 case 'i':
16616 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16617 p = highlight_has_attr(id, HL_INVERSE, modec);
16618 else /* italic */
16619 p = highlight_has_attr(id, HL_ITALIC, modec);
16620 break;
16621
16622 case 'n': /* name */
16623 p = get_highlight_name(NULL, id - 1);
16624 break;
16625
16626 case 'r': /* reverse */
16627 p = highlight_has_attr(id, HL_INVERSE, modec);
16628 break;
16629
16630 case 's': /* standout */
16631 p = highlight_has_attr(id, HL_STANDOUT, modec);
16632 break;
16633
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016634 case 'u':
16635 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16636 /* underline */
16637 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16638 else
16639 /* undercurl */
16640 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 break;
16642 }
16643
16644 if (p != NULL)
16645 p = vim_strsave(p);
16646#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647 rettv->v_type = VAR_STRING;
16648 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649}
16650
16651/*
16652 * "synIDtrans(id)" function
16653 */
16654/*ARGSUSED*/
16655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016656f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016657 typval_T *argvars;
16658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659{
16660 int id;
16661
16662#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664
16665 if (id > 0)
16666 id = syn_get_final_id(id);
16667 else
16668#endif
16669 id = 0;
16670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016671 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672}
16673
16674/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016675 * "synstack(lnum, col)" function
16676 */
16677/*ARGSUSED*/
16678 static void
16679f_synstack(argvars, rettv)
16680 typval_T *argvars;
16681 typval_T *rettv;
16682{
16683#ifdef FEAT_SYN_HL
16684 long lnum;
16685 long col;
16686 int i;
16687 int id;
16688#endif
16689
16690 rettv->v_type = VAR_LIST;
16691 rettv->vval.v_list = NULL;
16692
16693#ifdef FEAT_SYN_HL
16694 lnum = get_tv_lnum(argvars); /* -1 on type error */
16695 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16696
16697 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016698 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016699 && rettv_list_alloc(rettv) != FAIL)
16700 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016701 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016702 for (i = 0; ; ++i)
16703 {
16704 id = syn_get_stack_item(i);
16705 if (id < 0)
16706 break;
16707 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16708 break;
16709 }
16710 }
16711#endif
16712}
16713
16714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 * "system()" function
16716 */
16717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016718f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016719 typval_T *argvars;
16720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016722 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016724 char_u *infile = NULL;
16725 char_u buf[NUMBUFLEN];
16726 int err = FALSE;
16727 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016729 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016730 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016731
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016732 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016733 {
16734 /*
16735 * Write the string to a temp file, to be used for input of the shell
16736 * command.
16737 */
16738 if ((infile = vim_tempname('i')) == NULL)
16739 {
16740 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016741 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016742 }
16743
16744 fd = mch_fopen((char *)infile, WRITEBIN);
16745 if (fd == NULL)
16746 {
16747 EMSG2(_(e_notopen), infile);
16748 goto done;
16749 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016750 p = get_tv_string_buf_chk(&argvars[1], buf);
16751 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016752 {
16753 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016754 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016755 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016756 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16757 err = TRUE;
16758 if (fclose(fd) != 0)
16759 err = TRUE;
16760 if (err)
16761 {
16762 EMSG(_("E677: Error writing temp file"));
16763 goto done;
16764 }
16765 }
16766
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016767 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16768 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016769
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770#ifdef USE_CR
16771 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016772 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 {
16774 char_u *s;
16775
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016776 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 {
16778 if (*s == CAR)
16779 *s = NL;
16780 }
16781 }
16782#else
16783# ifdef USE_CRNL
16784 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016785 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 {
16787 char_u *s, *d;
16788
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016789 d = res;
16790 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 {
16792 if (s[0] == CAR && s[1] == NL)
16793 ++s;
16794 *d++ = *s;
16795 }
16796 *d = NUL;
16797 }
16798# endif
16799#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016800
16801done:
16802 if (infile != NULL)
16803 {
16804 mch_remove(infile);
16805 vim_free(infile);
16806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016807 rettv->v_type = VAR_STRING;
16808 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809}
16810
16811/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016812 * "tabpagebuflist()" function
16813 */
16814/* ARGSUSED */
16815 static void
16816f_tabpagebuflist(argvars, rettv)
16817 typval_T *argvars;
16818 typval_T *rettv;
16819{
16820#ifndef FEAT_WINDOWS
16821 rettv->vval.v_number = 0;
16822#else
16823 tabpage_T *tp;
16824 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016825
16826 if (argvars[0].v_type == VAR_UNKNOWN)
16827 wp = firstwin;
16828 else
16829 {
16830 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16831 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016832 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016833 }
16834 if (wp == NULL)
16835 rettv->vval.v_number = 0;
16836 else
16837 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016838 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016839 rettv->vval.v_number = 0;
16840 else
16841 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016842 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016843 if (list_append_number(rettv->vval.v_list,
16844 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016845 break;
16846 }
16847 }
16848#endif
16849}
16850
16851
16852/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016853 * "tabpagenr()" function
16854 */
16855/* ARGSUSED */
16856 static void
16857f_tabpagenr(argvars, rettv)
16858 typval_T *argvars;
16859 typval_T *rettv;
16860{
16861 int nr = 1;
16862#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016863 char_u *arg;
16864
16865 if (argvars[0].v_type != VAR_UNKNOWN)
16866 {
16867 arg = get_tv_string_chk(&argvars[0]);
16868 nr = 0;
16869 if (arg != NULL)
16870 {
16871 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016872 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016873 else
16874 EMSG2(_(e_invexpr2), arg);
16875 }
16876 }
16877 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016878 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016879#endif
16880 rettv->vval.v_number = nr;
16881}
16882
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016883
16884#ifdef FEAT_WINDOWS
16885static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16886
16887/*
16888 * Common code for tabpagewinnr() and winnr().
16889 */
16890 static int
16891get_winnr(tp, argvar)
16892 tabpage_T *tp;
16893 typval_T *argvar;
16894{
16895 win_T *twin;
16896 int nr = 1;
16897 win_T *wp;
16898 char_u *arg;
16899
16900 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16901 if (argvar->v_type != VAR_UNKNOWN)
16902 {
16903 arg = get_tv_string_chk(argvar);
16904 if (arg == NULL)
16905 nr = 0; /* type error; errmsg already given */
16906 else if (STRCMP(arg, "$") == 0)
16907 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16908 else if (STRCMP(arg, "#") == 0)
16909 {
16910 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16911 if (twin == NULL)
16912 nr = 0;
16913 }
16914 else
16915 {
16916 EMSG2(_(e_invexpr2), arg);
16917 nr = 0;
16918 }
16919 }
16920
16921 if (nr > 0)
16922 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16923 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016924 {
16925 if (wp == NULL)
16926 {
16927 /* didn't find it in this tabpage */
16928 nr = 0;
16929 break;
16930 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016931 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016932 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016933 return nr;
16934}
16935#endif
16936
16937/*
16938 * "tabpagewinnr()" function
16939 */
16940/* ARGSUSED */
16941 static void
16942f_tabpagewinnr(argvars, rettv)
16943 typval_T *argvars;
16944 typval_T *rettv;
16945{
16946 int nr = 1;
16947#ifdef FEAT_WINDOWS
16948 tabpage_T *tp;
16949
16950 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16951 if (tp == NULL)
16952 nr = 0;
16953 else
16954 nr = get_winnr(tp, &argvars[1]);
16955#endif
16956 rettv->vval.v_number = nr;
16957}
16958
16959
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016960/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016961 * "tagfiles()" function
16962 */
16963/*ARGSUSED*/
16964 static void
16965f_tagfiles(argvars, rettv)
16966 typval_T *argvars;
16967 typval_T *rettv;
16968{
16969 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016970 tagname_T tn;
16971 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016973 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016974 {
16975 rettv->vval.v_number = 0;
16976 return;
16977 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016978
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016979 for (first = TRUE; ; first = FALSE)
16980 if (get_tagfname(&tn, first, fname) == FAIL
16981 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016982 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016983 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016984}
16985
16986/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016987 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016988 */
16989 static void
16990f_taglist(argvars, rettv)
16991 typval_T *argvars;
16992 typval_T *rettv;
16993{
16994 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016995
16996 tag_pattern = get_tv_string(&argvars[0]);
16997
16998 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 if (*tag_pattern == NUL)
17000 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017001
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017002 if (rettv_list_alloc(rettv) == OK)
17003 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017004}
17005
17006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 * "tempname()" function
17008 */
17009/*ARGSUSED*/
17010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017011f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 typval_T *argvars;
17013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014{
17015 static int x = 'A';
17016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017017 rettv->v_type = VAR_STRING;
17018 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019
17020 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17021 * names. Skip 'I' and 'O', they are used for shell redirection. */
17022 do
17023 {
17024 if (x == 'Z')
17025 x = '0';
17026 else if (x == '9')
17027 x = 'A';
17028 else
17029 {
17030#ifdef EBCDIC
17031 if (x == 'I')
17032 x = 'J';
17033 else if (x == 'R')
17034 x = 'S';
17035 else
17036#endif
17037 ++x;
17038 }
17039 } while (x == 'I' || x == 'O');
17040}
17041
17042/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017043 * "test(list)" function: Just checking the walls...
17044 */
17045/*ARGSUSED*/
17046 static void
17047f_test(argvars, rettv)
17048 typval_T *argvars;
17049 typval_T *rettv;
17050{
17051 /* Used for unit testing. Change the code below to your liking. */
17052#if 0
17053 listitem_T *li;
17054 list_T *l;
17055 char_u *bad, *good;
17056
17057 if (argvars[0].v_type != VAR_LIST)
17058 return;
17059 l = argvars[0].vval.v_list;
17060 if (l == NULL)
17061 return;
17062 li = l->lv_first;
17063 if (li == NULL)
17064 return;
17065 bad = get_tv_string(&li->li_tv);
17066 li = li->li_next;
17067 if (li == NULL)
17068 return;
17069 good = get_tv_string(&li->li_tv);
17070 rettv->vval.v_number = test_edit_score(bad, good);
17071#endif
17072}
17073
17074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 * "tolower(string)" function
17076 */
17077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017078f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 typval_T *argvars;
17080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081{
17082 char_u *p;
17083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017084 p = vim_strsave(get_tv_string(&argvars[0]));
17085 rettv->v_type = VAR_STRING;
17086 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087
17088 if (p != NULL)
17089 while (*p != NUL)
17090 {
17091#ifdef FEAT_MBYTE
17092 int l;
17093
17094 if (enc_utf8)
17095 {
17096 int c, lc;
17097
17098 c = utf_ptr2char(p);
17099 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017100 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 /* TODO: reallocate string when byte count changes. */
17102 if (utf_char2len(lc) == l)
17103 utf_char2bytes(lc, p);
17104 p += l;
17105 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017106 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 p += l; /* skip multi-byte character */
17108 else
17109#endif
17110 {
17111 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17112 ++p;
17113 }
17114 }
17115}
17116
17117/*
17118 * "toupper(string)" function
17119 */
17120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017121f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 typval_T *argvars;
17123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127}
17128
17129/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017130 * "tr(string, fromstr, tostr)" function
17131 */
17132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017134 typval_T *argvars;
17135 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017136{
17137 char_u *instr;
17138 char_u *fromstr;
17139 char_u *tostr;
17140 char_u *p;
17141#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017142 int inlen;
17143 int fromlen;
17144 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017145 int idx;
17146 char_u *cpstr;
17147 int cplen;
17148 int first = TRUE;
17149#endif
17150 char_u buf[NUMBUFLEN];
17151 char_u buf2[NUMBUFLEN];
17152 garray_T ga;
17153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017155 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17156 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017157
17158 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017159 rettv->v_type = VAR_STRING;
17160 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017161 if (fromstr == NULL || tostr == NULL)
17162 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017163 ga_init2(&ga, (int)sizeof(char), 80);
17164
17165#ifdef FEAT_MBYTE
17166 if (!has_mbyte)
17167#endif
17168 /* not multi-byte: fromstr and tostr must be the same length */
17169 if (STRLEN(fromstr) != STRLEN(tostr))
17170 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017171#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017172error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017173#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017174 EMSG2(_(e_invarg2), fromstr);
17175 ga_clear(&ga);
17176 return;
17177 }
17178
17179 /* fromstr and tostr have to contain the same number of chars */
17180 while (*instr != NUL)
17181 {
17182#ifdef FEAT_MBYTE
17183 if (has_mbyte)
17184 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017185 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017186 cpstr = instr;
17187 cplen = inlen;
17188 idx = 0;
17189 for (p = fromstr; *p != NUL; p += fromlen)
17190 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017191 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017192 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17193 {
17194 for (p = tostr; *p != NUL; p += tolen)
17195 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017196 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017197 if (idx-- == 0)
17198 {
17199 cplen = tolen;
17200 cpstr = p;
17201 break;
17202 }
17203 }
17204 if (*p == NUL) /* tostr is shorter than fromstr */
17205 goto error;
17206 break;
17207 }
17208 ++idx;
17209 }
17210
17211 if (first && cpstr == instr)
17212 {
17213 /* Check that fromstr and tostr have the same number of
17214 * (multi-byte) characters. Done only once when a character
17215 * of instr doesn't appear in fromstr. */
17216 first = FALSE;
17217 for (p = tostr; *p != NUL; p += tolen)
17218 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017219 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017220 --idx;
17221 }
17222 if (idx != 0)
17223 goto error;
17224 }
17225
17226 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017227 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017228 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017229
17230 instr += inlen;
17231 }
17232 else
17233#endif
17234 {
17235 /* When not using multi-byte chars we can do it faster. */
17236 p = vim_strchr(fromstr, *instr);
17237 if (p != NULL)
17238 ga_append(&ga, tostr[p - fromstr]);
17239 else
17240 ga_append(&ga, *instr);
17241 ++instr;
17242 }
17243 }
17244
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017245 /* add a terminating NUL */
17246 ga_grow(&ga, 1);
17247 ga_append(&ga, NUL);
17248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017249 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017250}
17251
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017252#ifdef FEAT_FLOAT
17253/*
17254 * "trunc({float})" function
17255 */
17256 static void
17257f_trunc(argvars, rettv)
17258 typval_T *argvars;
17259 typval_T *rettv;
17260{
17261 float_T f;
17262
17263 rettv->v_type = VAR_FLOAT;
17264 if (get_float_arg(argvars, &f) == OK)
17265 /* trunc() is not in C90, use floor() or ceil() instead. */
17266 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17267 else
17268 rettv->vval.v_float = 0.0;
17269}
17270#endif
17271
Bram Moolenaar8299df92004-07-10 09:47:34 +000017272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 * "type(expr)" function
17274 */
17275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017276f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 typval_T *argvars;
17278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017280 int n;
17281
17282 switch (argvars[0].v_type)
17283 {
17284 case VAR_NUMBER: n = 0; break;
17285 case VAR_STRING: n = 1; break;
17286 case VAR_FUNC: n = 2; break;
17287 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017288 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017289#ifdef FEAT_FLOAT
17290 case VAR_FLOAT: n = 5; break;
17291#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017292 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17293 }
17294 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295}
17296
17297/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017298 * "values(dict)" function
17299 */
17300 static void
17301f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 typval_T *argvars;
17303 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017304{
17305 dict_list(argvars, rettv, 1);
17306}
17307
17308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 * "virtcol(string)" function
17310 */
17311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017312f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017313 typval_T *argvars;
17314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315{
17316 colnr_T vcol = 0;
17317 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017318 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017320 fp = var2fpos(&argvars[0], FALSE, &fnum);
17321 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17322 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 {
17324 getvvcol(curwin, fp, NULL, NULL, &vcol);
17325 ++vcol;
17326 }
17327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017328 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329}
17330
17331/*
17332 * "visualmode()" function
17333 */
17334/*ARGSUSED*/
17335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 typval_T *argvars;
17338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339{
17340#ifdef FEAT_VISUAL
17341 char_u str[2];
17342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017343 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 str[0] = curbuf->b_visual_mode_eval;
17345 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017346 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347
17348 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017349 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 curbuf->b_visual_mode_eval = NUL;
17351#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017352 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353#endif
17354}
17355
17356/*
17357 * "winbufnr(nr)" function
17358 */
17359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 typval_T *argvars;
17362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363{
17364 win_T *wp;
17365
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017366 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017370 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371}
17372
17373/*
17374 * "wincol()" function
17375 */
17376/*ARGSUSED*/
17377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 typval_T *argvars;
17380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381{
17382 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384}
17385
17386/*
17387 * "winheight(nr)" function
17388 */
17389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 typval_T *argvars;
17392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393{
17394 win_T *wp;
17395
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017396 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017398 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017400 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401}
17402
17403/*
17404 * "winline()" function
17405 */
17406/*ARGSUSED*/
17407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 typval_T *argvars;
17410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
17412 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414}
17415
17416/*
17417 * "winnr()" function
17418 */
17419/* ARGSUSED */
17420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017422 typval_T *argvars;
17423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424{
17425 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017426
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017428 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017430 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431}
17432
17433/*
17434 * "winrestcmd()" function
17435 */
17436/* ARGSUSED */
17437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017438f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017439 typval_T *argvars;
17440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441{
17442#ifdef FEAT_WINDOWS
17443 win_T *wp;
17444 int winnr = 1;
17445 garray_T ga;
17446 char_u buf[50];
17447
17448 ga_init2(&ga, (int)sizeof(char), 70);
17449 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17450 {
17451 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17452 ga_concat(&ga, buf);
17453# ifdef FEAT_VERTSPLIT
17454 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17455 ga_concat(&ga, buf);
17456# endif
17457 ++winnr;
17458 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017459 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017461 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017463 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017465 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466}
17467
17468/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017469 * "winrestview()" function
17470 */
17471/* ARGSUSED */
17472 static void
17473f_winrestview(argvars, rettv)
17474 typval_T *argvars;
17475 typval_T *rettv;
17476{
17477 dict_T *dict;
17478
17479 if (argvars[0].v_type != VAR_DICT
17480 || (dict = argvars[0].vval.v_dict) == NULL)
17481 EMSG(_(e_invarg));
17482 else
17483 {
17484 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17485 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17486#ifdef FEAT_VIRTUALEDIT
17487 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17488#endif
17489 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017490 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017491
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017492 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017493#ifdef FEAT_DIFF
17494 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17495#endif
17496 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17497 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17498
17499 check_cursor();
17500 changed_cline_bef_curs();
17501 invalidate_botline();
17502 redraw_later(VALID);
17503
17504 if (curwin->w_topline == 0)
17505 curwin->w_topline = 1;
17506 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17507 curwin->w_topline = curbuf->b_ml.ml_line_count;
17508#ifdef FEAT_DIFF
17509 check_topfill(curwin, TRUE);
17510#endif
17511 }
17512}
17513
17514/*
17515 * "winsaveview()" function
17516 */
17517/* ARGSUSED */
17518 static void
17519f_winsaveview(argvars, rettv)
17520 typval_T *argvars;
17521 typval_T *rettv;
17522{
17523 dict_T *dict;
17524
17525 dict = dict_alloc();
17526 if (dict == NULL)
17527 return;
17528 rettv->v_type = VAR_DICT;
17529 rettv->vval.v_dict = dict;
17530 ++dict->dv_refcount;
17531
17532 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17533 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17534#ifdef FEAT_VIRTUALEDIT
17535 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17536#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017537 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017538 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17539
17540 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17541#ifdef FEAT_DIFF
17542 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17543#endif
17544 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17545 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17546}
17547
17548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 * "winwidth(nr)" function
17550 */
17551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017553 typval_T *argvars;
17554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555{
17556 win_T *wp;
17557
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017558 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 else
17562#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017563 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017565 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566#endif
17567}
17568
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017570 * "writefile()" function
17571 */
17572 static void
17573f_writefile(argvars, rettv)
17574 typval_T *argvars;
17575 typval_T *rettv;
17576{
17577 int binary = FALSE;
17578 char_u *fname;
17579 FILE *fd;
17580 listitem_T *li;
17581 char_u *s;
17582 int ret = 0;
17583 int c;
17584
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017585 if (check_restricted() || check_secure())
17586 return;
17587
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017588 if (argvars[0].v_type != VAR_LIST)
17589 {
17590 EMSG2(_(e_listarg), "writefile()");
17591 return;
17592 }
17593 if (argvars[0].vval.v_list == NULL)
17594 return;
17595
17596 if (argvars[2].v_type != VAR_UNKNOWN
17597 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17598 binary = TRUE;
17599
17600 /* Always open the file in binary mode, library functions have a mind of
17601 * their own about CR-LF conversion. */
17602 fname = get_tv_string(&argvars[1]);
17603 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17604 {
17605 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17606 ret = -1;
17607 }
17608 else
17609 {
17610 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17611 li = li->li_next)
17612 {
17613 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17614 {
17615 if (*s == '\n')
17616 c = putc(NUL, fd);
17617 else
17618 c = putc(*s, fd);
17619 if (c == EOF)
17620 {
17621 ret = -1;
17622 break;
17623 }
17624 }
17625 if (!binary || li->li_next != NULL)
17626 if (putc('\n', fd) == EOF)
17627 {
17628 ret = -1;
17629 break;
17630 }
17631 if (ret < 0)
17632 {
17633 EMSG(_(e_write));
17634 break;
17635 }
17636 }
17637 fclose(fd);
17638 }
17639
17640 rettv->vval.v_number = ret;
17641}
17642
17643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017645 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 */
17647 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017648var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017649 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017650 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017651 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017653 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017655 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656
Bram Moolenaara5525202006-03-02 22:52:09 +000017657 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017658 if (varp->v_type == VAR_LIST)
17659 {
17660 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017661 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017662 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017663 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017664
17665 l = varp->vval.v_list;
17666 if (l == NULL)
17667 return NULL;
17668
17669 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017670 pos.lnum = list_find_nr(l, 0L, &error);
17671 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017672 return NULL; /* invalid line number */
17673
17674 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017675 pos.col = list_find_nr(l, 1L, &error);
17676 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017677 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017678 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017679
17680 /* We accept "$" for the column number: last column. */
17681 li = list_find(l, 1L);
17682 if (li != NULL && li->li_tv.v_type == VAR_STRING
17683 && li->li_tv.vval.v_string != NULL
17684 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17685 pos.col = len + 1;
17686
Bram Moolenaara5525202006-03-02 22:52:09 +000017687 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017688 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017689 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017690 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017691
Bram Moolenaara5525202006-03-02 22:52:09 +000017692#ifdef FEAT_VIRTUALEDIT
17693 /* Get the virtual offset. Defaults to zero. */
17694 pos.coladd = list_find_nr(l, 2L, &error);
17695 if (error)
17696 pos.coladd = 0;
17697#endif
17698
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017699 return &pos;
17700 }
17701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017702 name = get_tv_string_chk(varp);
17703 if (name == NULL)
17704 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017705 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017707#ifdef FEAT_VISUAL
17708 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17709 {
17710 if (VIsual_active)
17711 return &VIsual;
17712 return &curwin->w_cursor;
17713 }
17714#endif
17715 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017717 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17719 return NULL;
17720 return pp;
17721 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017722
17723#ifdef FEAT_VIRTUALEDIT
17724 pos.coladd = 0;
17725#endif
17726
Bram Moolenaar477933c2007-07-17 14:32:23 +000017727 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017728 {
17729 pos.col = 0;
17730 if (name[1] == '0') /* "w0": first visible line */
17731 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017732 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017733 pos.lnum = curwin->w_topline;
17734 return &pos;
17735 }
17736 else if (name[1] == '$') /* "w$": last visible line */
17737 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017738 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017739 pos.lnum = curwin->w_botline - 1;
17740 return &pos;
17741 }
17742 }
17743 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017745 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 {
17747 pos.lnum = curbuf->b_ml.ml_line_count;
17748 pos.col = 0;
17749 }
17750 else
17751 {
17752 pos.lnum = curwin->w_cursor.lnum;
17753 pos.col = (colnr_T)STRLEN(ml_get_curline());
17754 }
17755 return &pos;
17756 }
17757 return NULL;
17758}
17759
17760/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017761 * Convert list in "arg" into a position and optional file number.
17762 * When "fnump" is NULL there is no file number, only 3 items.
17763 * Note that the column is passed on as-is, the caller may want to decrement
17764 * it to use 1 for the first column.
17765 * Return FAIL when conversion is not possible, doesn't check the position for
17766 * validity.
17767 */
17768 static int
17769list2fpos(arg, posp, fnump)
17770 typval_T *arg;
17771 pos_T *posp;
17772 int *fnump;
17773{
17774 list_T *l = arg->vval.v_list;
17775 long i = 0;
17776 long n;
17777
Bram Moolenaarbde35262006-07-23 20:12:24 +000017778 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17779 * when "fnump" isn't NULL and "coladd" is optional. */
17780 if (arg->v_type != VAR_LIST
17781 || l == NULL
17782 || l->lv_len < (fnump == NULL ? 2 : 3)
17783 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017784 return FAIL;
17785
17786 if (fnump != NULL)
17787 {
17788 n = list_find_nr(l, i++, NULL); /* fnum */
17789 if (n < 0)
17790 return FAIL;
17791 if (n == 0)
17792 n = curbuf->b_fnum; /* current buffer */
17793 *fnump = n;
17794 }
17795
17796 n = list_find_nr(l, i++, NULL); /* lnum */
17797 if (n < 0)
17798 return FAIL;
17799 posp->lnum = n;
17800
17801 n = list_find_nr(l, i++, NULL); /* col */
17802 if (n < 0)
17803 return FAIL;
17804 posp->col = n;
17805
17806#ifdef FEAT_VIRTUALEDIT
17807 n = list_find_nr(l, i, NULL);
17808 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017809 posp->coladd = 0;
17810 else
17811 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017812#endif
17813
17814 return OK;
17815}
17816
17817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 * Get the length of an environment variable name.
17819 * Advance "arg" to the first character after the name.
17820 * Return 0 for error.
17821 */
17822 static int
17823get_env_len(arg)
17824 char_u **arg;
17825{
17826 char_u *p;
17827 int len;
17828
17829 for (p = *arg; vim_isIDc(*p); ++p)
17830 ;
17831 if (p == *arg) /* no name found */
17832 return 0;
17833
17834 len = (int)(p - *arg);
17835 *arg = p;
17836 return len;
17837}
17838
17839/*
17840 * Get the length of the name of a function or internal variable.
17841 * "arg" is advanced to the first non-white character after the name.
17842 * Return 0 if something is wrong.
17843 */
17844 static int
17845get_id_len(arg)
17846 char_u **arg;
17847{
17848 char_u *p;
17849 int len;
17850
17851 /* Find the end of the name. */
17852 for (p = *arg; eval_isnamec(*p); ++p)
17853 ;
17854 if (p == *arg) /* no name found */
17855 return 0;
17856
17857 len = (int)(p - *arg);
17858 *arg = skipwhite(p);
17859
17860 return len;
17861}
17862
17863/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017864 * Get the length of the name of a variable or function.
17865 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017867 * Return -1 if curly braces expansion failed.
17868 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 * If the name contains 'magic' {}'s, expand them and return the
17870 * expanded name in an allocated string via 'alias' - caller must free.
17871 */
17872 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017873get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 char_u **arg;
17875 char_u **alias;
17876 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017877 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878{
17879 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 char_u *p;
17881 char_u *expr_start;
17882 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883
17884 *alias = NULL; /* default to no alias */
17885
17886 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17887 && (*arg)[2] == (int)KE_SNR)
17888 {
17889 /* hard coded <SNR>, already translated */
17890 *arg += 3;
17891 return get_id_len(arg) + 3;
17892 }
17893 len = eval_fname_script(*arg);
17894 if (len > 0)
17895 {
17896 /* literal "<SID>", "s:" or "<SNR>" */
17897 *arg += len;
17898 }
17899
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017901 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017903 p = find_name_end(*arg, &expr_start, &expr_end,
17904 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 if (expr_start != NULL)
17906 {
17907 char_u *temp_string;
17908
17909 if (!evaluate)
17910 {
17911 len += (int)(p - *arg);
17912 *arg = skipwhite(p);
17913 return len;
17914 }
17915
17916 /*
17917 * Include any <SID> etc in the expanded string:
17918 * Thus the -len here.
17919 */
17920 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17921 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017922 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 *alias = temp_string;
17924 *arg = skipwhite(p);
17925 return (int)STRLEN(temp_string);
17926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927
17928 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017929 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 EMSG2(_(e_invexpr2), *arg);
17931
17932 return len;
17933}
17934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017935/*
17936 * Find the end of a variable or function name, taking care of magic braces.
17937 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17938 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017939 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017940 * Return a pointer to just after the name. Equal to "arg" if there is no
17941 * valid name.
17942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017944find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 char_u *arg;
17946 char_u **expr_start;
17947 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017948 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017950 int mb_nest = 0;
17951 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 char_u *p;
17953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017954 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017956 *expr_start = NULL;
17957 *expr_end = NULL;
17958 }
17959
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017960 /* Quick check for valid starting character. */
17961 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17962 return arg;
17963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017964 for (p = arg; *p != NUL
17965 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017966 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017967 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017968 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017969 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017970 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017971 if (*p == '\'')
17972 {
17973 /* skip over 'string' to avoid counting [ and ] inside it. */
17974 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17975 ;
17976 if (*p == NUL)
17977 break;
17978 }
17979 else if (*p == '"')
17980 {
17981 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17982 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17983 if (*p == '\\' && p[1] != NUL)
17984 ++p;
17985 if (*p == NUL)
17986 break;
17987 }
17988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017989 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017991 if (*p == '[')
17992 ++br_nest;
17993 else if (*p == ']')
17994 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017997 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017999 if (*p == '{')
18000 {
18001 mb_nest++;
18002 if (expr_start != NULL && *expr_start == NULL)
18003 *expr_start = p;
18004 }
18005 else if (*p == '}')
18006 {
18007 mb_nest--;
18008 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18009 *expr_end = p;
18010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 }
18013
18014 return p;
18015}
18016
18017/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018018 * Expands out the 'magic' {}'s in a variable/function name.
18019 * Note that this can call itself recursively, to deal with
18020 * constructs like foo{bar}{baz}{bam}
18021 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18022 * "in_start" ^
18023 * "expr_start" ^
18024 * "expr_end" ^
18025 * "in_end" ^
18026 *
18027 * Returns a new allocated string, which the caller must free.
18028 * Returns NULL for failure.
18029 */
18030 static char_u *
18031make_expanded_name(in_start, expr_start, expr_end, in_end)
18032 char_u *in_start;
18033 char_u *expr_start;
18034 char_u *expr_end;
18035 char_u *in_end;
18036{
18037 char_u c1;
18038 char_u *retval = NULL;
18039 char_u *temp_result;
18040 char_u *nextcmd = NULL;
18041
18042 if (expr_end == NULL || in_end == NULL)
18043 return NULL;
18044 *expr_start = NUL;
18045 *expr_end = NUL;
18046 c1 = *in_end;
18047 *in_end = NUL;
18048
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018049 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018050 if (temp_result != NULL && nextcmd == NULL)
18051 {
18052 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18053 + (in_end - expr_end) + 1));
18054 if (retval != NULL)
18055 {
18056 STRCPY(retval, in_start);
18057 STRCAT(retval, temp_result);
18058 STRCAT(retval, expr_end + 1);
18059 }
18060 }
18061 vim_free(temp_result);
18062
18063 *in_end = c1; /* put char back for error messages */
18064 *expr_start = '{';
18065 *expr_end = '}';
18066
18067 if (retval != NULL)
18068 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018069 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018070 if (expr_start != NULL)
18071 {
18072 /* Further expansion! */
18073 temp_result = make_expanded_name(retval, expr_start,
18074 expr_end, temp_result);
18075 vim_free(retval);
18076 retval = temp_result;
18077 }
18078 }
18079
18080 return retval;
18081}
18082
18083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018085 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 */
18087 static int
18088eval_isnamec(c)
18089 int c;
18090{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018091 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18092}
18093
18094/*
18095 * Return TRUE if character "c" can be used as the first character in a
18096 * variable or function name (excluding '{' and '}').
18097 */
18098 static int
18099eval_isnamec1(c)
18100 int c;
18101{
18102 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103}
18104
18105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 * Set number v: variable to "val".
18107 */
18108 void
18109set_vim_var_nr(idx, val)
18110 int idx;
18111 long val;
18112{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018113 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114}
18115
18116/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018117 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 */
18119 long
18120get_vim_var_nr(idx)
18121 int idx;
18122{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018123 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124}
18125
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018126/*
18127 * Get string v: variable value. Uses a static buffer, can only be used once.
18128 */
18129 char_u *
18130get_vim_var_str(idx)
18131 int idx;
18132{
18133 return get_tv_string(&vimvars[idx].vv_tv);
18134}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018135
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018137 * Get List v: variable value. Caller must take care of reference count when
18138 * needed.
18139 */
18140 list_T *
18141get_vim_var_list(idx)
18142 int idx;
18143{
18144 return vimvars[idx].vv_list;
18145}
18146
18147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 * Set v:count, v:count1 and v:prevcount.
18149 */
18150 void
18151set_vcount(count, count1)
18152 long count;
18153 long count1;
18154{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018155 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
18156 vimvars[VV_COUNT].vv_nr = count;
18157 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158}
18159
18160/*
18161 * Set string v: variable to a copy of "val".
18162 */
18163 void
18164set_vim_var_string(idx, val, len)
18165 int idx;
18166 char_u *val;
18167 int len; /* length of "val" to use or -1 (whole string) */
18168{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018169 /* Need to do this (at least) once, since we can't initialize a union.
18170 * Will always be invoked when "v:progname" is set. */
18171 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18172
Bram Moolenaare9a41262005-01-15 22:18:47 +000018173 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018175 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018177 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018179 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180}
18181
18182/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018183 * Set List v: variable to "val".
18184 */
18185 void
18186set_vim_var_list(idx, val)
18187 int idx;
18188 list_T *val;
18189{
18190 list_unref(vimvars[idx].vv_list);
18191 vimvars[idx].vv_list = val;
18192 if (val != NULL)
18193 ++val->lv_refcount;
18194}
18195
18196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 * Set v:register if needed.
18198 */
18199 void
18200set_reg_var(c)
18201 int c;
18202{
18203 char_u regname;
18204
18205 if (c == 0 || c == ' ')
18206 regname = '"';
18207 else
18208 regname = c;
18209 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018210 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 set_vim_var_string(VV_REG, &regname, 1);
18212}
18213
18214/*
18215 * Get or set v:exception. If "oldval" == NULL, return the current value.
18216 * Otherwise, restore the value to "oldval" and return NULL.
18217 * Must always be called in pairs to save and restore v:exception! Does not
18218 * take care of memory allocations.
18219 */
18220 char_u *
18221v_exception(oldval)
18222 char_u *oldval;
18223{
18224 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018225 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226
Bram Moolenaare9a41262005-01-15 22:18:47 +000018227 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 return NULL;
18229}
18230
18231/*
18232 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18233 * Otherwise, restore the value to "oldval" and return NULL.
18234 * Must always be called in pairs to save and restore v:throwpoint! Does not
18235 * take care of memory allocations.
18236 */
18237 char_u *
18238v_throwpoint(oldval)
18239 char_u *oldval;
18240{
18241 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018242 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243
Bram Moolenaare9a41262005-01-15 22:18:47 +000018244 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 return NULL;
18246}
18247
18248#if defined(FEAT_AUTOCMD) || defined(PROTO)
18249/*
18250 * Set v:cmdarg.
18251 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18252 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18253 * Must always be called in pairs!
18254 */
18255 char_u *
18256set_cmdarg(eap, oldarg)
18257 exarg_T *eap;
18258 char_u *oldarg;
18259{
18260 char_u *oldval;
18261 char_u *newval;
18262 unsigned len;
18263
Bram Moolenaare9a41262005-01-15 22:18:47 +000018264 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018265 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018267 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018268 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018269 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 }
18271
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018272 if (eap->force_bin == FORCE_BIN)
18273 len = 6;
18274 else if (eap->force_bin == FORCE_NOBIN)
18275 len = 8;
18276 else
18277 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018278
18279 if (eap->read_edit)
18280 len += 7;
18281
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018282 if (eap->force_ff != 0)
18283 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18284# ifdef FEAT_MBYTE
18285 if (eap->force_enc != 0)
18286 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018287 if (eap->bad_char != 0)
18288 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018289# endif
18290
18291 newval = alloc(len + 1);
18292 if (newval == NULL)
18293 return NULL;
18294
18295 if (eap->force_bin == FORCE_BIN)
18296 sprintf((char *)newval, " ++bin");
18297 else if (eap->force_bin == FORCE_NOBIN)
18298 sprintf((char *)newval, " ++nobin");
18299 else
18300 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018301
18302 if (eap->read_edit)
18303 STRCAT(newval, " ++edit");
18304
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018305 if (eap->force_ff != 0)
18306 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18307 eap->cmd + eap->force_ff);
18308# ifdef FEAT_MBYTE
18309 if (eap->force_enc != 0)
18310 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18311 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018312 if (eap->bad_char != 0)
18313 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18314 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018315# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018316 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018317 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318}
18319#endif
18320
18321/*
18322 * Get the value of internal variable "name".
18323 * Return OK or FAIL.
18324 */
18325 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018326get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 char_u *name;
18328 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018329 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018330 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331{
18332 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018333 typval_T *tv = NULL;
18334 typval_T atv;
18335 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337
18338 /* truncate the name, so that we can use strcmp() */
18339 cc = name[len];
18340 name[len] = NUL;
18341
18342 /*
18343 * Check for "b:changedtick".
18344 */
18345 if (STRCMP(name, "b:changedtick") == 0)
18346 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018347 atv.v_type = VAR_NUMBER;
18348 atv.vval.v_number = curbuf->b_changedtick;
18349 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 }
18351
18352 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 * Check for user-defined variables.
18354 */
18355 else
18356 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018357 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018359 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 }
18361
Bram Moolenaare9a41262005-01-15 22:18:47 +000018362 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018364 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018365 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 ret = FAIL;
18367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018368 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018369 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370
18371 name[len] = cc;
18372
18373 return ret;
18374}
18375
18376/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018377 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18378 * Also handle function call with Funcref variable: func(expr)
18379 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18380 */
18381 static int
18382handle_subscript(arg, rettv, evaluate, verbose)
18383 char_u **arg;
18384 typval_T *rettv;
18385 int evaluate; /* do more than finding the end */
18386 int verbose; /* give error messages */
18387{
18388 int ret = OK;
18389 dict_T *selfdict = NULL;
18390 char_u *s;
18391 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018392 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018393
18394 while (ret == OK
18395 && (**arg == '['
18396 || (**arg == '.' && rettv->v_type == VAR_DICT)
18397 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18398 && !vim_iswhite(*(*arg - 1)))
18399 {
18400 if (**arg == '(')
18401 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018402 /* need to copy the funcref so that we can clear rettv */
18403 functv = *rettv;
18404 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018405
18406 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018407 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018408 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018409 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18410 &len, evaluate, selfdict);
18411
18412 /* Clear the funcref afterwards, so that deleting it while
18413 * evaluating the arguments is possible (see test55). */
18414 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018415
18416 /* Stop the expression evaluation when immediately aborting on
18417 * error, or when an interrupt occurred or an exception was thrown
18418 * but not caught. */
18419 if (aborting())
18420 {
18421 if (ret == OK)
18422 clear_tv(rettv);
18423 ret = FAIL;
18424 }
18425 dict_unref(selfdict);
18426 selfdict = NULL;
18427 }
18428 else /* **arg == '[' || **arg == '.' */
18429 {
18430 dict_unref(selfdict);
18431 if (rettv->v_type == VAR_DICT)
18432 {
18433 selfdict = rettv->vval.v_dict;
18434 if (selfdict != NULL)
18435 ++selfdict->dv_refcount;
18436 }
18437 else
18438 selfdict = NULL;
18439 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18440 {
18441 clear_tv(rettv);
18442 ret = FAIL;
18443 }
18444 }
18445 }
18446 dict_unref(selfdict);
18447 return ret;
18448}
18449
18450/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018451 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018452 * value).
18453 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018454 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018455alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018456{
Bram Moolenaar33570922005-01-25 22:26:29 +000018457 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018458}
18459
18460/*
18461 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 * The string "s" must have been allocated, it is consumed.
18463 * Return NULL for out of memory, the variable otherwise.
18464 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018466alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 char_u *s;
18468{
Bram Moolenaar33570922005-01-25 22:26:29 +000018469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018471 rettv = alloc_tv();
18472 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474 rettv->v_type = VAR_STRING;
18475 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 }
18477 else
18478 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480}
18481
18482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018483 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018485 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018487 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488{
18489 if (varp != NULL)
18490 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018491 switch (varp->v_type)
18492 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018493 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018494 func_unref(varp->vval.v_string);
18495 /*FALLTHROUGH*/
18496 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018497 vim_free(varp->vval.v_string);
18498 break;
18499 case VAR_LIST:
18500 list_unref(varp->vval.v_list);
18501 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018502 case VAR_DICT:
18503 dict_unref(varp->vval.v_dict);
18504 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018505 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018506#ifdef FEAT_FLOAT
18507 case VAR_FLOAT:
18508#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018509 case VAR_UNKNOWN:
18510 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018511 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018512 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018513 break;
18514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 vim_free(varp);
18516 }
18517}
18518
18519/*
18520 * Free the memory for a variable value and set the value to NULL or 0.
18521 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018522 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018523clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018524 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525{
18526 if (varp != NULL)
18527 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018528 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018530 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018531 func_unref(varp->vval.v_string);
18532 /*FALLTHROUGH*/
18533 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018534 vim_free(varp->vval.v_string);
18535 varp->vval.v_string = NULL;
18536 break;
18537 case VAR_LIST:
18538 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018539 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018540 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018541 case VAR_DICT:
18542 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018543 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018544 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018545 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018546 varp->vval.v_number = 0;
18547 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018548#ifdef FEAT_FLOAT
18549 case VAR_FLOAT:
18550 varp->vval.v_float = 0.0;
18551 break;
18552#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553 case VAR_UNKNOWN:
18554 break;
18555 default:
18556 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018558 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 }
18560}
18561
18562/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 * Set the value of a variable to NULL without freeing items.
18564 */
18565 static void
18566init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018567 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568{
18569 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018570 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571}
18572
18573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 * Get the number value of a variable.
18575 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018576 * For incompatible types, return 0.
18577 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18578 * caller of incompatible types: it sets *denote to TRUE if "denote"
18579 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 */
18581 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018585 int error = FALSE;
18586
18587 return get_tv_number_chk(varp, &error); /* return 0L on error */
18588}
18589
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018590 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018591get_tv_number_chk(varp, denote)
18592 typval_T *varp;
18593 int *denote;
18594{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018595 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018597 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018599 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018600 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018601#ifdef FEAT_FLOAT
18602 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018603 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018604 break;
18605#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018606 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018607 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018608 break;
18609 case VAR_STRING:
18610 if (varp->vval.v_string != NULL)
18611 vim_str2nr(varp->vval.v_string, NULL, NULL,
18612 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018613 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018614 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018615 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018616 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018617 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018618 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018619 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018620 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018621 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018622 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018624 if (denote == NULL) /* useful for values that must be unsigned */
18625 n = -1;
18626 else
18627 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018628 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629}
18630
18631/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018632 * Get the lnum from the first argument.
18633 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018634 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 */
18636 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018638 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639{
Bram Moolenaar33570922005-01-25 22:26:29 +000018640 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 linenr_T lnum;
18642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018643 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 if (lnum == 0) /* no valid number, try using line() */
18645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 rettv.v_type = VAR_NUMBER;
18647 f_line(argvars, &rettv);
18648 lnum = rettv.vval.v_number;
18649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 }
18651 return lnum;
18652}
18653
18654/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018655 * Get the lnum from the first argument.
18656 * Also accepts "$", then "buf" is used.
18657 * Returns 0 on error.
18658 */
18659 static linenr_T
18660get_tv_lnum_buf(argvars, buf)
18661 typval_T *argvars;
18662 buf_T *buf;
18663{
18664 if (argvars[0].v_type == VAR_STRING
18665 && argvars[0].vval.v_string != NULL
18666 && argvars[0].vval.v_string[0] == '$'
18667 && buf != NULL)
18668 return buf->b_ml.ml_line_count;
18669 return get_tv_number_chk(&argvars[0], NULL);
18670}
18671
18672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 * Get the string value of a variable.
18674 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018675 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18676 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 * If the String variable has never been set, return an empty string.
18678 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018679 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18680 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 */
18682 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018684 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685{
18686 static char_u mybuf[NUMBUFLEN];
18687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689}
18690
18691 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018694 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018696 char_u *res = get_tv_string_buf_chk(varp, buf);
18697
18698 return res != NULL ? res : (char_u *)"";
18699}
18700
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018701 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018702get_tv_string_chk(varp)
18703 typval_T *varp;
18704{
18705 static char_u mybuf[NUMBUFLEN];
18706
18707 return get_tv_string_buf_chk(varp, mybuf);
18708}
18709
18710 static char_u *
18711get_tv_string_buf_chk(varp, buf)
18712 typval_T *varp;
18713 char_u *buf;
18714{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018715 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018717 case VAR_NUMBER:
18718 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18719 return buf;
18720 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018721 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018722 break;
18723 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018724 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018725 break;
18726 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018727 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018728 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018729#ifdef FEAT_FLOAT
18730 case VAR_FLOAT:
18731 EMSG(_("E806: using Float as a String"));
18732 break;
18733#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018734 case VAR_STRING:
18735 if (varp->vval.v_string != NULL)
18736 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018737 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018738 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018742 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743}
18744
18745/*
18746 * Find variable "name" in the list of variables.
18747 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018748 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018749 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018750 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018752 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018753find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018755 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018758 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759
Bram Moolenaara7043832005-01-21 11:56:39 +000018760 ht = find_var_ht(name, &varname);
18761 if (htp != NULL)
18762 *htp = ht;
18763 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018765 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766}
18767
18768/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018770 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018772 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018773find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018774 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018775 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018776 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018777{
Bram Moolenaar33570922005-01-25 22:26:29 +000018778 hashitem_T *hi;
18779
18780 if (*varname == NUL)
18781 {
18782 /* Must be something like "s:", otherwise "ht" would be NULL. */
18783 switch (varname[-2])
18784 {
18785 case 's': return &SCRIPT_SV(current_SID).sv_var;
18786 case 'g': return &globvars_var;
18787 case 'v': return &vimvars_var;
18788 case 'b': return &curbuf->b_bufvar;
18789 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018790#ifdef FEAT_WINDOWS
18791 case 't': return &curtab->tp_winvar;
18792#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018793 case 'l': return current_funccal == NULL
18794 ? NULL : &current_funccal->l_vars_var;
18795 case 'a': return current_funccal == NULL
18796 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018797 }
18798 return NULL;
18799 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018800
18801 hi = hash_find(ht, varname);
18802 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018803 {
18804 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018805 * worked find the variable again. Don't auto-load a script if it was
18806 * loaded already, otherwise it would be loaded every time when
18807 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018808 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018809 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018810 hi = hash_find(ht, varname);
18811 if (HASHITEM_EMPTY(hi))
18812 return NULL;
18813 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018814 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018815}
18816
18817/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018818 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018819 * Set "varname" to the start of name without ':'.
18820 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018821 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018822find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 char_u *name;
18824 char_u **varname;
18825{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018826 hashitem_T *hi;
18827
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 if (name[1] != ':')
18829 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018830 /* The name must not start with a colon or #. */
18831 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 return NULL;
18833 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018834
18835 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018836 hi = hash_find(&compat_hashtab, name);
18837 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018838 return &compat_hashtab;
18839
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 return &globvarht; /* global variable */
18842 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 }
18844 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018845 if (*name == 'g') /* global variable */
18846 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018847 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18848 */
18849 if (vim_strchr(name + 2, ':') != NULL
18850 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018851 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018853 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018855 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018856#ifdef FEAT_WINDOWS
18857 if (*name == 't') /* tab page variable */
18858 return &curtab->tp_vars.dv_hashtab;
18859#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018860 if (*name == 'v') /* v: variable */
18861 return &vimvarht;
18862 if (*name == 'a' && current_funccal != NULL) /* function argument */
18863 return &current_funccal->l_avars.dv_hashtab;
18864 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18865 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 if (*name == 's' /* script variable */
18867 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18868 return &SCRIPT_VARS(current_SID);
18869 return NULL;
18870}
18871
18872/*
18873 * Get the string value of a (global/local) variable.
18874 * Returns NULL when it doesn't exist.
18875 */
18876 char_u *
18877get_var_value(name)
18878 char_u *name;
18879{
Bram Moolenaar33570922005-01-25 22:26:29 +000018880 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881
Bram Moolenaara7043832005-01-21 11:56:39 +000018882 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 if (v == NULL)
18884 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886}
18887
18888/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018889 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 * sourcing this script and when executing functions defined in the script.
18891 */
18892 void
18893new_script_vars(id)
18894 scid_T id;
18895{
Bram Moolenaara7043832005-01-21 11:56:39 +000018896 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018897 hashtab_T *ht;
18898 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018899
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18901 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018902 /* Re-allocating ga_data means that an ht_array pointing to
18903 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018904 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018905 for (i = 1; i <= ga_scripts.ga_len; ++i)
18906 {
18907 ht = &SCRIPT_VARS(i);
18908 if (ht->ht_mask == HT_INIT_SIZE - 1)
18909 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 sv = &SCRIPT_SV(i);
18911 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018912 }
18913
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 while (ga_scripts.ga_len < id)
18915 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018916 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18917 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 }
18920 }
18921}
18922
18923/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018924 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18925 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926 */
18927 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018928init_var_dict(dict, dict_var)
18929 dict_T *dict;
18930 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931{
Bram Moolenaar33570922005-01-25 22:26:29 +000018932 hash_init(&dict->dv_hashtab);
18933 dict->dv_refcount = 99999;
18934 dict_var->di_tv.vval.v_dict = dict;
18935 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018936 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018937 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18938 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939}
18940
18941/*
18942 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018943 * Frees all allocated variables and the value they contain.
18944 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 */
18946 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018947vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 hashtab_T *ht;
18949{
18950 vars_clear_ext(ht, TRUE);
18951}
18952
18953/*
18954 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18955 */
18956 static void
18957vars_clear_ext(ht, free_val)
18958 hashtab_T *ht;
18959 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960{
Bram Moolenaara7043832005-01-21 11:56:39 +000018961 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018962 hashitem_T *hi;
18963 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964
Bram Moolenaar33570922005-01-25 22:26:29 +000018965 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018966 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018967 for (hi = ht->ht_array; todo > 0; ++hi)
18968 {
18969 if (!HASHITEM_EMPTY(hi))
18970 {
18971 --todo;
18972
Bram Moolenaar33570922005-01-25 22:26:29 +000018973 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018974 * ht_array might change then. hash_clear() takes care of it
18975 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018976 v = HI2DI(hi);
18977 if (free_val)
18978 clear_tv(&v->di_tv);
18979 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18980 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018981 }
18982 }
18983 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018984 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985}
18986
Bram Moolenaara7043832005-01-21 11:56:39 +000018987/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018988 * Delete a variable from hashtab "ht" at item "hi".
18989 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018992delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018993 hashtab_T *ht;
18994 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995{
Bram Moolenaar33570922005-01-25 22:26:29 +000018996 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018997
18998 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018999 clear_tv(&di->di_tv);
19000 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001}
19002
19003/*
19004 * List the value of one internal variable.
19005 */
19006 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019007list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019008 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019010 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019012 char_u *tofree;
19013 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019014 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019015
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019016 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019018 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019019 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020}
19021
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019023list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 char_u *prefix;
19025 char_u *name;
19026 int type;
19027 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019028 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029{
Bram Moolenaar31859182007-08-14 20:41:13 +000019030 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19031 msg_start();
19032 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 if (name != NULL) /* "a:" vars don't have a name stored */
19034 msg_puts(name);
19035 msg_putchar(' ');
19036 msg_advance(22);
19037 if (type == VAR_NUMBER)
19038 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019039 else if (type == VAR_FUNC)
19040 msg_putchar('*');
19041 else if (type == VAR_LIST)
19042 {
19043 msg_putchar('[');
19044 if (*string == '[')
19045 ++string;
19046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019047 else if (type == VAR_DICT)
19048 {
19049 msg_putchar('{');
19050 if (*string == '{')
19051 ++string;
19052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 else
19054 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019055
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019057
19058 if (type == VAR_FUNC)
19059 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019060 if (*first)
19061 {
19062 msg_clr_eos();
19063 *first = FALSE;
19064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065}
19066
19067/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019068 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 * If the variable already exists, the value is updated.
19070 * Otherwise the variable is created.
19071 */
19072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019073set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019075 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019076 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077{
Bram Moolenaar33570922005-01-25 22:26:29 +000019078 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019080 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019081 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019083 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019084 {
19085 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19086 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19087 ? name[2] : name[0]))
19088 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019089 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019090 return;
19091 }
19092 if (function_exists(name))
19093 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019094 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019095 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019096 return;
19097 }
19098 }
19099
Bram Moolenaara7043832005-01-21 11:56:39 +000019100 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019102 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019103 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019104 return;
19105 }
19106
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019107 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019108 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019111 if (var_check_ro(v->di_flags, name)
19112 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019113 return;
19114 if (v->di_tv.v_type != tv->v_type
19115 && !((v->di_tv.v_type == VAR_STRING
19116 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019117 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019118 || tv->v_type == VAR_NUMBER))
19119#ifdef FEAT_FLOAT
19120 && !((v->di_tv.v_type == VAR_NUMBER
19121 || v->di_tv.v_type == VAR_FLOAT)
19122 && (tv->v_type == VAR_NUMBER
19123 || tv->v_type == VAR_FLOAT))
19124#endif
19125 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019126 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019127 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019128 return;
19129 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019130
19131 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019132 * Handle setting internal v: variables separately: we don't change
19133 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019134 */
19135 if (ht == &vimvarht)
19136 {
19137 if (v->di_tv.v_type == VAR_STRING)
19138 {
19139 vim_free(v->di_tv.vval.v_string);
19140 if (copy || tv->v_type != VAR_STRING)
19141 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19142 else
19143 {
19144 /* Take over the string to avoid an extra alloc/free. */
19145 v->di_tv.vval.v_string = tv->vval.v_string;
19146 tv->vval.v_string = NULL;
19147 }
19148 }
19149 else if (v->di_tv.v_type != VAR_NUMBER)
19150 EMSG2(_(e_intern2), "set_var()");
19151 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019152 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019153 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019154 if (STRCMP(varname, "searchforward") == 0)
19155 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19156 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019157 return;
19158 }
19159
19160 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 }
19162 else /* add a new variable */
19163 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019164 /* Can't add "v:" variable. */
19165 if (ht == &vimvarht)
19166 {
19167 EMSG2(_(e_illvar), name);
19168 return;
19169 }
19170
Bram Moolenaar92124a32005-06-17 22:03:40 +000019171 /* Make sure the variable name is valid. */
19172 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019173 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19174 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019175 {
19176 EMSG2(_(e_illvar), varname);
19177 return;
19178 }
19179
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019180 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19181 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019182 if (v == NULL)
19183 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019184 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019185 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019187 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 return;
19189 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019190 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019192
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019193 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019194 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019195 else
19196 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019197 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019198 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201}
19202
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019203/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019204 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019205 * Also give an error message.
19206 */
19207 static int
19208var_check_ro(flags, name)
19209 int flags;
19210 char_u *name;
19211{
19212 if (flags & DI_FLAGS_RO)
19213 {
19214 EMSG2(_(e_readonlyvar), name);
19215 return TRUE;
19216 }
19217 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19218 {
19219 EMSG2(_(e_readonlysbx), name);
19220 return TRUE;
19221 }
19222 return FALSE;
19223}
19224
19225/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019226 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19227 * Also give an error message.
19228 */
19229 static int
19230var_check_fixed(flags, name)
19231 int flags;
19232 char_u *name;
19233{
19234 if (flags & DI_FLAGS_FIX)
19235 {
19236 EMSG2(_("E795: Cannot delete variable %s"), name);
19237 return TRUE;
19238 }
19239 return FALSE;
19240}
19241
19242/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019243 * Return TRUE if typeval "tv" is set to be locked (immutable).
19244 * Also give an error message, using "name".
19245 */
19246 static int
19247tv_check_lock(lock, name)
19248 int lock;
19249 char_u *name;
19250{
19251 if (lock & VAR_LOCKED)
19252 {
19253 EMSG2(_("E741: Value is locked: %s"),
19254 name == NULL ? (char_u *)_("Unknown") : name);
19255 return TRUE;
19256 }
19257 if (lock & VAR_FIXED)
19258 {
19259 EMSG2(_("E742: Cannot change value of %s"),
19260 name == NULL ? (char_u *)_("Unknown") : name);
19261 return TRUE;
19262 }
19263 return FALSE;
19264}
19265
19266/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019267 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019268 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019269 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019272copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019273 typval_T *from;
19274 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019277 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019278 switch (from->v_type)
19279 {
19280 case VAR_NUMBER:
19281 to->vval.v_number = from->vval.v_number;
19282 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019283#ifdef FEAT_FLOAT
19284 case VAR_FLOAT:
19285 to->vval.v_float = from->vval.v_float;
19286 break;
19287#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019288 case VAR_STRING:
19289 case VAR_FUNC:
19290 if (from->vval.v_string == NULL)
19291 to->vval.v_string = NULL;
19292 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019293 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019294 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019295 if (from->v_type == VAR_FUNC)
19296 func_ref(to->vval.v_string);
19297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019298 break;
19299 case VAR_LIST:
19300 if (from->vval.v_list == NULL)
19301 to->vval.v_list = NULL;
19302 else
19303 {
19304 to->vval.v_list = from->vval.v_list;
19305 ++to->vval.v_list->lv_refcount;
19306 }
19307 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019308 case VAR_DICT:
19309 if (from->vval.v_dict == NULL)
19310 to->vval.v_dict = NULL;
19311 else
19312 {
19313 to->vval.v_dict = from->vval.v_dict;
19314 ++to->vval.v_dict->dv_refcount;
19315 }
19316 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019317 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019319 break;
19320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321}
19322
19323/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019324 * Make a copy of an item.
19325 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019326 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19327 * reference to an already copied list/dict can be used.
19328 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019329 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019330 static int
19331item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019332 typval_T *from;
19333 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019334 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019335 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019336{
19337 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019338 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019339
Bram Moolenaar33570922005-01-25 22:26:29 +000019340 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019341 {
19342 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019343 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019344 }
19345 ++recurse;
19346
19347 switch (from->v_type)
19348 {
19349 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019350#ifdef FEAT_FLOAT
19351 case VAR_FLOAT:
19352#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019353 case VAR_STRING:
19354 case VAR_FUNC:
19355 copy_tv(from, to);
19356 break;
19357 case VAR_LIST:
19358 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019359 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019360 if (from->vval.v_list == NULL)
19361 to->vval.v_list = NULL;
19362 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19363 {
19364 /* use the copy made earlier */
19365 to->vval.v_list = from->vval.v_list->lv_copylist;
19366 ++to->vval.v_list->lv_refcount;
19367 }
19368 else
19369 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19370 if (to->vval.v_list == NULL)
19371 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019372 break;
19373 case VAR_DICT:
19374 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019375 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019376 if (from->vval.v_dict == NULL)
19377 to->vval.v_dict = NULL;
19378 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19379 {
19380 /* use the copy made earlier */
19381 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19382 ++to->vval.v_dict->dv_refcount;
19383 }
19384 else
19385 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19386 if (to->vval.v_dict == NULL)
19387 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019388 break;
19389 default:
19390 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019391 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019392 }
19393 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019394 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019395}
19396
19397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 * ":echo expr1 ..." print each argument separated with a space, add a
19399 * newline at the end.
19400 * ":echon expr1 ..." print each argument plain.
19401 */
19402 void
19403ex_echo(eap)
19404 exarg_T *eap;
19405{
19406 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019407 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019408 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 char_u *p;
19410 int needclr = TRUE;
19411 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019412 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413
19414 if (eap->skip)
19415 ++emsg_skip;
19416 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19417 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019418 /* If eval1() causes an error message the text from the command may
19419 * still need to be cleared. E.g., "echo 22,44". */
19420 need_clr_eos = needclr;
19421
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 {
19425 /*
19426 * Report the invalid expression unless the expression evaluation
19427 * has been cancelled due to an aborting error, an interrupt, or an
19428 * exception.
19429 */
19430 if (!aborting())
19431 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019432 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 break;
19434 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019435 need_clr_eos = FALSE;
19436
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 if (!eap->skip)
19438 {
19439 if (atstart)
19440 {
19441 atstart = FALSE;
19442 /* Call msg_start() after eval1(), evaluating the expression
19443 * may cause a message to appear. */
19444 if (eap->cmdidx == CMD_echo)
19445 msg_start();
19446 }
19447 else if (eap->cmdidx == CMD_echo)
19448 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019449 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019450 if (p != NULL)
19451 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019453 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019455 if (*p != TAB && needclr)
19456 {
19457 /* remove any text still there from the command */
19458 msg_clr_eos();
19459 needclr = FALSE;
19460 }
19461 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 }
19463 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019464 {
19465#ifdef FEAT_MBYTE
19466 if (has_mbyte)
19467 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019468 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019469
19470 (void)msg_outtrans_len_attr(p, i, echo_attr);
19471 p += i - 1;
19472 }
19473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019475 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019478 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 arg = skipwhite(arg);
19482 }
19483 eap->nextcmd = check_nextcmd(arg);
19484
19485 if (eap->skip)
19486 --emsg_skip;
19487 else
19488 {
19489 /* remove text that may still be there from the command */
19490 if (needclr)
19491 msg_clr_eos();
19492 if (eap->cmdidx == CMD_echo)
19493 msg_end();
19494 }
19495}
19496
19497/*
19498 * ":echohl {name}".
19499 */
19500 void
19501ex_echohl(eap)
19502 exarg_T *eap;
19503{
19504 int id;
19505
19506 id = syn_name2id(eap->arg);
19507 if (id == 0)
19508 echo_attr = 0;
19509 else
19510 echo_attr = syn_id2attr(id);
19511}
19512
19513/*
19514 * ":execute expr1 ..." execute the result of an expression.
19515 * ":echomsg expr1 ..." Print a message
19516 * ":echoerr expr1 ..." Print an error
19517 * Each gets spaces around each argument and a newline at the end for
19518 * echo commands
19519 */
19520 void
19521ex_execute(eap)
19522 exarg_T *eap;
19523{
19524 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019525 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 int ret = OK;
19527 char_u *p;
19528 garray_T ga;
19529 int len;
19530 int save_did_emsg;
19531
19532 ga_init2(&ga, 1, 80);
19533
19534 if (eap->skip)
19535 ++emsg_skip;
19536 while (*arg != NUL && *arg != '|' && *arg != '\n')
19537 {
19538 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 {
19541 /*
19542 * Report the invalid expression unless the expression evaluation
19543 * has been cancelled due to an aborting error, an interrupt, or an
19544 * exception.
19545 */
19546 if (!aborting())
19547 EMSG2(_(e_invexpr2), p);
19548 ret = FAIL;
19549 break;
19550 }
19551
19552 if (!eap->skip)
19553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019554 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 len = (int)STRLEN(p);
19556 if (ga_grow(&ga, len + 2) == FAIL)
19557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559 ret = FAIL;
19560 break;
19561 }
19562 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565 ga.ga_len += len;
19566 }
19567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019568 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 arg = skipwhite(arg);
19570 }
19571
19572 if (ret != FAIL && ga.ga_data != NULL)
19573 {
19574 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019577 out_flush();
19578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 else if (eap->cmdidx == CMD_echoerr)
19580 {
19581 /* We don't want to abort following commands, restore did_emsg. */
19582 save_did_emsg = did_emsg;
19583 EMSG((char_u *)ga.ga_data);
19584 if (!force_abort)
19585 did_emsg = save_did_emsg;
19586 }
19587 else if (eap->cmdidx == CMD_execute)
19588 do_cmdline((char_u *)ga.ga_data,
19589 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19590 }
19591
19592 ga_clear(&ga);
19593
19594 if (eap->skip)
19595 --emsg_skip;
19596
19597 eap->nextcmd = check_nextcmd(arg);
19598}
19599
19600/*
19601 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19602 * "arg" points to the "&" or '+' when called, to "option" when returning.
19603 * Returns NULL when no option name found. Otherwise pointer to the char
19604 * after the option name.
19605 */
19606 static char_u *
19607find_option_end(arg, opt_flags)
19608 char_u **arg;
19609 int *opt_flags;
19610{
19611 char_u *p = *arg;
19612
19613 ++p;
19614 if (*p == 'g' && p[1] == ':')
19615 {
19616 *opt_flags = OPT_GLOBAL;
19617 p += 2;
19618 }
19619 else if (*p == 'l' && p[1] == ':')
19620 {
19621 *opt_flags = OPT_LOCAL;
19622 p += 2;
19623 }
19624 else
19625 *opt_flags = 0;
19626
19627 if (!ASCII_ISALPHA(*p))
19628 return NULL;
19629 *arg = p;
19630
19631 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19632 p += 4; /* termcap option */
19633 else
19634 while (ASCII_ISALPHA(*p))
19635 ++p;
19636 return p;
19637}
19638
19639/*
19640 * ":function"
19641 */
19642 void
19643ex_function(eap)
19644 exarg_T *eap;
19645{
19646 char_u *theline;
19647 int j;
19648 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650 char_u *name = NULL;
19651 char_u *p;
19652 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019653 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 garray_T newargs;
19655 garray_T newlines;
19656 int varargs = FALSE;
19657 int mustend = FALSE;
19658 int flags = 0;
19659 ufunc_T *fp;
19660 int indent;
19661 int nesting;
19662 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019663 dictitem_T *v;
19664 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019665 static int func_nr = 0; /* number for nameless function */
19666 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019667 hashtab_T *ht;
19668 int todo;
19669 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019670 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671
19672 /*
19673 * ":function" without argument: list functions.
19674 */
19675 if (ends_excmd(*eap->arg))
19676 {
19677 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019678 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019679 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019680 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019681 {
19682 if (!HASHITEM_EMPTY(hi))
19683 {
19684 --todo;
19685 fp = HI2UF(hi);
19686 if (!isdigit(*fp->uf_name))
19687 list_func_head(fp, FALSE);
19688 }
19689 }
19690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 eap->nextcmd = check_nextcmd(eap->arg);
19692 return;
19693 }
19694
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019695 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019696 * ":function /pat": list functions matching pattern.
19697 */
19698 if (*eap->arg == '/')
19699 {
19700 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19701 if (!eap->skip)
19702 {
19703 regmatch_T regmatch;
19704
19705 c = *p;
19706 *p = NUL;
19707 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19708 *p = c;
19709 if (regmatch.regprog != NULL)
19710 {
19711 regmatch.rm_ic = p_ic;
19712
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019713 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019714 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19715 {
19716 if (!HASHITEM_EMPTY(hi))
19717 {
19718 --todo;
19719 fp = HI2UF(hi);
19720 if (!isdigit(*fp->uf_name)
19721 && vim_regexec(&regmatch, fp->uf_name, 0))
19722 list_func_head(fp, FALSE);
19723 }
19724 }
19725 }
19726 }
19727 if (*p == '/')
19728 ++p;
19729 eap->nextcmd = check_nextcmd(p);
19730 return;
19731 }
19732
19733 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019734 * Get the function name. There are these situations:
19735 * func normal function name
19736 * "name" == func, "fudi.fd_dict" == NULL
19737 * dict.func new dictionary entry
19738 * "name" == NULL, "fudi.fd_dict" set,
19739 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19740 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019741 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019742 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19743 * dict.func existing dict entry that's not a Funcref
19744 * "name" == NULL, "fudi.fd_dict" set,
19745 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019748 name = trans_function_name(&p, eap->skip, 0, &fudi);
19749 paren = (vim_strchr(p, '(') != NULL);
19750 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 {
19752 /*
19753 * Return on an invalid expression in braces, unless the expression
19754 * evaluation has been cancelled due to an aborting error, an
19755 * interrupt, or an exception.
19756 */
19757 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019758 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019759 if (!eap->skip && fudi.fd_newkey != NULL)
19760 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019761 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 else
19765 eap->skip = TRUE;
19766 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019767
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 /* An error in a function call during evaluation of an expression in magic
19769 * braces should not cause the function not to be defined. */
19770 saved_did_emsg = did_emsg;
19771 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772
19773 /*
19774 * ":function func" with only function name: list function.
19775 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019776 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 {
19778 if (!ends_excmd(*skipwhite(p)))
19779 {
19780 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019781 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 }
19783 eap->nextcmd = check_nextcmd(p);
19784 if (eap->nextcmd != NULL)
19785 *p = NUL;
19786 if (!eap->skip && !got_int)
19787 {
19788 fp = find_func(name);
19789 if (fp != NULL)
19790 {
19791 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019792 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019794 if (FUNCLINE(fp, j) == NULL)
19795 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 msg_putchar('\n');
19797 msg_outnum((long)(j + 1));
19798 if (j < 9)
19799 msg_putchar(' ');
19800 if (j < 99)
19801 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019802 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 out_flush(); /* show a line at a time */
19804 ui_breakcheck();
19805 }
19806 if (!got_int)
19807 {
19808 msg_putchar('\n');
19809 msg_puts((char_u *)" endfunction");
19810 }
19811 }
19812 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019813 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019815 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 }
19817
19818 /*
19819 * ":function name(arg1, arg2)" Define function.
19820 */
19821 p = skipwhite(p);
19822 if (*p != '(')
19823 {
19824 if (!eap->skip)
19825 {
19826 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019827 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 }
19829 /* attempt to continue by skipping some text */
19830 if (vim_strchr(p, '(') != NULL)
19831 p = vim_strchr(p, '(');
19832 }
19833 p = skipwhite(p + 1);
19834
19835 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19836 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19837
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019838 if (!eap->skip)
19839 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019840 /* Check the name of the function. Unless it's a dictionary function
19841 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019842 if (name != NULL)
19843 arg = name;
19844 else
19845 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019846 if (arg != NULL && (fudi.fd_di == NULL
19847 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019848 {
19849 if (*arg == K_SPECIAL)
19850 j = 3;
19851 else
19852 j = 0;
19853 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19854 : eval_isnamec(arg[j])))
19855 ++j;
19856 if (arg[j] != NUL)
19857 emsg_funcname(_(e_invarg2), arg);
19858 }
19859 }
19860
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 /*
19862 * Isolate the arguments: "arg1, arg2, ...)"
19863 */
19864 while (*p != ')')
19865 {
19866 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19867 {
19868 varargs = TRUE;
19869 p += 3;
19870 mustend = TRUE;
19871 }
19872 else
19873 {
19874 arg = p;
19875 while (ASCII_ISALNUM(*p) || *p == '_')
19876 ++p;
19877 if (arg == p || isdigit(*arg)
19878 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19879 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19880 {
19881 if (!eap->skip)
19882 EMSG2(_("E125: Illegal argument: %s"), arg);
19883 break;
19884 }
19885 if (ga_grow(&newargs, 1) == FAIL)
19886 goto erret;
19887 c = *p;
19888 *p = NUL;
19889 arg = vim_strsave(arg);
19890 if (arg == NULL)
19891 goto erret;
19892 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19893 *p = c;
19894 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 if (*p == ',')
19896 ++p;
19897 else
19898 mustend = TRUE;
19899 }
19900 p = skipwhite(p);
19901 if (mustend && *p != ')')
19902 {
19903 if (!eap->skip)
19904 EMSG2(_(e_invarg2), eap->arg);
19905 break;
19906 }
19907 }
19908 ++p; /* skip the ')' */
19909
Bram Moolenaare9a41262005-01-15 22:18:47 +000019910 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 for (;;)
19912 {
19913 p = skipwhite(p);
19914 if (STRNCMP(p, "range", 5) == 0)
19915 {
19916 flags |= FC_RANGE;
19917 p += 5;
19918 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019919 else if (STRNCMP(p, "dict", 4) == 0)
19920 {
19921 flags |= FC_DICT;
19922 p += 4;
19923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 else if (STRNCMP(p, "abort", 5) == 0)
19925 {
19926 flags |= FC_ABORT;
19927 p += 5;
19928 }
19929 else
19930 break;
19931 }
19932
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019933 /* When there is a line break use what follows for the function body.
19934 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19935 if (*p == '\n')
19936 line_arg = p + 1;
19937 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 EMSG(_(e_trailing));
19939
19940 /*
19941 * Read the body of the function, until ":endfunction" is found.
19942 */
19943 if (KeyTyped)
19944 {
19945 /* Check if the function already exists, don't let the user type the
19946 * whole function before telling him it doesn't work! For a script we
19947 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019948 if (!eap->skip && !eap->forceit)
19949 {
19950 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19951 EMSG(_(e_funcdict));
19952 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019953 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019956 if (!eap->skip && did_emsg)
19957 goto erret;
19958
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 msg_putchar('\n'); /* don't overwrite the function name */
19960 cmdline_row = msg_row;
19961 }
19962
19963 indent = 2;
19964 nesting = 0;
19965 for (;;)
19966 {
19967 msg_scroll = TRUE;
19968 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019969 sourcing_lnum_off = sourcing_lnum;
19970
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019971 if (line_arg != NULL)
19972 {
19973 /* Use eap->arg, split up in parts by line breaks. */
19974 theline = line_arg;
19975 p = vim_strchr(theline, '\n');
19976 if (p == NULL)
19977 line_arg += STRLEN(line_arg);
19978 else
19979 {
19980 *p = NUL;
19981 line_arg = p + 1;
19982 }
19983 }
19984 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 theline = getcmdline(':', 0L, indent);
19986 else
19987 theline = eap->getline(':', eap->cookie, indent);
19988 if (KeyTyped)
19989 lines_left = Rows - 1;
19990 if (theline == NULL)
19991 {
19992 EMSG(_("E126: Missing :endfunction"));
19993 goto erret;
19994 }
19995
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019996 /* Detect line continuation: sourcing_lnum increased more than one. */
19997 if (sourcing_lnum > sourcing_lnum_off + 1)
19998 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19999 else
20000 sourcing_lnum_off = 0;
20001
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 if (skip_until != NULL)
20003 {
20004 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20005 * don't check for ":endfunc". */
20006 if (STRCMP(theline, skip_until) == 0)
20007 {
20008 vim_free(skip_until);
20009 skip_until = NULL;
20010 }
20011 }
20012 else
20013 {
20014 /* skip ':' and blanks*/
20015 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20016 ;
20017
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020018 /* Check for "endfunction". */
20019 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020021 if (line_arg == NULL)
20022 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 break;
20024 }
20025
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020026 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 * at "end". */
20028 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20029 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020030 else if (STRNCMP(p, "if", 2) == 0
20031 || STRNCMP(p, "wh", 2) == 0
20032 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 || STRNCMP(p, "try", 3) == 0)
20034 indent += 2;
20035
20036 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020037 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020039 if (*p == '!')
20040 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 p += eval_fname_script(p);
20042 if (ASCII_ISALPHA(*p))
20043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020044 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 if (*skipwhite(p) == '(')
20046 {
20047 ++nesting;
20048 indent += 2;
20049 }
20050 }
20051 }
20052
20053 /* Check for ":append" or ":insert". */
20054 p = skip_range(p, NULL);
20055 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20056 || (p[0] == 'i'
20057 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20058 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20059 skip_until = vim_strsave((char_u *)".");
20060
20061 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20062 arg = skipwhite(skiptowhite(p));
20063 if (arg[0] == '<' && arg[1] =='<'
20064 && ((p[0] == 'p' && p[1] == 'y'
20065 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20066 || (p[0] == 'p' && p[1] == 'e'
20067 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20068 || (p[0] == 't' && p[1] == 'c'
20069 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20070 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20071 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020072 || (p[0] == 'm' && p[1] == 'z'
20073 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 ))
20075 {
20076 /* ":python <<" continues until a dot, like ":append" */
20077 p = skipwhite(arg + 2);
20078 if (*p == NUL)
20079 skip_until = vim_strsave((char_u *)".");
20080 else
20081 skip_until = vim_strsave(p);
20082 }
20083 }
20084
20085 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020086 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020087 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020088 if (line_arg == NULL)
20089 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020091 }
20092
20093 /* Copy the line to newly allocated memory. get_one_sourceline()
20094 * allocates 250 bytes per line, this saves 80% on average. The cost
20095 * is an extra alloc/free. */
20096 p = vim_strsave(theline);
20097 if (p != NULL)
20098 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020099 if (line_arg == NULL)
20100 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020101 theline = p;
20102 }
20103
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020104 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20105
20106 /* Add NULL lines for continuation lines, so that the line count is
20107 * equal to the index in the growarray. */
20108 while (sourcing_lnum_off-- > 0)
20109 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020110
20111 /* Check for end of eap->arg. */
20112 if (line_arg != NULL && *line_arg == NUL)
20113 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 }
20115
20116 /* Don't define the function when skipping commands or when an error was
20117 * detected. */
20118 if (eap->skip || did_emsg)
20119 goto erret;
20120
20121 /*
20122 * If there are no errors, add the function
20123 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020124 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020125 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020126 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020128 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020129 emsg_funcname("E707: Function name conflicts with variable: %s",
20130 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020131 goto erret;
20132 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020133
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020134 fp = find_func(name);
20135 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020137 if (!eap->forceit)
20138 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020139 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020140 goto erret;
20141 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020142 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020143 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020144 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020145 name);
20146 goto erret;
20147 }
20148 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020149 ga_clear_strings(&(fp->uf_args));
20150 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020151 vim_free(name);
20152 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 }
20155 else
20156 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020157 char numbuf[20];
20158
20159 fp = NULL;
20160 if (fudi.fd_newkey == NULL && !eap->forceit)
20161 {
20162 EMSG(_(e_funcdict));
20163 goto erret;
20164 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020165 if (fudi.fd_di == NULL)
20166 {
20167 /* Can't add a function to a locked dictionary */
20168 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20169 goto erret;
20170 }
20171 /* Can't change an existing function if it is locked */
20172 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20173 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020174
20175 /* Give the function a sequential number. Can only be used with a
20176 * Funcref! */
20177 vim_free(name);
20178 sprintf(numbuf, "%d", ++func_nr);
20179 name = vim_strsave((char_u *)numbuf);
20180 if (name == NULL)
20181 goto erret;
20182 }
20183
20184 if (fp == NULL)
20185 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020186 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020187 {
20188 int slen, plen;
20189 char_u *scriptname;
20190
20191 /* Check that the autoload name matches the script name. */
20192 j = FAIL;
20193 if (sourcing_name != NULL)
20194 {
20195 scriptname = autoload_name(name);
20196 if (scriptname != NULL)
20197 {
20198 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020199 plen = (int)STRLEN(p);
20200 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020201 if (slen > plen && fnamecmp(p,
20202 sourcing_name + slen - plen) == 0)
20203 j = OK;
20204 vim_free(scriptname);
20205 }
20206 }
20207 if (j == FAIL)
20208 {
20209 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20210 goto erret;
20211 }
20212 }
20213
20214 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 if (fp == NULL)
20216 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020217
20218 if (fudi.fd_dict != NULL)
20219 {
20220 if (fudi.fd_di == NULL)
20221 {
20222 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020223 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020224 if (fudi.fd_di == NULL)
20225 {
20226 vim_free(fp);
20227 goto erret;
20228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020229 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20230 {
20231 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020232 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020233 goto erret;
20234 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020235 }
20236 else
20237 /* overwrite existing dict entry */
20238 clear_tv(&fudi.fd_di->di_tv);
20239 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020240 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020241 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020242 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020243
20244 /* behave like "dict" was used */
20245 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020246 }
20247
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020249 STRCPY(fp->uf_name, name);
20250 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020252 fp->uf_args = newargs;
20253 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020254#ifdef FEAT_PROFILE
20255 fp->uf_tml_count = NULL;
20256 fp->uf_tml_total = NULL;
20257 fp->uf_tml_self = NULL;
20258 fp->uf_profiling = FALSE;
20259 if (prof_def_func())
20260 func_do_profile(fp);
20261#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020262 fp->uf_varargs = varargs;
20263 fp->uf_flags = flags;
20264 fp->uf_calls = 0;
20265 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020266 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267
20268erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 ga_clear_strings(&newargs);
20270 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020271ret_free:
20272 vim_free(skip_until);
20273 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276}
20277
20278/*
20279 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020280 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020282 * flags:
20283 * TFN_INT: internal function name OK
20284 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 * Advances "pp" to just after the function name (if no error).
20286 */
20287 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020288trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 char_u **pp;
20290 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020291 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020294 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 char_u *start;
20296 char_u *end;
20297 int lead;
20298 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020301
20302 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020305
20306 /* Check for hard coded <SNR>: already translated function ID (from a user
20307 * command). */
20308 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20309 && (*pp)[2] == (int)KE_SNR)
20310 {
20311 *pp += 3;
20312 len = get_id_len(pp) + 3;
20313 return vim_strnsave(start, len);
20314 }
20315
20316 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20317 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020319 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020321
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020322 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20323 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020324 if (end == start)
20325 {
20326 if (!skip)
20327 EMSG(_("E129: Function name required"));
20328 goto theend;
20329 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020330 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020331 {
20332 /*
20333 * Report an invalid expression in braces, unless the expression
20334 * evaluation has been cancelled due to an aborting error, an
20335 * interrupt, or an exception.
20336 */
20337 if (!aborting())
20338 {
20339 if (end != NULL)
20340 EMSG2(_(e_invarg2), start);
20341 }
20342 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020343 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020344 goto theend;
20345 }
20346
20347 if (lv.ll_tv != NULL)
20348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020349 if (fdp != NULL)
20350 {
20351 fdp->fd_dict = lv.ll_dict;
20352 fdp->fd_newkey = lv.ll_newkey;
20353 lv.ll_newkey = NULL;
20354 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020355 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020356 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20357 {
20358 name = vim_strsave(lv.ll_tv->vval.v_string);
20359 *pp = end;
20360 }
20361 else
20362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020363 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20364 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020365 EMSG(_(e_funcref));
20366 else
20367 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020368 name = NULL;
20369 }
20370 goto theend;
20371 }
20372
20373 if (lv.ll_name == NULL)
20374 {
20375 /* Error found, but continue after the function name. */
20376 *pp = end;
20377 goto theend;
20378 }
20379
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020380 /* Check if the name is a Funcref. If so, use the value. */
20381 if (lv.ll_exp_name != NULL)
20382 {
20383 len = (int)STRLEN(lv.ll_exp_name);
20384 name = deref_func_name(lv.ll_exp_name, &len);
20385 if (name == lv.ll_exp_name)
20386 name = NULL;
20387 }
20388 else
20389 {
20390 len = (int)(end - *pp);
20391 name = deref_func_name(*pp, &len);
20392 if (name == *pp)
20393 name = NULL;
20394 }
20395 if (name != NULL)
20396 {
20397 name = vim_strsave(name);
20398 *pp = end;
20399 goto theend;
20400 }
20401
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020402 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020403 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020404 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020405 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20406 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20407 {
20408 /* When there was "s:" already or the name expanded to get a
20409 * leading "s:" then remove it. */
20410 lv.ll_name += 2;
20411 len -= 2;
20412 lead = 2;
20413 }
20414 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020415 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020416 {
20417 if (lead == 2) /* skip over "s:" */
20418 lv.ll_name += 2;
20419 len = (int)(end - lv.ll_name);
20420 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020421
20422 /*
20423 * Copy the function name to allocated memory.
20424 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20425 * Accept <SNR>123_name() outside a script.
20426 */
20427 if (skip)
20428 lead = 0; /* do nothing */
20429 else if (lead > 0)
20430 {
20431 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020432 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20433 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020434 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020435 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020436 if (current_SID <= 0)
20437 {
20438 EMSG(_(e_usingsid));
20439 goto theend;
20440 }
20441 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20442 lead += (int)STRLEN(sid_buf);
20443 }
20444 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020445 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020446 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020447 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020448 goto theend;
20449 }
20450 name = alloc((unsigned)(len + lead + 1));
20451 if (name != NULL)
20452 {
20453 if (lead > 0)
20454 {
20455 name[0] = K_SPECIAL;
20456 name[1] = KS_EXTRA;
20457 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020458 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020459 STRCPY(name + 3, sid_buf);
20460 }
20461 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20462 name[len + lead] = NUL;
20463 }
20464 *pp = end;
20465
20466theend:
20467 clear_lval(&lv);
20468 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469}
20470
20471/*
20472 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20473 * Return 2 if "p" starts with "s:".
20474 * Return 0 otherwise.
20475 */
20476 static int
20477eval_fname_script(p)
20478 char_u *p;
20479{
20480 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20481 || STRNICMP(p + 1, "SNR>", 4) == 0))
20482 return 5;
20483 if (p[0] == 's' && p[1] == ':')
20484 return 2;
20485 return 0;
20486}
20487
20488/*
20489 * Return TRUE if "p" starts with "<SID>" or "s:".
20490 * Only works if eval_fname_script() returned non-zero for "p"!
20491 */
20492 static int
20493eval_fname_sid(p)
20494 char_u *p;
20495{
20496 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20497}
20498
20499/*
20500 * List the head of the function: "name(arg1, arg2)".
20501 */
20502 static void
20503list_func_head(fp, indent)
20504 ufunc_T *fp;
20505 int indent;
20506{
20507 int j;
20508
20509 msg_start();
20510 if (indent)
20511 MSG_PUTS(" ");
20512 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020513 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 {
20515 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020516 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 }
20518 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020519 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020521 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 {
20523 if (j)
20524 MSG_PUTS(", ");
20525 msg_puts(FUNCARG(fp, j));
20526 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020527 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 {
20529 if (j)
20530 MSG_PUTS(", ");
20531 MSG_PUTS("...");
20532 }
20533 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020534 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020535 if (p_verbose > 0)
20536 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537}
20538
20539/*
20540 * Find a function by name, return pointer to it in ufuncs.
20541 * Return NULL for unknown function.
20542 */
20543 static ufunc_T *
20544find_func(name)
20545 char_u *name;
20546{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020547 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020549 hi = hash_find(&func_hashtab, name);
20550 if (!HASHITEM_EMPTY(hi))
20551 return HI2UF(hi);
20552 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553}
20554
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020555#if defined(EXITFREE) || defined(PROTO)
20556 void
20557free_all_functions()
20558{
20559 hashitem_T *hi;
20560
20561 /* Need to start all over every time, because func_free() may change the
20562 * hash table. */
20563 while (func_hashtab.ht_used > 0)
20564 for (hi = func_hashtab.ht_array; ; ++hi)
20565 if (!HASHITEM_EMPTY(hi))
20566 {
20567 func_free(HI2UF(hi));
20568 break;
20569 }
20570}
20571#endif
20572
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020573/*
20574 * Return TRUE if a function "name" exists.
20575 */
20576 static int
20577function_exists(name)
20578 char_u *name;
20579{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020580 char_u *nm = name;
20581 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020582 int n = FALSE;
20583
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020584 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020585 nm = skipwhite(nm);
20586
20587 /* Only accept "funcname", "funcname ", "funcname (..." and
20588 * "funcname(...", not "funcname!...". */
20589 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020590 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020591 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020592 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020593 else
20594 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020595 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020596 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020597 return n;
20598}
20599
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020600/*
20601 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020602 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020603 */
20604 static int
20605builtin_function(name)
20606 char_u *name;
20607{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020608 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20609 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020610}
20611
Bram Moolenaar05159a02005-02-26 23:04:13 +000020612#if defined(FEAT_PROFILE) || defined(PROTO)
20613/*
20614 * Start profiling function "fp".
20615 */
20616 static void
20617func_do_profile(fp)
20618 ufunc_T *fp;
20619{
20620 fp->uf_tm_count = 0;
20621 profile_zero(&fp->uf_tm_self);
20622 profile_zero(&fp->uf_tm_total);
20623 if (fp->uf_tml_count == NULL)
20624 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20625 (sizeof(int) * fp->uf_lines.ga_len));
20626 if (fp->uf_tml_total == NULL)
20627 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20628 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20629 if (fp->uf_tml_self == NULL)
20630 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20631 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20632 fp->uf_tml_idx = -1;
20633 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20634 || fp->uf_tml_self == NULL)
20635 return; /* out of memory */
20636
20637 fp->uf_profiling = TRUE;
20638}
20639
20640/*
20641 * Dump the profiling results for all functions in file "fd".
20642 */
20643 void
20644func_dump_profile(fd)
20645 FILE *fd;
20646{
20647 hashitem_T *hi;
20648 int todo;
20649 ufunc_T *fp;
20650 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020651 ufunc_T **sorttab;
20652 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020653
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020654 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020655 if (todo == 0)
20656 return; /* nothing to dump */
20657
Bram Moolenaar73830342005-02-28 22:48:19 +000020658 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20659
Bram Moolenaar05159a02005-02-26 23:04:13 +000020660 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20661 {
20662 if (!HASHITEM_EMPTY(hi))
20663 {
20664 --todo;
20665 fp = HI2UF(hi);
20666 if (fp->uf_profiling)
20667 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020668 if (sorttab != NULL)
20669 sorttab[st_len++] = fp;
20670
Bram Moolenaar05159a02005-02-26 23:04:13 +000020671 if (fp->uf_name[0] == K_SPECIAL)
20672 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20673 else
20674 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20675 if (fp->uf_tm_count == 1)
20676 fprintf(fd, "Called 1 time\n");
20677 else
20678 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20679 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20680 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20681 fprintf(fd, "\n");
20682 fprintf(fd, "count total (s) self (s)\n");
20683
20684 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20685 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020686 if (FUNCLINE(fp, i) == NULL)
20687 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020688 prof_func_line(fd, fp->uf_tml_count[i],
20689 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020690 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20691 }
20692 fprintf(fd, "\n");
20693 }
20694 }
20695 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020696
20697 if (sorttab != NULL && st_len > 0)
20698 {
20699 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20700 prof_total_cmp);
20701 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20702 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20703 prof_self_cmp);
20704 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20705 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020706
20707 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020708}
Bram Moolenaar73830342005-02-28 22:48:19 +000020709
20710 static void
20711prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20712 FILE *fd;
20713 ufunc_T **sorttab;
20714 int st_len;
20715 char *title;
20716 int prefer_self; /* when equal print only self time */
20717{
20718 int i;
20719 ufunc_T *fp;
20720
20721 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20722 fprintf(fd, "count total (s) self (s) function\n");
20723 for (i = 0; i < 20 && i < st_len; ++i)
20724 {
20725 fp = sorttab[i];
20726 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20727 prefer_self);
20728 if (fp->uf_name[0] == K_SPECIAL)
20729 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20730 else
20731 fprintf(fd, " %s()\n", fp->uf_name);
20732 }
20733 fprintf(fd, "\n");
20734}
20735
20736/*
20737 * Print the count and times for one function or function line.
20738 */
20739 static void
20740prof_func_line(fd, count, total, self, prefer_self)
20741 FILE *fd;
20742 int count;
20743 proftime_T *total;
20744 proftime_T *self;
20745 int prefer_self; /* when equal print only self time */
20746{
20747 if (count > 0)
20748 {
20749 fprintf(fd, "%5d ", count);
20750 if (prefer_self && profile_equal(total, self))
20751 fprintf(fd, " ");
20752 else
20753 fprintf(fd, "%s ", profile_msg(total));
20754 if (!prefer_self && profile_equal(total, self))
20755 fprintf(fd, " ");
20756 else
20757 fprintf(fd, "%s ", profile_msg(self));
20758 }
20759 else
20760 fprintf(fd, " ");
20761}
20762
20763/*
20764 * Compare function for total time sorting.
20765 */
20766 static int
20767#ifdef __BORLANDC__
20768_RTLENTRYF
20769#endif
20770prof_total_cmp(s1, s2)
20771 const void *s1;
20772 const void *s2;
20773{
20774 ufunc_T *p1, *p2;
20775
20776 p1 = *(ufunc_T **)s1;
20777 p2 = *(ufunc_T **)s2;
20778 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20779}
20780
20781/*
20782 * Compare function for self time sorting.
20783 */
20784 static int
20785#ifdef __BORLANDC__
20786_RTLENTRYF
20787#endif
20788prof_self_cmp(s1, s2)
20789 const void *s1;
20790 const void *s2;
20791{
20792 ufunc_T *p1, *p2;
20793
20794 p1 = *(ufunc_T **)s1;
20795 p2 = *(ufunc_T **)s2;
20796 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20797}
20798
Bram Moolenaar05159a02005-02-26 23:04:13 +000020799#endif
20800
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020801/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020802 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020803 * Return TRUE if a package was loaded.
20804 */
20805 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020806script_autoload(name, reload)
20807 char_u *name;
20808 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020809{
20810 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020811 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020812 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020813 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020814
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020815 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020816 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020817 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020818 return FALSE;
20819
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020820 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020821
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020822 /* Find the name in the list of previously loaded package names. Skip
20823 * "autoload/", it's always the same. */
20824 for (i = 0; i < ga_loaded.ga_len; ++i)
20825 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20826 break;
20827 if (!reload && i < ga_loaded.ga_len)
20828 ret = FALSE; /* was loaded already */
20829 else
20830 {
20831 /* Remember the name if it wasn't loaded already. */
20832 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20833 {
20834 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20835 tofree = NULL;
20836 }
20837
20838 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020839 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020840 ret = TRUE;
20841 }
20842
20843 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020844 return ret;
20845}
20846
20847/*
20848 * Return the autoload script name for a function or variable name.
20849 * Returns NULL when out of memory.
20850 */
20851 static char_u *
20852autoload_name(name)
20853 char_u *name;
20854{
20855 char_u *p;
20856 char_u *scriptname;
20857
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020858 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020859 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20860 if (scriptname == NULL)
20861 return FALSE;
20862 STRCPY(scriptname, "autoload/");
20863 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020864 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020865 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020866 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020867 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020868 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020869}
20870
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20872
20873/*
20874 * Function given to ExpandGeneric() to obtain the list of user defined
20875 * function names.
20876 */
20877 char_u *
20878get_user_func_name(xp, idx)
20879 expand_T *xp;
20880 int idx;
20881{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020882 static long_u done;
20883 static hashitem_T *hi;
20884 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885
20886 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020888 done = 0;
20889 hi = func_hashtab.ht_array;
20890 }
20891 if (done < func_hashtab.ht_used)
20892 {
20893 if (done++ > 0)
20894 ++hi;
20895 while (HASHITEM_EMPTY(hi))
20896 ++hi;
20897 fp = HI2UF(hi);
20898
20899 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20900 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901
20902 cat_func_name(IObuff, fp);
20903 if (xp->xp_context != EXPAND_USER_FUNC)
20904 {
20905 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020906 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 STRCAT(IObuff, ")");
20908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 return IObuff;
20910 }
20911 return NULL;
20912}
20913
20914#endif /* FEAT_CMDL_COMPL */
20915
20916/*
20917 * Copy the function name of "fp" to buffer "buf".
20918 * "buf" must be able to hold the function name plus three bytes.
20919 * Takes care of script-local function names.
20920 */
20921 static void
20922cat_func_name(buf, fp)
20923 char_u *buf;
20924 ufunc_T *fp;
20925{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020926 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 {
20928 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020929 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 }
20931 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020932 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933}
20934
20935/*
20936 * ":delfunction {name}"
20937 */
20938 void
20939ex_delfunction(eap)
20940 exarg_T *eap;
20941{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020942 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 char_u *p;
20944 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020945 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946
20947 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020948 name = trans_function_name(&p, eap->skip, 0, &fudi);
20949 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020951 {
20952 if (fudi.fd_dict != NULL && !eap->skip)
20953 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 if (!ends_excmd(*skipwhite(p)))
20957 {
20958 vim_free(name);
20959 EMSG(_(e_trailing));
20960 return;
20961 }
20962 eap->nextcmd = check_nextcmd(p);
20963 if (eap->nextcmd != NULL)
20964 *p = NUL;
20965
20966 if (!eap->skip)
20967 fp = find_func(name);
20968 vim_free(name);
20969
20970 if (!eap->skip)
20971 {
20972 if (fp == NULL)
20973 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020974 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 return;
20976 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020977 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 {
20979 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20980 return;
20981 }
20982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020983 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020985 /* Delete the dict item that refers to the function, it will
20986 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020987 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020989 else
20990 func_free(fp);
20991 }
20992}
20993
20994/*
20995 * Free a function and remove it from the list of functions.
20996 */
20997 static void
20998func_free(fp)
20999 ufunc_T *fp;
21000{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021001 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021002
21003 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021004 ga_clear_strings(&(fp->uf_args));
21005 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021006#ifdef FEAT_PROFILE
21007 vim_free(fp->uf_tml_count);
21008 vim_free(fp->uf_tml_total);
21009 vim_free(fp->uf_tml_self);
21010#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021011
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021012 /* remove the function from the function hashtable */
21013 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21014 if (HASHITEM_EMPTY(hi))
21015 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021017 hash_remove(&func_hashtab, hi);
21018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021019 vim_free(fp);
21020}
21021
21022/*
21023 * Unreference a Function: decrement the reference count and free it when it
21024 * becomes zero. Only for numbered functions.
21025 */
21026 static void
21027func_unref(name)
21028 char_u *name;
21029{
21030 ufunc_T *fp;
21031
21032 if (name != NULL && isdigit(*name))
21033 {
21034 fp = find_func(name);
21035 if (fp == NULL)
21036 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021037 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021038 {
21039 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021040 * when "uf_calls" becomes zero. */
21041 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021042 func_free(fp);
21043 }
21044 }
21045}
21046
21047/*
21048 * Count a reference to a Function.
21049 */
21050 static void
21051func_ref(name)
21052 char_u *name;
21053{
21054 ufunc_T *fp;
21055
21056 if (name != NULL && isdigit(*name))
21057 {
21058 fp = find_func(name);
21059 if (fp == NULL)
21060 EMSG2(_(e_intern2), "func_ref()");
21061 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021062 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 }
21064}
21065
21066/*
21067 * Call a user function.
21068 */
21069 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021070call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 ufunc_T *fp; /* pointer to function */
21072 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021073 typval_T *argvars; /* arguments */
21074 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075 linenr_T firstline; /* first line of range */
21076 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021077 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078{
Bram Moolenaar33570922005-01-25 22:26:29 +000021079 char_u *save_sourcing_name;
21080 linenr_T save_sourcing_lnum;
21081 scid_T save_current_SID;
21082 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021083 int save_did_emsg;
21084 static int depth = 0;
21085 dictitem_T *v;
21086 int fixvar_idx = 0; /* index in fixvar[] */
21087 int i;
21088 int ai;
21089 char_u numbuf[NUMBUFLEN];
21090 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021091#ifdef FEAT_PROFILE
21092 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021093 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095
21096 /* If depth of calling is getting too high, don't execute the function */
21097 if (depth >= p_mfd)
21098 {
21099 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021100 rettv->v_type = VAR_NUMBER;
21101 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 return;
21103 }
21104 ++depth;
21105
21106 line_breakcheck(); /* check for CTRL-C hit */
21107
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021108 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000021109 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111 fc.rettv = rettv;
21112 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 fc.linenr = 0;
21114 fc.returned = FALSE;
21115 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021117 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 fc.dbg_tick = debug_tick;
21119
Bram Moolenaar33570922005-01-25 22:26:29 +000021120 /*
21121 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
21122 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21123 * each argument variable and saves a lot of time.
21124 */
21125 /*
21126 * Init l: variables.
21127 */
21128 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021129 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021130 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021131 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21132 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021133 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021134 name = v->di_key;
21135 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021136 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
21137 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
21138 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021139 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021140 v->di_tv.vval.v_dict = selfdict;
21141 ++selfdict->dv_refcount;
21142 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021143
Bram Moolenaar33570922005-01-25 22:26:29 +000021144 /*
21145 * Init a: variables.
21146 * Set a:0 to "argcount".
21147 * Set a:000 to a list with room for the "..." arguments.
21148 */
21149 init_var_dict(&fc.l_avars, &fc.l_avars_var);
21150 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021151 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000021152 v = &fc.fixvar[fixvar_idx++].var;
21153 STRCPY(v->di_key, "000");
21154 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21155 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21156 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021157 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021158 v->di_tv.vval.v_list = &fc.l_varlist;
21159 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
21160 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000021161 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021162
21163 /*
21164 * Set a:firstline to "firstline" and a:lastline to "lastline".
21165 * Set a:name to named arguments.
21166 * Set a:N to the "..." arguments.
21167 */
21168 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
21169 (varnumber_T)firstline);
21170 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
21171 (varnumber_T)lastline);
21172 for (i = 0; i < argcount; ++i)
21173 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021174 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021175 if (ai < 0)
21176 /* named argument a:name */
21177 name = FUNCARG(fp, i);
21178 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021179 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021180 /* "..." argument a:1, a:2, etc. */
21181 sprintf((char *)numbuf, "%d", ai + 1);
21182 name = numbuf;
21183 }
21184 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21185 {
21186 v = &fc.fixvar[fixvar_idx++].var;
21187 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21188 }
21189 else
21190 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021191 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21192 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021193 if (v == NULL)
21194 break;
21195 v->di_flags = DI_FLAGS_RO;
21196 }
21197 STRCPY(v->di_key, name);
21198 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
21199
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021200 /* Note: the values are copied directly to avoid alloc/free.
21201 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021202 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021203 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021204
21205 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21206 {
21207 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
21208 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021209 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021210 }
21211 }
21212
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 /* Don't redraw while executing the function. */
21214 ++RedrawingDisabled;
21215 save_sourcing_name = sourcing_name;
21216 save_sourcing_lnum = sourcing_lnum;
21217 sourcing_lnum = 1;
21218 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021219 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 if (sourcing_name != NULL)
21221 {
21222 if (save_sourcing_name != NULL
21223 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21224 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21225 else
21226 STRCPY(sourcing_name, "function ");
21227 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21228
21229 if (p_verbose >= 12)
21230 {
21231 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021232 verbose_enter_scroll();
21233
Bram Moolenaar555b2802005-05-19 21:08:39 +000021234 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 if (p_verbose >= 14)
21236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021238 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021239 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021240 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241
21242 msg_puts((char_u *)"(");
21243 for (i = 0; i < argcount; ++i)
21244 {
21245 if (i > 0)
21246 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021247 if (argvars[i].v_type == VAR_NUMBER)
21248 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 else
21250 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021251 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21252 if (s != NULL)
21253 {
21254 trunc_string(s, buf, MSG_BUF_CLEN);
21255 msg_puts(buf);
21256 vim_free(tofree);
21257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 }
21259 }
21260 msg_puts((char_u *)")");
21261 }
21262 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021263
21264 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 --no_wait_return;
21266 }
21267 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021268#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021269 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021270 {
21271 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21272 func_do_profile(fp);
21273 if (fp->uf_profiling
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021274 || (fc.caller != NULL && fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021275 {
21276 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021277 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021278 profile_zero(&fp->uf_tm_children);
21279 }
21280 script_prof_save(&wait_start);
21281 }
21282#endif
21283
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021285 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 save_did_emsg = did_emsg;
21287 did_emsg = FALSE;
21288
21289 /* call do_cmdline() to execute the lines */
21290 do_cmdline(NULL, get_func_line, (void *)&fc,
21291 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21292
21293 --RedrawingDisabled;
21294
21295 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021296 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021298 clear_tv(rettv);
21299 rettv->v_type = VAR_NUMBER;
21300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 }
21302
Bram Moolenaar05159a02005-02-26 23:04:13 +000021303#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021304 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021305 || (fc.caller != NULL && fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021306 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021307 profile_end(&call_start);
21308 profile_sub_wait(&wait_start, &call_start);
21309 profile_add(&fp->uf_tm_total, &call_start);
21310 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021311 if (fc.caller != NULL && fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021312 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021313 profile_add(&fc.caller->func->uf_tm_children, &call_start);
21314 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021315 }
21316 }
21317#endif
21318
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 /* when being verbose, mention the return value */
21320 if (p_verbose >= 12)
21321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021323 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021326 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021327 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021328 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
21329 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021332 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021333 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021334 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021335 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021336
Bram Moolenaar555b2802005-05-19 21:08:39 +000021337 /* The value may be very long. Skip the middle part, so that we
21338 * have some idea how it starts and ends. smsg() would always
21339 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021340 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
21341 if (s != NULL)
21342 {
21343 trunc_string(s, buf, MSG_BUF_CLEN);
21344 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21345 vim_free(tofree);
21346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 }
21348 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021349
21350 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 --no_wait_return;
21352 }
21353
21354 vim_free(sourcing_name);
21355 sourcing_name = save_sourcing_name;
21356 sourcing_lnum = save_sourcing_lnum;
21357 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021358#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021359 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021360 script_prof_restore(&wait_start);
21361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362
21363 if (p_verbose >= 12 && sourcing_name != NULL)
21364 {
21365 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021366 verbose_enter_scroll();
21367
Bram Moolenaar555b2802005-05-19 21:08:39 +000021368 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021370
21371 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 --no_wait_return;
21373 }
21374
21375 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021376 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021378 /* The a: variables typevals were not allocated, only free the allocated
Bram Moolenaar33570922005-01-25 22:26:29 +000021379 * variables. */
21380 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
21381
21382 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 --depth;
21384}
21385
21386/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021387 * Add a number variable "name" to dict "dp" with value "nr".
21388 */
21389 static void
21390add_nr_var(dp, v, name, nr)
21391 dict_T *dp;
21392 dictitem_T *v;
21393 char *name;
21394 varnumber_T nr;
21395{
21396 STRCPY(v->di_key, name);
21397 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21398 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21399 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021400 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021401 v->di_tv.vval.v_number = nr;
21402}
21403
21404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 * ":return [expr]"
21406 */
21407 void
21408ex_return(eap)
21409 exarg_T *eap;
21410{
21411 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 int returning = FALSE;
21414
21415 if (current_funccal == NULL)
21416 {
21417 EMSG(_("E133: :return not inside a function"));
21418 return;
21419 }
21420
21421 if (eap->skip)
21422 ++emsg_skip;
21423
21424 eap->nextcmd = NULL;
21425 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021426 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 {
21428 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021429 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 }
21433 /* It's safer to return also on error. */
21434 else if (!eap->skip)
21435 {
21436 /*
21437 * Return unless the expression evaluation has been cancelled due to an
21438 * aborting error, an interrupt, or an exception.
21439 */
21440 if (!aborting())
21441 returning = do_return(eap, FALSE, TRUE, NULL);
21442 }
21443
21444 /* When skipping or the return gets pending, advance to the next command
21445 * in this line (!returning). Otherwise, ignore the rest of the line.
21446 * Following lines will be ignored by get_func_line(). */
21447 if (returning)
21448 eap->nextcmd = NULL;
21449 else if (eap->nextcmd == NULL) /* no argument */
21450 eap->nextcmd = check_nextcmd(arg);
21451
21452 if (eap->skip)
21453 --emsg_skip;
21454}
21455
21456/*
21457 * Return from a function. Possibly makes the return pending. Also called
21458 * for a pending return at the ":endtry" or after returning from an extra
21459 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021460 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021461 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 * FALSE when the return gets pending.
21463 */
21464 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021465do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 exarg_T *eap;
21467 int reanimate;
21468 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021469 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
21471 int idx;
21472 struct condstack *cstack = eap->cstack;
21473
21474 if (reanimate)
21475 /* Undo the return. */
21476 current_funccal->returned = FALSE;
21477
21478 /*
21479 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21480 * not in its finally clause (which then is to be executed next) is found.
21481 * In this case, make the ":return" pending for execution at the ":endtry".
21482 * Otherwise, return normally.
21483 */
21484 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21485 if (idx >= 0)
21486 {
21487 cstack->cs_pending[idx] = CSTP_RETURN;
21488
21489 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021490 /* A pending return again gets pending. "rettv" points to an
21491 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021493 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 else
21495 {
21496 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021497 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021499 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021501 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 {
21503 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021504 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 else
21507 EMSG(_(e_outofmem));
21508 }
21509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021510 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511
21512 if (reanimate)
21513 {
21514 /* The pending return value could be overwritten by a ":return"
21515 * without argument in a finally clause; reset the default
21516 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021517 current_funccal->rettv->v_type = VAR_NUMBER;
21518 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 }
21520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021521 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 }
21523 else
21524 {
21525 current_funccal->returned = TRUE;
21526
21527 /* If the return is carried out now, store the return value. For
21528 * a return immediately after reanimation, the value is already
21529 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021530 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021532 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 }
21537 }
21538
21539 return idx < 0;
21540}
21541
21542/*
21543 * Free the variable with a pending return value.
21544 */
21545 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021546discard_pending_return(rettv)
21547 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548{
Bram Moolenaar33570922005-01-25 22:26:29 +000021549 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550}
21551
21552/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021553 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 * is an allocated string. Used by report_pending() for verbose messages.
21555 */
21556 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021557get_return_cmd(rettv)
21558 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021560 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021561 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021562 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021564 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021565 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021566 if (s == NULL)
21567 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021568
21569 STRCPY(IObuff, ":return ");
21570 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21571 if (STRLEN(s) + 8 >= IOSIZE)
21572 STRCPY(IObuff + IOSIZE - 4, "...");
21573 vim_free(tofree);
21574 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575}
21576
21577/*
21578 * Get next function line.
21579 * Called by do_cmdline() to get the next line.
21580 * Returns allocated string, or NULL for end of function.
21581 */
21582/* ARGSUSED */
21583 char_u *
21584get_func_line(c, cookie, indent)
21585 int c; /* not used */
21586 void *cookie;
21587 int indent; /* not used */
21588{
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021590 ufunc_T *fp = fcp->func;
21591 char_u *retval;
21592 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593
21594 /* If breakpoints have been added/deleted need to check for it. */
21595 if (fcp->dbg_tick != debug_tick)
21596 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021597 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 sourcing_lnum);
21599 fcp->dbg_tick = debug_tick;
21600 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021601#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021602 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021603 func_line_end(cookie);
21604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605
Bram Moolenaar05159a02005-02-26 23:04:13 +000021606 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021607 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21608 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 retval = NULL;
21610 else
21611 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021612 /* Skip NULL lines (continuation lines). */
21613 while (fcp->linenr < gap->ga_len
21614 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21615 ++fcp->linenr;
21616 if (fcp->linenr >= gap->ga_len)
21617 retval = NULL;
21618 else
21619 {
21620 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21621 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021622#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021623 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021624 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021625#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 }
21628
21629 /* Did we encounter a breakpoint? */
21630 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21631 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021632 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021634 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 sourcing_lnum);
21636 fcp->dbg_tick = debug_tick;
21637 }
21638
21639 return retval;
21640}
21641
Bram Moolenaar05159a02005-02-26 23:04:13 +000021642#if defined(FEAT_PROFILE) || defined(PROTO)
21643/*
21644 * Called when starting to read a function line.
21645 * "sourcing_lnum" must be correct!
21646 * When skipping lines it may not actually be executed, but we won't find out
21647 * until later and we need to store the time now.
21648 */
21649 void
21650func_line_start(cookie)
21651 void *cookie;
21652{
21653 funccall_T *fcp = (funccall_T *)cookie;
21654 ufunc_T *fp = fcp->func;
21655
21656 if (fp->uf_profiling && sourcing_lnum >= 1
21657 && sourcing_lnum <= fp->uf_lines.ga_len)
21658 {
21659 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021660 /* Skip continuation lines. */
21661 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21662 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021663 fp->uf_tml_execed = FALSE;
21664 profile_start(&fp->uf_tml_start);
21665 profile_zero(&fp->uf_tml_children);
21666 profile_get_wait(&fp->uf_tml_wait);
21667 }
21668}
21669
21670/*
21671 * Called when actually executing a function line.
21672 */
21673 void
21674func_line_exec(cookie)
21675 void *cookie;
21676{
21677 funccall_T *fcp = (funccall_T *)cookie;
21678 ufunc_T *fp = fcp->func;
21679
21680 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21681 fp->uf_tml_execed = TRUE;
21682}
21683
21684/*
21685 * Called when done with a function line.
21686 */
21687 void
21688func_line_end(cookie)
21689 void *cookie;
21690{
21691 funccall_T *fcp = (funccall_T *)cookie;
21692 ufunc_T *fp = fcp->func;
21693
21694 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21695 {
21696 if (fp->uf_tml_execed)
21697 {
21698 ++fp->uf_tml_count[fp->uf_tml_idx];
21699 profile_end(&fp->uf_tml_start);
21700 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021701 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021702 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21703 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021704 }
21705 fp->uf_tml_idx = -1;
21706 }
21707}
21708#endif
21709
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710/*
21711 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021712 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 */
21714 int
21715func_has_ended(cookie)
21716 void *cookie;
21717{
Bram Moolenaar33570922005-01-25 22:26:29 +000021718 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719
21720 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21721 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021722 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 || fcp->returned);
21724}
21725
21726/*
21727 * return TRUE if cookie indicates a function which "abort"s on errors.
21728 */
21729 int
21730func_has_abort(cookie)
21731 void *cookie;
21732{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021733 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734}
21735
21736#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21737typedef enum
21738{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021739 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21740 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21741 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742} var_flavour_T;
21743
21744static var_flavour_T var_flavour __ARGS((char_u *varname));
21745
21746 static var_flavour_T
21747var_flavour(varname)
21748 char_u *varname;
21749{
21750 char_u *p = varname;
21751
21752 if (ASCII_ISUPPER(*p))
21753 {
21754 while (*(++p))
21755 if (ASCII_ISLOWER(*p))
21756 return VAR_FLAVOUR_SESSION;
21757 return VAR_FLAVOUR_VIMINFO;
21758 }
21759 else
21760 return VAR_FLAVOUR_DEFAULT;
21761}
21762#endif
21763
21764#if defined(FEAT_VIMINFO) || defined(PROTO)
21765/*
21766 * Restore global vars that start with a capital from the viminfo file
21767 */
21768 int
21769read_viminfo_varlist(virp, writing)
21770 vir_T *virp;
21771 int writing;
21772{
21773 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021774 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776
21777 if (!writing && (find_viminfo_parameter('!') != NULL))
21778 {
21779 tab = vim_strchr(virp->vir_line + 1, '\t');
21780 if (tab != NULL)
21781 {
21782 *tab++ = '\0'; /* isolate the variable name */
21783 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021784 type = VAR_STRING;
21785#ifdef FEAT_FLOAT
21786 else if (*tab == 'F')
21787 type = VAR_FLOAT;
21788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789
21790 tab = vim_strchr(tab, '\t');
21791 if (tab != NULL)
21792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021793 tv.v_type = type;
21794 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021795 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021797#ifdef FEAT_FLOAT
21798 else if (type == VAR_FLOAT)
21799 (void)string2float(tab + 1, &tv.vval.v_float);
21800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021802 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021803 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021804 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021805 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 }
21807 }
21808 }
21809
21810 return viminfo_readline(virp);
21811}
21812
21813/*
21814 * Write global vars that start with a capital to the viminfo file
21815 */
21816 void
21817write_viminfo_varlist(fp)
21818 FILE *fp;
21819{
Bram Moolenaar33570922005-01-25 22:26:29 +000021820 hashitem_T *hi;
21821 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021822 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021823 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021824 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021825 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021826 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827
21828 if (find_viminfo_parameter('!') == NULL)
21829 return;
21830
21831 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021832
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021833 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021834 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021836 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021838 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 this_var = HI2DI(hi);
21840 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021841 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021843 {
21844 case VAR_STRING: s = "STR"; break;
21845 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021846#ifdef FEAT_FLOAT
21847 case VAR_FLOAT: s = "FLO"; break;
21848#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021849 default: continue;
21850 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021852 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021853 if (p != NULL)
21854 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021855 vim_free(tofree);
21856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 }
21858 }
21859}
21860#endif
21861
21862#if defined(FEAT_SESSION) || defined(PROTO)
21863 int
21864store_session_globals(fd)
21865 FILE *fd;
21866{
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 hashitem_T *hi;
21868 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021869 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 char_u *p, *t;
21871
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021872 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021873 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021875 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021877 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021878 this_var = HI2DI(hi);
21879 if ((this_var->di_tv.v_type == VAR_NUMBER
21880 || this_var->di_tv.v_type == VAR_STRING)
21881 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021882 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021883 /* Escape special characters with a backslash. Turn a LF and
21884 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021886 (char_u *)"\\\"\n\r");
21887 if (p == NULL) /* out of memory */
21888 break;
21889 for (t = p; *t != NUL; ++t)
21890 if (*t == '\n')
21891 *t = 'n';
21892 else if (*t == '\r')
21893 *t = 'r';
21894 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 this_var->di_key,
21896 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21897 : ' ',
21898 p,
21899 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21900 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021901 || put_eol(fd) == FAIL)
21902 {
21903 vim_free(p);
21904 return FAIL;
21905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 vim_free(p);
21907 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021908#ifdef FEAT_FLOAT
21909 else if (this_var->di_tv.v_type == VAR_FLOAT
21910 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21911 {
21912 float_T f = this_var->di_tv.vval.v_float;
21913 int sign = ' ';
21914
21915 if (f < 0)
21916 {
21917 f = -f;
21918 sign = '-';
21919 }
21920 if ((fprintf(fd, "let %s = %c&%f",
21921 this_var->di_key, sign, f) < 0)
21922 || put_eol(fd) == FAIL)
21923 return FAIL;
21924 }
21925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 }
21927 }
21928 return OK;
21929}
21930#endif
21931
Bram Moolenaar661b1822005-07-28 22:36:45 +000021932/*
21933 * Display script name where an item was last set.
21934 * Should only be invoked when 'verbose' is non-zero.
21935 */
21936 void
21937last_set_msg(scriptID)
21938 scid_T scriptID;
21939{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021940 char_u *p;
21941
Bram Moolenaar661b1822005-07-28 22:36:45 +000021942 if (scriptID != 0)
21943 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021944 p = home_replace_save(NULL, get_scriptname(scriptID));
21945 if (p != NULL)
21946 {
21947 verbose_enter();
21948 MSG_PUTS(_("\n\tLast set from "));
21949 MSG_PUTS(p);
21950 vim_free(p);
21951 verbose_leave();
21952 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000021953 }
21954}
21955
Bram Moolenaard812df62008-11-09 12:46:09 +000021956/*
21957 * List v:oldfiles in a nice way.
21958 */
21959/*ARGSUSED*/
21960 void
21961ex_oldfiles(eap)
21962 exarg_T *eap;
21963{
21964 list_T *l = vimvars[VV_OLDFILES].vv_list;
21965 listitem_T *li;
21966 int nr = 0;
21967
21968 if (l == NULL)
21969 msg((char_u *)_("No old files"));
21970 else
21971 {
21972 msg_start();
21973 msg_scroll = TRUE;
21974 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
21975 {
21976 msg_outnum((long)++nr);
21977 MSG_PUTS(": ");
21978 msg_outtrans(get_tv_string(&li->li_tv));
21979 msg_putchar('\n');
21980 out_flush(); /* output one line at a time */
21981 ui_breakcheck();
21982 }
21983 /* Assume "got_int" was set to truncate the listing. */
21984 got_int = FALSE;
21985
21986#ifdef FEAT_BROWSE_CMD
21987 if (cmdmod.browse)
21988 {
21989 quit_more = FALSE;
21990 nr = prompt_for_number(FALSE);
21991 msg_starthere();
21992 if (nr > 0)
21993 {
21994 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
21995 (long)nr);
21996
21997 if (p != NULL)
21998 {
21999 p = expand_env_save(p);
22000 eap->arg = p;
22001 eap->cmdidx = CMD_edit;
22002 cmdmod.browse = FALSE;
22003 do_exedit(eap, NULL);
22004 vim_free(p);
22005 }
22006 }
22007 }
22008#endif
22009 }
22010}
22011
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012#endif /* FEAT_EVAL */
22013
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022015#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016
22017#ifdef WIN3264
22018/*
22019 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22020 */
22021static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22022static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22023static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22024
22025/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022026 * Get the short path (8.3) for the filename in "fnamep".
22027 * Only works for a valid file name.
22028 * When the path gets longer "fnamep" is changed and the allocated buffer
22029 * is put in "bufp".
22030 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22031 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 */
22033 static int
22034get_short_pathname(fnamep, bufp, fnamelen)
22035 char_u **fnamep;
22036 char_u **bufp;
22037 int *fnamelen;
22038{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022039 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 char_u *newbuf;
22041
22042 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 l = GetShortPathName(*fnamep, *fnamep, len);
22044 if (l > len - 1)
22045 {
22046 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022047 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 newbuf = vim_strnsave(*fnamep, l);
22049 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022050 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051
22052 vim_free(*bufp);
22053 *fnamep = *bufp = newbuf;
22054
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022055 /* Really should always succeed, as the buffer is big enough. */
22056 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 }
22058
22059 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022060 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061}
22062
22063/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022064 * Get the short path (8.3) for the filename in "fname". The converted
22065 * path is returned in "bufp".
22066 *
22067 * Some of the directories specified in "fname" may not exist. This function
22068 * will shorten the existing directories at the beginning of the path and then
22069 * append the remaining non-existing path.
22070 *
22071 * fname - Pointer to the filename to shorten. On return, contains the
22072 * pointer to the shortened pathname
22073 * bufp - Pointer to an allocated buffer for the filename.
22074 * fnamelen - Length of the filename pointed to by fname
22075 *
22076 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 */
22078 static int
22079shortpath_for_invalid_fname(fname, bufp, fnamelen)
22080 char_u **fname;
22081 char_u **bufp;
22082 int *fnamelen;
22083{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022084 char_u *short_fname, *save_fname, *pbuf_unused;
22085 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022087 int old_len, len;
22088 int new_len, sfx_len;
22089 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090
22091 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022092 old_len = *fnamelen;
22093 save_fname = vim_strnsave(*fname, old_len);
22094 pbuf_unused = NULL;
22095 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022097 endp = save_fname + old_len - 1; /* Find the end of the copy */
22098 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022100 /*
22101 * Try shortening the supplied path till it succeeds by removing one
22102 * directory at a time from the tail of the path.
22103 */
22104 len = 0;
22105 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022107 /* go back one path-separator */
22108 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22109 --endp;
22110 if (endp <= save_fname)
22111 break; /* processed the complete path */
22112
22113 /*
22114 * Replace the path separator with a NUL and try to shorten the
22115 * resulting path.
22116 */
22117 ch = *endp;
22118 *endp = 0;
22119 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022120 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022121 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22122 {
22123 retval = FAIL;
22124 goto theend;
22125 }
22126 *endp = ch; /* preserve the string */
22127
22128 if (len > 0)
22129 break; /* successfully shortened the path */
22130
22131 /* failed to shorten the path. Skip the path separator */
22132 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 }
22134
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022135 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022137 /*
22138 * Succeeded in shortening the path. Now concatenate the shortened
22139 * path with the remaining path at the tail.
22140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022142 /* Compute the length of the new path. */
22143 sfx_len = (int)(save_endp - endp) + 1;
22144 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022146 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022148 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022150 /* There is not enough space in the currently allocated string,
22151 * copy it to a buffer big enough. */
22152 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022154 {
22155 retval = FAIL;
22156 goto theend;
22157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 }
22159 else
22160 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022161 /* Transfer short_fname to the main buffer (it's big enough),
22162 * unless get_short_pathname() did its work in-place. */
22163 *fname = *bufp = save_fname;
22164 if (short_fname != save_fname)
22165 vim_strncpy(save_fname, short_fname, len);
22166 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022168
22169 /* concat the not-shortened part of the path */
22170 vim_strncpy(*fname + len, endp, sfx_len);
22171 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022173
22174theend:
22175 vim_free(pbuf_unused);
22176 vim_free(save_fname);
22177
22178 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179}
22180
22181/*
22182 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022183 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 */
22185 static int
22186shortpath_for_partial(fnamep, bufp, fnamelen)
22187 char_u **fnamep;
22188 char_u **bufp;
22189 int *fnamelen;
22190{
22191 int sepcount, len, tflen;
22192 char_u *p;
22193 char_u *pbuf, *tfname;
22194 int hasTilde;
22195
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022196 /* Count up the path separators from the RHS.. so we know which part
22197 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022199 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 if (vim_ispathsep(*p))
22201 ++sepcount;
22202
22203 /* Need full path first (use expand_env() to remove a "~/") */
22204 hasTilde = (**fnamep == '~');
22205 if (hasTilde)
22206 pbuf = tfname = expand_env_save(*fnamep);
22207 else
22208 pbuf = tfname = FullName_save(*fnamep, FALSE);
22209
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022210 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022212 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22213 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214
22215 if (len == 0)
22216 {
22217 /* Don't have a valid filename, so shorten the rest of the
22218 * path if we can. This CAN give us invalid 8.3 filenames, but
22219 * there's not a lot of point in guessing what it might be.
22220 */
22221 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022222 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 }
22225
22226 /* Count the paths backward to find the beginning of the desired string. */
22227 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022228 {
22229#ifdef FEAT_MBYTE
22230 if (has_mbyte)
22231 p -= mb_head_off(tfname, p);
22232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 if (vim_ispathsep(*p))
22234 {
22235 if (sepcount == 0 || (hasTilde && sepcount == 1))
22236 break;
22237 else
22238 sepcount --;
22239 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 if (hasTilde)
22242 {
22243 --p;
22244 if (p >= tfname)
22245 *p = '~';
22246 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 }
22249 else
22250 ++p;
22251
22252 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22253 vim_free(*bufp);
22254 *fnamelen = (int)STRLEN(p);
22255 *bufp = pbuf;
22256 *fnamep = p;
22257
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022258 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259}
22260#endif /* WIN3264 */
22261
22262/*
22263 * Adjust a filename, according to a string of modifiers.
22264 * *fnamep must be NUL terminated when called. When returning, the length is
22265 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022266 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 * When there is an error, *fnamep is set to NULL.
22268 */
22269 int
22270modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22271 char_u *src; /* string with modifiers */
22272 int *usedlen; /* characters after src that are used */
22273 char_u **fnamep; /* file name so far */
22274 char_u **bufp; /* buffer for allocated file name or NULL */
22275 int *fnamelen; /* length of fnamep */
22276{
22277 int valid = 0;
22278 char_u *tail;
22279 char_u *s, *p, *pbuf;
22280 char_u dirname[MAXPATHL];
22281 int c;
22282 int has_fullname = 0;
22283#ifdef WIN3264
22284 int has_shortname = 0;
22285#endif
22286
22287repeat:
22288 /* ":p" - full path/file_name */
22289 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22290 {
22291 has_fullname = 1;
22292
22293 valid |= VALID_PATH;
22294 *usedlen += 2;
22295
22296 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22297 if ((*fnamep)[0] == '~'
22298#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22299 && ((*fnamep)[1] == '/'
22300# ifdef BACKSLASH_IN_FILENAME
22301 || (*fnamep)[1] == '\\'
22302# endif
22303 || (*fnamep)[1] == NUL)
22304
22305#endif
22306 )
22307 {
22308 *fnamep = expand_env_save(*fnamep);
22309 vim_free(*bufp); /* free any allocated file name */
22310 *bufp = *fnamep;
22311 if (*fnamep == NULL)
22312 return -1;
22313 }
22314
22315 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022316 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 {
22318 if (vim_ispathsep(*p)
22319 && p[1] == '.'
22320 && (p[2] == NUL
22321 || vim_ispathsep(p[2])
22322 || (p[2] == '.'
22323 && (p[3] == NUL || vim_ispathsep(p[3])))))
22324 break;
22325 }
22326
22327 /* FullName_save() is slow, don't use it when not needed. */
22328 if (*p != NUL || !vim_isAbsName(*fnamep))
22329 {
22330 *fnamep = FullName_save(*fnamep, *p != NUL);
22331 vim_free(*bufp); /* free any allocated file name */
22332 *bufp = *fnamep;
22333 if (*fnamep == NULL)
22334 return -1;
22335 }
22336
22337 /* Append a path separator to a directory. */
22338 if (mch_isdir(*fnamep))
22339 {
22340 /* Make room for one or two extra characters. */
22341 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22342 vim_free(*bufp); /* free any allocated file name */
22343 *bufp = *fnamep;
22344 if (*fnamep == NULL)
22345 return -1;
22346 add_pathsep(*fnamep);
22347 }
22348 }
22349
22350 /* ":." - path relative to the current directory */
22351 /* ":~" - path relative to the home directory */
22352 /* ":8" - shortname path - postponed till after */
22353 while (src[*usedlen] == ':'
22354 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22355 {
22356 *usedlen += 2;
22357 if (c == '8')
22358 {
22359#ifdef WIN3264
22360 has_shortname = 1; /* Postpone this. */
22361#endif
22362 continue;
22363 }
22364 pbuf = NULL;
22365 /* Need full path first (use expand_env() to remove a "~/") */
22366 if (!has_fullname)
22367 {
22368 if (c == '.' && **fnamep == '~')
22369 p = pbuf = expand_env_save(*fnamep);
22370 else
22371 p = pbuf = FullName_save(*fnamep, FALSE);
22372 }
22373 else
22374 p = *fnamep;
22375
22376 has_fullname = 0;
22377
22378 if (p != NULL)
22379 {
22380 if (c == '.')
22381 {
22382 mch_dirname(dirname, MAXPATHL);
22383 s = shorten_fname(p, dirname);
22384 if (s != NULL)
22385 {
22386 *fnamep = s;
22387 if (pbuf != NULL)
22388 {
22389 vim_free(*bufp); /* free any allocated file name */
22390 *bufp = pbuf;
22391 pbuf = NULL;
22392 }
22393 }
22394 }
22395 else
22396 {
22397 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22398 /* Only replace it when it starts with '~' */
22399 if (*dirname == '~')
22400 {
22401 s = vim_strsave(dirname);
22402 if (s != NULL)
22403 {
22404 *fnamep = s;
22405 vim_free(*bufp);
22406 *bufp = s;
22407 }
22408 }
22409 }
22410 vim_free(pbuf);
22411 }
22412 }
22413
22414 tail = gettail(*fnamep);
22415 *fnamelen = (int)STRLEN(*fnamep);
22416
22417 /* ":h" - head, remove "/file_name", can be repeated */
22418 /* Don't remove the first "/" or "c:\" */
22419 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22420 {
22421 valid |= VALID_HEAD;
22422 *usedlen += 2;
22423 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022424 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022425 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 *fnamelen = (int)(tail - *fnamep);
22427#ifdef VMS
22428 if (*fnamelen > 0)
22429 *fnamelen += 1; /* the path separator is part of the path */
22430#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022431 if (*fnamelen == 0)
22432 {
22433 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22434 p = vim_strsave((char_u *)".");
22435 if (p == NULL)
22436 return -1;
22437 vim_free(*bufp);
22438 *bufp = *fnamep = tail = p;
22439 *fnamelen = 1;
22440 }
22441 else
22442 {
22443 while (tail > s && !after_pathsep(s, tail))
22444 mb_ptr_back(*fnamep, tail);
22445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 }
22447
22448 /* ":8" - shortname */
22449 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22450 {
22451 *usedlen += 2;
22452#ifdef WIN3264
22453 has_shortname = 1;
22454#endif
22455 }
22456
22457#ifdef WIN3264
22458 /* Check shortname after we have done 'heads' and before we do 'tails'
22459 */
22460 if (has_shortname)
22461 {
22462 pbuf = NULL;
22463 /* Copy the string if it is shortened by :h */
22464 if (*fnamelen < (int)STRLEN(*fnamep))
22465 {
22466 p = vim_strnsave(*fnamep, *fnamelen);
22467 if (p == 0)
22468 return -1;
22469 vim_free(*bufp);
22470 *bufp = *fnamep = p;
22471 }
22472
22473 /* Split into two implementations - makes it easier. First is where
22474 * there isn't a full name already, second is where there is.
22475 */
22476 if (!has_fullname && !vim_isAbsName(*fnamep))
22477 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022478 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 return -1;
22480 }
22481 else
22482 {
22483 int l;
22484
22485 /* Simple case, already have the full-name
22486 * Nearly always shorter, so try first time. */
22487 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022488 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 return -1;
22490
22491 if (l == 0)
22492 {
22493 /* Couldn't find the filename.. search the paths.
22494 */
22495 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022496 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 return -1;
22498 }
22499 *fnamelen = l;
22500 }
22501 }
22502#endif /* WIN3264 */
22503
22504 /* ":t" - tail, just the basename */
22505 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22506 {
22507 *usedlen += 2;
22508 *fnamelen -= (int)(tail - *fnamep);
22509 *fnamep = tail;
22510 }
22511
22512 /* ":e" - extension, can be repeated */
22513 /* ":r" - root, without extension, can be repeated */
22514 while (src[*usedlen] == ':'
22515 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22516 {
22517 /* find a '.' in the tail:
22518 * - for second :e: before the current fname
22519 * - otherwise: The last '.'
22520 */
22521 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22522 s = *fnamep - 2;
22523 else
22524 s = *fnamep + *fnamelen - 1;
22525 for ( ; s > tail; --s)
22526 if (s[0] == '.')
22527 break;
22528 if (src[*usedlen + 1] == 'e') /* :e */
22529 {
22530 if (s > tail)
22531 {
22532 *fnamelen += (int)(*fnamep - (s + 1));
22533 *fnamep = s + 1;
22534#ifdef VMS
22535 /* cut version from the extension */
22536 s = *fnamep + *fnamelen - 1;
22537 for ( ; s > *fnamep; --s)
22538 if (s[0] == ';')
22539 break;
22540 if (s > *fnamep)
22541 *fnamelen = s - *fnamep;
22542#endif
22543 }
22544 else if (*fnamep <= tail)
22545 *fnamelen = 0;
22546 }
22547 else /* :r */
22548 {
22549 if (s > tail) /* remove one extension */
22550 *fnamelen = (int)(s - *fnamep);
22551 }
22552 *usedlen += 2;
22553 }
22554
22555 /* ":s?pat?foo?" - substitute */
22556 /* ":gs?pat?foo?" - global substitute */
22557 if (src[*usedlen] == ':'
22558 && (src[*usedlen + 1] == 's'
22559 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22560 {
22561 char_u *str;
22562 char_u *pat;
22563 char_u *sub;
22564 int sep;
22565 char_u *flags;
22566 int didit = FALSE;
22567
22568 flags = (char_u *)"";
22569 s = src + *usedlen + 2;
22570 if (src[*usedlen + 1] == 'g')
22571 {
22572 flags = (char_u *)"g";
22573 ++s;
22574 }
22575
22576 sep = *s++;
22577 if (sep)
22578 {
22579 /* find end of pattern */
22580 p = vim_strchr(s, sep);
22581 if (p != NULL)
22582 {
22583 pat = vim_strnsave(s, (int)(p - s));
22584 if (pat != NULL)
22585 {
22586 s = p + 1;
22587 /* find end of substitution */
22588 p = vim_strchr(s, sep);
22589 if (p != NULL)
22590 {
22591 sub = vim_strnsave(s, (int)(p - s));
22592 str = vim_strnsave(*fnamep, *fnamelen);
22593 if (sub != NULL && str != NULL)
22594 {
22595 *usedlen = (int)(p + 1 - src);
22596 s = do_string_sub(str, pat, sub, flags);
22597 if (s != NULL)
22598 {
22599 *fnamep = s;
22600 *fnamelen = (int)STRLEN(s);
22601 vim_free(*bufp);
22602 *bufp = s;
22603 didit = TRUE;
22604 }
22605 }
22606 vim_free(sub);
22607 vim_free(str);
22608 }
22609 vim_free(pat);
22610 }
22611 }
22612 /* after using ":s", repeat all the modifiers */
22613 if (didit)
22614 goto repeat;
22615 }
22616 }
22617
22618 return valid;
22619}
22620
22621/*
22622 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22623 * "flags" can be "g" to do a global substitute.
22624 * Returns an allocated string, NULL for error.
22625 */
22626 char_u *
22627do_string_sub(str, pat, sub, flags)
22628 char_u *str;
22629 char_u *pat;
22630 char_u *sub;
22631 char_u *flags;
22632{
22633 int sublen;
22634 regmatch_T regmatch;
22635 int i;
22636 int do_all;
22637 char_u *tail;
22638 garray_T ga;
22639 char_u *ret;
22640 char_u *save_cpo;
22641
22642 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22643 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022644 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645
22646 ga_init2(&ga, 1, 200);
22647
22648 do_all = (flags[0] == 'g');
22649
22650 regmatch.rm_ic = p_ic;
22651 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22652 if (regmatch.regprog != NULL)
22653 {
22654 tail = str;
22655 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22656 {
22657 /*
22658 * Get some space for a temporary buffer to do the substitution
22659 * into. It will contain:
22660 * - The text up to where the match is.
22661 * - The substituted text.
22662 * - The text after the match.
22663 */
22664 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22665 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22666 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22667 {
22668 ga_clear(&ga);
22669 break;
22670 }
22671
22672 /* copy the text up to where the match is */
22673 i = (int)(regmatch.startp[0] - tail);
22674 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22675 /* add the substituted text */
22676 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22677 + ga.ga_len + i, TRUE, TRUE, FALSE);
22678 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 /* avoid getting stuck on a match with an empty string */
22680 if (tail == regmatch.endp[0])
22681 {
22682 if (*tail == NUL)
22683 break;
22684 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22685 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 }
22687 else
22688 {
22689 tail = regmatch.endp[0];
22690 if (*tail == NUL)
22691 break;
22692 }
22693 if (!do_all)
22694 break;
22695 }
22696
22697 if (ga.ga_data != NULL)
22698 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22699
22700 vim_free(regmatch.regprog);
22701 }
22702
22703 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22704 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022705 if (p_cpo == empty_option)
22706 p_cpo = save_cpo;
22707 else
22708 /* Darn, evaluating {sub} expression changed the value. */
22709 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710
22711 return ret;
22712}
22713
22714#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */