blob: ef84aeecc0bbf4cf2e2d71d8205085695a05a579 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
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 Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 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 */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 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
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000341};
342
343/* shorthand */
344#define vv_type vv_di.di_tv.v_type
345#define vv_nr vv_di.di_tv.vval.v_number
346#define vv_str vv_di.di_tv.vval.v_string
347#define vv_tv vv_di.di_tv
348
349/*
350 * The v: variables are stored in dictionary "vimvardict".
351 * "vimvars_var" is the variable that is used for the "l:" scope.
352 */
353static dict_T vimvardict;
354static dictitem_T vimvars_var;
355#define vimvarht vimvardict.dv_hashtab
356
Bram Moolenaara40058a2005-07-11 22:42:07 +0000357static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
358static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
359#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
360static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
361#endif
362static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
363static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
364static char_u *skip_var_one __ARGS((char_u *arg));
365static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
366static void list_glob_vars __ARGS((void));
367static void list_buf_vars __ARGS((void));
368static void list_win_vars __ARGS((void));
369static void list_vim_vars __ARGS((void));
370static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
371static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
372static int check_changedtick __ARGS((char_u *arg));
373static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
374static void clear_lval __ARGS((lval_T *lp));
375static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
376static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
377static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
378static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
379static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
380static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
381static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
382static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
383static void item_lock __ARGS((typval_T *tv, int deep, int lock));
384static int tv_islocked __ARGS((typval_T *tv));
385
Bram Moolenaar33570922005-01-25 22:26:29 +0000386static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
387static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000395static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000396static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static void list_free __ARGS((list_T *l));
402static listitem_T *listitem_alloc __ARGS((void));
403static void listitem_free __ARGS((listitem_T *item));
404static void listitem_remove __ARGS((list_T *l, listitem_T *item));
405static long list_len __ARGS((list_T *l));
406static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
407static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
408static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static listitem_T *list_find __ARGS((list_T *l, long n));
410static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static void list_append __ARGS((list_T *l, listitem_T *item));
412static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000413static int list_append_string __ARGS((list_T *l, char_u *str, int len));
414static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
416static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
417static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
420static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000422static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
423static void set_ref_in_list __ARGS((list_T *l, int copyID));
424static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static void dict_unref __ARGS((dict_T *d));
426static void dict_free __ARGS((dict_T *d));
427static dictitem_T *dictitem_alloc __ARGS((char_u *key));
428static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
429static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
430static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static int dict_add __ARGS((dict_T *d, dictitem_T *item));
433static long dict_len __ARGS((dict_T *d));
434static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
435static char_u *dict2string __ARGS((typval_T *tv));
436static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
438static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
439static char_u *string_quote __ARGS((char_u *str, int function));
440static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
441static int find_internal_func __ARGS((char_u *name));
442static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
443static 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));
444static 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 Moolenaar81bf7082005-02-12 14:31:42 +0000445static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446
447static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000466#if defined(FEAT_INS_EXPAND)
467static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
475static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000501static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000503static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000509static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000517static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000540static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000546static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000563static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000567#ifdef vim_mkdir
568static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
569#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000574static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000588static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000595static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000600static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000601static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
604#ifdef HAVE_STRFTIME
605static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
606#endif
607static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000619static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000620static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000622static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
640static int get_env_len __ARGS((char_u **arg));
641static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000643static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
644#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
645#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
646 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000647static 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 +0000648static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000649static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
651static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static typval_T *alloc_tv __ARGS((void));
653static typval_T *alloc_string_tv __ARGS((char_u *string));
654static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void init_tv __ARGS((typval_T *varp));
656static long get_tv_number __ARGS((typval_T *varp));
657static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000658static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static char_u *get_tv_string __ARGS((typval_T *varp));
660static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000661static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000663static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
665static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
666static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
667static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
668static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
669static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
670static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000671static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000673static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
675static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
676static int eval_fname_script __ARGS((char_u *p));
677static int eval_fname_sid __ARGS((char_u *p));
678static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static ufunc_T *find_func __ARGS((char_u *name));
680static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000681static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000682#ifdef FEAT_PROFILE
683static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000684static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
685static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
686static int
687# ifdef __BORLANDC__
688 _RTLENTRYF
689# endif
690 prof_total_cmp __ARGS((const void *s1, const void *s2));
691static int
692# ifdef __BORLANDC__
693 _RTLENTRYF
694# endif
695 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000697static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000698static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000699static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void func_free __ARGS((ufunc_T *fp));
701static void func_unref __ARGS((char_u *name));
702static void func_ref __ARGS((char_u *name));
703static 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));
704static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
705
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000706/* Character used as separated in autoload function/variable names. */
707#define AUTOLOAD_CHAR '#'
708
Bram Moolenaar33570922005-01-25 22:26:29 +0000709/*
710 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000711 */
712 void
713eval_init()
714{
Bram Moolenaar33570922005-01-25 22:26:29 +0000715 int i;
716 struct vimvar *p;
717
718 init_var_dict(&globvardict, &globvars_var);
719 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000720 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000721 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000722
723 for (i = 0; i < VV_LEN; ++i)
724 {
725 p = &vimvars[i];
726 STRCPY(p->vv_di.di_key, p->vv_name);
727 if (p->vv_flags & VV_RO)
728 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
729 else if (p->vv_flags & VV_RO_SBX)
730 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
731 else
732 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000733
734 /* add to v: scope dict, unless the value is not always available */
735 if (p->vv_type != VAR_UNKNOWN)
736 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000737 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000738 /* add to compat scope dict */
739 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000740 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000741}
742
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000743#if defined(EXITFREE) || defined(PROTO)
744 void
745eval_clear()
746{
747 int i;
748 struct vimvar *p;
749
750 for (i = 0; i < VV_LEN; ++i)
751 {
752 p = &vimvars[i];
753 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000756 p->vv_di.di_tv.vval.v_string = NULL;
757 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000758 }
759 hash_clear(&vimvarht);
760 hash_clear(&compat_hashtab);
761
762 /* script-local variables */
763 for (i = 1; i <= ga_scripts.ga_len; ++i)
764 vars_clear(&SCRIPT_VARS(i));
765 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000766 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000767
768 /* global variables */
769 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000770
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000771 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000772 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000773 hash_clear(&func_hashtab);
774
775 /* unreferenced lists and dicts */
776 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000777}
778#endif
779
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 * Return the name of the executed function.
782 */
783 char_u *
784func_name(cookie)
785 void *cookie;
786{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000787 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790/*
791 * Return the address holding the next breakpoint line for a funccall cookie.
792 */
793 linenr_T *
794func_breakpoint(cookie)
795 void *cookie;
796{
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
800/*
801 * Return the address holding the debug tick for a funccall cookie.
802 */
803 int *
804func_dbg_tick(cookie)
805 void *cookie;
806{
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808}
809
810/*
811 * Return the nesting level for a funccall cookie.
812 */
813 int
814func_level(cookie)
815 void *cookie;
816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000821funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823/*
824 * Return TRUE when a function was ended by a ":return" command.
825 */
826 int
827current_func_returned()
828{
829 return current_funccal->returned;
830}
831
832
833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * Set an internal variable to a string value. Creates the variable if it does
835 * not already exist.
836 */
837 void
838set_internal_string_var(name, value)
839 char_u *name;
840 char_u *value;
841{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000842 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845 val = vim_strsave(value);
846 if (val != NULL)
847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000849 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000851 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 }
855}
856
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857static lval_T *redir_lval = NULL;
858static char_u *redir_endp = NULL;
859static char_u *redir_varname = NULL;
860
861/*
862 * Start recording command output to a variable
863 * Returns OK if successfully completed the setup. FAIL otherwise.
864 */
865 int
866var_redir_start(name, append)
867 char_u *name;
868 int append; /* append to an existing variable */
869{
870 int save_emsg;
871 int err;
872 typval_T tv;
873
874 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000875 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000876 {
877 EMSG(_(e_invarg));
878 return FAIL;
879 }
880
881 redir_varname = vim_strsave(name);
882 if (redir_varname == NULL)
883 return FAIL;
884
885 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
886 if (redir_lval == NULL)
887 {
888 var_redir_stop();
889 return FAIL;
890 }
891
892 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000893 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
894 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000895 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
896 {
897 if (redir_endp != NULL && *redir_endp != NUL)
898 /* Trailing characters are present after the variable name */
899 EMSG(_(e_trailing));
900 else
901 EMSG(_(e_invarg));
902 var_redir_stop();
903 return FAIL;
904 }
905
906 /* check if we can write to the variable: set it to or append an empty
907 * string */
908 save_emsg = did_emsg;
909 did_emsg = FALSE;
910 tv.v_type = VAR_STRING;
911 tv.vval.v_string = (char_u *)"";
912 if (append)
913 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
914 else
915 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
916 err = did_emsg;
917 did_emsg += save_emsg;
918 if (err)
919 {
920 var_redir_stop();
921 return FAIL;
922 }
923 if (redir_lval->ll_newkey != NULL)
924 {
925 /* Dictionary item was created, don't do it again. */
926 vim_free(redir_lval->ll_newkey);
927 redir_lval->ll_newkey = NULL;
928 }
929
930 return OK;
931}
932
933/*
934 * Append "value[len]" to the variable set by var_redir_start().
935 */
936 void
937var_redir_str(value, len)
938 char_u *value;
939 int len;
940{
941 char_u *val;
942 typval_T tv;
943 int save_emsg;
944 int err;
945
946 if (redir_lval == NULL)
947 return;
948
949 if (len == -1)
950 /* Append the entire string */
951 val = vim_strsave(value);
952 else
953 /* Append only the specified number of characters */
954 val = vim_strnsave(value, len);
955 if (val == NULL)
956 return;
957
958 tv.v_type = VAR_STRING;
959 tv.vval.v_string = val;
960
961 save_emsg = did_emsg;
962 did_emsg = FALSE;
963 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
964 err = did_emsg;
965 did_emsg += save_emsg;
966 if (err)
967 var_redir_stop();
968
969 vim_free(tv.vval.v_string);
970}
971
972/*
973 * Stop redirecting command output to a variable.
974 */
975 void
976var_redir_stop()
977{
978 if (redir_lval != NULL)
979 {
980 clear_lval(redir_lval);
981 vim_free(redir_lval);
982 redir_lval = NULL;
983 }
984 vim_free(redir_varname);
985 redir_varname = NULL;
986}
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988# if defined(FEAT_MBYTE) || defined(PROTO)
989 int
990eval_charconvert(enc_from, enc_to, fname_from, fname_to)
991 char_u *enc_from;
992 char_u *enc_to;
993 char_u *fname_from;
994 char_u *fname_to;
995{
996 int err = FALSE;
997
998 set_vim_var_string(VV_CC_FROM, enc_from, -1);
999 set_vim_var_string(VV_CC_TO, enc_to, -1);
1000 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1001 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1002 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1003 err = TRUE;
1004 set_vim_var_string(VV_CC_FROM, NULL, -1);
1005 set_vim_var_string(VV_CC_TO, NULL, -1);
1006 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1007 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1008
1009 if (err)
1010 return FAIL;
1011 return OK;
1012}
1013# endif
1014
1015# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1016 int
1017eval_printexpr(fname, args)
1018 char_u *fname;
1019 char_u *args;
1020{
1021 int err = FALSE;
1022
1023 set_vim_var_string(VV_FNAME_IN, fname, -1);
1024 set_vim_var_string(VV_CMDARG, args, -1);
1025 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1026 err = TRUE;
1027 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1028 set_vim_var_string(VV_CMDARG, NULL, -1);
1029
1030 if (err)
1031 {
1032 mch_remove(fname);
1033 return FAIL;
1034 }
1035 return OK;
1036}
1037# endif
1038
1039# if defined(FEAT_DIFF) || defined(PROTO)
1040 void
1041eval_diff(origfile, newfile, outfile)
1042 char_u *origfile;
1043 char_u *newfile;
1044 char_u *outfile;
1045{
1046 int err = FALSE;
1047
1048 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1049 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1050 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1051 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1052 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1053 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1054 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1055}
1056
1057 void
1058eval_patch(origfile, difffile, outfile)
1059 char_u *origfile;
1060 char_u *difffile;
1061 char_u *outfile;
1062{
1063 int err;
1064
1065 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1066 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1067 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1068 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1069 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1070 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1071 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1072}
1073# endif
1074
1075/*
1076 * Top level evaluation function, returning a boolean.
1077 * Sets "error" to TRUE if there was an error.
1078 * Return TRUE or FALSE.
1079 */
1080 int
1081eval_to_bool(arg, error, nextcmd, skip)
1082 char_u *arg;
1083 int *error;
1084 char_u **nextcmd;
1085 int skip; /* only parse, don't execute */
1086{
Bram Moolenaar33570922005-01-25 22:26:29 +00001087 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 int retval = FALSE;
1089
1090 if (skip)
1091 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 else
1095 {
1096 *error = FALSE;
1097 if (!skip)
1098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001099 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102 }
1103 if (skip)
1104 --emsg_skip;
1105
1106 return retval;
1107}
1108
1109/*
1110 * Top level evaluation function, returning a string. If "skip" is TRUE,
1111 * only parsing to "nextcmd" is done, without reporting errors. Return
1112 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1113 */
1114 char_u *
1115eval_to_string_skip(arg, nextcmd, skip)
1116 char_u *arg;
1117 char_u **nextcmd;
1118 int skip; /* only parse, don't execute */
1119{
Bram Moolenaar33570922005-01-25 22:26:29 +00001120 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 char_u *retval;
1122
1123 if (skip)
1124 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 retval = NULL;
1127 else
1128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 retval = vim_strsave(get_tv_string(&tv));
1130 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 if (skip)
1133 --emsg_skip;
1134
1135 return retval;
1136}
1137
1138/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001139 * Skip over an expression at "*pp".
1140 * Return FAIL for an error, OK otherwise.
1141 */
1142 int
1143skip_expr(pp)
1144 char_u **pp;
1145{
Bram Moolenaar33570922005-01-25 22:26:29 +00001146 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001147
1148 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001150}
1151
1152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * Top level evaluation function, returning a string.
1154 * Return pointer to allocated memory, or NULL for failure.
1155 */
1156 char_u *
1157eval_to_string(arg, nextcmd)
1158 char_u *arg;
1159 char_u **nextcmd;
1160{
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *retval;
1163
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 retval = NULL;
1166 else
1167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 retval = vim_strsave(get_tv_string(&tv));
1169 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171
1172 return retval;
1173}
1174
1175/*
1176 * Call eval_to_string() with "sandbox" set and not using local variables.
1177 */
1178 char_u *
1179eval_to_string_safe(arg, nextcmd)
1180 char_u *arg;
1181 char_u **nextcmd;
1182{
1183 char_u *retval;
1184 void *save_funccalp;
1185
1186 save_funccalp = save_funccal();
1187 ++sandbox;
1188 retval = eval_to_string(arg, nextcmd);
1189 --sandbox;
1190 restore_funccal(save_funccalp);
1191 return retval;
1192}
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194/*
1195 * Top level evaluation function, returning a number.
1196 * Evaluates "expr" silently.
1197 * Returns -1 for an error.
1198 */
1199 int
1200eval_to_number(expr)
1201 char_u *expr;
1202{
Bram Moolenaar33570922005-01-25 22:26:29 +00001203 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001205 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
1207 ++emsg_off;
1208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 retval = -1;
1211 else
1212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001213 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001214 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 --emsg_off;
1217
1218 return retval;
1219}
1220
Bram Moolenaara40058a2005-07-11 22:42:07 +00001221/*
1222 * Prepare v: variable "idx" to be used.
1223 * Save the current typeval in "save_tv".
1224 * When not used yet add the variable to the v: hashtable.
1225 */
1226 static void
1227prepare_vimvar(idx, save_tv)
1228 int idx;
1229 typval_T *save_tv;
1230{
1231 *save_tv = vimvars[idx].vv_tv;
1232 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1233 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1234}
1235
1236/*
1237 * Restore v: variable "idx" to typeval "save_tv".
1238 * When no longer defined, remove the variable from the v: hashtable.
1239 */
1240 static void
1241restore_vimvar(idx, save_tv)
1242 int idx;
1243 typval_T *save_tv;
1244{
1245 hashitem_T *hi;
1246
1247 clear_tv(&vimvars[idx].vv_tv);
1248 vimvars[idx].vv_tv = *save_tv;
1249 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1250 {
1251 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1252 if (HASHITEM_EMPTY(hi))
1253 EMSG2(_(e_intern2), "restore_vimvar()");
1254 else
1255 hash_remove(&vimvarht, hi);
1256 }
1257}
1258
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001259#if defined(FEAT_SYN_HL) || defined(PROTO)
1260/*
1261 * Evaluate an expression to a list with suggestions.
1262 * For the "expr:" part of 'spellsuggest'.
1263 */
1264 list_T *
1265eval_spell_expr(badword, expr)
1266 char_u *badword;
1267 char_u *expr;
1268{
1269 typval_T save_val;
1270 typval_T rettv;
1271 list_T *list = NULL;
1272 char_u *p = skipwhite(expr);
1273
1274 /* Set "v:val" to the bad word. */
1275 prepare_vimvar(VV_VAL, &save_val);
1276 vimvars[VV_VAL].vv_type = VAR_STRING;
1277 vimvars[VV_VAL].vv_str = badword;
1278 if (p_verbose == 0)
1279 ++emsg_off;
1280
1281 if (eval1(&p, &rettv, TRUE) == OK)
1282 {
1283 if (rettv.v_type != VAR_LIST)
1284 clear_tv(&rettv);
1285 else
1286 list = rettv.vval.v_list;
1287 }
1288
1289 if (p_verbose == 0)
1290 --emsg_off;
1291 vimvars[VV_VAL].vv_str = NULL;
1292 restore_vimvar(VV_VAL, &save_val);
1293
1294 return list;
1295}
1296
1297/*
1298 * "list" is supposed to contain two items: a word and a number. Return the
1299 * word in "pp" and the number as the return value.
1300 * Return -1 if anything isn't right.
1301 * Used to get the good word and score from the eval_spell_expr() result.
1302 */
1303 int
1304get_spellword(list, pp)
1305 list_T *list;
1306 char_u **pp;
1307{
1308 listitem_T *li;
1309
1310 li = list->lv_first;
1311 if (li == NULL)
1312 return -1;
1313 *pp = get_tv_string(&li->li_tv);
1314
1315 li = li->li_next;
1316 if (li == NULL)
1317 return -1;
1318 return get_tv_number(&li->li_tv);
1319}
1320#endif
1321
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001322/*
1323 * Top level evaluation function,
1324 */
1325 typval_T *
1326eval_expr(arg, nextcmd)
1327 char_u *arg;
1328 char_u **nextcmd;
1329{
1330 typval_T *tv;
1331
1332 tv = (typval_T *)alloc(sizeof(typval_T));
1333 if (!tv)
1334 return NULL;
1335
1336 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1337 {
1338 vim_free(tv);
1339 return NULL;
1340 }
1341
1342 return tv;
1343}
1344
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1347/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001348 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001350 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001352 static int
1353call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 char_u *func;
1355 int argc;
1356 char_u **argv;
1357 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
Bram Moolenaar33570922005-01-25 22:26:29 +00001360 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 long n;
1362 int len;
1363 int i;
1364 int doesrange;
1365 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001370 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
1372 for (i = 0; i < argc; i++)
1373 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001374 /* Pass a NULL or empty argument as an empty string */
1375 if (argv[i] == NULL || *argv[i] == NUL)
1376 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001377 argvars[i].v_type = VAR_STRING;
1378 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001379 continue;
1380 }
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 /* Recognize a number argument, the others must be strings. */
1383 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1384 if (len != 0 && len == (int)STRLEN(argv[i]))
1385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001386 argvars[i].v_type = VAR_NUMBER;
1387 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 else
1390 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001391 argvars[i].v_type = VAR_STRING;
1392 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 }
1395
1396 if (safe)
1397 {
1398 save_funccalp = save_funccal();
1399 ++sandbox;
1400 }
1401
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001402 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1403 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (safe)
1407 {
1408 --sandbox;
1409 restore_funccal(save_funccalp);
1410 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001411 vim_free(argvars);
1412
1413 if (ret == FAIL)
1414 clear_tv(rettv);
1415
1416 return ret;
1417}
1418
1419/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001420 * Call vimL function "func" and return the result as a string.
1421 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Uses argv[argc] for the function arguments.
1423 */
1424 void *
1425call_func_retstr(func, argc, argv, safe)
1426 char_u *func;
1427 int argc;
1428 char_u **argv;
1429 int safe; /* use the sandbox */
1430{
1431 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001432 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001433
1434 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1435 return NULL;
1436
1437 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 return retval;
1440}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001442#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001443/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001444 * Call vimL function "func" and return the result as a number.
1445 * Returns -1 when calling the function fails.
1446 * Uses argv[argc] for the function arguments.
1447 */
1448 long
1449call_func_retnr(func, argc, argv, safe)
1450 char_u *func;
1451 int argc;
1452 char_u **argv;
1453 int safe; /* use the sandbox */
1454{
1455 typval_T rettv;
1456 long retval;
1457
1458 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1459 return -1;
1460
1461 retval = get_tv_number_chk(&rettv, NULL);
1462 clear_tv(&rettv);
1463 return retval;
1464}
1465#endif
1466
1467/*
1468 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 * Uses argv[argc] for the function arguments.
1470 */
1471 void *
1472call_func_retlist(func, argc, argv, safe)
1473 char_u *func;
1474 int argc;
1475 char_u **argv;
1476 int safe; /* use the sandbox */
1477{
1478 typval_T rettv;
1479
1480 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1481 return NULL;
1482
1483 if (rettv.v_type != VAR_LIST)
1484 {
1485 clear_tv(&rettv);
1486 return NULL;
1487 }
1488
1489 return rettv.vval.v_list;
1490}
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492#endif
1493
1494/*
1495 * Save the current function call pointer, and set it to NULL.
1496 * Used when executing autocommands and for ":source".
1497 */
1498 void *
1499save_funccal()
1500{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001501 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 current_funccal = NULL;
1504 return (void *)fc;
1505}
1506
1507 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001508restore_funccal(vfc)
1509 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001511 funccall_T *fc = (funccall_T *)vfc;
1512
1513 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514}
1515
Bram Moolenaar05159a02005-02-26 23:04:13 +00001516#if defined(FEAT_PROFILE) || defined(PROTO)
1517/*
1518 * Prepare profiling for entering a child or something else that is not
1519 * counted for the script/function itself.
1520 * Should always be called in pair with prof_child_exit().
1521 */
1522 void
1523prof_child_enter(tm)
1524 proftime_T *tm; /* place to store waittime */
1525{
1526 funccall_T *fc = current_funccal;
1527
1528 if (fc != NULL && fc->func->uf_profiling)
1529 profile_start(&fc->prof_child);
1530 script_prof_save(tm);
1531}
1532
1533/*
1534 * Take care of time spent in a child.
1535 * Should always be called after prof_child_enter().
1536 */
1537 void
1538prof_child_exit(tm)
1539 proftime_T *tm; /* where waittime was stored */
1540{
1541 funccall_T *fc = current_funccal;
1542
1543 if (fc != NULL && fc->func->uf_profiling)
1544 {
1545 profile_end(&fc->prof_child);
1546 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1547 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1548 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1549 }
1550 script_prof_restore(tm);
1551}
1552#endif
1553
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#ifdef FEAT_FOLDING
1556/*
1557 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1558 * it in "*cp". Doesn't give error messages.
1559 */
1560 int
1561eval_foldexpr(arg, cp)
1562 char_u *arg;
1563 int *cp;
1564{
Bram Moolenaar33570922005-01-25 22:26:29 +00001565 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 int retval;
1567 char_u *s;
1568
1569 ++emsg_off;
1570 ++sandbox;
1571 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 retval = 0;
1574 else
1575 {
1576 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 if (tv.v_type == VAR_NUMBER)
1578 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001579 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 retval = 0;
1581 else
1582 {
1583 /* If the result is a string, check if there is a non-digit before
1584 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (!VIM_ISDIGIT(*s) && *s != '-')
1587 *cp = *s++;
1588 retval = atol((char *)s);
1589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 --emsg_off;
1593 --sandbox;
1594
1595 return retval;
1596}
1597#endif
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 * ":let" list all variable values
1601 * ":let var1 var2" list variable values
1602 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603 * ":let var += expr" assignment command.
1604 * ":let var -= expr" assignment command.
1605 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
1608 void
1609ex_let(eap)
1610 exarg_T *eap;
1611{
1612 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 int var_count = 0;
1617 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 expr = skip_var_list(arg, &var_count, &semicolon);
1621 if (expr == NULL)
1622 return;
1623 expr = vim_strchr(expr, '=');
1624 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001626 /*
1627 * ":let" without "=": list variables
1628 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629 if (*arg == '[')
1630 EMSG(_(e_invarg));
1631 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 /* ":let var1 var2" */
1633 arg = list_arg_vars(eap, arg);
1634 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001637 list_glob_vars();
1638 list_buf_vars();
1639 list_win_vars();
1640 list_vim_vars();
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 eap->nextcmd = check_nextcmd(arg);
1643 }
1644 else
1645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 op[0] = '=';
1647 op[1] = NUL;
1648 if (expr > arg)
1649 {
1650 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1651 op[0] = expr[-1]; /* +=, -= or .= */
1652 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (eap->skip)
1656 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (eap->skip)
1659 {
1660 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 --emsg_skip;
1663 }
1664 else if (i != FAIL)
1665 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 }
1671}
1672
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673/*
1674 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1675 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 * When "nextchars" is not NULL it points to a string with characters that
1677 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1678 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 * Returns OK or FAIL;
1680 */
1681 static int
1682ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1683 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001684 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 int copy; /* copy values from "tv", don't move */
1686 int semicolon; /* from skip_var_list() */
1687 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689{
1690 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001691 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001693 listitem_T *item;
1694 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695
1696 if (*arg != '[')
1697 {
1698 /*
1699 * ":let var = expr" or ":for var in list"
1700 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 return FAIL;
1703 return OK;
1704 }
1705
1706 /*
1707 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1708 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001709 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 {
1711 EMSG(_(e_listreq));
1712 return FAIL;
1713 }
1714
1715 i = list_len(l);
1716 if (semicolon == 0 && var_count < i)
1717 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001718 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 return FAIL;
1720 }
1721 if (var_count - semicolon > i)
1722 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001723 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 return FAIL;
1725 }
1726
1727 item = l->lv_first;
1728 while (*arg != ']')
1729 {
1730 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 item = item->li_next;
1733 if (arg == NULL)
1734 return FAIL;
1735
1736 arg = skipwhite(arg);
1737 if (*arg == ';')
1738 {
1739 /* Put the rest of the list (may be empty) in the var after ';'.
1740 * Create a new list for this. */
1741 l = list_alloc();
1742 if (l == NULL)
1743 return FAIL;
1744 while (item != NULL)
1745 {
1746 list_append_tv(l, &item->li_tv);
1747 item = item->li_next;
1748 }
1749
1750 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001751 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 ltv.vval.v_list = l;
1753 l->lv_refcount = 1;
1754
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1756 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 clear_tv(&ltv);
1758 if (arg == NULL)
1759 return FAIL;
1760 break;
1761 }
1762 else if (*arg != ',' && *arg != ']')
1763 {
1764 EMSG2(_(e_intern2), "ex_let_vars()");
1765 return FAIL;
1766 }
1767 }
1768
1769 return OK;
1770}
1771
1772/*
1773 * Skip over assignable variable "var" or list of variables "[var, var]".
1774 * Used for ":let varvar = expr" and ":for varvar in expr".
1775 * For "[var, var]" increment "*var_count" for each variable.
1776 * for "[var, var; var]" set "semicolon".
1777 * Return NULL for an error.
1778 */
1779 static char_u *
1780skip_var_list(arg, var_count, semicolon)
1781 char_u *arg;
1782 int *var_count;
1783 int *semicolon;
1784{
1785 char_u *p, *s;
1786
1787 if (*arg == '[')
1788 {
1789 /* "[var, var]": find the matching ']'. */
1790 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001791 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 {
1793 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1794 s = skip_var_one(p);
1795 if (s == p)
1796 {
1797 EMSG2(_(e_invarg2), p);
1798 return NULL;
1799 }
1800 ++*var_count;
1801
1802 p = skipwhite(s);
1803 if (*p == ']')
1804 break;
1805 else if (*p == ';')
1806 {
1807 if (*semicolon == 1)
1808 {
1809 EMSG(_("Double ; in list of variables"));
1810 return NULL;
1811 }
1812 *semicolon = 1;
1813 }
1814 else if (*p != ',')
1815 {
1816 EMSG2(_(e_invarg2), p);
1817 return NULL;
1818 }
1819 }
1820 return p + 1;
1821 }
1822 else
1823 return skip_var_one(arg);
1824}
1825
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001826/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001827 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1828 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001829 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 static char_u *
1831skip_var_one(arg)
1832 char_u *arg;
1833{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001834 if (*arg == '@' && arg[1] != NUL)
1835 return arg + 2;
1836 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1837 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838}
1839
Bram Moolenaara7043832005-01-21 11:56:39 +00001840/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 * List variables for hashtab "ht" with prefix "prefix".
1842 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001843 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001845list_hashtable_vars(ht, prefix, empty)
1846 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001847 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001849{
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 hashitem_T *hi;
1851 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001852 int todo;
1853
1854 todo = ht->ht_used;
1855 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1856 {
1857 if (!HASHITEM_EMPTY(hi))
1858 {
1859 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001860 di = HI2DI(hi);
1861 if (empty || di->di_tv.v_type != VAR_STRING
1862 || di->di_tv.vval.v_string != NULL)
1863 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 }
1865 }
1866}
1867
1868/*
1869 * List global variables.
1870 */
1871 static void
1872list_glob_vars()
1873{
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001875}
1876
1877/*
1878 * List buffer variables.
1879 */
1880 static void
1881list_buf_vars()
1882{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001883 char_u numbuf[NUMBUFLEN];
1884
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001886
1887 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1888 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001889}
1890
1891/*
1892 * List window variables.
1893 */
1894 static void
1895list_win_vars()
1896{
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001898}
1899
1900/*
1901 * List Vim variables.
1902 */
1903 static void
1904list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905{
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907}
1908
1909/*
1910 * List variables in "arg".
1911 */
1912 static char_u *
1913list_arg_vars(eap, arg)
1914 exarg_T *eap;
1915 char_u *arg;
1916{
1917 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 char_u *name_start;
1921 char_u *arg_subsc;
1922 char_u *tofree;
1923 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924
1925 while (!ends_excmd(*arg) && !got_int)
1926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001927 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001929 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1931 {
1932 emsg_severe = TRUE;
1933 EMSG(_(e_trailing));
1934 break;
1935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001937 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001939 /* get_name_len() takes care of expanding curly braces */
1940 name_start = name = arg;
1941 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1942 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944 /* This is mainly to keep test 49 working: when expanding
1945 * curly braces fails overrule the exception error message. */
1946 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001948 emsg_severe = TRUE;
1949 EMSG2(_(e_invarg2), arg);
1950 break;
1951 }
1952 error = TRUE;
1953 }
1954 else
1955 {
1956 if (tofree != NULL)
1957 name = tofree;
1958 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 else
1961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962 /* handle d.key, l[idx], f(expr) */
1963 arg_subsc = arg;
1964 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001965 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001970 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001972 case 'g': list_glob_vars(); break;
1973 case 'b': list_buf_vars(); break;
1974 case 'w': list_win_vars(); break;
1975 case 'v': list_vim_vars(); break;
1976 default:
1977 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001978 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 }
1980 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001981 {
1982 char_u numbuf[NUMBUFLEN];
1983 char_u *tf;
1984 int c;
1985 char_u *s;
1986
1987 s = echo_string(&tv, &tf, numbuf);
1988 c = *arg;
1989 *arg = NUL;
1990 list_one_var_a((char_u *)"",
1991 arg == arg_subsc ? name : name_start,
1992 tv.v_type, s == NULL ? (char_u *)"" : s);
1993 *arg = c;
1994 vim_free(tf);
1995 }
1996 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 }
1999 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002000
2001 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003
2004 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 }
2006
2007 return arg;
2008}
2009
2010/*
2011 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2012 * Returns a pointer to the char just after the var name.
2013 * Returns NULL if there is an error.
2014 */
2015 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002018 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022{
2023 int c1;
2024 char_u *name;
2025 char_u *p;
2026 char_u *arg_end = NULL;
2027 int len;
2028 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002029 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030
2031 /*
2032 * ":let $VAR = expr": Set environment variable.
2033 */
2034 if (*arg == '$')
2035 {
2036 /* Find the end of the name. */
2037 ++arg;
2038 name = arg;
2039 len = get_env_len(&arg);
2040 if (len == 0)
2041 EMSG2(_(e_invarg2), name - 1);
2042 else
2043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 if (op != NULL && (*op == '+' || *op == '-'))
2045 EMSG2(_(e_letwrong), op);
2046 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 EMSG(_(e_letunexp));
2049 else
2050 {
2051 c1 = name[len];
2052 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002053 p = get_tv_string_chk(tv);
2054 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 {
2056 int mustfree = FALSE;
2057 char_u *s = vim_getenv(name, &mustfree);
2058
2059 if (s != NULL)
2060 {
2061 p = tofree = concat_str(s, p);
2062 if (mustfree)
2063 vim_free(s);
2064 }
2065 }
2066 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002067 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002068 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002069 if (STRICMP(name, "HOME") == 0)
2070 init_homedir();
2071 else if (didset_vim && STRICMP(name, "VIM") == 0)
2072 didset_vim = FALSE;
2073 else if (didset_vimruntime
2074 && STRICMP(name, "VIMRUNTIME") == 0)
2075 didset_vimruntime = FALSE;
2076 arg_end = arg;
2077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002079 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 }
2081 }
2082 }
2083
2084 /*
2085 * ":let &option = expr": Set option value.
2086 * ":let &l:option = expr": Set local option value.
2087 * ":let &g:option = expr": Set global option value.
2088 */
2089 else if (*arg == '&')
2090 {
2091 /* Find the end of the name. */
2092 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 if (p == NULL || (endchars != NULL
2094 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 EMSG(_(e_letunexp));
2096 else
2097 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 long n;
2099 int opt_type;
2100 long numval;
2101 char_u *stringval = NULL;
2102 char_u *s;
2103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 c1 = *p;
2105 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002106
2107 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002108 s = get_tv_string_chk(tv); /* != NULL if number or string */
2109 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002110 {
2111 opt_type = get_option_value(arg, &numval,
2112 &stringval, opt_flags);
2113 if ((opt_type == 1 && *op == '.')
2114 || (opt_type == 0 && *op != '.'))
2115 EMSG2(_(e_letwrong), op);
2116 else
2117 {
2118 if (opt_type == 1) /* number */
2119 {
2120 if (*op == '+')
2121 n = numval + n;
2122 else
2123 n = numval - n;
2124 }
2125 else if (opt_type == 0 && stringval != NULL) /* string */
2126 {
2127 s = concat_str(stringval, s);
2128 vim_free(stringval);
2129 stringval = s;
2130 }
2131 }
2132 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002133 if (s != NULL)
2134 {
2135 set_option_value(arg, n, s, opt_flags);
2136 arg_end = p;
2137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 }
2141 }
2142
2143 /*
2144 * ":let @r = expr": Set register contents.
2145 */
2146 else if (*arg == '@')
2147 {
2148 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 if (op != NULL && (*op == '+' || *op == '-'))
2150 EMSG2(_(e_letwrong), op);
2151 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 EMSG(_(e_letunexp));
2154 else
2155 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002156 char_u *tofree = NULL;
2157 char_u *s;
2158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002159 p = get_tv_string_chk(tv);
2160 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 if (s != NULL)
2164 {
2165 p = tofree = concat_str(s, p);
2166 vim_free(s);
2167 }
2168 }
2169 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002170 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 arg_end = arg + 1;
2173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 }
2176 }
2177
2178 /*
2179 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002182 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002186 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2190 EMSG(_(e_letunexp));
2191 else
2192 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 arg_end = p;
2195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
2199
2200 else
2201 EMSG2(_(e_invarg2), arg);
2202
2203 return arg_end;
2204}
2205
2206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2208 */
2209 static int
2210check_changedtick(arg)
2211 char_u *arg;
2212{
2213 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2214 {
2215 EMSG2(_(e_readonlyvar), arg);
2216 return TRUE;
2217 }
2218 return FALSE;
2219}
2220
2221/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 * Get an lval: variable, Dict item or List item that can be assigned a value
2223 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2224 * "name.key", "name.key[expr]" etc.
2225 * Indexing only works if "name" is an existing List or Dictionary.
2226 * "name" points to the start of the name.
2227 * If "rettv" is not NULL it points to the value to be assigned.
2228 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2229 * wrong; must end in space or cmd separator.
2230 *
2231 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002232 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 */
2235 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002236get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002238 typval_T *rettv;
2239 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 int unlet;
2241 int skip;
2242 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002243 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 char_u *expr_start, *expr_end;
2247 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 dictitem_T *v;
2249 typval_T var1;
2250 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002258 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259
2260 if (skip)
2261 {
2262 /* When skipping just find the end of the name. */
2263 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 }
2266
2267 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002268 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 if (expr_start != NULL)
2270 {
2271 /* Don't expand the name when we already know there is an error. */
2272 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2273 && *p != '[' && *p != '.')
2274 {
2275 EMSG(_(e_trailing));
2276 return NULL;
2277 }
2278
2279 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2280 if (lp->ll_exp_name == NULL)
2281 {
2282 /* Report an invalid expression in braces, unless the
2283 * expression evaluation has been cancelled due to an
2284 * aborting error, an interrupt, or an exception. */
2285 if (!aborting() && !quiet)
2286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002287 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 EMSG2(_(e_invarg2), name);
2289 return NULL;
2290 }
2291 }
2292 lp->ll_name = lp->ll_exp_name;
2293 }
2294 else
2295 lp->ll_name = name;
2296
2297 /* Without [idx] or .key we are done. */
2298 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2299 return p;
2300
2301 cc = *p;
2302 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 if (v == NULL && !quiet)
2305 EMSG2(_(e_undefvar), lp->ll_name);
2306 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 if (v == NULL)
2308 return NULL;
2309
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 /*
2311 * Loop until no more [idx] or .key is following.
2312 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002313 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2317 && !(lp->ll_tv->v_type == VAR_DICT
2318 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 if (!quiet)
2321 EMSG(_("E689: Can only index a List or Dictionary"));
2322 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 if (!quiet)
2327 EMSG(_("E708: [:] must come last"));
2328 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002330
Bram Moolenaar8c711452005-01-14 21:53:12 +00002331 len = -1;
2332 if (*p == '.')
2333 {
2334 key = p + 1;
2335 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2336 ;
2337 if (len == 0)
2338 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 if (!quiet)
2340 EMSG(_(e_emptykey));
2341 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002342 }
2343 p = key + len;
2344 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002345 else
2346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002348 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 if (*p == ':')
2350 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351 else
2352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 empty1 = FALSE;
2354 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (get_tv_string_chk(&var1) == NULL)
2357 {
2358 /* not a number or string */
2359 clear_tv(&var1);
2360 return NULL;
2361 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 }
2363
2364 /* Optionally get the second index [ :expr]. */
2365 if (*p == ':')
2366 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 if (!empty1)
2372 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 if (rettv != NULL && (rettv->v_type != VAR_LIST
2376 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (!quiet)
2379 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 if (!empty1)
2381 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 }
2384 p = skipwhite(p + 1);
2385 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002387 else
2388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2391 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 if (!empty1)
2393 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 if (get_tv_string_chk(&var2) == NULL)
2397 {
2398 /* not a number or string */
2399 if (!empty1)
2400 clear_tv(&var1);
2401 clear_tv(&var2);
2402 return NULL;
2403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002409
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (!quiet)
2413 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 if (!empty1)
2415 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 }
2420
2421 /* Skip to past ']'. */
2422 ++p;
2423 }
2424
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 {
2427 if (len == -1)
2428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002430 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 if (*key == NUL)
2432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (!quiet)
2434 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 }
2438 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 lp->ll_list = NULL;
2440 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002441 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002444 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (len == -1)
2450 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 }
2453 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 if (len == -1)
2458 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 p = NULL;
2461 break;
2462 }
2463 if (len == -1)
2464 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 }
2467 else
2468 {
2469 /*
2470 * Get the number and item for the only or first index of the List.
2471 */
2472 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 else
2475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 clear_tv(&var1);
2478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 lp->ll_list = lp->ll_tv->vval.v_list;
2481 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2482 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (!quiet)
2485 EMSGN(_(e_listidx), lp->ll_n1);
2486 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 }
2490
2491 /*
2492 * May need to find the item or absolute index for the second
2493 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 * When no index given: "lp->ll_empty2" is TRUE.
2495 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 if (ni == NULL)
2505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!quiet)
2507 EMSGN(_(e_listidx), lp->ll_n2);
2508 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 }
2512
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2514 if (lp->ll_n1 < 0)
2515 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2516 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (!quiet)
2519 EMSGN(_(e_listidx), lp->ll_n2);
2520 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002521 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002522 }
2523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 }
2527
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 return p;
2529}
2530
2531/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 */
2534 static void
2535clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537{
2538 vim_free(lp->ll_exp_name);
2539 vim_free(lp->ll_newkey);
2540}
2541
2542/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002543 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 */
2547 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002553 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554{
2555 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 listitem_T *ri;
2557 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558
2559 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 cc = *endp;
2564 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 if (op != NULL && *op != '=')
2566 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002569 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002570 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2571 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002572 {
2573 if (tv_op(&tv, rettv, op) == OK)
2574 set_var(lp->ll_name, &tv, FALSE);
2575 clear_tv(&tv);
2576 }
2577 }
2578 else
2579 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002583 else if (tv_check_lock(lp->ll_newkey == NULL
2584 ? lp->ll_tv->v_lock
2585 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2586 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 else if (lp->ll_range)
2588 {
2589 /*
2590 * Assign the List values to the list items.
2591 */
2592 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 if (op != NULL && *op != '=')
2595 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2596 else
2597 {
2598 clear_tv(&lp->ll_li->li_tv);
2599 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2600 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 ri = ri->li_next;
2602 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2603 break;
2604 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002607 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 ri = NULL;
2610 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 lp->ll_li = lp->ll_li->li_next;
2614 ++lp->ll_n1;
2615 }
2616 if (ri != NULL)
2617 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002618 else if (lp->ll_empty2
2619 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 : lp->ll_n1 != lp->ll_n2)
2621 EMSG(_("E711: List value has not enough items"));
2622 }
2623 else
2624 {
2625 /*
2626 * Assign to a List or Dictionary item.
2627 */
2628 if (lp->ll_newkey != NULL)
2629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002630 if (op != NULL && *op != '=')
2631 {
2632 EMSG2(_(e_letwrong), op);
2633 return;
2634 }
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002637 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (di == NULL)
2639 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002640 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2641 {
2642 vim_free(di);
2643 return;
2644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 else if (op != NULL && *op != '=')
2648 {
2649 tv_op(lp->ll_tv, rettv, op);
2650 return;
2651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 /*
2656 * Assign the value to the variable or list item.
2657 */
2658 if (copy)
2659 copy_tv(rettv, lp->ll_tv);
2660 else
2661 {
2662 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002663 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 }
2666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667}
2668
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2671 * Returns OK or FAIL.
2672 */
2673 static int
2674tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002675 typval_T *tv1;
2676 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 char_u *op;
2678{
2679 long n;
2680 char_u numbuf[NUMBUFLEN];
2681 char_u *s;
2682
2683 /* Can't do anything with a Funcref or a Dict on the right. */
2684 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2685 {
2686 switch (tv1->v_type)
2687 {
2688 case VAR_DICT:
2689 case VAR_FUNC:
2690 break;
2691
2692 case VAR_LIST:
2693 if (*op != '+' || tv2->v_type != VAR_LIST)
2694 break;
2695 /* List += List */
2696 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2697 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2698 return OK;
2699
2700 case VAR_NUMBER:
2701 case VAR_STRING:
2702 if (tv2->v_type == VAR_LIST)
2703 break;
2704 if (*op == '+' || *op == '-')
2705 {
2706 /* nr += nr or nr -= nr*/
2707 n = get_tv_number(tv1);
2708 if (*op == '+')
2709 n += get_tv_number(tv2);
2710 else
2711 n -= get_tv_number(tv2);
2712 clear_tv(tv1);
2713 tv1->v_type = VAR_NUMBER;
2714 tv1->vval.v_number = n;
2715 }
2716 else
2717 {
2718 /* str .= str */
2719 s = get_tv_string(tv1);
2720 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2721 clear_tv(tv1);
2722 tv1->v_type = VAR_STRING;
2723 tv1->vval.v_string = s;
2724 }
2725 return OK;
2726 }
2727 }
2728
2729 EMSG2(_(e_letwrong), op);
2730 return FAIL;
2731}
2732
2733/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734 * Add a watcher to a list.
2735 */
2736 static void
2737list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002738 list_T *l;
2739 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002740{
2741 lw->lw_next = l->lv_watch;
2742 l->lv_watch = lw;
2743}
2744
2745/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002746 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747 * No warning when it isn't found...
2748 */
2749 static void
2750list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002751 list_T *l;
2752 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002753{
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002755
2756 lwp = &l->lv_watch;
2757 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2758 {
2759 if (lw == lwrem)
2760 {
2761 *lwp = lw->lw_next;
2762 break;
2763 }
2764 lwp = &lw->lw_next;
2765 }
2766}
2767
2768/*
2769 * Just before removing an item from a list: advance watchers to the next
2770 * item.
2771 */
2772 static void
2773list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 list_T *l;
2775 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776{
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002778
2779 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2780 if (lw->lw_item == item)
2781 lw->lw_item = item->li_next;
2782}
2783
2784/*
2785 * Evaluate the expression used in a ":for var in expr" command.
2786 * "arg" points to "var".
2787 * Set "*errp" to TRUE for an error, FALSE otherwise;
2788 * Return a pointer that holds the info. Null when there is an error.
2789 */
2790 void *
2791eval_for_line(arg, errp, nextcmdp, skip)
2792 char_u *arg;
2793 int *errp;
2794 char_u **nextcmdp;
2795 int skip;
2796{
Bram Moolenaar33570922005-01-25 22:26:29 +00002797 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002798 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 typval_T tv;
2800 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002801
2802 *errp = TRUE; /* default: there is an error */
2803
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002805 if (fi == NULL)
2806 return NULL;
2807
2808 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2809 if (expr == NULL)
2810 return fi;
2811
2812 expr = skipwhite(expr);
2813 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2814 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002815 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816 return fi;
2817 }
2818
2819 if (skip)
2820 ++emsg_skip;
2821 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2822 {
2823 *errp = FALSE;
2824 if (!skip)
2825 {
2826 l = tv.vval.v_list;
2827 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002828 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002830 clear_tv(&tv);
2831 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 else
2833 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002834 /* No need to increment the refcount, it's already set for the
2835 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836 fi->fi_list = l;
2837 list_add_watch(l, &fi->fi_lw);
2838 fi->fi_lw.lw_item = l->lv_first;
2839 }
2840 }
2841 }
2842 if (skip)
2843 --emsg_skip;
2844
2845 return fi;
2846}
2847
2848/*
2849 * Use the first item in a ":for" list. Advance to the next.
2850 * Assign the values to the variable (list). "arg" points to the first one.
2851 * Return TRUE when a valid item was found, FALSE when at end of list or
2852 * something wrong.
2853 */
2854 int
2855next_for_item(fi_void, arg)
2856 void *fi_void;
2857 char_u *arg;
2858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862
2863 item = fi->fi_lw.lw_item;
2864 if (item == NULL)
2865 result = FALSE;
2866 else
2867 {
2868 fi->fi_lw.lw_item = item->li_next;
2869 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2870 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2871 }
2872 return result;
2873}
2874
2875/*
2876 * Free the structure used to store info used by ":for".
2877 */
2878 void
2879free_for_info(fi_void)
2880 void *fi_void;
2881{
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002884 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002885 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002887 list_unref(fi->fi_list);
2888 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 vim_free(fi);
2890}
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2893
2894 void
2895set_context_for_expression(xp, arg, cmdidx)
2896 expand_T *xp;
2897 char_u *arg;
2898 cmdidx_T cmdidx;
2899{
2900 int got_eq = FALSE;
2901 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002902 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 if (cmdidx == CMD_let)
2905 {
2906 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002907 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 {
2909 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002910 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 {
2912 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 if (vim_iswhite(*p))
2915 break;
2916 }
2917 return;
2918 }
2919 }
2920 else
2921 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2922 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 while ((xp->xp_pattern = vim_strpbrk(arg,
2924 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2925 {
2926 c = *xp->xp_pattern;
2927 if (c == '&')
2928 {
2929 c = xp->xp_pattern[1];
2930 if (c == '&')
2931 {
2932 ++xp->xp_pattern;
2933 xp->xp_context = cmdidx != CMD_let || got_eq
2934 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2935 }
2936 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002939 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2940 xp->xp_pattern += 2;
2941
2942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944 else if (c == '$')
2945 {
2946 /* environment variable */
2947 xp->xp_context = EXPAND_ENV_VARS;
2948 }
2949 else if (c == '=')
2950 {
2951 got_eq = TRUE;
2952 xp->xp_context = EXPAND_EXPRESSION;
2953 }
2954 else if (c == '<'
2955 && xp->xp_context == EXPAND_FUNCTIONS
2956 && vim_strchr(xp->xp_pattern, '(') == NULL)
2957 {
2958 /* Function name can start with "<SNR>" */
2959 break;
2960 }
2961 else if (cmdidx != CMD_let || got_eq)
2962 {
2963 if (c == '"') /* string */
2964 {
2965 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2966 if (c == '\\' && xp->xp_pattern[1] != NUL)
2967 ++xp->xp_pattern;
2968 xp->xp_context = EXPAND_NOTHING;
2969 }
2970 else if (c == '\'') /* literal string */
2971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002972 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2974 /* skip */ ;
2975 xp->xp_context = EXPAND_NOTHING;
2976 }
2977 else if (c == '|')
2978 {
2979 if (xp->xp_pattern[1] == '|')
2980 {
2981 ++xp->xp_pattern;
2982 xp->xp_context = EXPAND_EXPRESSION;
2983 }
2984 else
2985 xp->xp_context = EXPAND_COMMANDS;
2986 }
2987 else
2988 xp->xp_context = EXPAND_EXPRESSION;
2989 }
2990 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 /* Doesn't look like something valid, expand as an expression
2992 * anyway. */
2993 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 arg = xp->xp_pattern;
2995 if (*arg != NUL)
2996 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2997 /* skip */ ;
2998 }
2999 xp->xp_pattern = arg;
3000}
3001
3002#endif /* FEAT_CMDL_COMPL */
3003
3004/*
3005 * ":1,25call func(arg1, arg2)" function call.
3006 */
3007 void
3008ex_call(eap)
3009 exarg_T *eap;
3010{
3011 char_u *arg = eap->arg;
3012 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 linenr_T lnum;
3018 int doesrange;
3019 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003022 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3023 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (tofree == NULL)
3025 return;
3026
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003027 /* Increase refcount on dictionary, it could get deleted when evaluating
3028 * the arguments. */
3029 if (fudi.fd_dict != NULL)
3030 ++fudi.fd_dict->dv_refcount;
3031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3033 len = STRLEN(tofree);
3034 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar532c7802005-01-27 14:44:31 +00003036 /* Skip white space to allow ":call func ()". Not good, but required for
3037 * backward compatibility. */
3038 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 if (*startarg != '(')
3042 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003043 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 goto end;
3045 }
3046
3047 /*
3048 * When skipping, evaluate the function once, to find the end of the
3049 * arguments.
3050 * When the function takes a range, this is discovered after the first
3051 * call, and the loop is broken.
3052 */
3053 if (eap->skip)
3054 {
3055 ++emsg_skip;
3056 lnum = eap->line2; /* do it once, also with an invalid range */
3057 }
3058 else
3059 lnum = eap->line1;
3060 for ( ; lnum <= eap->line2; ++lnum)
3061 {
3062 if (!eap->skip && eap->addr_count > 0)
3063 {
3064 curwin->w_cursor.lnum = lnum;
3065 curwin->w_cursor.col = 0;
3066 }
3067 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003069 eap->line1, eap->line2, &doesrange,
3070 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 failed = TRUE;
3073 break;
3074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (doesrange || eap->skip)
3077 break;
3078 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003079 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003081 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (aborting())
3083 break;
3084 }
3085 if (eap->skip)
3086 --emsg_skip;
3087
3088 if (!failed)
3089 {
3090 /* Check for trailing illegal characters and a following command. */
3091 if (!ends_excmd(*arg))
3092 {
3093 emsg_severe = TRUE;
3094 EMSG(_(e_trailing));
3095 }
3096 else
3097 eap->nextcmd = check_nextcmd(arg);
3098 }
3099
3100end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003101 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105/*
3106 * ":unlet[!] var1 ... " command.
3107 */
3108 void
3109ex_unlet(eap)
3110 exarg_T *eap;
3111{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003112 ex_unletlock(eap, eap->arg, 0);
3113}
3114
3115/*
3116 * ":lockvar" and ":unlockvar" commands
3117 */
3118 void
3119ex_lockvar(eap)
3120 exarg_T *eap;
3121{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003123 int deep = 2;
3124
3125 if (eap->forceit)
3126 deep = -1;
3127 else if (vim_isdigit(*arg))
3128 {
3129 deep = getdigits(&arg);
3130 arg = skipwhite(arg);
3131 }
3132
3133 ex_unletlock(eap, arg, deep);
3134}
3135
3136/*
3137 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3138 */
3139 static void
3140ex_unletlock(eap, argstart, deep)
3141 exarg_T *eap;
3142 char_u *argstart;
3143 int deep;
3144{
3145 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003148 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 do
3151 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003152 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003153 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3154 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003155 if (lv.ll_name == NULL)
3156 error = TRUE; /* error but continue parsing */
3157 if (name_end == NULL || (!vim_iswhite(*name_end)
3158 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003160 if (name_end != NULL)
3161 {
3162 emsg_severe = TRUE;
3163 EMSG(_(e_trailing));
3164 }
3165 if (!(eap->skip || error))
3166 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 break;
3168 }
3169
3170 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003171 {
3172 if (eap->cmdidx == CMD_unlet)
3173 {
3174 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3175 error = TRUE;
3176 }
3177 else
3178 {
3179 if (do_lock_var(&lv, name_end, deep,
3180 eap->cmdidx == CMD_lockvar) == FAIL)
3181 error = TRUE;
3182 }
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003185 if (!eap->skip)
3186 clear_lval(&lv);
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 arg = skipwhite(name_end);
3189 } while (!ends_excmd(*arg));
3190
3191 eap->nextcmd = check_nextcmd(arg);
3192}
3193
Bram Moolenaar8c711452005-01-14 21:53:12 +00003194 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003195do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003198 int forceit;
3199{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 int ret = OK;
3201 int cc;
3202
3203 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003205 cc = *name_end;
3206 *name_end = NUL;
3207
3208 /* Normal name or expanded name. */
3209 if (check_changedtick(lp->ll_name))
3210 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003211 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003215 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3216 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 else if (lp->ll_range)
3218 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220
3221 /* Delete a range of List items. */
3222 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3223 {
3224 li = lp->ll_li->li_next;
3225 listitem_remove(lp->ll_list, lp->ll_li);
3226 lp->ll_li = li;
3227 ++lp->ll_n1;
3228 }
3229 }
3230 else
3231 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 /* unlet a List item. */
3234 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003237 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 }
3239
3240 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003241}
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243/*
3244 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003245 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
3247 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003248do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003250 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 hashtab_T *ht;
3253 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003254 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 ht = find_var_ht(name, &varname);
3257 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 hi = hash_find(ht, varname);
3260 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003261 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 if (var_check_ro(HI2DI(hi)->di_flags, name))
3263 return FAIL;
3264 delete_var(ht, hi);
3265 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 if (forceit)
3269 return OK;
3270 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 return FAIL;
3272}
3273
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003274/*
3275 * Lock or unlock variable indicated by "lp".
3276 * "deep" is the levels to go (-1 for unlimited);
3277 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3278 */
3279 static int
3280do_lock_var(lp, name_end, deep, lock)
3281 lval_T *lp;
3282 char_u *name_end;
3283 int deep;
3284 int lock;
3285{
3286 int ret = OK;
3287 int cc;
3288 dictitem_T *di;
3289
3290 if (deep == 0) /* nothing to do */
3291 return OK;
3292
3293 if (lp->ll_tv == NULL)
3294 {
3295 cc = *name_end;
3296 *name_end = NUL;
3297
3298 /* Normal name or expanded name. */
3299 if (check_changedtick(lp->ll_name))
3300 ret = FAIL;
3301 else
3302 {
3303 di = find_var(lp->ll_name, NULL);
3304 if (di == NULL)
3305 ret = FAIL;
3306 else
3307 {
3308 if (lock)
3309 di->di_flags |= DI_FLAGS_LOCK;
3310 else
3311 di->di_flags &= ~DI_FLAGS_LOCK;
3312 item_lock(&di->di_tv, deep, lock);
3313 }
3314 }
3315 *name_end = cc;
3316 }
3317 else if (lp->ll_range)
3318 {
3319 listitem_T *li = lp->ll_li;
3320
3321 /* (un)lock a range of List items. */
3322 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3323 {
3324 item_lock(&li->li_tv, deep, lock);
3325 li = li->li_next;
3326 ++lp->ll_n1;
3327 }
3328 }
3329 else if (lp->ll_list != NULL)
3330 /* (un)lock a List item. */
3331 item_lock(&lp->ll_li->li_tv, deep, lock);
3332 else
3333 /* un(lock) a Dictionary item. */
3334 item_lock(&lp->ll_di->di_tv, deep, lock);
3335
3336 return ret;
3337}
3338
3339/*
3340 * Lock or unlock an item. "deep" is nr of levels to go.
3341 */
3342 static void
3343item_lock(tv, deep, lock)
3344 typval_T *tv;
3345 int deep;
3346 int lock;
3347{
3348 static int recurse = 0;
3349 list_T *l;
3350 listitem_T *li;
3351 dict_T *d;
3352 hashitem_T *hi;
3353 int todo;
3354
3355 if (recurse >= DICT_MAXNEST)
3356 {
3357 EMSG(_("E743: variable nested too deep for (un)lock"));
3358 return;
3359 }
3360 if (deep == 0)
3361 return;
3362 ++recurse;
3363
3364 /* lock/unlock the item itself */
3365 if (lock)
3366 tv->v_lock |= VAR_LOCKED;
3367 else
3368 tv->v_lock &= ~VAR_LOCKED;
3369
3370 switch (tv->v_type)
3371 {
3372 case VAR_LIST:
3373 if ((l = tv->vval.v_list) != NULL)
3374 {
3375 if (lock)
3376 l->lv_lock |= VAR_LOCKED;
3377 else
3378 l->lv_lock &= ~VAR_LOCKED;
3379 if (deep < 0 || deep > 1)
3380 /* recursive: lock/unlock the items the List contains */
3381 for (li = l->lv_first; li != NULL; li = li->li_next)
3382 item_lock(&li->li_tv, deep - 1, lock);
3383 }
3384 break;
3385 case VAR_DICT:
3386 if ((d = tv->vval.v_dict) != NULL)
3387 {
3388 if (lock)
3389 d->dv_lock |= VAR_LOCKED;
3390 else
3391 d->dv_lock &= ~VAR_LOCKED;
3392 if (deep < 0 || deep > 1)
3393 {
3394 /* recursive: lock/unlock the items the List contains */
3395 todo = d->dv_hashtab.ht_used;
3396 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3397 {
3398 if (!HASHITEM_EMPTY(hi))
3399 {
3400 --todo;
3401 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3402 }
3403 }
3404 }
3405 }
3406 }
3407 --recurse;
3408}
3409
Bram Moolenaara40058a2005-07-11 22:42:07 +00003410/*
3411 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3412 * it refers to a List or Dictionary that is locked.
3413 */
3414 static int
3415tv_islocked(tv)
3416 typval_T *tv;
3417{
3418 return (tv->v_lock & VAR_LOCKED)
3419 || (tv->v_type == VAR_LIST
3420 && tv->vval.v_list != NULL
3421 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3422 || (tv->v_type == VAR_DICT
3423 && tv->vval.v_dict != NULL
3424 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3425}
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3428/*
3429 * Delete all "menutrans_" variables.
3430 */
3431 void
3432del_menutrans_vars()
3433{
Bram Moolenaar33570922005-01-25 22:26:29 +00003434 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003435 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 hash_lock(&globvarht);
3438 todo = globvarht.ht_used;
3439 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003440 {
3441 if (!HASHITEM_EMPTY(hi))
3442 {
3443 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3445 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003446 }
3447 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449}
3450#endif
3451
3452#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3453
3454/*
3455 * Local string buffer for the next two functions to store a variable name
3456 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3457 * get_user_var_name().
3458 */
3459
3460static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3461
3462static char_u *varnamebuf = NULL;
3463static int varnamebuflen = 0;
3464
3465/*
3466 * Function to concatenate a prefix and a variable name.
3467 */
3468 static char_u *
3469cat_prefix_varname(prefix, name)
3470 int prefix;
3471 char_u *name;
3472{
3473 int len;
3474
3475 len = (int)STRLEN(name) + 3;
3476 if (len > varnamebuflen)
3477 {
3478 vim_free(varnamebuf);
3479 len += 10; /* some additional space */
3480 varnamebuf = alloc(len);
3481 if (varnamebuf == NULL)
3482 {
3483 varnamebuflen = 0;
3484 return NULL;
3485 }
3486 varnamebuflen = len;
3487 }
3488 *varnamebuf = prefix;
3489 varnamebuf[1] = ':';
3490 STRCPY(varnamebuf + 2, name);
3491 return varnamebuf;
3492}
3493
3494/*
3495 * Function given to ExpandGeneric() to obtain the list of user defined
3496 * (global/buffer/window/built-in) variable names.
3497 */
3498/*ARGSUSED*/
3499 char_u *
3500get_user_var_name(xp, idx)
3501 expand_T *xp;
3502 int idx;
3503{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003504 static long_u gdone;
3505 static long_u bdone;
3506 static long_u wdone;
3507 static int vidx;
3508 static hashitem_T *hi;
3509 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003512 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003513
3514 /* Global variables */
3515 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003517 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003518 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003519 else
3520 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003521 while (HASHITEM_EMPTY(hi))
3522 ++hi;
3523 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3524 return cat_prefix_varname('g', hi->hi_key);
3525 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003527
3528 /* b: variables */
3529 ht = &curbuf->b_vars.dv_hashtab;
3530 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003532 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003534 else
3535 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 while (HASHITEM_EMPTY(hi))
3537 ++hi;
3538 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003540 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003542 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 return (char_u *)"b:changedtick";
3544 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003545
3546 /* w: variables */
3547 ht = &curwin->w_vars.dv_hashtab;
3548 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003550 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003552 else
3553 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 while (HASHITEM_EMPTY(hi))
3555 ++hi;
3556 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003558
3559 /* v: variables */
3560 if (vidx < VV_LEN)
3561 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 vim_free(varnamebuf);
3564 varnamebuf = NULL;
3565 varnamebuflen = 0;
3566 return NULL;
3567}
3568
3569#endif /* FEAT_CMDL_COMPL */
3570
3571/*
3572 * types for expressions.
3573 */
3574typedef enum
3575{
3576 TYPE_UNKNOWN = 0
3577 , TYPE_EQUAL /* == */
3578 , TYPE_NEQUAL /* != */
3579 , TYPE_GREATER /* > */
3580 , TYPE_GEQUAL /* >= */
3581 , TYPE_SMALLER /* < */
3582 , TYPE_SEQUAL /* <= */
3583 , TYPE_MATCH /* =~ */
3584 , TYPE_NOMATCH /* !~ */
3585} exptype_T;
3586
3587/*
3588 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003589 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3591 */
3592
3593/*
3594 * Handle zero level expression.
3595 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003597 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 * Return OK or FAIL.
3599 */
3600 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003601eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 char_u **nextcmd;
3605 int evaluate;
3606{
3607 int ret;
3608 char_u *p;
3609
3610 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003611 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (ret == FAIL || !ends_excmd(*p))
3613 {
3614 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003615 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /*
3617 * Report the invalid expression unless the expression evaluation has
3618 * been cancelled due to an aborting error, an interrupt, or an
3619 * exception.
3620 */
3621 if (!aborting())
3622 EMSG2(_(e_invexpr2), arg);
3623 ret = FAIL;
3624 }
3625 if (nextcmd != NULL)
3626 *nextcmd = check_nextcmd(p);
3627
3628 return ret;
3629}
3630
3631/*
3632 * Handle top level expression:
3633 * expr1 ? expr0 : expr0
3634 *
3635 * "arg" must point to the first non-white of the expression.
3636 * "arg" is advanced to the next non-white after the recognized expression.
3637 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003638 * Note: "rettv.v_lock" is not set.
3639 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * Return OK or FAIL.
3641 */
3642 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 int evaluate;
3647{
3648 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003649 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
3652 * Get the first variable.
3653 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 return FAIL;
3656
3657 if ((*arg)[0] == '?')
3658 {
3659 result = FALSE;
3660 if (evaluate)
3661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003662 int error = FALSE;
3663
3664 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003667 if (error)
3668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670
3671 /*
3672 * Get the second variable.
3673 */
3674 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677
3678 /*
3679 * Check for the ":".
3680 */
3681 if ((*arg)[0] != ':')
3682 {
3683 EMSG(_("E109: Missing ':' after '?'"));
3684 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003685 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687 }
3688
3689 /*
3690 * Get the third variable.
3691 */
3692 *arg = skipwhite(*arg + 1);
3693 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3694 {
3695 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003696 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return FAIL;
3698 }
3699 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003700 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702
3703 return OK;
3704}
3705
3706/*
3707 * Handle first level expression:
3708 * expr2 || expr2 || expr2 logical OR
3709 *
3710 * "arg" must point to the first non-white of the expression.
3711 * "arg" is advanced to the next non-white after the recognized expression.
3712 *
3713 * Return OK or FAIL.
3714 */
3715 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003716eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int evaluate;
3720{
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 long result;
3723 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003724 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
3726 /*
3727 * Get the first variable.
3728 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return FAIL;
3731
3732 /*
3733 * Repeat until there is no following "||".
3734 */
3735 first = TRUE;
3736 result = FALSE;
3737 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3738 {
3739 if (evaluate && first)
3740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 if (error)
3745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 first = FALSE;
3747 }
3748
3749 /*
3750 * Get the second variable.
3751 */
3752 *arg = skipwhite(*arg + 2);
3753 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3754 return FAIL;
3755
3756 /*
3757 * Compute the result.
3758 */
3759 if (evaluate && !result)
3760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 if (error)
3765 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 if (evaluate)
3768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 rettv->v_type = VAR_NUMBER;
3770 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772 }
3773
3774 return OK;
3775}
3776
3777/*
3778 * Handle second level expression:
3779 * expr3 && expr3 && expr3 logical AND
3780 *
3781 * "arg" must point to the first non-white of the expression.
3782 * "arg" is advanced to the next non-white after the recognized expression.
3783 *
3784 * Return OK or FAIL.
3785 */
3786 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 int evaluate;
3791{
Bram Moolenaar33570922005-01-25 22:26:29 +00003792 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 long result;
3794 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /*
3798 * Get the first variable.
3799 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802
3803 /*
3804 * Repeat until there is no following "&&".
3805 */
3806 first = TRUE;
3807 result = TRUE;
3808 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3809 {
3810 if (evaluate && first)
3811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003815 if (error)
3816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 first = FALSE;
3818 }
3819
3820 /*
3821 * Get the second variable.
3822 */
3823 *arg = skipwhite(*arg + 2);
3824 if (eval4(arg, &var2, evaluate && result) == FAIL)
3825 return FAIL;
3826
3827 /*
3828 * Compute the result.
3829 */
3830 if (evaluate && result)
3831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 if (error)
3836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 if (evaluate)
3839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 rettv->v_type = VAR_NUMBER;
3841 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 }
3844
3845 return OK;
3846}
3847
3848/*
3849 * Handle third level expression:
3850 * var1 == var2
3851 * var1 =~ var2
3852 * var1 != var2
3853 * var1 !~ var2
3854 * var1 > var2
3855 * var1 >= var2
3856 * var1 < var2
3857 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003858 * var1 is var2
3859 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 *
3861 * "arg" must point to the first non-white of the expression.
3862 * "arg" is advanced to the next non-white after the recognized expression.
3863 *
3864 * Return OK or FAIL.
3865 */
3866 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int evaluate;
3871{
Bram Moolenaar33570922005-01-25 22:26:29 +00003872 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u *p;
3874 int i;
3875 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003876 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 int len = 2;
3878 long n1, n2;
3879 char_u *s1, *s2;
3880 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3881 regmatch_T regmatch;
3882 int ic;
3883 char_u *save_cpo;
3884
3885 /*
3886 * Get the first variable.
3887 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003888 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 return FAIL;
3890
3891 p = *arg;
3892 switch (p[0])
3893 {
3894 case '=': if (p[1] == '=')
3895 type = TYPE_EQUAL;
3896 else if (p[1] == '~')
3897 type = TYPE_MATCH;
3898 break;
3899 case '!': if (p[1] == '=')
3900 type = TYPE_NEQUAL;
3901 else if (p[1] == '~')
3902 type = TYPE_NOMATCH;
3903 break;
3904 case '>': if (p[1] != '=')
3905 {
3906 type = TYPE_GREATER;
3907 len = 1;
3908 }
3909 else
3910 type = TYPE_GEQUAL;
3911 break;
3912 case '<': if (p[1] != '=')
3913 {
3914 type = TYPE_SMALLER;
3915 len = 1;
3916 }
3917 else
3918 type = TYPE_SEQUAL;
3919 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003920 case 'i': if (p[1] == 's')
3921 {
3922 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3923 len = 5;
3924 if (!vim_isIDc(p[len]))
3925 {
3926 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3927 type_is = TRUE;
3928 }
3929 }
3930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932
3933 /*
3934 * If there is a comparitive operator, use it.
3935 */
3936 if (type != TYPE_UNKNOWN)
3937 {
3938 /* extra question mark appended: ignore case */
3939 if (p[len] == '?')
3940 {
3941 ic = TRUE;
3942 ++len;
3943 }
3944 /* extra '#' appended: match case */
3945 else if (p[len] == '#')
3946 {
3947 ic = FALSE;
3948 ++len;
3949 }
3950 /* nothing appened: use 'ignorecase' */
3951 else
3952 ic = p_ic;
3953
3954 /*
3955 * Get the second variable.
3956 */
3957 *arg = skipwhite(p + len);
3958 if (eval5(arg, &var2, evaluate) == FAIL)
3959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 return FAIL;
3962 }
3963
3964 if (evaluate)
3965 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003966 if (type_is && rettv->v_type != var2.v_type)
3967 {
3968 /* For "is" a different type always means FALSE, for "notis"
3969 * it means TRUE. */
3970 n1 = (type == TYPE_NEQUAL);
3971 }
3972 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3973 {
3974 if (type_is)
3975 {
3976 n1 = (rettv->v_type == var2.v_type
3977 && rettv->vval.v_list == var2.vval.v_list);
3978 if (type == TYPE_NEQUAL)
3979 n1 = !n1;
3980 }
3981 else if (rettv->v_type != var2.v_type
3982 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3983 {
3984 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003985 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003986 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003987 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 clear_tv(rettv);
3989 clear_tv(&var2);
3990 return FAIL;
3991 }
3992 else
3993 {
3994 /* Compare two Lists for being equal or unequal. */
3995 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3996 if (type == TYPE_NEQUAL)
3997 n1 = !n1;
3998 }
3999 }
4000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004001 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4002 {
4003 if (type_is)
4004 {
4005 n1 = (rettv->v_type == var2.v_type
4006 && rettv->vval.v_dict == var2.vval.v_dict);
4007 if (type == TYPE_NEQUAL)
4008 n1 = !n1;
4009 }
4010 else if (rettv->v_type != var2.v_type
4011 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4012 {
4013 if (rettv->v_type != var2.v_type)
4014 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4015 else
4016 EMSG(_("E736: Invalid operation for Dictionary"));
4017 clear_tv(rettv);
4018 clear_tv(&var2);
4019 return FAIL;
4020 }
4021 else
4022 {
4023 /* Compare two Dictionaries for being equal or unequal. */
4024 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4025 if (type == TYPE_NEQUAL)
4026 n1 = !n1;
4027 }
4028 }
4029
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004030 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4031 {
4032 if (rettv->v_type != var2.v_type
4033 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4034 {
4035 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004036 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004038 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 clear_tv(rettv);
4040 clear_tv(&var2);
4041 return FAIL;
4042 }
4043 else
4044 {
4045 /* Compare two Funcrefs for being equal or unequal. */
4046 if (rettv->vval.v_string == NULL
4047 || var2.vval.v_string == NULL)
4048 n1 = FALSE;
4049 else
4050 n1 = STRCMP(rettv->vval.v_string,
4051 var2.vval.v_string) == 0;
4052 if (type == TYPE_NEQUAL)
4053 n1 = !n1;
4054 }
4055 }
4056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 /*
4058 * If one of the two variables is a number, compare as a number.
4059 * When using "=~" or "!~", always compare as string.
4060 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 n1 = get_tv_number(rettv);
4065 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 switch (type)
4067 {
4068 case TYPE_EQUAL: n1 = (n1 == n2); break;
4069 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4070 case TYPE_GREATER: n1 = (n1 > n2); break;
4071 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4072 case TYPE_SMALLER: n1 = (n1 < n2); break;
4073 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4074 case TYPE_UNKNOWN:
4075 case TYPE_MATCH:
4076 case TYPE_NOMATCH: break; /* avoid gcc warning */
4077 }
4078 }
4079 else
4080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 s1 = get_tv_string_buf(rettv, buf1);
4082 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4084 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4085 else
4086 i = 0;
4087 n1 = FALSE;
4088 switch (type)
4089 {
4090 case TYPE_EQUAL: n1 = (i == 0); break;
4091 case TYPE_NEQUAL: n1 = (i != 0); break;
4092 case TYPE_GREATER: n1 = (i > 0); break;
4093 case TYPE_GEQUAL: n1 = (i >= 0); break;
4094 case TYPE_SMALLER: n1 = (i < 0); break;
4095 case TYPE_SEQUAL: n1 = (i <= 0); break;
4096
4097 case TYPE_MATCH:
4098 case TYPE_NOMATCH:
4099 /* avoid 'l' flag in 'cpoptions' */
4100 save_cpo = p_cpo;
4101 p_cpo = (char_u *)"";
4102 regmatch.regprog = vim_regcomp(s2,
4103 RE_MAGIC + RE_STRING);
4104 regmatch.rm_ic = ic;
4105 if (regmatch.regprog != NULL)
4106 {
4107 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4108 vim_free(regmatch.regprog);
4109 if (type == TYPE_NOMATCH)
4110 n1 = !n1;
4111 }
4112 p_cpo = save_cpo;
4113 break;
4114
4115 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4116 }
4117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 clear_tv(rettv);
4119 clear_tv(&var2);
4120 rettv->v_type = VAR_NUMBER;
4121 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 }
4124
4125 return OK;
4126}
4127
4128/*
4129 * Handle fourth level expression:
4130 * + number addition
4131 * - number subtraction
4132 * . string concatenation
4133 *
4134 * "arg" must point to the first non-white of the expression.
4135 * "arg" is advanced to the next non-white after the recognized expression.
4136 *
4137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T var2;
4146 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 int op;
4148 long n1, n2;
4149 char_u *s1, *s2;
4150 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4151 char_u *p;
4152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 /*
4160 * Repeat computing, until no '+', '-' or '.' is following.
4161 */
4162 for (;;)
4163 {
4164 op = **arg;
4165 if (op != '+' && op != '-' && op != '.')
4166 break;
4167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (op != '+' || rettv->v_type != VAR_LIST)
4169 {
4170 /* For "list + ...", an illegal use of the first operand as
4171 * a number cannot be determined before evaluating the 2nd
4172 * operand: if this is also a list, all is ok.
4173 * For "something . ...", "something - ..." or "non-list + ...",
4174 * we know that the first operand needs to be a string or number
4175 * without evaluating the 2nd operand. So check before to avoid
4176 * side effects after an error. */
4177 if (evaluate && get_tv_string_chk(rettv) == NULL)
4178 {
4179 clear_tv(rettv);
4180 return FAIL;
4181 }
4182 }
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 /*
4185 * Get the second variable.
4186 */
4187 *arg = skipwhite(*arg + 1);
4188 if (eval6(arg, &var2, evaluate) == FAIL)
4189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192 }
4193
4194 if (evaluate)
4195 {
4196 /*
4197 * Compute the result.
4198 */
4199 if (op == '.')
4200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4202 s2 = get_tv_string_buf_chk(&var2, buf2);
4203 if (s2 == NULL) /* type error ? */
4204 {
4205 clear_tv(rettv);
4206 clear_tv(&var2);
4207 return FAIL;
4208 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004209 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
4211 rettv->v_type = VAR_STRING;
4212 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004214 else if (op == '+' && rettv->v_type == VAR_LIST
4215 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 {
4217 /* concatenate Lists */
4218 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4219 &var3) == FAIL)
4220 {
4221 clear_tv(rettv);
4222 clear_tv(&var2);
4223 return FAIL;
4224 }
4225 clear_tv(rettv);
4226 *rettv = var3;
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 else
4229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004230 int error = FALSE;
4231
4232 n1 = get_tv_number_chk(rettv, &error);
4233 if (error)
4234 {
4235 /* This can only happen for "list + non-list".
4236 * For "non-list + ..." or "something - ...", we returned
4237 * before evaluating the 2nd operand. */
4238 clear_tv(rettv);
4239 return FAIL;
4240 }
4241 n2 = get_tv_number_chk(&var2, &error);
4242 if (error)
4243 {
4244 clear_tv(rettv);
4245 clear_tv(&var2);
4246 return FAIL;
4247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (op == '+')
4250 n1 = n1 + n2;
4251 else
4252 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 rettv->v_type = VAR_NUMBER;
4254 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259 return OK;
4260}
4261
4262/*
4263 * Handle fifth level expression:
4264 * * number multiplication
4265 * / number division
4266 * % number modulo
4267 *
4268 * "arg" must point to the first non-white of the expression.
4269 * "arg" is advanced to the next non-white after the recognized expression.
4270 *
4271 * Return OK or FAIL.
4272 */
4273 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int evaluate;
4278{
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int op;
4281 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
4284 /*
4285 * Get the first variable.
4286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Repeat computing, until no '*', '/' or '%' is following.
4292 */
4293 for (;;)
4294 {
4295 op = **arg;
4296 if (op != '*' && op != '/' && op != '%')
4297 break;
4298
4299 if (evaluate)
4300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 if (error)
4304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else
4307 n1 = 0;
4308
4309 /*
4310 * Get the second variable.
4311 */
4312 *arg = skipwhite(*arg + 1);
4313 if (eval7(arg, &var2, evaluate) == FAIL)
4314 return FAIL;
4315
4316 if (evaluate)
4317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (error)
4321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Compute the result.
4325 */
4326 if (op == '*')
4327 n1 = n1 * n2;
4328 else if (op == '/')
4329 {
4330 if (n2 == 0) /* give an error message? */
4331 n1 = 0x7fffffffL;
4332 else
4333 n1 = n1 / n2;
4334 }
4335 else
4336 {
4337 if (n2 == 0) /* give an error message? */
4338 n1 = 0;
4339 else
4340 n1 = n1 % n2;
4341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 rettv->v_type = VAR_NUMBER;
4343 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
4346
4347 return OK;
4348}
4349
4350/*
4351 * Handle sixth level expression:
4352 * number number constant
4353 * "string" string contstant
4354 * 'string' literal string contstant
4355 * &option-name option value
4356 * @r register contents
4357 * identifier variable value
4358 * function() function call
4359 * $VAR environment variable
4360 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004361 * [expr, expr] List
4362 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 *
4364 * Also handle:
4365 * ! in front logical NOT
4366 * - in front unary minus
4367 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 * trailing [] subscript in String or List
4369 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int evaluate;
4381{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 long n;
4383 int len;
4384 char_u *s;
4385 int val;
4386 char_u *start_leader, *end_leader;
4387 int ret = OK;
4388 char_u *alias;
4389
4390 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004392 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 /*
4397 * Skip '!' and '-' characters. They are handled later.
4398 */
4399 start_leader = *arg;
4400 while (**arg == '!' || **arg == '-' || **arg == '+')
4401 *arg = skipwhite(*arg + 1);
4402 end_leader = *arg;
4403
4404 switch (**arg)
4405 {
4406 /*
4407 * Number constant.
4408 */
4409 case '0':
4410 case '1':
4411 case '2':
4412 case '3':
4413 case '4':
4414 case '5':
4415 case '6':
4416 case '7':
4417 case '8':
4418 case '9':
4419 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4420 *arg += len;
4421 if (evaluate)
4422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004423 rettv->v_type = VAR_NUMBER;
4424 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 break;
4427
4428 /*
4429 * String constant: "string".
4430 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 break;
4433
4434 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 break;
4439
4440 /*
4441 * List: [expr, expr]
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 break;
4445
4446 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 * Dictionary: {key: val, key: val}
4448 */
4449 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4450 break;
4451
4452 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004453 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004455 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457
4458 /*
4459 * Environment variable: $VAR.
4460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 break;
4463
4464 /*
4465 * Register contents: @r.
4466 */
4467 case '@': ++*arg;
4468 if (evaluate)
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004471 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 if (**arg != NUL)
4474 ++*arg;
4475 break;
4476
4477 /*
4478 * nested expression: (expression).
4479 */
4480 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (**arg == ')')
4483 ++*arg;
4484 else if (ret == OK)
4485 {
4486 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 ret = FAIL;
4489 }
4490 break;
4491
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 break;
4494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495
4496 if (ret == NOTDONE)
4497 {
4498 /*
4499 * Must be a variable or function name.
4500 * Can also be a curly-braces kind of name: {expr}.
4501 */
4502 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004503 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 if (alias != NULL)
4505 s = alias;
4506
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004507 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 ret = FAIL;
4509 else
4510 {
4511 if (**arg == '(') /* recursive! */
4512 {
4513 /* If "s" is the name of a variable of type VAR_FUNC
4514 * use its contents. */
4515 s = deref_func_name(s, &len);
4516
4517 /* Invoke the function. */
4518 ret = get_func_tv(s, len, rettv, arg,
4519 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004520 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 /* Stop the expression evaluation when immediately
4522 * aborting on error, or when an interrupt occurred or
4523 * an exception was thrown but not caught. */
4524 if (aborting())
4525 {
4526 if (ret == OK)
4527 clear_tv(rettv);
4528 ret = FAIL;
4529 }
4530 }
4531 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004532 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004533 else
4534 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 }
4536
4537 if (alias != NULL)
4538 vim_free(alias);
4539 }
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 *arg = skipwhite(*arg);
4542
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4544 * expr(expr). */
4545 if (ret == OK)
4546 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 /*
4549 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4550 */
4551 if (ret == OK && evaluate && end_leader > start_leader)
4552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 int error = FALSE;
4554
4555 val = get_tv_number_chk(rettv, &error);
4556 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 clear_tv(rettv);
4559 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561 else
4562 {
4563 while (end_leader > start_leader)
4564 {
4565 --end_leader;
4566 if (*end_leader == '!')
4567 val = !val;
4568 else if (*end_leader == '-')
4569 val = -val;
4570 }
4571 clear_tv(rettv);
4572 rettv->v_type = VAR_NUMBER;
4573 rettv->vval.v_number = val;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576
4577 return ret;
4578}
4579
4580/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 * Evaluate an "[expr]" or "[expr:expr]" index.
4582 * "*arg" points to the '['.
4583 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4584 */
4585 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004586eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004588 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004590 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591{
4592 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595 long len = -1;
4596 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004602 if (verbose)
4603 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 return FAIL;
4605 }
4606
Bram Moolenaar8c711452005-01-14 21:53:12 +00004607 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 /*
4610 * dict.name
4611 */
4612 key = *arg + 1;
4613 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4614 ;
4615 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 }
4619 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 /*
4622 * something[idx]
4623 *
4624 * Get the (first) variable from inside the [].
4625 */
4626 *arg = skipwhite(*arg + 1);
4627 if (**arg == ':')
4628 empty1 = TRUE;
4629 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4630 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4632 {
4633 /* not a number or string */
4634 clear_tv(&var1);
4635 return FAIL;
4636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637
4638 /*
4639 * Get the second variable from inside the [:].
4640 */
4641 if (**arg == ':')
4642 {
4643 range = TRUE;
4644 *arg = skipwhite(*arg + 1);
4645 if (**arg == ']')
4646 empty2 = TRUE;
4647 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 if (!empty1)
4650 clear_tv(&var1);
4651 return FAIL;
4652 }
4653 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4654 {
4655 /* not a number or string */
4656 if (!empty1)
4657 clear_tv(&var1);
4658 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 return FAIL;
4660 }
4661 }
4662
4663 /* Check for the ']'. */
4664 if (**arg != ']')
4665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004666 if (verbose)
4667 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 clear_tv(&var1);
4669 if (range)
4670 clear_tv(&var2);
4671 return FAIL;
4672 }
4673 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 }
4675
4676 if (evaluate)
4677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 n1 = 0;
4679 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 n1 = get_tv_number(&var1);
4682 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 }
4684 if (range)
4685 {
4686 if (empty2)
4687 n2 = -1;
4688 else
4689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 n2 = get_tv_number(&var2);
4691 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 }
4693 }
4694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 {
4697 case VAR_NUMBER:
4698 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 len = (long)STRLEN(s);
4701 if (range)
4702 {
4703 /* The resulting variable is a substring. If the indexes
4704 * are out of range the result is empty. */
4705 if (n1 < 0)
4706 {
4707 n1 = len + n1;
4708 if (n1 < 0)
4709 n1 = 0;
4710 }
4711 if (n2 < 0)
4712 n2 = len + n2;
4713 else if (n2 >= len)
4714 n2 = len;
4715 if (n1 >= len || n2 < 0 || n1 > n2)
4716 s = NULL;
4717 else
4718 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4719 }
4720 else
4721 {
4722 /* The resulting variable is a string of a single
4723 * character. If the index is too big or negative the
4724 * result is empty. */
4725 if (n1 >= len || n1 < 0)
4726 s = NULL;
4727 else
4728 s = vim_strnsave(s + n1, 1);
4729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
4731 rettv->v_type = VAR_STRING;
4732 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 break;
4734
4735 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004736 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 if (n1 < 0)
4738 n1 = len + n1;
4739 if (!empty1 && (n1 < 0 || n1 >= len))
4740 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004741 if (verbose)
4742 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 return FAIL;
4744 }
4745 if (range)
4746 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004747 list_T *l;
4748 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749
4750 if (n2 < 0)
4751 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004754 if (verbose)
4755 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 return FAIL;
4757 }
4758 l = list_alloc();
4759 if (l == NULL)
4760 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 n1 <= n2; ++n1)
4763 {
4764 if (list_append_tv(l, &item->li_tv) == FAIL)
4765 {
4766 list_free(l);
4767 return FAIL;
4768 }
4769 item = item->li_next;
4770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 clear_tv(rettv);
4772 rettv->v_type = VAR_LIST;
4773 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004774 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 }
4776 else
4777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 }
4783 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004784
4785 case VAR_DICT:
4786 if (range)
4787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004788 if (verbose)
4789 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 if (len == -1)
4791 clear_tv(&var1);
4792 return FAIL;
4793 }
4794 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004795 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796
4797 if (len == -1)
4798 {
4799 key = get_tv_string(&var1);
4800 if (*key == NUL)
4801 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004802 if (verbose)
4803 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 clear_tv(&var1);
4805 return FAIL;
4806 }
4807 }
4808
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004809 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004811 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004812 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 if (len == -1)
4814 clear_tv(&var1);
4815 if (item == NULL)
4816 return FAIL;
4817
4818 copy_tv(&item->di_tv, &var1);
4819 clear_tv(rettv);
4820 *rettv = var1;
4821 }
4822 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
4824 }
4825
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 return OK;
4827}
4828
4829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 * Get an option value.
4831 * "arg" points to the '&' or '+' before the option name.
4832 * "arg" is advanced to character after the option name.
4833 * Return OK or FAIL.
4834 */
4835 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004838 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 int evaluate;
4840{
4841 char_u *option_end;
4842 long numval;
4843 char_u *stringval;
4844 int opt_type;
4845 int c;
4846 int working = (**arg == '+'); /* has("+option") */
4847 int ret = OK;
4848 int opt_flags;
4849
4850 /*
4851 * Isolate the option name and find its value.
4852 */
4853 option_end = find_option_end(arg, &opt_flags);
4854 if (option_end == NULL)
4855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 EMSG2(_("E112: Option name missing: %s"), *arg);
4858 return FAIL;
4859 }
4860
4861 if (!evaluate)
4862 {
4863 *arg = option_end;
4864 return OK;
4865 }
4866
4867 c = *option_end;
4868 *option_end = NUL;
4869 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 if (opt_type == -3) /* invalid name */
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 EMSG2(_("E113: Unknown option: %s"), *arg);
4876 ret = FAIL;
4877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 if (opt_type == -2) /* hidden string option */
4881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 rettv->v_type = VAR_STRING;
4883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 else if (opt_type == -1) /* hidden number option */
4886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 rettv->v_type = VAR_NUMBER;
4888 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 else if (opt_type == 1) /* number option */
4891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 rettv->v_type = VAR_NUMBER;
4893 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895 else /* string option */
4896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 rettv->v_type = VAR_STRING;
4898 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 }
4901 else if (working && (opt_type == -2 || opt_type == -1))
4902 ret = FAIL;
4903
4904 *option_end = c; /* put back for error messages */
4905 *arg = option_end;
4906
4907 return ret;
4908}
4909
4910/*
4911 * Allocate a variable for a string constant.
4912 * Return OK or FAIL.
4913 */
4914 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 int evaluate;
4919{
4920 char_u *p;
4921 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 int extra = 0;
4923
4924 /*
4925 * Find the end of the string, skipping backslashed characters.
4926 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004927 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 if (*p == '\\' && p[1] != NUL)
4930 {
4931 ++p;
4932 /* A "\<x>" form occupies at least 4 characters, and produces up
4933 * to 6 characters: reserve space for 2 extra */
4934 if (*p == '<')
4935 extra += 2;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938
4939 if (*p != '"')
4940 {
4941 EMSG2(_("E114: Missing quote: %s"), *arg);
4942 return FAIL;
4943 }
4944
4945 /* If only parsing, set *arg and return here */
4946 if (!evaluate)
4947 {
4948 *arg = p + 1;
4949 return OK;
4950 }
4951
4952 /*
4953 * Copy the string into allocated memory, handling backslashed
4954 * characters.
4955 */
4956 name = alloc((unsigned)(p - *arg + extra));
4957 if (name == NULL)
4958 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 rettv->v_type = VAR_STRING;
4960 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
4964 if (*p == '\\')
4965 {
4966 switch (*++p)
4967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 case 'b': *name++ = BS; ++p; break;
4969 case 'e': *name++ = ESC; ++p; break;
4970 case 'f': *name++ = FF; ++p; break;
4971 case 'n': *name++ = NL; ++p; break;
4972 case 'r': *name++ = CAR; ++p; break;
4973 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 case 'X': /* hex: "\x1", "\x12" */
4976 case 'x':
4977 case 'u': /* Unicode: "\u0023" */
4978 case 'U':
4979 if (vim_isxdigit(p[1]))
4980 {
4981 int n, nr;
4982 int c = toupper(*p);
4983
4984 if (c == 'X')
4985 n = 2;
4986 else
4987 n = 4;
4988 nr = 0;
4989 while (--n >= 0 && vim_isxdigit(p[1]))
4990 {
4991 ++p;
4992 nr = (nr << 4) + hex2nr(*p);
4993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#ifdef FEAT_MBYTE
4996 /* For "\u" store the number according to
4997 * 'encoding'. */
4998 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
5001#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 break;
5005
5006 /* octal: "\1", "\12", "\123" */
5007 case '0':
5008 case '1':
5009 case '2':
5010 case '3':
5011 case '4':
5012 case '5':
5013 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 case '7': *name = *p++ - '0';
5015 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 *name = (*name << 3) + *p++ - '0';
5018 if (*p >= '0' && *p <= '7')
5019 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 break;
5023
5024 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (extra != 0)
5027 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030 }
5031 /* FALLTHROUGH */
5032
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 break;
5035 }
5036 }
5037 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *arg = p + 1;
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 return OK;
5045}
5046
5047/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 * Return OK or FAIL.
5050 */
5051 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 int evaluate;
5056{
5057 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 int reduce = 0;
5060
5061 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 */
5064 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005069 break;
5070 ++reduce;
5071 ++p;
5072 }
5073 }
5074
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 return FAIL;
5079 }
5080
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 if (!evaluate)
5083 {
5084 *arg = p + 1;
5085 return OK;
5086 }
5087
5088 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 */
5091 str = alloc((unsigned)((p - *arg) - reduce));
5092 if (str == NULL)
5093 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 rettv->v_type = VAR_STRING;
5095 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 break;
5103 ++p;
5104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 *arg = p + 1;
5109
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 return OK;
5111}
5112
5113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 * Allocate a variable for a List and fill it from "*arg".
5115 * Return OK or FAIL.
5116 */
5117 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005120 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 int evaluate;
5122{
Bram Moolenaar33570922005-01-25 22:26:29 +00005123 list_T *l = NULL;
5124 typval_T tv;
5125 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126
5127 if (evaluate)
5128 {
5129 l = list_alloc();
5130 if (l == NULL)
5131 return FAIL;
5132 }
5133
5134 *arg = skipwhite(*arg + 1);
5135 while (**arg != ']' && **arg != NUL)
5136 {
5137 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5138 goto failret;
5139 if (evaluate)
5140 {
5141 item = listitem_alloc();
5142 if (item != NULL)
5143 {
5144 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005145 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 list_append(l, item);
5147 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005148 else
5149 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 }
5151
5152 if (**arg == ']')
5153 break;
5154 if (**arg != ',')
5155 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 goto failret;
5158 }
5159 *arg = skipwhite(*arg + 1);
5160 }
5161
5162 if (**arg != ']')
5163 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165failret:
5166 if (evaluate)
5167 list_free(l);
5168 return FAIL;
5169 }
5170
5171 *arg = skipwhite(*arg + 1);
5172 if (evaluate)
5173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 rettv->v_type = VAR_LIST;
5175 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 ++l->lv_refcount;
5177 }
5178
5179 return OK;
5180}
5181
5182/*
5183 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005184 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187list_alloc()
5188{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005189 list_T *l;
5190
5191 l = (list_T *)alloc_clear(sizeof(list_T));
5192 if (l != NULL)
5193 {
5194 /* Prepend the list to the list of lists for garbage collection. */
5195 if (first_list != NULL)
5196 first_list->lv_used_prev = l;
5197 l->lv_used_prev = NULL;
5198 l->lv_used_next = first_list;
5199 first_list = l;
5200 }
5201 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202}
5203
5204/*
5205 * Unreference a list: decrement the reference count and free it when it
5206 * becomes zero.
5207 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005208 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005210 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005212 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5213 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214}
5215
5216/*
5217 * Free a list, including all items it points to.
5218 * Ignores the reference count.
5219 */
5220 static void
5221list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223{
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225
Bram Moolenaard9fba312005-06-26 22:34:35 +00005226 /* Avoid that recursive reference to the list frees us again. */
5227 l->lv_refcount = DEL_REFCOUNT;
5228
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229 /* Remove the list from the list of lists for garbage collection. */
5230 if (l->lv_used_prev == NULL)
5231 first_list = l->lv_used_next;
5232 else
5233 l->lv_used_prev->lv_used_next = l->lv_used_next;
5234 if (l->lv_used_next != NULL)
5235 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5236
Bram Moolenaard9fba312005-06-26 22:34:35 +00005237 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005239 /* Remove the item before deleting it. */
5240 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 listitem_free(item);
5242 }
5243 vim_free(l);
5244}
5245
5246/*
5247 * Allocate a list item.
5248 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005249 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250listitem_alloc()
5251{
Bram Moolenaar33570922005-01-25 22:26:29 +00005252 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253}
5254
5255/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005256 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 */
5258 static void
5259listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005262 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 vim_free(item);
5264}
5265
5266/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005267 * Remove a list item from a List and free it. Also clears the value.
5268 */
5269 static void
5270listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005271 list_T *l;
5272 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005273{
5274 list_remove(l, item, item);
5275 listitem_free(item);
5276}
5277
5278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Get the number of items in a list.
5280 */
5281 static long
5282list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 if (l == NULL)
5286 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005287 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288}
5289
5290/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005291 * Return TRUE when two lists have exactly the same values.
5292 */
5293 static int
5294list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 list_T *l1;
5296 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 int ic; /* ignore case for strings */
5298{
Bram Moolenaar33570922005-01-25 22:26:29 +00005299 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005301 if (list_len(l1) != list_len(l2))
5302 return FALSE;
5303
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005304 for (item1 = l1->lv_first, item2 = l2->lv_first;
5305 item1 != NULL && item2 != NULL;
5306 item1 = item1->li_next, item2 = item2->li_next)
5307 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5308 return FALSE;
5309 return item1 == NULL && item2 == NULL;
5310}
5311
5312/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005313 * Return TRUE when two dictionaries have exactly the same key/values.
5314 */
5315 static int
5316dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 dict_T *d1;
5318 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005319 int ic; /* ignore case for strings */
5320{
Bram Moolenaar33570922005-01-25 22:26:29 +00005321 hashitem_T *hi;
5322 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005323 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005324
5325 if (dict_len(d1) != dict_len(d2))
5326 return FALSE;
5327
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 todo = d1->dv_hashtab.ht_used;
5329 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005331 if (!HASHITEM_EMPTY(hi))
5332 {
5333 item2 = dict_find(d2, hi->hi_key, -1);
5334 if (item2 == NULL)
5335 return FALSE;
5336 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5337 return FALSE;
5338 --todo;
5339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005340 }
5341 return TRUE;
5342}
5343
5344/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005346 * Compares the items just like "==" would compare them, but strings and
5347 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348 */
5349 static int
5350tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005351 typval_T *tv1;
5352 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005353 int ic; /* ignore case */
5354{
5355 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005357
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005358 if (tv1->v_type != tv2->v_type)
5359 return FALSE;
5360
5361 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005362 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005363 case VAR_LIST:
5364 /* recursive! */
5365 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5366
5367 case VAR_DICT:
5368 /* recursive! */
5369 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5370
5371 case VAR_FUNC:
5372 return (tv1->vval.v_string != NULL
5373 && tv2->vval.v_string != NULL
5374 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5375
5376 case VAR_NUMBER:
5377 return tv1->vval.v_number == tv2->vval.v_number;
5378
5379 case VAR_STRING:
5380 s1 = get_tv_string_buf(tv1, buf1);
5381 s2 = get_tv_string_buf(tv2, buf2);
5382 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005384
5385 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 return TRUE;
5387}
5388
5389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 * Locate item with index "n" in list "l" and return it.
5391 * A negative index is counted from the end; -1 is the last item.
5392 * Returns NULL when "n" is out of range.
5393 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005394 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 long n;
5398{
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 long idx;
5401
5402 if (l == NULL)
5403 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005404
5405 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005407 n = l->lv_len + n;
5408
5409 /* Check for index out of range. */
5410 if (n < 0 || n >= l->lv_len)
5411 return NULL;
5412
5413 /* When there is a cached index may start search from there. */
5414 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005416 if (n < l->lv_idx / 2)
5417 {
5418 /* closest to the start of the list */
5419 item = l->lv_first;
5420 idx = 0;
5421 }
5422 else if (n > (l->lv_idx + l->lv_len) / 2)
5423 {
5424 /* closest to the end of the list */
5425 item = l->lv_last;
5426 idx = l->lv_len - 1;
5427 }
5428 else
5429 {
5430 /* closest to the cached index */
5431 item = l->lv_idx_item;
5432 idx = l->lv_idx;
5433 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 }
5435 else
5436 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437 if (n < l->lv_len / 2)
5438 {
5439 /* closest to the start of the list */
5440 item = l->lv_first;
5441 idx = 0;
5442 }
5443 else
5444 {
5445 /* closest to the end of the list */
5446 item = l->lv_last;
5447 idx = l->lv_len - 1;
5448 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005450
5451 while (n > idx)
5452 {
5453 /* search forward */
5454 item = item->li_next;
5455 ++idx;
5456 }
5457 while (n < idx)
5458 {
5459 /* search backward */
5460 item = item->li_prev;
5461 --idx;
5462 }
5463
5464 /* cache the used index */
5465 l->lv_idx = idx;
5466 l->lv_idx_item = item;
5467
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 return item;
5469}
5470
5471/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005472 * Locate "item" list "l" and return its index.
5473 * Returns -1 when "item" is not in the list.
5474 */
5475 static long
5476list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 list_T *l;
5478 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005479{
5480 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005481 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005482
5483 if (l == NULL)
5484 return -1;
5485 idx = 0;
5486 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5487 ++idx;
5488 if (li == NULL)
5489 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005490 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005491}
5492
5493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 * Append item "item" to the end of list "l".
5495 */
5496 static void
5497list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 list_T *l;
5499 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500{
5501 if (l->lv_last == NULL)
5502 {
5503 /* empty list */
5504 l->lv_first = item;
5505 l->lv_last = item;
5506 item->li_prev = NULL;
5507 }
5508 else
5509 {
5510 l->lv_last->li_next = item;
5511 item->li_prev = l->lv_last;
5512 l->lv_last = item;
5513 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005514 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 item->li_next = NULL;
5516}
5517
5518/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005520 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 */
5522 static int
5523list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 list_T *l;
5525 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005527 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528
Bram Moolenaar05159a02005-02-26 23:04:13 +00005529 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005531 copy_tv(tv, &li->li_tv);
5532 list_append(l, li);
5533 return OK;
5534}
5535
5536/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005537 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005538 * Return FAIL when out of memory.
5539 */
5540 int
5541list_append_dict(list, dict)
5542 list_T *list;
5543 dict_T *dict;
5544{
5545 listitem_T *li = listitem_alloc();
5546
5547 if (li == NULL)
5548 return FAIL;
5549 li->li_tv.v_type = VAR_DICT;
5550 li->li_tv.v_lock = 0;
5551 li->li_tv.vval.v_dict = dict;
5552 list_append(list, li);
5553 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 return OK;
5555}
5556
5557/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005558 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005559 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005560 * Returns FAIL when out of memory.
5561 */
5562 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005563list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005564 list_T *l;
5565 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005566 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005567{
5568 listitem_T *li = listitem_alloc();
5569
5570 if (li == NULL)
5571 return FAIL;
5572 list_append(l, li);
5573 li->li_tv.v_type = VAR_STRING;
5574 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005575 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5576 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005577 return FAIL;
5578 return OK;
5579}
5580
5581/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005582 * Append "n" to list "l".
5583 * Returns FAIL when out of memory.
5584 */
5585 static int
5586list_append_number(l, n)
5587 list_T *l;
5588 varnumber_T n;
5589{
5590 listitem_T *li;
5591
5592 li = listitem_alloc();
5593 if (li == NULL)
5594 return FAIL;
5595 li->li_tv.v_type = VAR_NUMBER;
5596 li->li_tv.v_lock = 0;
5597 li->li_tv.vval.v_number = n;
5598 list_append(l, li);
5599 return OK;
5600}
5601
5602/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005603 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005604 * If "item" is NULL append at the end.
5605 * Return FAIL when out of memory.
5606 */
5607 static int
5608list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 list_T *l;
5610 typval_T *tv;
5611 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005612{
Bram Moolenaar33570922005-01-25 22:26:29 +00005613 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614
5615 if (ni == NULL)
5616 return FAIL;
5617 copy_tv(tv, &ni->li_tv);
5618 if (item == NULL)
5619 /* Append new item at end of list. */
5620 list_append(l, ni);
5621 else
5622 {
5623 /* Insert new item before existing item. */
5624 ni->li_prev = item->li_prev;
5625 ni->li_next = item;
5626 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005627 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005628 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005629 ++l->lv_idx;
5630 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005631 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005633 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634 l->lv_idx_item = NULL;
5635 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005636 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005638 }
5639 return OK;
5640}
5641
5642/*
5643 * Extend "l1" with "l2".
5644 * If "bef" is NULL append at the end, otherwise insert before this item.
5645 * Returns FAIL when out of memory.
5646 */
5647 static int
5648list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 list_T *l1;
5650 list_T *l2;
5651 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005652{
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005654
5655 for (item = l2->lv_first; item != NULL; item = item->li_next)
5656 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5657 return FAIL;
5658 return OK;
5659}
5660
5661/*
5662 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5663 * Return FAIL when out of memory.
5664 */
5665 static int
5666list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 list_T *l1;
5668 list_T *l2;
5669 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005670{
Bram Moolenaar33570922005-01-25 22:26:29 +00005671 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005672
5673 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005674 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005675 if (l == NULL)
5676 return FAIL;
5677 tv->v_type = VAR_LIST;
5678 tv->vval.v_list = l;
5679
5680 /* append all items from the second list */
5681 return list_extend(l, l2, NULL);
5682}
5683
5684/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005685 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005687 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 * Returns NULL when out of memory.
5689 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005690 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005691list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005692 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005694 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695{
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 list_T *copy;
5697 listitem_T *item;
5698 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699
5700 if (orig == NULL)
5701 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702
5703 copy = list_alloc();
5704 if (copy != NULL)
5705 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005706 if (copyID != 0)
5707 {
5708 /* Do this before adding the items, because one of the items may
5709 * refer back to this list. */
5710 orig->lv_copyID = copyID;
5711 orig->lv_copylist = copy;
5712 }
5713 for (item = orig->lv_first; item != NULL && !got_int;
5714 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 {
5716 ni = listitem_alloc();
5717 if (ni == NULL)
5718 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005719 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 {
5721 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5722 {
5723 vim_free(ni);
5724 break;
5725 }
5726 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729 list_append(copy, ni);
5730 }
5731 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005732 if (item != NULL)
5733 {
5734 list_unref(copy);
5735 copy = NULL;
5736 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 }
5738
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 return copy;
5740}
5741
5742/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005743 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005744 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005746 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005747list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005748 list_T *l;
5749 listitem_T *item;
5750 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005751{
Bram Moolenaar33570922005-01-25 22:26:29 +00005752 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005754 /* notify watchers */
5755 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005757 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005758 list_fix_watch(l, ip);
5759 if (ip == item2)
5760 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005762
5763 if (item2->li_next == NULL)
5764 l->lv_last = item->li_prev;
5765 else
5766 item2->li_next->li_prev = item->li_prev;
5767 if (item->li_prev == NULL)
5768 l->lv_first = item2->li_next;
5769 else
5770 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005771 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772}
5773
5774/*
5775 * Return an allocated string with the string representation of a list.
5776 * May return NULL.
5777 */
5778 static char_u *
5779list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781{
5782 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783
5784 if (tv->vval.v_list == NULL)
5785 return NULL;
5786 ga_init2(&ga, (int)sizeof(char), 80);
5787 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005788 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5789 {
5790 vim_free(ga.ga_data);
5791 return NULL;
5792 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 ga_append(&ga, ']');
5794 ga_append(&ga, NUL);
5795 return (char_u *)ga.ga_data;
5796}
5797
5798/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 * Join list "l" into a string in "*gap", using separator "sep".
5800 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005801 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005803 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804list_join(gap, l, sep, echo)
5805 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005806 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 char_u *sep;
5808 int echo;
5809{
5810 int first = TRUE;
5811 char_u *tofree;
5812 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 char_u *s;
5815
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005816 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 {
5818 if (first)
5819 first = FALSE;
5820 else
5821 ga_concat(gap, sep);
5822
5823 if (echo)
5824 s = echo_string(&item->li_tv, &tofree, numbuf);
5825 else
5826 s = tv2string(&item->li_tv, &tofree, numbuf);
5827 if (s != NULL)
5828 ga_concat(gap, s);
5829 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005830 if (s == NULL)
5831 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005833 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834}
5835
5836/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005837 * Garbage collection for lists and dictionaries.
5838 *
5839 * We use reference counts to be able to free most items right away when they
5840 * are no longer used. But for composite items it's possible that it becomes
5841 * unused while the reference count is > 0: When there is a recursive
5842 * reference. Example:
5843 * :let l = [1, 2, 3]
5844 * :let d = {9: l}
5845 * :let l[1] = d
5846 *
5847 * Since this is quite unusual we handle this with garbage collection: every
5848 * once in a while find out which lists and dicts are not referenced from any
5849 * variable.
5850 *
5851 * Here is a good reference text about garbage collection (refers to Python
5852 * but it applies to all reference-counting mechanisms):
5853 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005854 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005855
5856/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005857 * Do garbage collection for lists and dicts.
5858 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005859 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005860 int
5861garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005862{
5863 dict_T *dd;
5864 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005865 int copyID = ++current_copyID;
5866 buf_T *buf;
5867 win_T *wp;
5868 int i;
5869 funccall_T *fc;
5870 int did_free = FALSE;
5871
5872 /*
5873 * 1. Go through all accessible variables and mark all lists and dicts
5874 * with copyID.
5875 */
5876 /* script-local variables */
5877 for (i = 1; i <= ga_scripts.ga_len; ++i)
5878 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5879
5880 /* buffer-local variables */
5881 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5882 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5883
5884 /* window-local variables */
5885 FOR_ALL_WINDOWS(wp)
5886 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5887
5888 /* global variables */
5889 set_ref_in_ht(&globvarht, copyID);
5890
5891 /* function-local variables */
5892 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5893 {
5894 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5895 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5896 }
5897
5898 /*
5899 * 2. Go through the list of dicts and free items without the copyID.
5900 */
5901 for (dd = first_dict; dd != NULL; )
5902 if (dd->dv_copyID != copyID)
5903 {
5904 dict_free(dd);
5905 did_free = TRUE;
5906
5907 /* restart, next dict may also have been freed */
5908 dd = first_dict;
5909 }
5910 else
5911 dd = dd->dv_used_next;
5912
5913 /*
5914 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005915 * But don't free a list that has a watcher (used in a for loop), these
5916 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917 */
5918 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005919 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005920 {
5921 list_free(ll);
5922 did_free = TRUE;
5923
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005924 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005925 ll = first_list;
5926 }
5927 else
5928 ll = ll->lv_used_next;
5929
5930 return did_free;
5931}
5932
5933/*
5934 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5935 */
5936 static void
5937set_ref_in_ht(ht, copyID)
5938 hashtab_T *ht;
5939 int copyID;
5940{
5941 int todo;
5942 hashitem_T *hi;
5943
5944 todo = ht->ht_used;
5945 for (hi = ht->ht_array; todo > 0; ++hi)
5946 if (!HASHITEM_EMPTY(hi))
5947 {
5948 --todo;
5949 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5950 }
5951}
5952
5953/*
5954 * Mark all lists and dicts referenced through list "l" with "copyID".
5955 */
5956 static void
5957set_ref_in_list(l, copyID)
5958 list_T *l;
5959 int copyID;
5960{
5961 listitem_T *li;
5962
5963 for (li = l->lv_first; li != NULL; li = li->li_next)
5964 set_ref_in_item(&li->li_tv, copyID);
5965}
5966
5967/*
5968 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5969 */
5970 static void
5971set_ref_in_item(tv, copyID)
5972 typval_T *tv;
5973 int copyID;
5974{
5975 dict_T *dd;
5976 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005977
5978 switch (tv->v_type)
5979 {
5980 case VAR_DICT:
5981 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005983 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984 /* Didn't see this dict yet. */
5985 dd->dv_copyID = copyID;
5986 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005988 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005989
5990 case VAR_LIST:
5991 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005992 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005993 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005994 /* Didn't see this list yet. */
5995 ll->lv_copyID = copyID;
5996 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005997 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005998 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006000 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006001}
6002
6003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 * Allocate an empty header for a dictionary.
6005 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006006 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006007dict_alloc()
6008{
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006010
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006012 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006013 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006014 /* Add the list to the hashtable for garbage collection. */
6015 if (first_dict != NULL)
6016 first_dict->dv_used_prev = d;
6017 d->dv_used_next = first_dict;
6018 d->dv_used_prev = NULL;
6019
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006021 d->dv_lock = 0;
6022 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006023 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006024 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006025 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006026}
6027
6028/*
6029 * Unreference a Dictionary: decrement the reference count and free it when it
6030 * becomes zero.
6031 */
6032 static void
6033dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006035{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006036 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6037 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038}
6039
6040/*
6041 * Free a Dictionary, including all items it contains.
6042 * Ignores the reference count.
6043 */
6044 static void
6045dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006047{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006050 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006051
Bram Moolenaard9fba312005-06-26 22:34:35 +00006052 /* Avoid that recursive reference to the dict frees us again. */
6053 d->dv_refcount = DEL_REFCOUNT;
6054
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 /* Remove the dict from the list of dicts for garbage collection. */
6056 if (d->dv_used_prev == NULL)
6057 first_dict = d->dv_used_next;
6058 else
6059 d->dv_used_prev->dv_used_next = d->dv_used_next;
6060 if (d->dv_used_next != NULL)
6061 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6062
6063 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006064 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 todo = d->dv_hashtab.ht_used;
6066 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006067 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006068 if (!HASHITEM_EMPTY(hi))
6069 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006070 /* Remove the item before deleting it, just in case there is
6071 * something recursive causing trouble. */
6072 di = HI2DI(hi);
6073 hash_remove(&d->dv_hashtab, hi);
6074 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006075 --todo;
6076 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006079 vim_free(d);
6080}
6081
6082/*
6083 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006084 * The "key" is copied to the new item.
6085 * Note that the value of the item "di_tv" still needs to be initialized!
6086 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006087 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006089dictitem_alloc(key)
6090 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006091{
Bram Moolenaar33570922005-01-25 22:26:29 +00006092 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006093
Bram Moolenaar33570922005-01-25 22:26:29 +00006094 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006095 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006097 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006098 di->di_flags = 0;
6099 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006101}
6102
6103/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006104 * Make a copy of a Dictionary item.
6105 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006109{
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006111
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006113 if (di != NULL)
6114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006116 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006117 copy_tv(&org->di_tv, &di->di_tv);
6118 }
6119 return di;
6120}
6121
6122/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 * Remove item "item" from Dictionary "dict" and free it.
6124 */
6125 static void
6126dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 dict_T *dict;
6128 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006129{
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006131
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 if (HASHITEM_EMPTY(hi))
6134 EMSG2(_(e_intern2), "dictitem_remove()");
6135 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 dictitem_free(item);
6138}
6139
6140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006141 * Free a dict item. Also clears the value.
6142 */
6143 static void
6144dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006147 clear_tv(&item->di_tv);
6148 vim_free(item);
6149}
6150
6151/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006152 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6153 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006154 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006155 * Returns NULL when out of memory.
6156 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006158dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006160 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006161 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162{
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 dict_T *copy;
6164 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006167
6168 if (orig == NULL)
6169 return NULL;
6170
6171 copy = dict_alloc();
6172 if (copy != NULL)
6173 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006174 if (copyID != 0)
6175 {
6176 orig->dv_copyID = copyID;
6177 orig->dv_copydict = copy;
6178 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006180 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184 --todo;
6185
6186 di = dictitem_alloc(hi->hi_key);
6187 if (di == NULL)
6188 break;
6189 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006190 {
6191 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6192 copyID) == FAIL)
6193 {
6194 vim_free(di);
6195 break;
6196 }
6197 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006198 else
6199 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6200 if (dict_add(copy, di) == FAIL)
6201 {
6202 dictitem_free(di);
6203 break;
6204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006209 if (todo > 0)
6210 {
6211 dict_unref(copy);
6212 copy = NULL;
6213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006214 }
6215
6216 return copy;
6217}
6218
6219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006220 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006222 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006223 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006224dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 dict_T *d;
6226 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006227{
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006229}
6230
Bram Moolenaar8c711452005-01-14 21:53:12 +00006231/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006232 * Add a number or string entry to dictionary "d".
6233 * When "str" is NULL use number "nr", otherwise use "str".
6234 * Returns FAIL when out of memory and when key already exists.
6235 */
6236 int
6237dict_add_nr_str(d, key, nr, str)
6238 dict_T *d;
6239 char *key;
6240 long nr;
6241 char_u *str;
6242{
6243 dictitem_T *item;
6244
6245 item = dictitem_alloc((char_u *)key);
6246 if (item == NULL)
6247 return FAIL;
6248 item->di_tv.v_lock = 0;
6249 if (str == NULL)
6250 {
6251 item->di_tv.v_type = VAR_NUMBER;
6252 item->di_tv.vval.v_number = nr;
6253 }
6254 else
6255 {
6256 item->di_tv.v_type = VAR_STRING;
6257 item->di_tv.vval.v_string = vim_strsave(str);
6258 }
6259 if (dict_add(d, item) == FAIL)
6260 {
6261 dictitem_free(item);
6262 return FAIL;
6263 }
6264 return OK;
6265}
6266
6267/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006268 * Get the number of items in a Dictionary.
6269 */
6270 static long
6271dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006273{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 if (d == NULL)
6275 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277}
6278
6279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280 * Find item "key[len]" in Dictionary "d".
6281 * If "len" is negative use strlen(key).
6282 * Returns NULL when not found.
6283 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287 char_u *key;
6288 int len;
6289{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006290#define AKEYLEN 200
6291 char_u buf[AKEYLEN];
6292 char_u *akey;
6293 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006295
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 if (len < 0)
6297 akey = key;
6298 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 tofree = akey = vim_strnsave(key, len);
6301 if (akey == NULL)
6302 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006304 else
6305 {
6306 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006307 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006308 akey = buf;
6309 }
6310
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006312 vim_free(tofree);
6313 if (HASHITEM_EMPTY(hi))
6314 return NULL;
6315 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316}
6317
6318/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006319 * Get a string item from a dictionary in allocated memory.
6320 * Returns NULL if the entry doesn't exist or out of memory.
6321 */
6322 char_u *
6323get_dict_string(d, key)
6324 dict_T *d;
6325 char_u *key;
6326{
6327 dictitem_T *di;
6328
6329 di = dict_find(d, key, -1);
6330 if (di == NULL)
6331 return NULL;
6332 return vim_strsave(get_tv_string(&di->di_tv));
6333}
6334
6335/*
6336 * Get a number item from a dictionary.
6337 * Returns 0 if the entry doesn't exist or out of memory.
6338 */
6339 long
6340get_dict_number(d, key)
6341 dict_T *d;
6342 char_u *key;
6343{
6344 dictitem_T *di;
6345
6346 di = dict_find(d, key, -1);
6347 if (di == NULL)
6348 return 0;
6349 return get_tv_number(&di->di_tv);
6350}
6351
6352/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 * Return an allocated string with the string representation of a Dictionary.
6354 * May return NULL.
6355 */
6356 static char_u *
6357dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006359{
6360 garray_T ga;
6361 int first = TRUE;
6362 char_u *tofree;
6363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370 return NULL;
6371 ga_init2(&ga, (int)sizeof(char), 80);
6372 ga_append(&ga, '{');
6373
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006375 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006377 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006378 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 --todo;
6380
6381 if (first)
6382 first = FALSE;
6383 else
6384 ga_concat(&ga, (char_u *)", ");
6385
6386 tofree = string_quote(hi->hi_key, FALSE);
6387 if (tofree != NULL)
6388 {
6389 ga_concat(&ga, tofree);
6390 vim_free(tofree);
6391 }
6392 ga_concat(&ga, (char_u *)": ");
6393 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6394 if (s != NULL)
6395 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006396 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006397 if (s == NULL)
6398 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006400 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006401 if (todo > 0)
6402 {
6403 vim_free(ga.ga_data);
6404 return NULL;
6405 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406
6407 ga_append(&ga, '}');
6408 ga_append(&ga, NUL);
6409 return (char_u *)ga.ga_data;
6410}
6411
6412/*
6413 * Allocate a variable for a Dictionary and fill it from "*arg".
6414 * Return OK or FAIL. Returns NOTDONE for {expr}.
6415 */
6416 static int
6417get_dict_tv(arg, rettv, evaluate)
6418 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420 int evaluate;
6421{
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 dict_T *d = NULL;
6423 typval_T tvkey;
6424 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006428 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429
6430 /*
6431 * First check if it's not a curly-braces thing: {expr}.
6432 * Must do this without evaluating, otherwise a function may be called
6433 * twice. Unfortunately this means we need to call eval1() twice for the
6434 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006436 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 if (*start != '}')
6438 {
6439 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6440 return FAIL;
6441 if (*start == '}')
6442 return NOTDONE;
6443 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006444
6445 if (evaluate)
6446 {
6447 d = dict_alloc();
6448 if (d == NULL)
6449 return FAIL;
6450 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006451 tvkey.v_type = VAR_UNKNOWN;
6452 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453
6454 *arg = skipwhite(*arg + 1);
6455 while (**arg != '}' && **arg != NUL)
6456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006457 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 goto failret;
6459 if (**arg != ':')
6460 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006461 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006462 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 goto failret;
6464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006465 key = get_tv_string_buf_chk(&tvkey, buf);
6466 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006468 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6469 if (key != NULL)
6470 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006471 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006472 goto failret;
6473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006474
6475 *arg = skipwhite(*arg + 1);
6476 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006478 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006479 goto failret;
6480 }
6481 if (evaluate)
6482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006483 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 if (item != NULL)
6485 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006486 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006488 clear_tv(&tv);
6489 goto failret;
6490 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006491 item = dictitem_alloc(key);
6492 clear_tv(&tvkey);
6493 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006496 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006497 if (dict_add(d, item) == FAIL)
6498 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 }
6500 }
6501
6502 if (**arg == '}')
6503 break;
6504 if (**arg != ',')
6505 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006506 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507 goto failret;
6508 }
6509 *arg = skipwhite(*arg + 1);
6510 }
6511
6512 if (**arg != '}')
6513 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006514 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006515failret:
6516 if (evaluate)
6517 dict_free(d);
6518 return FAIL;
6519 }
6520
6521 *arg = skipwhite(*arg + 1);
6522 if (evaluate)
6523 {
6524 rettv->v_type = VAR_DICT;
6525 rettv->vval.v_dict = d;
6526 ++d->dv_refcount;
6527 }
6528
6529 return OK;
6530}
6531
Bram Moolenaar8c711452005-01-14 21:53:12 +00006532/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 * Return a string with the string representation of a variable.
6534 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 * May return NULL;
6538 */
6539 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006540echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 static int recurse = 0;
6546 char_u *r = NULL;
6547
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006550 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 *tofree = NULL;
6552 return NULL;
6553 }
6554 ++recurse;
6555
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 switch (tv->v_type)
6557 {
6558 case VAR_FUNC:
6559 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006560 r = tv->vval.v_string;
6561 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 case VAR_LIST:
6563 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006564 r = *tofree;
6565 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566 case VAR_DICT:
6567 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006568 r = *tofree;
6569 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 case VAR_STRING:
6571 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006572 *tofree = NULL;
6573 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006576 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006577 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006579
6580 --recurse;
6581 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582}
6583
6584/*
6585 * Return a string with the string representation of a variable.
6586 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6587 * "numbuf" is used for a number.
6588 * Puts quotes around strings, so that they can be parsed back by eval().
6589 * May return NULL;
6590 */
6591 static char_u *
6592tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006593 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006594 char_u **tofree;
6595 char_u *numbuf;
6596{
6597 switch (tv->v_type)
6598 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006599 case VAR_FUNC:
6600 *tofree = string_quote(tv->vval.v_string, TRUE);
6601 return *tofree;
6602 case VAR_STRING:
6603 *tofree = string_quote(tv->vval.v_string, FALSE);
6604 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006606 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006609 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006610 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006612 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613}
6614
6615/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 * Return string "str" in ' quotes, doubling ' characters.
6617 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006619 */
6620 static char_u *
6621string_quote(str, function)
6622 char_u *str;
6623 int function;
6624{
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006626 char_u *p, *r, *s;
6627
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 len = (function ? 13 : 3);
6629 if (str != NULL)
6630 {
6631 len += STRLEN(str);
6632 for (p = str; *p != NUL; mb_ptr_adv(p))
6633 if (*p == '\'')
6634 ++len;
6635 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006636 s = r = alloc(len);
6637 if (r != NULL)
6638 {
6639 if (function)
6640 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006642 r += 10;
6643 }
6644 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 if (str != NULL)
6647 for (p = str; *p != NUL; )
6648 {
6649 if (*p == '\'')
6650 *r++ = '\'';
6651 MB_COPY_CHAR(p, r);
6652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006654 if (function)
6655 *r++ = ')';
6656 *r++ = NUL;
6657 }
6658 return s;
6659}
6660
6661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 * Get the value of an environment variable.
6663 * "arg" is pointing to the '$'. It is advanced to after the name.
6664 * If the environment variable was not set, silently assume it is empty.
6665 * Always return OK.
6666 */
6667 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006668get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 int evaluate;
6672{
6673 char_u *string = NULL;
6674 int len;
6675 int cc;
6676 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006677 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 ++*arg;
6680 name = *arg;
6681 len = get_env_len(arg);
6682 if (evaluate)
6683 {
6684 if (len != 0)
6685 {
6686 cc = name[len];
6687 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006688 /* first try vim_getenv(), fast for normal environment vars */
6689 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006691 {
6692 if (!mustfree)
6693 string = vim_strsave(string);
6694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 else
6696 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006697 if (mustfree)
6698 vim_free(string);
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 /* next try expanding things like $VIM and ${HOME} */
6701 string = expand_env_save(name - 1);
6702 if (string != NULL && *string == '$')
6703 {
6704 vim_free(string);
6705 string = NULL;
6706 }
6707 }
6708 name[len] = cc;
6709 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006710 rettv->v_type = VAR_STRING;
6711 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 }
6713
6714 return OK;
6715}
6716
6717/*
6718 * Array with names and number of arguments of all internal functions
6719 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6720 */
6721static struct fst
6722{
6723 char *f_name; /* function name */
6724 char f_min_argc; /* minimal number of arguments */
6725 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006726 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728} functions[] =
6729{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006730 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {"append", 2, 2, f_append},
6732 {"argc", 0, 0, f_argc},
6733 {"argidx", 0, 0, f_argidx},
6734 {"argv", 1, 1, f_argv},
6735 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006736 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {"bufexists", 1, 1, f_bufexists},
6738 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6739 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6740 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6741 {"buflisted", 1, 1, f_buflisted},
6742 {"bufloaded", 1, 1, f_bufloaded},
6743 {"bufname", 1, 1, f_bufname},
6744 {"bufnr", 1, 1, f_bufnr},
6745 {"bufwinnr", 1, 1, f_bufwinnr},
6746 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006747 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006748 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {"char2nr", 1, 1, f_char2nr},
6750 {"cindent", 1, 1, f_cindent},
6751 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006752#if defined(FEAT_INS_EXPAND)
6753 {"complete_add", 1, 1, f_complete_add},
6754 {"complete_check", 0, 0, f_complete_check},
6755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"cscope_connection",0,3, f_cscope_connection},
6760 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006761 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {"delete", 1, 1, f_delete},
6763 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006764 {"diff_filler", 1, 1, f_diff_filler},
6765 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006766 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {"eventhandler", 0, 0, f_eventhandler},
6770 {"executable", 1, 1, f_executable},
6771 {"exists", 1, 1, f_exists},
6772 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006773 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6775 {"filereadable", 1, 1, f_filereadable},
6776 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006778 {"finddir", 1, 3, f_finddir},
6779 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {"fnamemodify", 2, 2, f_fnamemodify},
6781 {"foldclosed", 1, 1, f_foldclosed},
6782 {"foldclosedend", 1, 1, f_foldclosedend},
6783 {"foldlevel", 1, 1, f_foldlevel},
6784 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006785 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006787 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006788 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006789 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006790 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {"getbufvar", 2, 2, f_getbufvar},
6792 {"getchar", 0, 1, f_getchar},
6793 {"getcharmod", 0, 0, f_getcharmod},
6794 {"getcmdline", 0, 0, f_getcmdline},
6795 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006796 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006798 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006799 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {"getfsize", 1, 1, f_getfsize},
6801 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006802 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006803 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006804 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006805 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {"getregtype", 0, 1, f_getregtype},
6807 {"getwinposx", 0, 0, f_getwinposx},
6808 {"getwinposy", 0, 0, f_getwinposy},
6809 {"getwinvar", 2, 2, f_getwinvar},
6810 {"glob", 1, 1, f_glob},
6811 {"globpath", 2, 2, f_globpath},
6812 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {"hasmapto", 1, 2, f_hasmapto},
6815 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6816 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6817 {"histadd", 2, 2, f_histadd},
6818 {"histdel", 1, 2, f_histdel},
6819 {"histget", 1, 2, f_histget},
6820 {"histnr", 1, 1, f_histnr},
6821 {"hlID", 1, 1, f_hlID},
6822 {"hlexists", 1, 1, f_hlexists},
6823 {"hostname", 0, 0, f_hostname},
6824 {"iconv", 3, 3, f_iconv},
6825 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006827 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006829 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {"inputrestore", 0, 0, f_inputrestore},
6831 {"inputsave", 0, 0, f_inputsave},
6832 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006835 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006836 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006837 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {"libcall", 3, 3, f_libcall},
6842 {"libcallnr", 3, 3, f_libcallnr},
6843 {"line", 1, 1, f_line},
6844 {"line2byte", 1, 1, f_line2byte},
6845 {"lispindent", 1, 1, f_lispindent},
6846 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006847 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {"maparg", 1, 2, f_maparg},
6849 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006850 {"match", 2, 4, f_match},
6851 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006852 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006853 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006854 {"max", 1, 1, f_max},
6855 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006856#ifdef vim_mkdir
6857 {"mkdir", 1, 3, f_mkdir},
6858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"mode", 0, 0, f_mode},
6860 {"nextnonblank", 1, 1, f_nextnonblank},
6861 {"nr2char", 1, 1, f_nr2char},
6862 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006863 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006864 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006865 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {"remote_expr", 2, 3, f_remote_expr},
6867 {"remote_foreground", 1, 1, f_remote_foreground},
6868 {"remote_peek", 1, 2, f_remote_peek},
6869 {"remote_read", 1, 1, f_remote_read},
6870 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006871 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006873 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006875 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006877 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {"searchpair", 3, 5, f_searchpair},
6879 {"server2client", 2, 2, f_server2client},
6880 {"serverlist", 0, 0, f_serverlist},
6881 {"setbufvar", 3, 3, f_setbufvar},
6882 {"setcmdpos", 1, 1, f_setcmdpos},
6883 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006884 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {"setreg", 2, 3, f_setreg},
6886 {"setwinvar", 3, 3, f_setwinvar},
6887 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006888 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006889 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006890 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006891 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006892 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893#ifdef HAVE_STRFTIME
6894 {"strftime", 1, 2, f_strftime},
6895#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006896 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006897 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {"strlen", 1, 1, f_strlen},
6899 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006900 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"strtrans", 1, 1, f_strtrans},
6902 {"submatch", 1, 1, f_submatch},
6903 {"substitute", 4, 4, f_substitute},
6904 {"synID", 3, 3, f_synID},
6905 {"synIDattr", 2, 3, f_synIDattr},
6906 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006907 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006908 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006909 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006911 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {"tolower", 1, 1, f_tolower},
6913 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006914 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006916 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {"virtcol", 1, 1, f_virtcol},
6918 {"visualmode", 0, 1, f_visualmode},
6919 {"winbufnr", 1, 1, f_winbufnr},
6920 {"wincol", 0, 0, f_wincol},
6921 {"winheight", 1, 1, f_winheight},
6922 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006923 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {"winrestcmd", 0, 0, f_winrestcmd},
6925 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006926 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927};
6928
6929#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6930
6931/*
6932 * Function given to ExpandGeneric() to obtain the list of internal
6933 * or user defined function names.
6934 */
6935 char_u *
6936get_function_name(xp, idx)
6937 expand_T *xp;
6938 int idx;
6939{
6940 static int intidx = -1;
6941 char_u *name;
6942
6943 if (idx == 0)
6944 intidx = -1;
6945 if (intidx < 0)
6946 {
6947 name = get_user_func_name(xp, idx);
6948 if (name != NULL)
6949 return name;
6950 }
6951 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6952 {
6953 STRCPY(IObuff, functions[intidx].f_name);
6954 STRCAT(IObuff, "(");
6955 if (functions[intidx].f_max_argc == 0)
6956 STRCAT(IObuff, ")");
6957 return IObuff;
6958 }
6959
6960 return NULL;
6961}
6962
6963/*
6964 * Function given to ExpandGeneric() to obtain the list of internal or
6965 * user defined variable or function names.
6966 */
6967/*ARGSUSED*/
6968 char_u *
6969get_expr_name(xp, idx)
6970 expand_T *xp;
6971 int idx;
6972{
6973 static int intidx = -1;
6974 char_u *name;
6975
6976 if (idx == 0)
6977 intidx = -1;
6978 if (intidx < 0)
6979 {
6980 name = get_function_name(xp, idx);
6981 if (name != NULL)
6982 return name;
6983 }
6984 return get_user_var_name(xp, ++intidx);
6985}
6986
6987#endif /* FEAT_CMDL_COMPL */
6988
6989/*
6990 * Find internal function in table above.
6991 * Return index, or -1 if not found
6992 */
6993 static int
6994find_internal_func(name)
6995 char_u *name; /* name of the function */
6996{
6997 int first = 0;
6998 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6999 int cmp;
7000 int x;
7001
7002 /*
7003 * Find the function name in the table. Binary search.
7004 */
7005 while (first <= last)
7006 {
7007 x = first + ((unsigned)(last - first) >> 1);
7008 cmp = STRCMP(name, functions[x].f_name);
7009 if (cmp < 0)
7010 last = x - 1;
7011 else if (cmp > 0)
7012 first = x + 1;
7013 else
7014 return x;
7015 }
7016 return -1;
7017}
7018
7019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007020 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7021 * name it contains, otherwise return "name".
7022 */
7023 static char_u *
7024deref_func_name(name, lenp)
7025 char_u *name;
7026 int *lenp;
7027{
Bram Moolenaar33570922005-01-25 22:26:29 +00007028 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007029 int cc;
7030
7031 cc = name[*lenp];
7032 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007033 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007034 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007036 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007038 {
7039 *lenp = 0;
7040 return (char_u *)""; /* just in case */
7041 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007042 *lenp = STRLEN(v->di_tv.vval.v_string);
7043 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007044 }
7045
7046 return name;
7047}
7048
7049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 * Allocate a variable for the result of a function.
7051 * Return OK or FAIL.
7052 */
7053 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007054get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7055 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 char_u *name; /* name of the function */
7057 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 char_u **arg; /* argument, pointing to the '(' */
7060 linenr_T firstline; /* first line of range */
7061 linenr_T lastline; /* last line of range */
7062 int *doesrange; /* return: function handled range */
7063 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007064 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 char_u *argp;
7067 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007068 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 int argcount = 0; /* number of arguments found */
7070
7071 /*
7072 * Get the arguments.
7073 */
7074 argp = *arg;
7075 while (argcount < MAX_FUNC_ARGS)
7076 {
7077 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7078 if (*argp == ')' || *argp == ',' || *argp == NUL)
7079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7081 {
7082 ret = FAIL;
7083 break;
7084 }
7085 ++argcount;
7086 if (*argp != ',')
7087 break;
7088 }
7089 if (*argp == ')')
7090 ++argp;
7091 else
7092 ret = FAIL;
7093
7094 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007095 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 {
7099 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007100 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007102 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
7105 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007106 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 *arg = skipwhite(argp);
7109 return ret;
7110}
7111
7112
7113/*
7114 * Call a function with its resolved parameters
7115 * Return OK or FAIL.
7116 */
7117 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007118call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 char_u *name; /* name of the function */
7121 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 linenr_T firstline; /* first line of range */
7126 linenr_T lastline; /* last line of range */
7127 int *doesrange; /* return: function handled range */
7128 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132#define ERROR_UNKNOWN 0
7133#define ERROR_TOOMANY 1
7134#define ERROR_TOOFEW 2
7135#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136#define ERROR_DICT 4
7137#define ERROR_NONE 5
7138#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 int error = ERROR_NONE;
7140 int i;
7141 int llen;
7142 ufunc_T *fp;
7143 int cc;
7144#define FLEN_FIXED 40
7145 char_u fname_buf[FLEN_FIXED + 1];
7146 char_u *fname;
7147
7148 /*
7149 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7150 * Change <SNR>123_name() to K_SNR 123_name().
7151 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7152 */
7153 cc = name[len];
7154 name[len] = NUL;
7155 llen = eval_fname_script(name);
7156 if (llen > 0)
7157 {
7158 fname_buf[0] = K_SPECIAL;
7159 fname_buf[1] = KS_EXTRA;
7160 fname_buf[2] = (int)KE_SNR;
7161 i = 3;
7162 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7163 {
7164 if (current_SID <= 0)
7165 error = ERROR_SCRIPT;
7166 else
7167 {
7168 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7169 i = (int)STRLEN(fname_buf);
7170 }
7171 }
7172 if (i + STRLEN(name + llen) < FLEN_FIXED)
7173 {
7174 STRCPY(fname_buf + i, name + llen);
7175 fname = fname_buf;
7176 }
7177 else
7178 {
7179 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7180 if (fname == NULL)
7181 error = ERROR_OTHER;
7182 else
7183 {
7184 mch_memmove(fname, fname_buf, (size_t)i);
7185 STRCPY(fname + i, name + llen);
7186 }
7187 }
7188 }
7189 else
7190 fname = name;
7191
7192 *doesrange = FALSE;
7193
7194
7195 /* execute the function if no errors detected and executing */
7196 if (evaluate && error == ERROR_NONE)
7197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007198 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 error = ERROR_UNKNOWN;
7200
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007201 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 /*
7204 * User defined function.
7205 */
7206 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007209 /* Trigger FuncUndefined event, may load the function. */
7210 if (fp == NULL
7211 && apply_autocmds(EVENT_FUNCUNDEFINED,
7212 fname, fname, TRUE, NULL)
7213 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007215 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 fp = find_func(fname);
7217 }
7218#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007219 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007220 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007221 {
7222 /* loaded a package, search for the function again */
7223 fp = find_func(fname);
7224 }
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (fp != NULL)
7227 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007228 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007230 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007232 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007234 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 else
7237 {
7238 /*
7239 * Call the user function.
7240 * Save and restore search patterns, script variables and
7241 * redo buffer.
7242 */
7243 save_search_patterns();
7244 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007245 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007248 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7249 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7250 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007251 /* Function was unreferenced while being used, free it
7252 * now. */
7253 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 restoreRedobuff();
7255 restore_search_patterns();
7256 error = ERROR_NONE;
7257 }
7258 }
7259 }
7260 else
7261 {
7262 /*
7263 * Find the function name in the table, call its implementation.
7264 */
7265 i = find_internal_func(fname);
7266 if (i >= 0)
7267 {
7268 if (argcount < functions[i].f_min_argc)
7269 error = ERROR_TOOFEW;
7270 else if (argcount > functions[i].f_max_argc)
7271 error = ERROR_TOOMANY;
7272 else
7273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007275 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 error = ERROR_NONE;
7277 }
7278 }
7279 }
7280 /*
7281 * The function call (or "FuncUndefined" autocommand sequence) might
7282 * have been aborted by an error, an interrupt, or an explicitly thrown
7283 * exception that has not been caught so far. This situation can be
7284 * tested for by calling aborting(). For an error in an internal
7285 * function or for the "E132" error in call_user_func(), however, the
7286 * throw point at which the "force_abort" flag (temporarily reset by
7287 * emsg()) is normally updated has not been reached yet. We need to
7288 * update that flag first to make aborting() reliable.
7289 */
7290 update_force_abort();
7291 }
7292 if (error == ERROR_NONE)
7293 ret = OK;
7294
7295 /*
7296 * Report an error unless the argument evaluation or function call has been
7297 * cancelled due to an aborting error, an interrupt, or an exception.
7298 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 if (!aborting())
7300 {
7301 switch (error)
7302 {
7303 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007304 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 break;
7306 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007307 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 break;
7309 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 name);
7312 break;
7313 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007314 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 name);
7316 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007318 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 name);
7320 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 }
7322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
7324 name[len] = cc;
7325 if (fname != name && fname != fname_buf)
7326 vim_free(fname);
7327
7328 return ret;
7329}
7330
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007331/*
7332 * Give an error message with a function name. Handle <SNR> things.
7333 */
7334 static void
7335emsg_funcname(msg, name)
7336 char *msg;
7337 char_u *name;
7338{
7339 char_u *p;
7340
7341 if (*name == K_SPECIAL)
7342 p = concat_str((char_u *)"<SNR>", name + 3);
7343 else
7344 p = name;
7345 EMSG2(_(msg), p);
7346 if (p != name)
7347 vim_free(p);
7348}
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350/*********************************************
7351 * Implementation of the built-in functions
7352 */
7353
7354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007355 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 */
7357 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007358f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 typval_T *argvars;
7360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007364 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007365 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007367 if ((l = argvars[0].vval.v_list) != NULL
7368 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7369 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007370 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007371 }
7372 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007373 EMSG(_(e_listreq));
7374}
7375
7376/*
7377 * "append(lnum, string/list)" function
7378 */
7379 static void
7380f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 typval_T *argvars;
7382 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007383{
7384 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007385 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 list_T *l = NULL;
7387 listitem_T *li = NULL;
7388 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007389 long added = 0;
7390
Bram Moolenaar0d660222005-01-07 21:51:51 +00007391 lnum = get_tv_lnum(argvars);
7392 if (lnum >= 0
7393 && lnum <= curbuf->b_ml.ml_line_count
7394 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007396 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007398 l = argvars[1].vval.v_list;
7399 if (l == NULL)
7400 return;
7401 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007403 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007404 for (;;)
7405 {
7406 if (l == NULL)
7407 tv = &argvars[1]; /* append a string */
7408 else if (li == NULL)
7409 break; /* end of list */
7410 else
7411 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007412 line = get_tv_string_chk(tv);
7413 if (line == NULL) /* type error */
7414 {
7415 rettv->vval.v_number = 1; /* Failed */
7416 break;
7417 }
7418 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007419 ++added;
7420 if (l == NULL)
7421 break;
7422 li = li->li_next;
7423 }
7424
7425 appended_lines_mark(lnum, added);
7426 if (curwin->w_cursor.lnum > lnum)
7427 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007429 else
7430 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432
7433/*
7434 * "argc()" function
7435 */
7436/* ARGSUSED */
7437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007438f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 typval_T *argvars;
7440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443}
7444
7445/*
7446 * "argidx()" function
7447 */
7448/* ARGSUSED */
7449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 typval_T *argvars;
7452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}
7456
7457/*
7458 * "argv(nr)" function
7459 */
7460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 typval_T *argvars;
7463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465 int idx;
7466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007467 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471 rettv->vval.v_string = NULL;
7472 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473}
7474
7475/*
7476 * "browse(save, title, initdir, default)" function
7477 */
7478/* ARGSUSED */
7479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007480f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 typval_T *argvars;
7482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
7484#ifdef FEAT_BROWSE
7485 int save;
7486 char_u *title;
7487 char_u *initdir;
7488 char_u *defname;
7489 char_u buf[NUMBUFLEN];
7490 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007491 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007493 save = get_tv_number_chk(&argvars[0], &error);
7494 title = get_tv_string_chk(&argvars[1]);
7495 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7496 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007498 if (error || title == NULL || initdir == NULL || defname == NULL)
7499 rettv->vval.v_string = NULL;
7500 else
7501 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007502 do_browse(save ? BROWSE_SAVE : 0,
7503 title, defname, NULL, initdir, NULL, curbuf);
7504#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007505 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007506#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007508}
7509
7510/*
7511 * "browsedir(title, initdir)" function
7512 */
7513/* ARGSUSED */
7514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007515f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 typval_T *argvars;
7517 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007518{
7519#ifdef FEAT_BROWSE
7520 char_u *title;
7521 char_u *initdir;
7522 char_u buf[NUMBUFLEN];
7523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007524 title = get_tv_string_chk(&argvars[0]);
7525 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 if (title == NULL || initdir == NULL)
7528 rettv->vval.v_string = NULL;
7529 else
7530 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007531 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007533 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536}
7537
Bram Moolenaar33570922005-01-25 22:26:29 +00007538static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007539
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540/*
7541 * Find a buffer by number or exact name.
7542 */
7543 static buf_T *
7544find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007549 if (avar->v_type == VAR_NUMBER)
7550 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007551 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007553 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007554 if (buf == NULL)
7555 {
7556 /* No full path name match, try a match with a URL or a "nofile"
7557 * buffer, these don't use the full path. */
7558 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7559 if (buf->b_fname != NULL
7560 && (path_with_url(buf->b_fname)
7561#ifdef FEAT_QUICKFIX
7562 || bt_nofile(buf)
7563#endif
7564 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007566 break;
7567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
7569 return buf;
7570}
7571
7572/*
7573 * "bufexists(expr)" function
7574 */
7575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 typval_T *argvars;
7578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581}
7582
7583/*
7584 * "buflisted(expr)" function
7585 */
7586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 typval_T *argvars;
7589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
7591 buf_T *buf;
7592
7593 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595}
7596
7597/*
7598 * "bufloaded(expr)" function
7599 */
7600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 typval_T *argvars;
7603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 buf_T *buf;
7606
7607 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609}
7610
Bram Moolenaar33570922005-01-25 22:26:29 +00007611static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007612
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613/*
7614 * Get buffer by number or pattern.
7615 */
7616 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 int save_magic;
7622 char_u *save_cpo;
7623 buf_T *buf;
7624
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 if (tv->v_type == VAR_NUMBER)
7626 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007627 if (tv->v_type != VAR_STRING)
7628 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (name == NULL || *name == NUL)
7630 return curbuf;
7631 if (name[0] == '$' && name[1] == NUL)
7632 return lastbuf;
7633
7634 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7635 save_magic = p_magic;
7636 p_magic = TRUE;
7637 save_cpo = p_cpo;
7638 p_cpo = (char_u *)"";
7639
7640 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7641 TRUE, FALSE));
7642
7643 p_magic = save_magic;
7644 p_cpo = save_cpo;
7645
7646 /* If not found, try expanding the name, like done for bufexists(). */
7647 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650 return buf;
7651}
7652
7653/*
7654 * "bufname(expr)" function
7655 */
7656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 typval_T *argvars;
7659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 buf_T *buf;
7662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007663 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 buf = get_buf_tv(&argvars[0]);
7666 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 --emsg_off;
7672}
7673
7674/*
7675 * "bufnr(expr)" function
7676 */
7677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
7682 buf_T *buf;
7683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 --emsg_off;
7692}
7693
7694/*
7695 * "bufwinnr(nr)" function
7696 */
7697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 typval_T *argvars;
7700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702#ifdef FEAT_WINDOWS
7703 win_T *wp;
7704 int winnr = 0;
7705#endif
7706 buf_T *buf;
7707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007708 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711#ifdef FEAT_WINDOWS
7712 for (wp = firstwin; wp; wp = wp->w_next)
7713 {
7714 ++winnr;
7715 if (wp->w_buffer == buf)
7716 break;
7717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721#endif
7722 --emsg_off;
7723}
7724
7725/*
7726 * "byte2line(byte)" function
7727 */
7728/*ARGSUSED*/
7729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007730f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 typval_T *argvars;
7732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736#else
7737 long boff = 0;
7738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 (linenr_T)0, &boff);
7745#endif
7746}
7747
7748/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007749 * "byteidx()" function
7750 */
7751/*ARGSUSED*/
7752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 typval_T *argvars;
7755 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007756{
7757#ifdef FEAT_MBYTE
7758 char_u *t;
7759#endif
7760 char_u *str;
7761 long idx;
7762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 str = get_tv_string_chk(&argvars[0]);
7764 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007767 return;
7768
7769#ifdef FEAT_MBYTE
7770 t = str;
7771 for ( ; idx > 0; idx--)
7772 {
7773 if (*t == NUL) /* EOL reached */
7774 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007775 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007778#else
7779 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007781#endif
7782}
7783
7784/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007785 * "call(func, arglist)" function
7786 */
7787 static void
7788f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 typval_T *argvars;
7790 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791{
7792 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007794 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007795 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007796 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007798
7799 rettv->vval.v_number = 0;
7800 if (argvars[1].v_type != VAR_LIST)
7801 {
7802 EMSG(_(e_listreq));
7803 return;
7804 }
7805 if (argvars[1].vval.v_list == NULL)
7806 return;
7807
7808 if (argvars[0].v_type == VAR_FUNC)
7809 func = argvars[0].vval.v_string;
7810 else
7811 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007812 if (*func == NUL)
7813 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007814
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 if (argvars[2].v_type != VAR_UNKNOWN)
7816 {
7817 if (argvars[2].v_type != VAR_DICT)
7818 {
7819 EMSG(_(e_dictreq));
7820 return;
7821 }
7822 selfdict = argvars[2].vval.v_dict;
7823 }
7824
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007825 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7826 item = item->li_next)
7827 {
7828 if (argc == MAX_FUNC_ARGS)
7829 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007830 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007831 break;
7832 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007833 /* Make a copy of each argument. This is needed to be able to set
7834 * v_lock to VAR_FIXED in the copy without changing the original list.
7835 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007836 copy_tv(&item->li_tv, &argv[argc++]);
7837 }
7838
7839 if (item == NULL)
7840 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7842 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007843
7844 /* Free the arguments. */
7845 while (argc > 0)
7846 clear_tv(&argv[--argc]);
7847}
7848
7849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 * "char2nr(string)" function
7851 */
7852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007854 typval_T *argvars;
7855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857#ifdef FEAT_MBYTE
7858 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 else
7861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
7865/*
7866 * "cindent(lnum)" function
7867 */
7868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 typval_T *argvars;
7871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
7873#ifdef FEAT_CINDENT
7874 pos_T pos;
7875 linenr_T lnum;
7876
7877 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7880 {
7881 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 curwin->w_cursor = pos;
7884 }
7885 else
7886#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890/*
7891 * "col(string)" function
7892 */
7893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 typval_T *argvars;
7896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 colnr_T col = 0;
7899 pos_T *fp;
7900
7901 fp = var2fpos(&argvars[0], FALSE);
7902 if (fp != NULL)
7903 {
7904 if (fp->col == MAXCOL)
7905 {
7906 /* '> can be MAXCOL, get the length of the line then */
7907 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7908 col = STRLEN(ml_get(fp->lnum)) + 1;
7909 else
7910 col = MAXCOL;
7911 }
7912 else
7913 {
7914 col = fp->col + 1;
7915#ifdef FEAT_VIRTUALEDIT
7916 /* col(".") when the cursor is on the NUL at the end of the line
7917 * because of "coladd" can be seen as an extra column. */
7918 if (virtual_active() && fp == &curwin->w_cursor)
7919 {
7920 char_u *p = ml_get_cursor();
7921
7922 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7923 curwin->w_virtcol - curwin->w_cursor.coladd))
7924 {
7925# ifdef FEAT_MBYTE
7926 int l;
7927
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007928 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 col += l;
7930# else
7931 if (*p != NUL && p[1] == NUL)
7932 ++col;
7933# endif
7934 }
7935 }
7936#endif
7937 }
7938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940}
7941
Bram Moolenaar572cb562005-08-05 21:35:02 +00007942#if defined(FEAT_INS_EXPAND)
7943/*
7944 * "complete_add()" function
7945 */
7946/*ARGSUSED*/
7947 static void
7948f_complete_add(argvars, rettv)
7949 typval_T *argvars;
7950 typval_T *rettv;
7951{
7952 char_u *s;
7953
7954 s = get_tv_string_chk(&argvars[0]);
7955 if (s != NULL)
7956 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7957}
7958
7959/*
7960 * "complete_check()" function
7961 */
7962/*ARGSUSED*/
7963 static void
7964f_complete_check(argvars, rettv)
7965 typval_T *argvars;
7966 typval_T *rettv;
7967{
7968 int saved = RedrawingDisabled;
7969
7970 RedrawingDisabled = 0;
7971 ins_compl_check_keys(0);
7972 rettv->vval.v_number = compl_interrupted;
7973 RedrawingDisabled = saved;
7974}
7975#endif
7976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977/*
7978 * "confirm(message, buttons[, default [, type]])" function
7979 */
7980/*ARGSUSED*/
7981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007982f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007983 typval_T *argvars;
7984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7987 char_u *message;
7988 char_u *buttons = NULL;
7989 char_u buf[NUMBUFLEN];
7990 char_u buf2[NUMBUFLEN];
7991 int def = 1;
7992 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007993 char_u *typestr;
7994 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007996 message = get_tv_string_chk(&argvars[0]);
7997 if (message == NULL)
7998 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007999 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008001 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8002 if (buttons == NULL)
8003 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008006 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008009 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8010 if (typestr == NULL)
8011 error = TRUE;
8012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008014 switch (TOUPPER_ASC(*typestr))
8015 {
8016 case 'E': type = VIM_ERROR; break;
8017 case 'Q': type = VIM_QUESTION; break;
8018 case 'I': type = VIM_INFO; break;
8019 case 'W': type = VIM_WARNING; break;
8020 case 'G': type = VIM_GENERIC; break;
8021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
8023 }
8024 }
8025 }
8026
8027 if (buttons == NULL || *buttons == NUL)
8028 buttons = (char_u *)_("&Ok");
8029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008030 if (error)
8031 rettv->vval.v_number = 0;
8032 else
8033 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 def, NULL);
8035#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#endif
8038}
8039
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008040/*
8041 * "copy()" function
8042 */
8043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 typval_T *argvars;
8046 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008047{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008048 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050
8051/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008052 * "count()" function
8053 */
8054 static void
8055f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 typval_T *argvars;
8057 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008058{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008059 long n = 0;
8060 int ic = FALSE;
8061
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008063 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 listitem_T *li;
8065 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008066 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008067
Bram Moolenaare9a41262005-01-15 22:18:47 +00008068 if ((l = argvars[0].vval.v_list) != NULL)
8069 {
8070 li = l->lv_first;
8071 if (argvars[2].v_type != VAR_UNKNOWN)
8072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008073 int error = FALSE;
8074
8075 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008076 if (argvars[3].v_type != VAR_UNKNOWN)
8077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008078 idx = get_tv_number_chk(&argvars[3], &error);
8079 if (!error)
8080 {
8081 li = list_find(l, idx);
8082 if (li == NULL)
8083 EMSGN(_(e_listidx), idx);
8084 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008086 if (error)
8087 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008088 }
8089
8090 for ( ; li != NULL; li = li->li_next)
8091 if (tv_equal(&li->li_tv, &argvars[1], ic))
8092 ++n;
8093 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008095 else if (argvars[0].v_type == VAR_DICT)
8096 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 int todo;
8098 dict_T *d;
8099 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008101 if ((d = argvars[0].vval.v_dict) != NULL)
8102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008103 int error = FALSE;
8104
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105 if (argvars[2].v_type != VAR_UNKNOWN)
8106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008107 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 if (argvars[3].v_type != VAR_UNKNOWN)
8109 EMSG(_(e_invarg));
8110 }
8111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008112 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008113 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008114 {
8115 if (!HASHITEM_EMPTY(hi))
8116 {
8117 --todo;
8118 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8119 ++n;
8120 }
8121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008122 }
8123 }
8124 else
8125 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 rettv->vval.v_number = n;
8127}
8128
8129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8131 *
8132 * Checks the existence of a cscope connection.
8133 */
8134/*ARGSUSED*/
8135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008137 typval_T *argvars;
8138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140#ifdef FEAT_CSCOPE
8141 int num = 0;
8142 char_u *dbpath = NULL;
8143 char_u *prepend = NULL;
8144 char_u buf[NUMBUFLEN];
8145
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 if (argvars[0].v_type != VAR_UNKNOWN
8147 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 num = (int)get_tv_number(&argvars[0]);
8150 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008151 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
8154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158#endif
8159}
8160
8161/*
8162 * "cursor(lnum, col)" function
8163 *
8164 * Moves the cursor to the specified line and column
8165 */
8166/*ARGSUSED*/
8167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008168f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 typval_T *argvars;
8170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171{
8172 long line, col;
8173
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008175 col = get_tv_number_chk(&argvars[1], NULL);
8176 if (line < 0 || col < 0)
8177 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (line > 0)
8179 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (col > 0)
8181 curwin->w_cursor.col = col - 1;
8182#ifdef FEAT_VIRTUALEDIT
8183 curwin->w_cursor.coladd = 0;
8184#endif
8185
8186 /* Make sure the cursor is in a valid position. */
8187 check_cursor();
8188#ifdef FEAT_MBYTE
8189 /* Correct cursor for multi-byte character. */
8190 if (has_mbyte)
8191 mb_adjust_cursor();
8192#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008193
8194 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195}
8196
8197/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008198 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 */
8200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201f_deepcopy(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 Moolenaar81bf7082005-02-12 14:31:42 +00008205 int noref = 0;
8206
8207 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008208 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008209 if (noref < 0 || noref > 1)
8210 EMSG(_(e_invarg));
8211 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008212 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213}
8214
8215/*
8216 * "delete()" function
8217 */
8218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008220 typval_T *argvars;
8221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008226 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227}
8228
8229/*
8230 * "did_filetype()" function
8231 */
8232/*ARGSUSED*/
8233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *argvars;
8236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242#endif
8243}
8244
8245/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008246 * "diff_filler()" function
8247 */
8248/*ARGSUSED*/
8249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008250f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008251 typval_T *argvars;
8252 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008253{
8254#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008256#endif
8257}
8258
8259/*
8260 * "diff_hlID()" function
8261 */
8262/*ARGSUSED*/
8263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 typval_T *argvars;
8266 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008267{
8268#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008270 static linenr_T prev_lnum = 0;
8271 static int changedtick = 0;
8272 static int fnum = 0;
8273 static int change_start = 0;
8274 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008275 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008276 int filler_lines;
8277 int col;
8278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 if (lnum < 0) /* ignore type error in {lnum} arg */
8280 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008281 if (lnum != prev_lnum
8282 || changedtick != curbuf->b_changedtick
8283 || fnum != curbuf->b_fnum)
8284 {
8285 /* New line, buffer, change: need to get the values. */
8286 filler_lines = diff_check(curwin, lnum);
8287 if (filler_lines < 0)
8288 {
8289 if (filler_lines == -1)
8290 {
8291 change_start = MAXCOL;
8292 change_end = -1;
8293 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8294 hlID = HLF_ADD; /* added line */
8295 else
8296 hlID = HLF_CHD; /* changed line */
8297 }
8298 else
8299 hlID = HLF_ADD; /* added line */
8300 }
8301 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008302 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008303 prev_lnum = lnum;
8304 changedtick = curbuf->b_changedtick;
8305 fnum = curbuf->b_fnum;
8306 }
8307
8308 if (hlID == HLF_CHD || hlID == HLF_TXD)
8309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008310 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008311 if (col >= change_start && col <= change_end)
8312 hlID = HLF_TXD; /* changed text */
8313 else
8314 hlID = HLF_CHD; /* changed line */
8315 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008316 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008317#endif
8318}
8319
8320/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008321 * "empty({expr})" function
8322 */
8323 static void
8324f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 typval_T *argvars;
8326 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008327{
8328 int n;
8329
8330 switch (argvars[0].v_type)
8331 {
8332 case VAR_STRING:
8333 case VAR_FUNC:
8334 n = argvars[0].vval.v_string == NULL
8335 || *argvars[0].vval.v_string == NUL;
8336 break;
8337 case VAR_NUMBER:
8338 n = argvars[0].vval.v_number == 0;
8339 break;
8340 case VAR_LIST:
8341 n = argvars[0].vval.v_list == NULL
8342 || argvars[0].vval.v_list->lv_first == NULL;
8343 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 case VAR_DICT:
8345 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008348 default:
8349 EMSG2(_(e_intern2), "f_empty()");
8350 n = 0;
8351 }
8352
8353 rettv->vval.v_number = n;
8354}
8355
8356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 * "escape({string}, {chars})" function
8358 */
8359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008360f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 typval_T *argvars;
8362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363{
8364 char_u buf[NUMBUFLEN];
8365
Bram Moolenaar758711c2005-02-02 23:11:38 +00008366 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8367 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369}
8370
8371/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008372 * "eval()" function
8373 */
8374/*ARGSUSED*/
8375 static void
8376f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 typval_T *argvars;
8378 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008379{
8380 char_u *s;
8381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008382 s = get_tv_string_chk(&argvars[0]);
8383 if (s != NULL)
8384 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008386 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8387 {
8388 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008389 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008390 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008391 else if (*s != NUL)
8392 EMSG(_(e_trailing));
8393}
8394
8395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 * "eventhandler()" function
8397 */
8398/*ARGSUSED*/
8399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008401 typval_T *argvars;
8402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405}
8406
8407/*
8408 * "executable()" function
8409 */
8410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *argvars;
8413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416}
8417
8418/*
8419 * "exists()" function
8420 */
8421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008422f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008423 typval_T *argvars;
8424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425{
8426 char_u *p;
8427 char_u *name;
8428 int n = FALSE;
8429 int len = 0;
8430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008431 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (*p == '$') /* environment variable */
8433 {
8434 /* first try "normal" environment variables (fast) */
8435 if (mch_getenv(p + 1) != NULL)
8436 n = TRUE;
8437 else
8438 {
8439 /* try expanding things like $VIM and ${HOME} */
8440 p = expand_env_save(p);
8441 if (p != NULL && *p != '$')
8442 n = TRUE;
8443 vim_free(p);
8444 }
8445 }
8446 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008447 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 else if (*p == '*') /* internal or user defined function */
8449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008450 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 }
8452 else if (*p == ':')
8453 {
8454 n = cmd_exists(p + 1);
8455 }
8456 else if (*p == '#')
8457 {
8458#ifdef FEAT_AUTOCMD
8459 name = p + 1;
8460 p = vim_strchr(name, '#');
8461 if (p != NULL)
8462 n = au_exists(name, p, p + 1);
8463 else
8464 n = au_exists(name, name + STRLEN(name), NULL);
8465#endif
8466 }
8467 else /* internal variable */
8468 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008469 char_u *tofree;
8470 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008472 /* get_name_len() takes care of expanding curly braces */
8473 name = p;
8474 len = get_name_len(&p, &tofree, TRUE, FALSE);
8475 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008477 if (tofree != NULL)
8478 name = tofree;
8479 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8480 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008482 /* handle d.key, l[idx], f(expr) */
8483 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8484 if (n)
8485 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 }
8487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008489 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 }
8491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493}
8494
8495/*
8496 * "expand()" function
8497 */
8498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 typval_T *argvars;
8501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 char_u *s;
8504 int len;
8505 char_u *errormsg;
8506 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8507 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008510 rettv->v_type = VAR_STRING;
8511 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (*s == '%' || *s == '#' || *s == '<')
8513 {
8514 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008515 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 --emsg_off;
8517 }
8518 else
8519 {
8520 /* When the optional second argument is non-zero, don't remove matches
8521 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008522 if (argvars[1].v_type != VAR_UNKNOWN
8523 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008525 if (!error)
8526 {
8527 ExpandInit(&xpc);
8528 xpc.xp_context = EXPAND_FILES;
8529 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8530 ExpandCleanup(&xpc);
8531 }
8532 else
8533 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 }
8535}
8536
8537/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008538 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008540 */
8541 static void
8542f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 typval_T *argvars;
8544 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008545{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008546 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008547 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008548 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 list_T *l1, *l2;
8550 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008551 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008552 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008553
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 l1 = argvars[0].vval.v_list;
8555 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008556 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8557 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008558 {
8559 if (argvars[2].v_type != VAR_UNKNOWN)
8560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 before = get_tv_number_chk(&argvars[2], &error);
8562 if (error)
8563 return; /* type error; errmsg already given */
8564
Bram Moolenaar758711c2005-02-02 23:11:38 +00008565 if (before == l1->lv_len)
8566 item = NULL;
8567 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008568 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008569 item = list_find(l1, before);
8570 if (item == NULL)
8571 {
8572 EMSGN(_(e_listidx), before);
8573 return;
8574 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 }
8576 }
8577 else
8578 item = NULL;
8579 list_extend(l1, l2, item);
8580
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 copy_tv(&argvars[0], rettv);
8582 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008584 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 dict_T *d1, *d2;
8587 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008588 char_u *action;
8589 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008591 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008592
8593 d1 = argvars[0].vval.v_dict;
8594 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008595 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8596 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 {
8598 /* Check the third argument. */
8599 if (argvars[2].v_type != VAR_UNKNOWN)
8600 {
8601 static char *(av[]) = {"keep", "force", "error"};
8602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 action = get_tv_string_chk(&argvars[2]);
8604 if (action == NULL)
8605 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008606 for (i = 0; i < 3; ++i)
8607 if (STRCMP(action, av[i]) == 0)
8608 break;
8609 if (i == 3)
8610 {
8611 EMSGN(_(e_invarg2), action);
8612 return;
8613 }
8614 }
8615 else
8616 action = (char_u *)"force";
8617
8618 /* Go over all entries in the second dict and add them to the
8619 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008620 todo = d2->dv_hashtab.ht_used;
8621 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008623 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008625 --todo;
8626 di1 = dict_find(d1, hi2->hi_key, -1);
8627 if (di1 == NULL)
8628 {
8629 di1 = dictitem_copy(HI2DI(hi2));
8630 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8631 dictitem_free(di1);
8632 }
8633 else if (*action == 'e')
8634 {
8635 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8636 break;
8637 }
8638 else if (*action == 'f')
8639 {
8640 clear_tv(&di1->di_tv);
8641 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008643 }
8644 }
8645
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 copy_tv(&argvars[0], rettv);
8647 }
8648 }
8649 else
8650 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008651}
8652
8653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 * "filereadable()" function
8655 */
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 FILE *fd;
8662 char_u *p;
8663 int n;
8664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8667 {
8668 n = TRUE;
8669 fclose(fd);
8670 }
8671 else
8672 n = FALSE;
8673
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675}
8676
8677/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008678 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 * rights to write into.
8680 */
8681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *argvars;
8684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008686 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687}
8688
Bram Moolenaar33570922005-01-25 22:26:29 +00008689static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008690
8691 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008692findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 typval_T *argvars;
8694 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008695 int dir;
8696{
8697#ifdef FEAT_SEARCHPATH
8698 char_u *fname;
8699 char_u *fresult = NULL;
8700 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8701 char_u *p;
8702 char_u pathbuf[NUMBUFLEN];
8703 int count = 1;
8704 int first = TRUE;
8705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008707
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008710 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8711 if (p == NULL)
8712 count = -1; /* error */
8713 else
8714 {
8715 if (*p != NUL)
8716 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008718 if (argvars[2].v_type != VAR_UNKNOWN)
8719 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8720 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008721 }
8722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008723 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008725 do
8726 {
8727 vim_free(fresult);
8728 fresult = find_file_in_path_option(first ? fname : NULL,
8729 first ? (int)STRLEN(fname) : 0,
8730 0, first, path, dir, NULL);
8731 first = FALSE;
8732 } while (--count > 0 && fresult != NULL);
8733 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008736#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008738#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008740}
8741
Bram Moolenaar33570922005-01-25 22:26:29 +00008742static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8743static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008744
8745/*
8746 * Implementation of map() and filter().
8747 */
8748 static void
8749filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *argvars;
8751 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008752 int map;
8753{
8754 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 listitem_T *li, *nli;
8757 list_T *l = NULL;
8758 dictitem_T *di;
8759 hashtab_T *ht;
8760 hashitem_T *hi;
8761 dict_T *d = NULL;
8762 typval_T save_val;
8763 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008764 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008765 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008766 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8767
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008768
8769 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008770 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008771 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008772 if ((l = argvars[0].vval.v_list) == NULL
8773 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008774 return;
8775 }
8776 else if (argvars[0].v_type == VAR_DICT)
8777 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008778 if ((d = argvars[0].vval.v_dict) == NULL
8779 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008780 return;
8781 }
8782 else
8783 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008784 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008785 return;
8786 }
8787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 expr = get_tv_string_buf_chk(&argvars[1], buf);
8789 /* On type errors, the preceding call has already displayed an error
8790 * message. Avoid a misleading error message for an empty string that
8791 * was not passed as argument. */
8792 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 prepare_vimvar(VV_VAL, &save_val);
8795 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008797 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008799 prepare_vimvar(VV_KEY, &save_key);
8800 vimvars[VV_KEY].vv_type = VAR_STRING;
8801
8802 ht = &d->dv_hashtab;
8803 hash_lock(ht);
8804 todo = ht->ht_used;
8805 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 if (!HASHITEM_EMPTY(hi))
8808 {
8809 --todo;
8810 di = HI2DI(hi);
8811 if (tv_check_lock(di->di_tv.v_lock, msg))
8812 break;
8813 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8814 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8815 break;
8816 if (!map && rem)
8817 dictitem_remove(d, di);
8818 clear_tv(&vimvars[VV_KEY].vv_tv);
8819 }
8820 }
8821 hash_unlock(ht);
8822
8823 restore_vimvar(VV_KEY, &save_key);
8824 }
8825 else
8826 {
8827 for (li = l->lv_first; li != NULL; li = nli)
8828 {
8829 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008830 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 nli = li->li_next;
8832 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008833 break;
8834 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008837 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841
8842 copy_tv(&argvars[0], rettv);
8843}
8844
8845 static int
8846filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 char_u *expr;
8849 int map;
8850 int *remp;
8851{
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008853 char_u *s;
8854
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 s = expr;
8857 if (eval1(&s, &rettv, TRUE) == FAIL)
8858 return FAIL;
8859 if (*s != NUL) /* check for trailing chars after expr */
8860 {
8861 EMSG2(_(e_invexpr2), s);
8862 return FAIL;
8863 }
8864 if (map)
8865 {
8866 /* map(): replace the list item value */
8867 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008868 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008869 *tv = rettv;
8870 }
8871 else
8872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008873 int error = FALSE;
8874
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008878 /* On type error, nothing has been removed; return FAIL to stop the
8879 * loop. The error message was given by get_tv_number_chk(). */
8880 if (error)
8881 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008883 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008885}
8886
8887/*
8888 * "filter()" function
8889 */
8890 static void
8891f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *argvars;
8893 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008894{
8895 filter_map(argvars, rettv, FALSE);
8896}
8897
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008899 * "finddir({fname}[, {path}[, {count}]])" function
8900 */
8901 static void
8902f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 typval_T *argvars;
8904 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008905{
8906 findfilendir(argvars, rettv, TRUE);
8907}
8908
8909/*
8910 * "findfile({fname}[, {path}[, {count}]])" function
8911 */
8912 static void
8913f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 typval_T *argvars;
8915 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008916{
8917 findfilendir(argvars, rettv, FALSE);
8918}
8919
8920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 * "fnamemodify({fname}, {mods})" function
8922 */
8923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *argvars;
8926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928 char_u *fname;
8929 char_u *mods;
8930 int usedlen = 0;
8931 int len;
8932 char_u *fbuf = NULL;
8933 char_u buf[NUMBUFLEN];
8934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 fname = get_tv_string_chk(&argvars[0]);
8936 mods = get_tv_string_buf_chk(&argvars[1], buf);
8937 if (fname == NULL || mods == NULL)
8938 fname = NULL;
8939 else
8940 {
8941 len = (int)STRLEN(fname);
8942 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 vim_free(fbuf);
8951}
8952
Bram Moolenaar33570922005-01-25 22:26:29 +00008953static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
8955/*
8956 * "foldclosed()" function
8957 */
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 int end;
8963{
8964#ifdef FEAT_FOLDING
8965 linenr_T lnum;
8966 linenr_T first, last;
8967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8970 {
8971 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8972 {
8973 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 return;
8978 }
8979 }
8980#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
8984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008985 * "foldclosed()" function
8986 */
8987 static void
8988f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991{
8992 foldclosed_both(argvars, rettv, FALSE);
8993}
8994
8995/*
8996 * "foldclosedend()" function
8997 */
8998 static void
8999f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 typval_T *argvars;
9001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002{
9003 foldclosed_both(argvars, rettv, TRUE);
9004}
9005
9006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 * "foldlevel()" function
9008 */
9009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *argvars;
9012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013{
9014#ifdef FEAT_FOLDING
9015 linenr_T lnum;
9016
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 else
9021#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024
9025/*
9026 * "foldtext()" function
9027 */
9028/*ARGSUSED*/
9029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *argvars;
9032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034#ifdef FEAT_FOLDING
9035 linenr_T lnum;
9036 char_u *s;
9037 char_u *r;
9038 int len;
9039 char *txt;
9040#endif
9041
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042 rettv->v_type = VAR_STRING;
9043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9046 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9047 <= curbuf->b_ml.ml_line_count
9048 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 {
9050 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009051 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9052 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 {
9054 if (!linewhite(lnum))
9055 break;
9056 ++lnum;
9057 }
9058
9059 /* Find interesting text in this line. */
9060 s = skipwhite(ml_get(lnum));
9061 /* skip C comment-start */
9062 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009065 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009066 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009067 {
9068 s = skipwhite(ml_get(lnum + 1));
9069 if (*s == '*')
9070 s = skipwhite(s + 1);
9071 }
9072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 txt = _("+-%s%3ld lines: ");
9074 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009075 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 + 20 /* for %3ld */
9077 + STRLEN(s))); /* concatenated */
9078 if (r != NULL)
9079 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009080 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9081 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9082 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 len = (int)STRLEN(r);
9084 STRCAT(r, s);
9085 /* remove 'foldmarker' and 'commentstring' */
9086 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
9089 }
9090#endif
9091}
9092
9093/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009094 * "foldtextresult(lnum)" function
9095 */
9096/*ARGSUSED*/
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009101{
9102#ifdef FEAT_FOLDING
9103 linenr_T lnum;
9104 char_u *text;
9105 char_u buf[51];
9106 foldinfo_T foldinfo;
9107 int fold_count;
9108#endif
9109
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->v_type = VAR_STRING;
9111 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009112#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 /* treat illegal types and illegal string values for {lnum} the same */
9115 if (lnum < 0)
9116 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009117 fold_count = foldedCount(curwin, lnum, &foldinfo);
9118 if (fold_count > 0)
9119 {
9120 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9121 &foldinfo, buf);
9122 if (text == buf)
9123 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009125 }
9126#endif
9127}
9128
9129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 * "foreground()" function
9131 */
9132/*ARGSUSED*/
9133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 typval_T *argvars;
9136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139#ifdef FEAT_GUI
9140 if (gui.in_use)
9141 gui_mch_set_foreground();
9142#else
9143# ifdef WIN32
9144 win32_set_foreground();
9145# endif
9146#endif
9147}
9148
9149/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009150 * "function()" function
9151 */
9152/*ARGSUSED*/
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009157{
9158 char_u *s;
9159
Bram Moolenaara7043832005-01-21 11:56:39 +00009160 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009162 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 EMSG2(_(e_invarg2), s);
9164 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009165 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009166 else
9167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 rettv->vval.v_string = vim_strsave(s);
9169 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 }
9171}
9172
9173/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009174 * "garbagecollect()" function
9175 */
9176/*ARGSUSED*/
9177 static void
9178f_garbagecollect(argvars, rettv)
9179 typval_T *argvars;
9180 typval_T *rettv;
9181{
9182 garbage_collect();
9183}
9184
9185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009186 * "get()" function
9187 */
9188 static void
9189f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009190 typval_T *argvars;
9191 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009192{
Bram Moolenaar33570922005-01-25 22:26:29 +00009193 listitem_T *li;
9194 list_T *l;
9195 dictitem_T *di;
9196 dict_T *d;
9197 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009198
Bram Moolenaare9a41262005-01-15 22:18:47 +00009199 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009201 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 int error = FALSE;
9204
9205 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9206 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009207 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009210 else if (argvars[0].v_type == VAR_DICT)
9211 {
9212 if ((d = argvars[0].vval.v_dict) != NULL)
9213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009214 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009215 if (di != NULL)
9216 tv = &di->di_tv;
9217 }
9218 }
9219 else
9220 EMSG2(_(e_listdictarg), "get()");
9221
9222 if (tv == NULL)
9223 {
9224 if (argvars[2].v_type == VAR_UNKNOWN)
9225 rettv->vval.v_number = 0;
9226 else
9227 copy_tv(&argvars[2], rettv);
9228 }
9229 else
9230 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009231}
9232
Bram Moolenaar342337a2005-07-21 21:11:17 +00009233static 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 +00009234
9235/*
9236 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009237 * Return a range (from start to end) of lines in rettv from the specified
9238 * buffer.
9239 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009240 */
9241 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009242get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009243 buf_T *buf;
9244 linenr_T start;
9245 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009246 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009247 typval_T *rettv;
9248{
9249 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009250 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009251
Bram Moolenaar342337a2005-07-21 21:11:17 +00009252 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009253 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009254 l = list_alloc();
9255 if (l == NULL)
9256 return;
9257
9258 rettv->vval.v_list = l;
9259 rettv->v_type = VAR_LIST;
9260 ++l->lv_refcount;
9261 }
9262 else
9263 rettv->vval.v_number = 0;
9264
9265 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9266 return;
9267
9268 if (!retlist)
9269 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009270 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9271 p = ml_get_buf(buf, start, FALSE);
9272 else
9273 p = (char_u *)"";
9274
9275 rettv->v_type = VAR_STRING;
9276 rettv->vval.v_string = vim_strsave(p);
9277 }
9278 else
9279 {
9280 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009281 return;
9282
9283 if (start < 1)
9284 start = 1;
9285 if (end > buf->b_ml.ml_line_count)
9286 end = buf->b_ml.ml_line_count;
9287 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009288 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9289 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009290 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009291 }
9292}
9293
9294/*
9295 * "getbufline()" function
9296 */
9297 static void
9298f_getbufline(argvars, rettv)
9299 typval_T *argvars;
9300 typval_T *rettv;
9301{
9302 linenr_T lnum;
9303 linenr_T end;
9304 buf_T *buf;
9305
9306 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9307 ++emsg_off;
9308 buf = get_buf_tv(&argvars[0]);
9309 --emsg_off;
9310
Bram Moolenaar661b1822005-07-28 22:36:45 +00009311 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009312 if (argvars[2].v_type == VAR_UNKNOWN)
9313 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009314 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009315 end = get_tv_lnum_buf(&argvars[2], buf);
9316
Bram Moolenaar342337a2005-07-21 21:11:17 +00009317 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009318}
9319
Bram Moolenaar0d660222005-01-07 21:51:51 +00009320/*
9321 * "getbufvar()" function
9322 */
9323 static void
9324f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009325 typval_T *argvars;
9326 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009327{
9328 buf_T *buf;
9329 buf_T *save_curbuf;
9330 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009331 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009333 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9334 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335 ++emsg_off;
9336 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009337
9338 rettv->v_type = VAR_STRING;
9339 rettv->vval.v_string = NULL;
9340
9341 if (buf != NULL && varname != NULL)
9342 {
9343 if (*varname == '&') /* buffer-local-option */
9344 {
9345 /* set curbuf to be our buf, temporarily */
9346 save_curbuf = curbuf;
9347 curbuf = buf;
9348
9349 get_option_tv(&varname, rettv, TRUE);
9350
9351 /* restore previous notion of curbuf */
9352 curbuf = save_curbuf;
9353 }
9354 else
9355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 if (*varname == NUL)
9357 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9358 * scope prefix before the NUL byte is required by
9359 * find_var_in_ht(). */
9360 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009361 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009362 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009363 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009364 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009365 }
9366 }
9367
9368 --emsg_off;
9369}
9370
9371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 * "getchar()" function
9373 */
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
9382 ++no_mapping;
9383 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009384 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 /* getchar(): blocking wait. */
9386 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 /* getchar(1): only check if char avail */
9389 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009390 else if (error || vpeekc() == NUL)
9391 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 n = 0;
9393 else
9394 /* getchar(0) and char avail: return char */
9395 n = safe_vgetc();
9396 --no_mapping;
9397 --allow_keys;
9398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 if (IS_SPECIAL(n) || mod_mask != 0)
9401 {
9402 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9403 int i = 0;
9404
9405 /* Turn a special key into three bytes, plus modifier. */
9406 if (mod_mask != 0)
9407 {
9408 temp[i++] = K_SPECIAL;
9409 temp[i++] = KS_MODIFIER;
9410 temp[i++] = mod_mask;
9411 }
9412 if (IS_SPECIAL(n))
9413 {
9414 temp[i++] = K_SPECIAL;
9415 temp[i++] = K_SECOND(n);
9416 temp[i++] = K_THIRD(n);
9417 }
9418#ifdef FEAT_MBYTE
9419 else if (has_mbyte)
9420 i += (*mb_char2bytes)(n, temp + i);
9421#endif
9422 else
9423 temp[i++] = n;
9424 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->v_type = VAR_STRING;
9426 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 }
9428}
9429
9430/*
9431 * "getcharmod()" function
9432 */
9433/*ARGSUSED*/
9434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440}
9441
9442/*
9443 * "getcmdline()" function
9444 */
9445/*ARGSUSED*/
9446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009448 typval_T *argvars;
9449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->v_type = VAR_STRING;
9452 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453}
9454
9455/*
9456 * "getcmdpos()" function
9457 */
9458/*ARGSUSED*/
9459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009461 typval_T *argvars;
9462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465}
9466
9467/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009468 * "getcmdtype()" function
9469 */
9470/*ARGSUSED*/
9471 static void
9472f_getcmdtype(argvars, rettv)
9473 typval_T *argvars;
9474 typval_T *rettv;
9475{
9476 rettv->v_type = VAR_STRING;
9477 rettv->vval.v_string = alloc(2);
9478 if (rettv->vval.v_string != NULL)
9479 {
9480 rettv->vval.v_string[0] = get_cmdline_type();
9481 rettv->vval.v_string[1] = NUL;
9482 }
9483}
9484
9485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 * "getcwd()" function
9487 */
9488/*ARGSUSED*/
9489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009491 typval_T *argvars;
9492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 char_u cwd[MAXPATHL];
9495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 else
9500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009503 if (rettv->vval.v_string != NULL)
9504 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505#endif
9506 }
9507}
9508
9509/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009510 * "getfontname()" function
9511 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009512/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009515 typval_T *argvars;
9516 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->v_type = VAR_STRING;
9519 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009520#ifdef FEAT_GUI
9521 if (gui.in_use)
9522 {
9523 GuiFont font;
9524 char_u *name = NULL;
9525
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009526 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009527 {
9528 /* Get the "Normal" font. Either the name saved by
9529 * hl_set_font_name() or from the font ID. */
9530 font = gui.norm_font;
9531 name = hl_get_font_name();
9532 }
9533 else
9534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009536 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9537 return;
9538 font = gui_mch_get_font(name, FALSE);
9539 if (font == NOFONT)
9540 return; /* Invalid font name, return empty string. */
9541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009544 gui_mch_free_font(font);
9545 }
9546#endif
9547}
9548
9549/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009550 * "getfperm({fname})" function
9551 */
9552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009554 typval_T *argvars;
9555 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009556{
9557 char_u *fname;
9558 struct stat st;
9559 char_u *perm = NULL;
9560 char_u flags[] = "rwx";
9561 int i;
9562
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009566 if (mch_stat((char *)fname, &st) >= 0)
9567 {
9568 perm = vim_strsave((char_u *)"---------");
9569 if (perm != NULL)
9570 {
9571 for (i = 0; i < 9; i++)
9572 {
9573 if (st.st_mode & (1 << (8 - i)))
9574 perm[i] = flags[i % 3];
9575 }
9576 }
9577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009579}
9580
9581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 * "getfsize({fname})" function
9583 */
9584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *argvars;
9587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589 char_u *fname;
9590 struct stat st;
9591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
9596 if (mch_stat((char *)fname, &st) >= 0)
9597 {
9598 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 }
9603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606
9607/*
9608 * "getftime({fname})" function
9609 */
9610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 typval_T *argvars;
9613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 char_u *fname;
9616 struct stat st;
9617
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619
9620 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624}
9625
9626/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009627 * "getftype({fname})" function
9628 */
9629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009631 typval_T *argvars;
9632 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009633{
9634 char_u *fname;
9635 struct stat st;
9636 char_u *type = NULL;
9637 char *t;
9638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009642 if (mch_lstat((char *)fname, &st) >= 0)
9643 {
9644#ifdef S_ISREG
9645 if (S_ISREG(st.st_mode))
9646 t = "file";
9647 else if (S_ISDIR(st.st_mode))
9648 t = "dir";
9649# ifdef S_ISLNK
9650 else if (S_ISLNK(st.st_mode))
9651 t = "link";
9652# endif
9653# ifdef S_ISBLK
9654 else if (S_ISBLK(st.st_mode))
9655 t = "bdev";
9656# endif
9657# ifdef S_ISCHR
9658 else if (S_ISCHR(st.st_mode))
9659 t = "cdev";
9660# endif
9661# ifdef S_ISFIFO
9662 else if (S_ISFIFO(st.st_mode))
9663 t = "fifo";
9664# endif
9665# ifdef S_ISSOCK
9666 else if (S_ISSOCK(st.st_mode))
9667 t = "fifo";
9668# endif
9669 else
9670 t = "other";
9671#else
9672# ifdef S_IFMT
9673 switch (st.st_mode & S_IFMT)
9674 {
9675 case S_IFREG: t = "file"; break;
9676 case S_IFDIR: t = "dir"; break;
9677# ifdef S_IFLNK
9678 case S_IFLNK: t = "link"; break;
9679# endif
9680# ifdef S_IFBLK
9681 case S_IFBLK: t = "bdev"; break;
9682# endif
9683# ifdef S_IFCHR
9684 case S_IFCHR: t = "cdev"; break;
9685# endif
9686# ifdef S_IFIFO
9687 case S_IFIFO: t = "fifo"; break;
9688# endif
9689# ifdef S_IFSOCK
9690 case S_IFSOCK: t = "socket"; break;
9691# endif
9692 default: t = "other";
9693 }
9694# else
9695 if (mch_isdir(fname))
9696 t = "dir";
9697 else
9698 t = "file";
9699# endif
9700#endif
9701 type = vim_strsave((char_u *)t);
9702 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009704}
9705
9706/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009707 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009708 */
9709 static void
9710f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009711 typval_T *argvars;
9712 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009713{
9714 linenr_T lnum;
9715 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009716 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009717
9718 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009719 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009720 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009721 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009722 retlist = FALSE;
9723 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009724 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009725 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009726 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009727 retlist = TRUE;
9728 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009729
Bram Moolenaar342337a2005-07-21 21:11:17 +00009730 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009731}
9732
9733/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009734 * "getqflist()" function
9735 */
9736/*ARGSUSED*/
9737 static void
9738f_getqflist(argvars, rettv)
9739 typval_T *argvars;
9740 typval_T *rettv;
9741{
9742#ifdef FEAT_QUICKFIX
9743 list_T *l;
9744#endif
9745
9746 rettv->vval.v_number = FALSE;
9747#ifdef FEAT_QUICKFIX
9748 l = list_alloc();
9749 if (l != NULL)
9750 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009751 rettv->vval.v_list = l;
9752 rettv->v_type = VAR_LIST;
9753 ++l->lv_refcount;
9754 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009755 }
9756#endif
9757}
9758
9759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 * "getreg()" function
9761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767 char_u *strregname;
9768 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009769 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009770 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009772 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009774 strregname = get_tv_string_chk(&argvars[0]);
9775 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009776 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009780 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 regname = (strregname == NULL ? '"' : *strregname);
9782 if (regname == 0)
9783 regname = '"';
9784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 rettv->vval.v_string = error ? NULL :
9787 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788}
9789
9790/*
9791 * "getregtype()" function
9792 */
9793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *argvars;
9796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798 char_u *strregname;
9799 int regname;
9800 char_u buf[NUMBUFLEN + 2];
9801 long reglen = 0;
9802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009803 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009804 {
9805 strregname = get_tv_string_chk(&argvars[0]);
9806 if (strregname == NULL) /* type error; errmsg already given */
9807 {
9808 rettv->v_type = VAR_STRING;
9809 rettv->vval.v_string = NULL;
9810 return;
9811 }
9812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else
9814 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009815 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816
9817 regname = (strregname == NULL ? '"' : *strregname);
9818 if (regname == 0)
9819 regname = '"';
9820
9821 buf[0] = NUL;
9822 buf[1] = NUL;
9823 switch (get_reg_type(regname, &reglen))
9824 {
9825 case MLINE: buf[0] = 'V'; break;
9826 case MCHAR: buf[0] = 'v'; break;
9827#ifdef FEAT_VISUAL
9828 case MBLOCK:
9829 buf[0] = Ctrl_V;
9830 sprintf((char *)buf + 1, "%ld", reglen + 1);
9831 break;
9832#endif
9833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834 rettv->v_type = VAR_STRING;
9835 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836}
9837
9838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 * "getwinposx()" function
9840 */
9841/*ARGSUSED*/
9842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009844 typval_T *argvars;
9845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848#ifdef FEAT_GUI
9849 if (gui.in_use)
9850 {
9851 int x, y;
9852
9853 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 }
9856#endif
9857}
9858
9859/*
9860 * "getwinposy()" function
9861 */
9862/*ARGSUSED*/
9863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009865 typval_T *argvars;
9866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869#ifdef FEAT_GUI
9870 if (gui.in_use)
9871 {
9872 int x, y;
9873
9874 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 }
9877#endif
9878}
9879
Bram Moolenaara40058a2005-07-11 22:42:07 +00009880static win_T *find_win_by_nr __ARGS((typval_T *vp));
9881
9882 static win_T *
9883find_win_by_nr(vp)
9884 typval_T *vp;
9885{
9886#ifdef FEAT_WINDOWS
9887 win_T *wp;
9888#endif
9889 int nr;
9890
9891 nr = get_tv_number_chk(vp, NULL);
9892
9893#ifdef FEAT_WINDOWS
9894 if (nr < 0)
9895 return NULL;
9896 if (nr == 0)
9897 return curwin;
9898
9899 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9900 if (--nr <= 0)
9901 break;
9902 return wp;
9903#else
9904 if (nr == 0 || nr == 1)
9905 return curwin;
9906 return NULL;
9907#endif
9908}
9909
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910/*
9911 * "getwinvar()" function
9912 */
9913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 typval_T *argvars;
9916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917{
9918 win_T *win, *oldcurwin;
9919 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 varname = get_tv_string_chk(&argvars[1]);
9924 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->v_type = VAR_STRING;
9927 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928
9929 if (win != NULL && varname != NULL)
9930 {
9931 if (*varname == '&') /* window-local-option */
9932 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009933 /* Set curwin to be our win, temporarily. Also set curbuf, so
9934 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 oldcurwin = curwin;
9936 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009937 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 /* restore previous notion of curwin */
9942 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009943 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 }
9945 else
9946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 if (*varname == NUL)
9948 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9949 * scope prefix before the NUL byte is required by
9950 * find_var_in_ht(). */
9951 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009953 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 }
9957 }
9958
9959 --emsg_off;
9960}
9961
9962/*
9963 * "glob()" function
9964 */
9965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T *argvars;
9968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970 expand_T xpc;
9971
9972 ExpandInit(&xpc);
9973 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 rettv->v_type = VAR_STRING;
9975 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9977 ExpandCleanup(&xpc);
9978}
9979
9980/*
9981 * "globpath()" function
9982 */
9983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 typval_T *argvars;
9986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009989 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 if (file == NULL)
9993 rettv->vval.v_string = NULL;
9994 else
9995 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996}
9997
9998/*
9999 * "has()" function
10000 */
10001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 typval_T *argvars;
10004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 int i;
10007 char_u *name;
10008 int n = FALSE;
10009 static char *(has_list[]) =
10010 {
10011#ifdef AMIGA
10012 "amiga",
10013# ifdef FEAT_ARP
10014 "arp",
10015# endif
10016#endif
10017#ifdef __BEOS__
10018 "beos",
10019#endif
10020#ifdef MSDOS
10021# ifdef DJGPP
10022 "dos32",
10023# else
10024 "dos16",
10025# endif
10026#endif
10027#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
10028 "mac",
10029#endif
10030#if defined(MACOS_X_UNIX)
10031 "macunix",
10032#endif
10033#ifdef OS2
10034 "os2",
10035#endif
10036#ifdef __QNX__
10037 "qnx",
10038#endif
10039#ifdef RISCOS
10040 "riscos",
10041#endif
10042#ifdef UNIX
10043 "unix",
10044#endif
10045#ifdef VMS
10046 "vms",
10047#endif
10048#ifdef WIN16
10049 "win16",
10050#endif
10051#ifdef WIN32
10052 "win32",
10053#endif
10054#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10055 "win32unix",
10056#endif
10057#ifdef WIN64
10058 "win64",
10059#endif
10060#ifdef EBCDIC
10061 "ebcdic",
10062#endif
10063#ifndef CASE_INSENSITIVE_FILENAME
10064 "fname_case",
10065#endif
10066#ifdef FEAT_ARABIC
10067 "arabic",
10068#endif
10069#ifdef FEAT_AUTOCMD
10070 "autocmd",
10071#endif
10072#ifdef FEAT_BEVAL
10073 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010074# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10075 "balloon_multiline",
10076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077#endif
10078#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10079 "builtin_terms",
10080# ifdef ALL_BUILTIN_TCAPS
10081 "all_builtin_terms",
10082# endif
10083#endif
10084#ifdef FEAT_BYTEOFF
10085 "byte_offset",
10086#endif
10087#ifdef FEAT_CINDENT
10088 "cindent",
10089#endif
10090#ifdef FEAT_CLIENTSERVER
10091 "clientserver",
10092#endif
10093#ifdef FEAT_CLIPBOARD
10094 "clipboard",
10095#endif
10096#ifdef FEAT_CMDL_COMPL
10097 "cmdline_compl",
10098#endif
10099#ifdef FEAT_CMDHIST
10100 "cmdline_hist",
10101#endif
10102#ifdef FEAT_COMMENTS
10103 "comments",
10104#endif
10105#ifdef FEAT_CRYPT
10106 "cryptv",
10107#endif
10108#ifdef FEAT_CSCOPE
10109 "cscope",
10110#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010111#ifdef CURSOR_SHAPE
10112 "cursorshape",
10113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114#ifdef DEBUG
10115 "debug",
10116#endif
10117#ifdef FEAT_CON_DIALOG
10118 "dialog_con",
10119#endif
10120#ifdef FEAT_GUI_DIALOG
10121 "dialog_gui",
10122#endif
10123#ifdef FEAT_DIFF
10124 "diff",
10125#endif
10126#ifdef FEAT_DIGRAPHS
10127 "digraphs",
10128#endif
10129#ifdef FEAT_DND
10130 "dnd",
10131#endif
10132#ifdef FEAT_EMACS_TAGS
10133 "emacs_tags",
10134#endif
10135 "eval", /* always present, of course! */
10136#ifdef FEAT_EX_EXTRA
10137 "ex_extra",
10138#endif
10139#ifdef FEAT_SEARCH_EXTRA
10140 "extra_search",
10141#endif
10142#ifdef FEAT_FKMAP
10143 "farsi",
10144#endif
10145#ifdef FEAT_SEARCHPATH
10146 "file_in_path",
10147#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010148#if defined(UNIX) && !defined(USE_SYSTEM)
10149 "filterpipe",
10150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#ifdef FEAT_FIND_ID
10152 "find_in_path",
10153#endif
10154#ifdef FEAT_FOLDING
10155 "folding",
10156#endif
10157#ifdef FEAT_FOOTER
10158 "footer",
10159#endif
10160#if !defined(USE_SYSTEM) && defined(UNIX)
10161 "fork",
10162#endif
10163#ifdef FEAT_GETTEXT
10164 "gettext",
10165#endif
10166#ifdef FEAT_GUI
10167 "gui",
10168#endif
10169#ifdef FEAT_GUI_ATHENA
10170# ifdef FEAT_GUI_NEXTAW
10171 "gui_neXtaw",
10172# else
10173 "gui_athena",
10174# endif
10175#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010176#ifdef FEAT_GUI_KDE
10177 "gui_kde",
10178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179#ifdef FEAT_GUI_GTK
10180 "gui_gtk",
10181# ifdef HAVE_GTK2
10182 "gui_gtk2",
10183# endif
10184#endif
10185#ifdef FEAT_GUI_MAC
10186 "gui_mac",
10187#endif
10188#ifdef FEAT_GUI_MOTIF
10189 "gui_motif",
10190#endif
10191#ifdef FEAT_GUI_PHOTON
10192 "gui_photon",
10193#endif
10194#ifdef FEAT_GUI_W16
10195 "gui_win16",
10196#endif
10197#ifdef FEAT_GUI_W32
10198 "gui_win32",
10199#endif
10200#ifdef FEAT_HANGULIN
10201 "hangul_input",
10202#endif
10203#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10204 "iconv",
10205#endif
10206#ifdef FEAT_INS_EXPAND
10207 "insert_expand",
10208#endif
10209#ifdef FEAT_JUMPLIST
10210 "jumplist",
10211#endif
10212#ifdef FEAT_KEYMAP
10213 "keymap",
10214#endif
10215#ifdef FEAT_LANGMAP
10216 "langmap",
10217#endif
10218#ifdef FEAT_LIBCALL
10219 "libcall",
10220#endif
10221#ifdef FEAT_LINEBREAK
10222 "linebreak",
10223#endif
10224#ifdef FEAT_LISP
10225 "lispindent",
10226#endif
10227#ifdef FEAT_LISTCMDS
10228 "listcmds",
10229#endif
10230#ifdef FEAT_LOCALMAP
10231 "localmap",
10232#endif
10233#ifdef FEAT_MENU
10234 "menu",
10235#endif
10236#ifdef FEAT_SESSION
10237 "mksession",
10238#endif
10239#ifdef FEAT_MODIFY_FNAME
10240 "modify_fname",
10241#endif
10242#ifdef FEAT_MOUSE
10243 "mouse",
10244#endif
10245#ifdef FEAT_MOUSESHAPE
10246 "mouseshape",
10247#endif
10248#if defined(UNIX) || defined(VMS)
10249# ifdef FEAT_MOUSE_DEC
10250 "mouse_dec",
10251# endif
10252# ifdef FEAT_MOUSE_GPM
10253 "mouse_gpm",
10254# endif
10255# ifdef FEAT_MOUSE_JSB
10256 "mouse_jsbterm",
10257# endif
10258# ifdef FEAT_MOUSE_NET
10259 "mouse_netterm",
10260# endif
10261# ifdef FEAT_MOUSE_PTERM
10262 "mouse_pterm",
10263# endif
10264# ifdef FEAT_MOUSE_XTERM
10265 "mouse_xterm",
10266# endif
10267#endif
10268#ifdef FEAT_MBYTE
10269 "multi_byte",
10270#endif
10271#ifdef FEAT_MBYTE_IME
10272 "multi_byte_ime",
10273#endif
10274#ifdef FEAT_MULTI_LANG
10275 "multi_lang",
10276#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010277#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010278#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010279 "mzscheme",
10280#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282#ifdef FEAT_OLE
10283 "ole",
10284#endif
10285#ifdef FEAT_OSFILETYPE
10286 "osfiletype",
10287#endif
10288#ifdef FEAT_PATH_EXTRA
10289 "path_extra",
10290#endif
10291#ifdef FEAT_PERL
10292#ifndef DYNAMIC_PERL
10293 "perl",
10294#endif
10295#endif
10296#ifdef FEAT_PYTHON
10297#ifndef DYNAMIC_PYTHON
10298 "python",
10299#endif
10300#endif
10301#ifdef FEAT_POSTSCRIPT
10302 "postscript",
10303#endif
10304#ifdef FEAT_PRINTER
10305 "printer",
10306#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010307#ifdef FEAT_PROFILE
10308 "profile",
10309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310#ifdef FEAT_QUICKFIX
10311 "quickfix",
10312#endif
10313#ifdef FEAT_RIGHTLEFT
10314 "rightleft",
10315#endif
10316#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10317 "ruby",
10318#endif
10319#ifdef FEAT_SCROLLBIND
10320 "scrollbind",
10321#endif
10322#ifdef FEAT_CMDL_INFO
10323 "showcmd",
10324 "cmdline_info",
10325#endif
10326#ifdef FEAT_SIGNS
10327 "signs",
10328#endif
10329#ifdef FEAT_SMARTINDENT
10330 "smartindent",
10331#endif
10332#ifdef FEAT_SNIFF
10333 "sniff",
10334#endif
10335#ifdef FEAT_STL_OPT
10336 "statusline",
10337#endif
10338#ifdef FEAT_SUN_WORKSHOP
10339 "sun_workshop",
10340#endif
10341#ifdef FEAT_NETBEANS_INTG
10342 "netbeans_intg",
10343#endif
10344#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010345 "spell",
10346#endif
10347#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 "syntax",
10349#endif
10350#if defined(USE_SYSTEM) || !defined(UNIX)
10351 "system",
10352#endif
10353#ifdef FEAT_TAG_BINS
10354 "tag_binary",
10355#endif
10356#ifdef FEAT_TAG_OLDSTATIC
10357 "tag_old_static",
10358#endif
10359#ifdef FEAT_TAG_ANYWHITE
10360 "tag_any_white",
10361#endif
10362#ifdef FEAT_TCL
10363# ifndef DYNAMIC_TCL
10364 "tcl",
10365# endif
10366#endif
10367#ifdef TERMINFO
10368 "terminfo",
10369#endif
10370#ifdef FEAT_TERMRESPONSE
10371 "termresponse",
10372#endif
10373#ifdef FEAT_TEXTOBJ
10374 "textobjects",
10375#endif
10376#ifdef HAVE_TGETENT
10377 "tgetent",
10378#endif
10379#ifdef FEAT_TITLE
10380 "title",
10381#endif
10382#ifdef FEAT_TOOLBAR
10383 "toolbar",
10384#endif
10385#ifdef FEAT_USR_CMDS
10386 "user-commands", /* was accidentally included in 5.4 */
10387 "user_commands",
10388#endif
10389#ifdef FEAT_VIMINFO
10390 "viminfo",
10391#endif
10392#ifdef FEAT_VERTSPLIT
10393 "vertsplit",
10394#endif
10395#ifdef FEAT_VIRTUALEDIT
10396 "virtualedit",
10397#endif
10398#ifdef FEAT_VISUAL
10399 "visual",
10400#endif
10401#ifdef FEAT_VISUALEXTRA
10402 "visualextra",
10403#endif
10404#ifdef FEAT_VREPLACE
10405 "vreplace",
10406#endif
10407#ifdef FEAT_WILDIGN
10408 "wildignore",
10409#endif
10410#ifdef FEAT_WILDMENU
10411 "wildmenu",
10412#endif
10413#ifdef FEAT_WINDOWS
10414 "windows",
10415#endif
10416#ifdef FEAT_WAK
10417 "winaltkeys",
10418#endif
10419#ifdef FEAT_WRITEBACKUP
10420 "writebackup",
10421#endif
10422#ifdef FEAT_XIM
10423 "xim",
10424#endif
10425#ifdef FEAT_XFONTSET
10426 "xfontset",
10427#endif
10428#ifdef USE_XSMP
10429 "xsmp",
10430#endif
10431#ifdef USE_XSMP_INTERACT
10432 "xsmp_interact",
10433#endif
10434#ifdef FEAT_XCLIPBOARD
10435 "xterm_clipboard",
10436#endif
10437#ifdef FEAT_XTERM_SAVE
10438 "xterm_save",
10439#endif
10440#if defined(UNIX) && defined(FEAT_X11)
10441 "X11",
10442#endif
10443 NULL
10444 };
10445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 for (i = 0; has_list[i] != NULL; ++i)
10448 if (STRICMP(name, has_list[i]) == 0)
10449 {
10450 n = TRUE;
10451 break;
10452 }
10453
10454 if (n == FALSE)
10455 {
10456 if (STRNICMP(name, "patch", 5) == 0)
10457 n = has_patch(atoi((char *)name + 5));
10458 else if (STRICMP(name, "vim_starting") == 0)
10459 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010460#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10461 else if (STRICMP(name, "balloon_multiline") == 0)
10462 n = multiline_balloon_available();
10463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464#ifdef DYNAMIC_TCL
10465 else if (STRICMP(name, "tcl") == 0)
10466 n = tcl_enabled(FALSE);
10467#endif
10468#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10469 else if (STRICMP(name, "iconv") == 0)
10470 n = iconv_enabled(FALSE);
10471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010472#ifdef DYNAMIC_MZSCHEME
10473 else if (STRICMP(name, "mzscheme") == 0)
10474 n = mzscheme_enabled(FALSE);
10475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476#ifdef DYNAMIC_RUBY
10477 else if (STRICMP(name, "ruby") == 0)
10478 n = ruby_enabled(FALSE);
10479#endif
10480#ifdef DYNAMIC_PYTHON
10481 else if (STRICMP(name, "python") == 0)
10482 n = python_enabled(FALSE);
10483#endif
10484#ifdef DYNAMIC_PERL
10485 else if (STRICMP(name, "perl") == 0)
10486 n = perl_enabled(FALSE);
10487#endif
10488#ifdef FEAT_GUI
10489 else if (STRICMP(name, "gui_running") == 0)
10490 n = (gui.in_use || gui.starting);
10491# ifdef FEAT_GUI_W32
10492 else if (STRICMP(name, "gui_win32s") == 0)
10493 n = gui_is_win32s();
10494# endif
10495# ifdef FEAT_BROWSE
10496 else if (STRICMP(name, "browse") == 0)
10497 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10498# endif
10499#endif
10500#ifdef FEAT_SYN_HL
10501 else if (STRICMP(name, "syntax_items") == 0)
10502 n = syntax_present(curbuf);
10503#endif
10504#if defined(WIN3264)
10505 else if (STRICMP(name, "win95") == 0)
10506 n = mch_windows95();
10507#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010508#ifdef FEAT_NETBEANS_INTG
10509 else if (STRICMP(name, "netbeans_enabled") == 0)
10510 n = usingNetbeans;
10511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 }
10513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515}
10516
10517/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010518 * "has_key()" function
10519 */
10520 static void
10521f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010522 typval_T *argvars;
10523 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010524{
10525 rettv->vval.v_number = 0;
10526 if (argvars[0].v_type != VAR_DICT)
10527 {
10528 EMSG(_(e_dictreq));
10529 return;
10530 }
10531 if (argvars[0].vval.v_dict == NULL)
10532 return;
10533
10534 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010535 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010536}
10537
10538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 * "hasmapto()" function
10540 */
10541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010543 typval_T *argvars;
10544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546 char_u *name;
10547 char_u *mode;
10548 char_u buf[NUMBUFLEN];
10549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010551 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 mode = (char_u *)"nvo";
10553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
10556 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560}
10561
10562/*
10563 * "histadd()" function
10564 */
10565/*ARGSUSED*/
10566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 typval_T *argvars;
10569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570{
10571#ifdef FEAT_CMDHIST
10572 int histype;
10573 char_u *str;
10574 char_u buf[NUMBUFLEN];
10575#endif
10576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010577 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 if (check_restricted() || check_secure())
10579 return;
10580#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10582 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (histype >= 0)
10584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010585 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 if (*str != NUL)
10587 {
10588 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 return;
10591 }
10592 }
10593#endif
10594}
10595
10596/*
10597 * "histdel()" function
10598 */
10599/*ARGSUSED*/
10600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 typval_T *argvars;
10603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
10605#ifdef FEAT_CMDHIST
10606 int n;
10607 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10611 if (str == NULL)
10612 n = 0;
10613 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010615 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010616 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 else
10621 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010622 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010623 get_tv_string_buf(&argvars[1], buf));
10624 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010626 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627#endif
10628}
10629
10630/*
10631 * "histget()" function
10632 */
10633/*ARGSUSED*/
10634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010636 typval_T *argvars;
10637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638{
10639#ifdef FEAT_CMDHIST
10640 int type;
10641 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10645 if (str == NULL)
10646 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010648 {
10649 type = get_histtype(str);
10650 if (argvars[1].v_type == VAR_UNKNOWN)
10651 idx = get_history_idx(type);
10652 else
10653 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10654 /* -1 on type error */
10655 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661}
10662
10663/*
10664 * "histnr()" function
10665 */
10666/*ARGSUSED*/
10667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010669 typval_T *argvars;
10670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672 int i;
10673
10674#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 char_u *history = get_tv_string_chk(&argvars[0]);
10676
10677 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 if (i >= HIST_CMD && i < HIST_COUNT)
10679 i = get_history_idx(i);
10680 else
10681#endif
10682 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684}
10685
10686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 * "highlightID(name)" function
10688 */
10689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695}
10696
10697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010698 * "highlight_exists()" function
10699 */
10700 static void
10701f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010702 typval_T *argvars;
10703 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704{
10705 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10706}
10707
10708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 * "hostname()" function
10710 */
10711/*ARGSUSED*/
10712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010714 typval_T *argvars;
10715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716{
10717 char_u hostname[256];
10718
10719 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720 rettv->v_type = VAR_STRING;
10721 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722}
10723
10724/*
10725 * iconv() function
10726 */
10727/*ARGSUSED*/
10728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010730 typval_T *argvars;
10731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732{
10733#ifdef FEAT_MBYTE
10734 char_u buf1[NUMBUFLEN];
10735 char_u buf2[NUMBUFLEN];
10736 char_u *from, *to, *str;
10737 vimconv_T vimconv;
10738#endif
10739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010740 rettv->v_type = VAR_STRING;
10741 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742
10743#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 str = get_tv_string(&argvars[0]);
10745 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10746 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 vimconv.vc_type = CONV_NONE;
10748 convert_setup(&vimconv, from, to);
10749
10750 /* If the encodings are equal, no conversion needed. */
10751 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
10756 convert_setup(&vimconv, NULL, NULL);
10757 vim_free(from);
10758 vim_free(to);
10759#endif
10760}
10761
10762/*
10763 * "indent()" function
10764 */
10765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010767 typval_T *argvars;
10768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769{
10770 linenr_T lnum;
10771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777}
10778
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010779/*
10780 * "index()" function
10781 */
10782 static void
10783f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 typval_T *argvars;
10785 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010786{
Bram Moolenaar33570922005-01-25 22:26:29 +000010787 list_T *l;
10788 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010789 long idx = 0;
10790 int ic = FALSE;
10791
10792 rettv->vval.v_number = -1;
10793 if (argvars[0].v_type != VAR_LIST)
10794 {
10795 EMSG(_(e_listreq));
10796 return;
10797 }
10798 l = argvars[0].vval.v_list;
10799 if (l != NULL)
10800 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010801 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010802 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 int error = FALSE;
10805
Bram Moolenaar758711c2005-02-02 23:11:38 +000010806 /* Start at specified item. Use the cached index that list_find()
10807 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010809 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010810 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 ic = get_tv_number_chk(&argvars[3], &error);
10812 if (error)
10813 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010814 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010815
Bram Moolenaar758711c2005-02-02 23:11:38 +000010816 for ( ; item != NULL; item = item->li_next, ++idx)
10817 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010818 {
10819 rettv->vval.v_number = idx;
10820 break;
10821 }
10822 }
10823}
10824
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825static int inputsecret_flag = 0;
10826
10827/*
10828 * "input()" function
10829 * Also handles inputsecret() when inputsecret is set.
10830 */
10831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010832f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 typval_T *argvars;
10834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 char_u *p = NULL;
10838 int c;
10839 char_u buf[NUMBUFLEN];
10840 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010841 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010842 int xp_type = EXPAND_NOTHING;
10843 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846
10847#ifdef NO_CONSOLE_INPUT
10848 /* While starting up, there is no place to enter text. */
10849 if (no_console_input())
10850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 return;
10853 }
10854#endif
10855
10856 cmd_silent = FALSE; /* Want to see the prompt. */
10857 if (prompt != NULL)
10858 {
10859 /* Only the part of the message after the last NL is considered as
10860 * prompt for the command line */
10861 p = vim_strrchr(prompt, '\n');
10862 if (p == NULL)
10863 p = prompt;
10864 else
10865 {
10866 ++p;
10867 c = *p;
10868 *p = NUL;
10869 msg_start();
10870 msg_clr_eos();
10871 msg_puts_attr(prompt, echo_attr);
10872 msg_didout = FALSE;
10873 msg_starthere();
10874 *p = c;
10875 }
10876 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 if (argvars[1].v_type != VAR_UNKNOWN)
10879 {
10880 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10881 if (defstr != NULL)
10882 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883
Bram Moolenaar4463f292005-09-25 22:20:24 +000010884 if (argvars[2].v_type != VAR_UNKNOWN)
10885 {
10886 char_u *xp_name;
10887 int xp_namelen;
10888 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010889
Bram Moolenaar4463f292005-09-25 22:20:24 +000010890 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010891
Bram Moolenaar4463f292005-09-25 22:20:24 +000010892 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10893 if (xp_name == NULL)
10894 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010895
Bram Moolenaar4463f292005-09-25 22:20:24 +000010896 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010897
Bram Moolenaar4463f292005-09-25 22:20:24 +000010898 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10899 &xp_arg) == FAIL)
10900 return;
10901 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010902 }
10903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 if (defstr != NULL)
10905 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010906 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10907 xp_type, xp_arg);
10908
10909 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 /* since the user typed this, no need to wait for return */
10912 need_wait_return = FALSE;
10913 msg_didout = FALSE;
10914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 cmd_silent = cmd_silent_save;
10916}
10917
10918/*
10919 * "inputdialog()" function
10920 */
10921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010923 typval_T *argvars;
10924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
10926#if defined(FEAT_GUI_TEXTDIALOG)
10927 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10928 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10929 {
10930 char_u *message;
10931 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 message = get_tv_string_chk(&argvars[0]);
10935 if (argvars[1].v_type != VAR_UNKNOWN
10936 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010937 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 else
10939 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 if (message != NULL && defstr != NULL
10941 && do_dialog(VIM_QUESTION, NULL, message,
10942 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 else
10945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 if (message != NULL && defstr != NULL
10947 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010948 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_string = vim_strsave(
10950 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 }
10956 else
10957#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959}
10960
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010961/*
10962 * "inputlist()" function
10963 */
10964 static void
10965f_inputlist(argvars, rettv)
10966 typval_T *argvars;
10967 typval_T *rettv;
10968{
10969 listitem_T *li;
10970 int selected;
10971 int mouse_used;
10972
10973 rettv->vval.v_number = 0;
10974#ifdef NO_CONSOLE_INPUT
10975 /* While starting up, there is no place to enter text. */
10976 if (no_console_input())
10977 return;
10978#endif
10979 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10980 {
10981 EMSG2(_(e_listarg), "inputlist()");
10982 return;
10983 }
10984
10985 msg_start();
10986 lines_left = Rows; /* avoid more prompt */
10987 msg_scroll = TRUE;
10988 msg_clr_eos();
10989
10990 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10991 {
10992 msg_puts(get_tv_string(&li->li_tv));
10993 msg_putchar('\n');
10994 }
10995
10996 /* Ask for choice. */
10997 selected = prompt_for_number(&mouse_used);
10998 if (mouse_used)
10999 selected -= lines_left;
11000
11001 rettv->vval.v_number = selected;
11002}
11003
11004
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11006
11007/*
11008 * "inputrestore()" function
11009 */
11010/*ARGSUSED*/
11011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011013 typval_T *argvars;
11014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015{
11016 if (ga_userinput.ga_len > 0)
11017 {
11018 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11020 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 }
11023 else if (p_verbose > 1)
11024 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011025 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 }
11028}
11029
11030/*
11031 * "inputsave()" function
11032 */
11033/*ARGSUSED*/
11034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011036 typval_T *argvars;
11037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038{
11039 /* Add an entry to the stack of typehead storage. */
11040 if (ga_grow(&ga_userinput, 1) == OK)
11041 {
11042 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11043 + ga_userinput.ga_len);
11044 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 }
11047 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049}
11050
11051/*
11052 * "inputsecret()" function
11053 */
11054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011056 typval_T *argvars;
11057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058{
11059 ++cmdline_star;
11060 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 --cmdline_star;
11063 --inputsecret_flag;
11064}
11065
11066/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011067 * "insert()" function
11068 */
11069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011071 typval_T *argvars;
11072 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011073{
11074 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011075 listitem_T *item;
11076 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011078
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011079 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011080 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011081 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011082 else if ((l = argvars[0].vval.v_list) != NULL
11083 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011084 {
11085 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011086 before = get_tv_number_chk(&argvars[2], &error);
11087 if (error)
11088 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011089
Bram Moolenaar758711c2005-02-02 23:11:38 +000011090 if (before == l->lv_len)
11091 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011092 else
11093 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011094 item = list_find(l, before);
11095 if (item == NULL)
11096 {
11097 EMSGN(_(e_listidx), before);
11098 l = NULL;
11099 }
11100 }
11101 if (l != NULL)
11102 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011103 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011104 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011105 }
11106 }
11107}
11108
11109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 * "isdirectory()" function
11111 */
11112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 typval_T *argvars;
11115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118}
11119
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011120/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011121 * "islocked()" function
11122 */
11123 static void
11124f_islocked(argvars, rettv)
11125 typval_T *argvars;
11126 typval_T *rettv;
11127{
11128 lval_T lv;
11129 char_u *end;
11130 dictitem_T *di;
11131
11132 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011133 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11134 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011135 if (end != NULL && lv.ll_name != NULL)
11136 {
11137 if (*end != NUL)
11138 EMSG(_(e_trailing));
11139 else
11140 {
11141 if (lv.ll_tv == NULL)
11142 {
11143 if (check_changedtick(lv.ll_name))
11144 rettv->vval.v_number = 1; /* always locked */
11145 else
11146 {
11147 di = find_var(lv.ll_name, NULL);
11148 if (di != NULL)
11149 {
11150 /* Consider a variable locked when:
11151 * 1. the variable itself is locked
11152 * 2. the value of the variable is locked.
11153 * 3. the List or Dict value is locked.
11154 */
11155 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11156 || tv_islocked(&di->di_tv));
11157 }
11158 }
11159 }
11160 else if (lv.ll_range)
11161 EMSG(_("E745: Range not allowed"));
11162 else if (lv.ll_newkey != NULL)
11163 EMSG2(_(e_dictkey), lv.ll_newkey);
11164 else if (lv.ll_list != NULL)
11165 /* List item. */
11166 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11167 else
11168 /* Dictionary item. */
11169 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11170 }
11171 }
11172
11173 clear_lval(&lv);
11174}
11175
Bram Moolenaar33570922005-01-25 22:26:29 +000011176static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011177
11178/*
11179 * Turn a dict into a list:
11180 * "what" == 0: list of keys
11181 * "what" == 1: list of values
11182 * "what" == 2: list of items
11183 */
11184 static void
11185dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011186 typval_T *argvars;
11187 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011188 int what;
11189{
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 list_T *l;
11191 list_T *l2;
11192 dictitem_T *di;
11193 hashitem_T *hi;
11194 listitem_T *li;
11195 listitem_T *li2;
11196 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011197 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011198
11199 rettv->vval.v_number = 0;
11200 if (argvars[0].v_type != VAR_DICT)
11201 {
11202 EMSG(_(e_dictreq));
11203 return;
11204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011205 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011206 return;
11207
11208 l = list_alloc();
11209 if (l == NULL)
11210 return;
11211 rettv->v_type = VAR_LIST;
11212 rettv->vval.v_list = l;
11213 ++l->lv_refcount;
11214
Bram Moolenaar33570922005-01-25 22:26:29 +000011215 todo = d->dv_hashtab.ht_used;
11216 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011218 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011220 --todo;
11221 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011222
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011223 li = listitem_alloc();
11224 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011225 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011226 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011227
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011228 if (what == 0)
11229 {
11230 /* keys() */
11231 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011232 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011233 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11234 }
11235 else if (what == 1)
11236 {
11237 /* values() */
11238 copy_tv(&di->di_tv, &li->li_tv);
11239 }
11240 else
11241 {
11242 /* items() */
11243 l2 = list_alloc();
11244 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011245 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011246 li->li_tv.vval.v_list = l2;
11247 if (l2 == NULL)
11248 break;
11249 ++l2->lv_refcount;
11250
11251 li2 = listitem_alloc();
11252 if (li2 == NULL)
11253 break;
11254 list_append(l2, li2);
11255 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011256 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011257 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11258
11259 li2 = listitem_alloc();
11260 if (li2 == NULL)
11261 break;
11262 list_append(l2, li2);
11263 copy_tv(&di->di_tv, &li2->li_tv);
11264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011265 }
11266 }
11267}
11268
11269/*
11270 * "items(dict)" function
11271 */
11272 static void
11273f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011274 typval_T *argvars;
11275 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011276{
11277 dict_list(argvars, rettv, 2);
11278}
11279
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011281 * "join()" function
11282 */
11283 static void
11284f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *argvars;
11286 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011287{
11288 garray_T ga;
11289 char_u *sep;
11290
11291 rettv->vval.v_number = 0;
11292 if (argvars[0].v_type != VAR_LIST)
11293 {
11294 EMSG(_(e_listreq));
11295 return;
11296 }
11297 if (argvars[0].vval.v_list == NULL)
11298 return;
11299 if (argvars[1].v_type == VAR_UNKNOWN)
11300 sep = (char_u *)" ";
11301 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011303
11304 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305
11306 if (sep != NULL)
11307 {
11308 ga_init2(&ga, (int)sizeof(char), 80);
11309 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11310 ga_append(&ga, NUL);
11311 rettv->vval.v_string = (char_u *)ga.ga_data;
11312 }
11313 else
11314 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011315}
11316
11317/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011318 * "keys()" function
11319 */
11320 static void
11321f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011324{
11325 dict_list(argvars, rettv, 0);
11326}
11327
11328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 * "last_buffer_nr()" function.
11330 */
11331/*ARGSUSED*/
11332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 typval_T *argvars;
11335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
11337 int n = 0;
11338 buf_T *buf;
11339
11340 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11341 if (n < buf->b_fnum)
11342 n = buf->b_fnum;
11343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011345}
11346
11347/*
11348 * "len()" function
11349 */
11350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011352 typval_T *argvars;
11353 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011354{
11355 switch (argvars[0].v_type)
11356 {
11357 case VAR_STRING:
11358 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->vval.v_number = (varnumber_T)STRLEN(
11360 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011361 break;
11362 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011364 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011365 case VAR_DICT:
11366 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11367 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011368 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011369 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011370 break;
11371 }
11372}
11373
Bram Moolenaar33570922005-01-25 22:26:29 +000011374static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011375
11376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011378 typval_T *argvars;
11379 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011380 int type;
11381{
11382#ifdef FEAT_LIBCALL
11383 char_u *string_in;
11384 char_u **string_result;
11385 int nr_result;
11386#endif
11387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011389 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011393
11394 if (check_restricted() || check_secure())
11395 return;
11396
11397#ifdef FEAT_LIBCALL
11398 /* The first two args must be strings, otherwise its meaningless */
11399 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11400 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011401 string_in = NULL;
11402 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011403 string_in = argvars[2].vval.v_string;
11404 if (type == VAR_NUMBER)
11405 string_result = NULL;
11406 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011408 if (mch_libcall(argvars[0].vval.v_string,
11409 argvars[1].vval.v_string,
11410 string_in,
11411 argvars[2].vval.v_number,
11412 string_result,
11413 &nr_result) == OK
11414 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011416 }
11417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418}
11419
11420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011421 * "libcall()" function
11422 */
11423 static void
11424f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011425 typval_T *argvars;
11426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011427{
11428 libcall_common(argvars, rettv, VAR_STRING);
11429}
11430
11431/*
11432 * "libcallnr()" function
11433 */
11434 static void
11435f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 typval_T *argvars;
11437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011438{
11439 libcall_common(argvars, rettv, VAR_NUMBER);
11440}
11441
11442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 * "line(string)" function
11444 */
11445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011447 typval_T *argvars;
11448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449{
11450 linenr_T lnum = 0;
11451 pos_T *fp;
11452
11453 fp = var2fpos(&argvars[0], TRUE);
11454 if (fp != NULL)
11455 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457}
11458
11459/*
11460 * "line2byte(lnum)" function
11461 */
11462/*ARGSUSED*/
11463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 typval_T *argvars;
11466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467{
11468#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470#else
11471 linenr_T lnum;
11472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11478 if (rettv->vval.v_number >= 0)
11479 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480#endif
11481}
11482
11483/*
11484 * "lispindent(lnum)" function
11485 */
11486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 typval_T *argvars;
11489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490{
11491#ifdef FEAT_LISP
11492 pos_T pos;
11493 linenr_T lnum;
11494
11495 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11498 {
11499 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 curwin->w_cursor = pos;
11502 }
11503 else
11504#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506}
11507
11508/*
11509 * "localtime()" function
11510 */
11511/*ARGSUSED*/
11512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011514 typval_T *argvars;
11515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518}
11519
Bram Moolenaar33570922005-01-25 22:26:29 +000011520static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521
11522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011524 typval_T *argvars;
11525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 int exact;
11527{
11528 char_u *keys;
11529 char_u *which;
11530 char_u buf[NUMBUFLEN];
11531 char_u *keys_buf = NULL;
11532 char_u *rhs;
11533 int mode;
11534 garray_T ga;
11535
11536 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 rettv->v_type = VAR_STRING;
11538 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 if (*keys == NUL)
11542 return;
11543
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011544 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 else
11547 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 if (which == NULL)
11549 return;
11550
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 mode = get_map_mode(&which, 0);
11552
11553 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011554 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 vim_free(keys_buf);
11556 if (rhs != NULL)
11557 {
11558 ga_init(&ga);
11559 ga.ga_itemsize = 1;
11560 ga.ga_growsize = 40;
11561
11562 while (*rhs != NUL)
11563 ga_concat(&ga, str2special(&rhs, FALSE));
11564
11565 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 }
11568}
11569
11570/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011571 * "map()" function
11572 */
11573 static void
11574f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011575 typval_T *argvars;
11576 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011577{
11578 filter_map(argvars, rettv, TRUE);
11579}
11580
11581/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011582 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 */
11584 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 typval_T *argvars;
11587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011589 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590}
11591
11592/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011593 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 */
11595 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011596f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011597 typval_T *argvars;
11598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011600 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601}
11602
Bram Moolenaar33570922005-01-25 22:26:29 +000011603static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604
11605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 typval_T *argvars;
11608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 int type;
11610{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011611 char_u *str = NULL;
11612 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 char_u *pat;
11614 regmatch_T regmatch;
11615 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011616 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 char_u *save_cpo;
11618 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011619 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011620 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011621 list_T *l = NULL;
11622 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011623 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011624 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625
11626 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11627 save_cpo = p_cpo;
11628 p_cpo = (char_u *)"";
11629
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011630 rettv->vval.v_number = -1;
11631 if (type == 3)
11632 {
11633 /* return empty list when there are no matches */
11634 if ((rettv->vval.v_list = list_alloc()) == NULL)
11635 goto theend;
11636 rettv->v_type = VAR_LIST;
11637 ++rettv->vval.v_list->lv_refcount;
11638 }
11639 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641 rettv->v_type = VAR_STRING;
11642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011645 if (argvars[0].v_type == VAR_LIST)
11646 {
11647 if ((l = argvars[0].vval.v_list) == NULL)
11648 goto theend;
11649 li = l->lv_first;
11650 }
11651 else
11652 expr = str = get_tv_string(&argvars[0]);
11653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11655 if (pat == NULL)
11656 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 int error = FALSE;
11661
11662 start = get_tv_number_chk(&argvars[2], &error);
11663 if (error)
11664 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011665 if (l != NULL)
11666 {
11667 li = list_find(l, start);
11668 if (li == NULL)
11669 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011670 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011671 }
11672 else
11673 {
11674 if (start < 0)
11675 start = 0;
11676 if (start > (long)STRLEN(str))
11677 goto theend;
11678 str += start;
11679 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011680
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011681 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 nth = get_tv_number_chk(&argvars[3], &error);
11683 if (error)
11684 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 }
11686
11687 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11688 if (regmatch.regprog != NULL)
11689 {
11690 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011691
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011692 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011693 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011694 if (l != NULL)
11695 {
11696 if (li == NULL)
11697 {
11698 match = FALSE;
11699 break;
11700 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011701 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011702 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011703 if (str == NULL)
11704 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011705 }
11706
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011707 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011708
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011709 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011711 if (l == NULL && !match)
11712 break;
11713
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011714 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011715 if (l != NULL)
11716 {
11717 li = li->li_next;
11718 ++idx;
11719 }
11720 else
11721 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011722#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011723 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011724#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011726#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011727 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011728 }
11729
11730 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011732 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011733 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011734 int i;
11735
11736 /* return list with matched string and submatches */
11737 for (i = 0; i < NSUBEXP; ++i)
11738 {
11739 if (regmatch.endp[i] == NULL)
11740 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011741 if (list_append_string(rettv->vval.v_list,
11742 regmatch.startp[i],
11743 (int)(regmatch.endp[i] - regmatch.startp[i]))
11744 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011745 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011746 }
11747 }
11748 else if (type == 2)
11749 {
11750 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011751 if (l != NULL)
11752 copy_tv(&li->li_tv, rettv);
11753 else
11754 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011756 }
11757 else if (l != NULL)
11758 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 else
11760 {
11761 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 (varnumber_T)(regmatch.startp[0] - str);
11764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 }
11769 }
11770 vim_free(regmatch.regprog);
11771 }
11772
11773theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011774 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 p_cpo = save_cpo;
11776}
11777
11778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011779 * "match()" function
11780 */
11781 static void
11782f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011783 typval_T *argvars;
11784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011785{
11786 find_some_match(argvars, rettv, 1);
11787}
11788
11789/*
11790 * "matchend()" function
11791 */
11792 static void
11793f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 typval_T *argvars;
11795 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011796{
11797 find_some_match(argvars, rettv, 0);
11798}
11799
11800/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011801 * "matchlist()" function
11802 */
11803 static void
11804f_matchlist(argvars, rettv)
11805 typval_T *argvars;
11806 typval_T *rettv;
11807{
11808 find_some_match(argvars, rettv, 3);
11809}
11810
11811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812 * "matchstr()" function
11813 */
11814 static void
11815f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011818{
11819 find_some_match(argvars, rettv, 2);
11820}
11821
Bram Moolenaar33570922005-01-25 22:26:29 +000011822static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011823
11824 static void
11825max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 typval_T *argvars;
11827 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011828 int domax;
11829{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011830 long n = 0;
11831 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011833
11834 if (argvars[0].v_type == VAR_LIST)
11835 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011836 list_T *l;
11837 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011838
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011839 l = argvars[0].vval.v_list;
11840 if (l != NULL)
11841 {
11842 li = l->lv_first;
11843 if (li != NULL)
11844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011846 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011847 {
11848 li = li->li_next;
11849 if (li == NULL)
11850 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011852 if (domax ? i > n : i < n)
11853 n = i;
11854 }
11855 }
11856 }
11857 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 else if (argvars[0].v_type == VAR_DICT)
11859 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011860 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011861 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011862 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011863 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011864
11865 d = argvars[0].vval.v_dict;
11866 if (d != NULL)
11867 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011868 todo = d->dv_hashtab.ht_used;
11869 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011871 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011873 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011875 if (first)
11876 {
11877 n = i;
11878 first = FALSE;
11879 }
11880 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011881 n = i;
11882 }
11883 }
11884 }
11885 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011886 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011887 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011888 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011889}
11890
11891/*
11892 * "max()" function
11893 */
11894 static void
11895f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 typval_T *argvars;
11897 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011898{
11899 max_min(argvars, rettv, TRUE);
11900}
11901
11902/*
11903 * "min()" function
11904 */
11905 static void
11906f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011907 typval_T *argvars;
11908 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011909{
11910 max_min(argvars, rettv, FALSE);
11911}
11912
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011913static int mkdir_recurse __ARGS((char_u *dir, int prot));
11914
11915/*
11916 * Create the directory in which "dir" is located, and higher levels when
11917 * needed.
11918 */
11919 static int
11920mkdir_recurse(dir, prot)
11921 char_u *dir;
11922 int prot;
11923{
11924 char_u *p;
11925 char_u *updir;
11926 int r = FAIL;
11927
11928 /* Get end of directory name in "dir".
11929 * We're done when it's "/" or "c:/". */
11930 p = gettail_sep(dir);
11931 if (p <= get_past_head(dir))
11932 return OK;
11933
11934 /* If the directory exists we're done. Otherwise: create it.*/
11935 updir = vim_strnsave(dir, (int)(p - dir));
11936 if (updir == NULL)
11937 return FAIL;
11938 if (mch_isdir(updir))
11939 r = OK;
11940 else if (mkdir_recurse(updir, prot) == OK)
11941 r = vim_mkdir_emsg(updir, prot);
11942 vim_free(updir);
11943 return r;
11944}
11945
11946#ifdef vim_mkdir
11947/*
11948 * "mkdir()" function
11949 */
11950 static void
11951f_mkdir(argvars, rettv)
11952 typval_T *argvars;
11953 typval_T *rettv;
11954{
11955 char_u *dir;
11956 char_u buf[NUMBUFLEN];
11957 int prot = 0755;
11958
11959 rettv->vval.v_number = FAIL;
11960 if (check_restricted() || check_secure())
11961 return;
11962
11963 dir = get_tv_string_buf(&argvars[0], buf);
11964 if (argvars[1].v_type != VAR_UNKNOWN)
11965 {
11966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 prot = get_tv_number_chk(&argvars[2], NULL);
11968 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011969 mkdir_recurse(dir, prot);
11970 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011972}
11973#endif
11974
Bram Moolenaar0d660222005-01-07 21:51:51 +000011975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 * "mode()" function
11977 */
11978/*ARGSUSED*/
11979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011981 typval_T *argvars;
11982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983{
11984 char_u buf[2];
11985
11986#ifdef FEAT_VISUAL
11987 if (VIsual_active)
11988 {
11989 if (VIsual_select)
11990 buf[0] = VIsual_mode + 's' - 'v';
11991 else
11992 buf[0] = VIsual_mode;
11993 }
11994 else
11995#endif
11996 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11997 buf[0] = 'r';
11998 else if (State & INSERT)
11999 {
12000 if (State & REPLACE_FLAG)
12001 buf[0] = 'R';
12002 else
12003 buf[0] = 'i';
12004 }
12005 else if (State & CMDLINE)
12006 buf[0] = 'c';
12007 else
12008 buf[0] = 'n';
12009
12010 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->vval.v_string = vim_strsave(buf);
12012 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013}
12014
12015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016 * "nextnonblank()" function
12017 */
12018 static void
12019f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022{
12023 linenr_T lnum;
12024
12025 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012028 {
12029 lnum = 0;
12030 break;
12031 }
12032 if (*skipwhite(ml_get(lnum)) != NUL)
12033 break;
12034 }
12035 rettv->vval.v_number = lnum;
12036}
12037
12038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 * "nr2char()" function
12040 */
12041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012043 typval_T *argvars;
12044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045{
12046 char_u buf[NUMBUFLEN];
12047
12048#ifdef FEAT_MBYTE
12049 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 else
12052#endif
12053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 buf[1] = NUL;
12056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->v_type = VAR_STRING;
12058 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012059}
12060
12061/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012062 * "prevnonblank()" function
12063 */
12064 static void
12065f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 typval_T *argvars;
12067 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068{
12069 linenr_T lnum;
12070
12071 lnum = get_tv_lnum(argvars);
12072 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12073 lnum = 0;
12074 else
12075 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12076 --lnum;
12077 rettv->vval.v_number = lnum;
12078}
12079
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012080#ifdef HAVE_STDARG_H
12081/* This dummy va_list is here because:
12082 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12083 * - locally in the function results in a "used before set" warning
12084 * - using va_start() to initialize it gives "function with fixed args" error */
12085static va_list ap;
12086#endif
12087
Bram Moolenaar8c711452005-01-14 21:53:12 +000012088/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012089 * "printf()" function
12090 */
12091 static void
12092f_printf(argvars, rettv)
12093 typval_T *argvars;
12094 typval_T *rettv;
12095{
12096 rettv->v_type = VAR_STRING;
12097 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012098#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012099 {
12100 char_u buf[NUMBUFLEN];
12101 int len;
12102 char_u *s;
12103 int saved_did_emsg = did_emsg;
12104 char *fmt;
12105
12106 /* Get the required length, allocate the buffer and do it for real. */
12107 did_emsg = FALSE;
12108 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012109 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012110 if (!did_emsg)
12111 {
12112 s = alloc(len + 1);
12113 if (s != NULL)
12114 {
12115 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012116 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012117 }
12118 }
12119 did_emsg |= saved_did_emsg;
12120 }
12121#endif
12122}
12123
12124/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012125 * "range()" function
12126 */
12127 static void
12128f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012129 typval_T *argvars;
12130 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012131{
12132 long start;
12133 long end;
12134 long stride = 1;
12135 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012136 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012139 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012140 if (argvars[1].v_type == VAR_UNKNOWN)
12141 {
12142 end = start - 1;
12143 start = 0;
12144 }
12145 else
12146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012147 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012148 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012149 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012150 }
12151
12152 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 if (error)
12154 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012155 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012156 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012157 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012158 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012159 else
12160 {
12161 l = list_alloc();
12162 if (l != NULL)
12163 {
12164 rettv->v_type = VAR_LIST;
12165 rettv->vval.v_list = l;
12166 ++l->lv_refcount;
12167
12168 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012169 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012170 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012171 }
12172 }
12173}
12174
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012175/*
12176 * "readfile()" function
12177 */
12178 static void
12179f_readfile(argvars, rettv)
12180 typval_T *argvars;
12181 typval_T *rettv;
12182{
12183 int binary = FALSE;
12184 char_u *fname;
12185 FILE *fd;
12186 list_T *l;
12187 listitem_T *li;
12188#define FREAD_SIZE 200 /* optimized for text lines */
12189 char_u buf[FREAD_SIZE];
12190 int readlen; /* size of last fread() */
12191 int buflen; /* nr of valid chars in buf[] */
12192 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12193 int tolist; /* first byte in buf[] still to be put in list */
12194 int chop; /* how many CR to chop off */
12195 char_u *prev = NULL; /* previously read bytes, if any */
12196 int prevlen = 0; /* length of "prev" if not NULL */
12197 char_u *s;
12198 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012199 long maxline = MAXLNUM;
12200 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012201
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012202 if (argvars[1].v_type != VAR_UNKNOWN)
12203 {
12204 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12205 binary = TRUE;
12206 if (argvars[2].v_type != VAR_UNKNOWN)
12207 maxline = get_tv_number(&argvars[2]);
12208 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012209
12210 l = list_alloc();
12211 if (l == NULL)
12212 return;
12213 rettv->v_type = VAR_LIST;
12214 rettv->vval.v_list = l;
12215 l->lv_refcount = 1;
12216
12217 /* Always open the file in binary mode, library functions have a mind of
12218 * their own about CR-LF conversion. */
12219 fname = get_tv_string(&argvars[0]);
12220 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12221 {
12222 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12223 return;
12224 }
12225
12226 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012227 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012228 {
12229 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12230 buflen = filtd + readlen;
12231 tolist = 0;
12232 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12233 {
12234 if (buf[filtd] == '\n' || readlen <= 0)
12235 {
12236 /* Only when in binary mode add an empty list item when the
12237 * last line ends in a '\n'. */
12238 if (!binary && readlen == 0 && filtd == 0)
12239 break;
12240
12241 /* Found end-of-line or end-of-file: add a text line to the
12242 * list. */
12243 chop = 0;
12244 if (!binary)
12245 while (filtd - chop - 1 >= tolist
12246 && buf[filtd - chop - 1] == '\r')
12247 ++chop;
12248 len = filtd - tolist - chop;
12249 if (prev == NULL)
12250 s = vim_strnsave(buf + tolist, len);
12251 else
12252 {
12253 s = alloc((unsigned)(prevlen + len + 1));
12254 if (s != NULL)
12255 {
12256 mch_memmove(s, prev, prevlen);
12257 vim_free(prev);
12258 prev = NULL;
12259 mch_memmove(s + prevlen, buf + tolist, len);
12260 s[prevlen + len] = NUL;
12261 }
12262 }
12263 tolist = filtd + 1;
12264
12265 li = listitem_alloc();
12266 if (li == NULL)
12267 {
12268 vim_free(s);
12269 break;
12270 }
12271 li->li_tv.v_type = VAR_STRING;
12272 li->li_tv.v_lock = 0;
12273 li->li_tv.vval.v_string = s;
12274 list_append(l, li);
12275
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012276 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012277 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012278 if (readlen <= 0)
12279 break;
12280 }
12281 else if (buf[filtd] == NUL)
12282 buf[filtd] = '\n';
12283 }
12284 if (readlen <= 0)
12285 break;
12286
12287 if (tolist == 0)
12288 {
12289 /* "buf" is full, need to move text to an allocated buffer */
12290 if (prev == NULL)
12291 {
12292 prev = vim_strnsave(buf, buflen);
12293 prevlen = buflen;
12294 }
12295 else
12296 {
12297 s = alloc((unsigned)(prevlen + buflen));
12298 if (s != NULL)
12299 {
12300 mch_memmove(s, prev, prevlen);
12301 mch_memmove(s + prevlen, buf, buflen);
12302 vim_free(prev);
12303 prev = s;
12304 prevlen += buflen;
12305 }
12306 }
12307 filtd = 0;
12308 }
12309 else
12310 {
12311 mch_memmove(buf, buf + tolist, buflen - tolist);
12312 filtd -= tolist;
12313 }
12314 }
12315
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012316 /*
12317 * For a negative line count use only the lines at the end of the file,
12318 * free the rest.
12319 */
12320 if (maxline < 0)
12321 while (cnt > -maxline)
12322 {
12323 listitem_remove(l, l->lv_first);
12324 --cnt;
12325 }
12326
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012327 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012328 fclose(fd);
12329}
12330
12331
Bram Moolenaar0d660222005-01-07 21:51:51 +000012332#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12333static void make_connection __ARGS((void));
12334static int check_connection __ARGS((void));
12335
12336 static void
12337make_connection()
12338{
12339 if (X_DISPLAY == NULL
12340# ifdef FEAT_GUI
12341 && !gui.in_use
12342# endif
12343 )
12344 {
12345 x_force_connect = TRUE;
12346 setup_term_clip();
12347 x_force_connect = FALSE;
12348 }
12349}
12350
12351 static int
12352check_connection()
12353{
12354 make_connection();
12355 if (X_DISPLAY == NULL)
12356 {
12357 EMSG(_("E240: No connection to Vim server"));
12358 return FAIL;
12359 }
12360 return OK;
12361}
12362#endif
12363
12364#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012365static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012366
12367 static void
12368remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012369 typval_T *argvars;
12370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371 int expr;
12372{
12373 char_u *server_name;
12374 char_u *keys;
12375 char_u *r = NULL;
12376 char_u buf[NUMBUFLEN];
12377# ifdef WIN32
12378 HWND w;
12379# else
12380 Window w;
12381# endif
12382
12383 if (check_restricted() || check_secure())
12384 return;
12385
12386# ifdef FEAT_X11
12387 if (check_connection() == FAIL)
12388 return;
12389# endif
12390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 server_name = get_tv_string_chk(&argvars[0]);
12392 if (server_name == NULL)
12393 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394 keys = get_tv_string_buf(&argvars[1], buf);
12395# ifdef WIN32
12396 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12397# else
12398 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12399 < 0)
12400# endif
12401 {
12402 if (r != NULL)
12403 EMSG(r); /* sending worked but evaluation failed */
12404 else
12405 EMSG2(_("E241: Unable to send to %s"), server_name);
12406 return;
12407 }
12408
12409 rettv->vval.v_string = r;
12410
12411 if (argvars[2].v_type != VAR_UNKNOWN)
12412 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012413 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012414 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012415 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416
12417 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012418 v.di_tv.v_type = VAR_STRING;
12419 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012420 idvar = get_tv_string_chk(&argvars[2]);
12421 if (idvar != NULL)
12422 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012423 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012424 }
12425}
12426#endif
12427
12428/*
12429 * "remote_expr()" function
12430 */
12431/*ARGSUSED*/
12432 static void
12433f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012434 typval_T *argvars;
12435 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012436{
12437 rettv->v_type = VAR_STRING;
12438 rettv->vval.v_string = NULL;
12439#ifdef FEAT_CLIENTSERVER
12440 remote_common(argvars, rettv, TRUE);
12441#endif
12442}
12443
12444/*
12445 * "remote_foreground()" function
12446 */
12447/*ARGSUSED*/
12448 static void
12449f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012450 typval_T *argvars;
12451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012452{
12453 rettv->vval.v_number = 0;
12454#ifdef FEAT_CLIENTSERVER
12455# ifdef WIN32
12456 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012457 {
12458 char_u *server_name = get_tv_string_chk(&argvars[0]);
12459
12460 if (server_name != NULL)
12461 serverForeground(server_name);
12462 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012463# else
12464 /* Send a foreground() expression to the server. */
12465 argvars[1].v_type = VAR_STRING;
12466 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12467 argvars[2].v_type = VAR_UNKNOWN;
12468 remote_common(argvars, rettv, TRUE);
12469 vim_free(argvars[1].vval.v_string);
12470# endif
12471#endif
12472}
12473
12474/*ARGSUSED*/
12475 static void
12476f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012477 typval_T *argvars;
12478 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012479{
12480#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012481 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482 char_u *s = NULL;
12483# ifdef WIN32
12484 int n = 0;
12485# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012486 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487
12488 if (check_restricted() || check_secure())
12489 {
12490 rettv->vval.v_number = -1;
12491 return;
12492 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012493 serverid = get_tv_string_chk(&argvars[0]);
12494 if (serverid == NULL)
12495 {
12496 rettv->vval.v_number = -1;
12497 return; /* type error; errmsg already given */
12498 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012500 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501 if (n == 0)
12502 rettv->vval.v_number = -1;
12503 else
12504 {
12505 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12506 rettv->vval.v_number = (s != NULL);
12507 }
12508# else
12509 rettv->vval.v_number = 0;
12510 if (check_connection() == FAIL)
12511 return;
12512
12513 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012515# endif
12516
12517 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012519 char_u *retvar;
12520
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 v.di_tv.v_type = VAR_STRING;
12522 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012523 retvar = get_tv_string_chk(&argvars[1]);
12524 if (retvar != NULL)
12525 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527 }
12528#else
12529 rettv->vval.v_number = -1;
12530#endif
12531}
12532
12533/*ARGSUSED*/
12534 static void
12535f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 typval_T *argvars;
12537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538{
12539 char_u *r = NULL;
12540
12541#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 char_u *serverid = get_tv_string_chk(&argvars[0]);
12543
12544 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545 {
12546# ifdef WIN32
12547 /* The server's HWND is encoded in the 'id' parameter */
12548 int n = 0;
12549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012550 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012551 if (n != 0)
12552 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12553 if (r == NULL)
12554# else
12555 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012556 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012557# endif
12558 EMSG(_("E277: Unable to read a server reply"));
12559 }
12560#endif
12561 rettv->v_type = VAR_STRING;
12562 rettv->vval.v_string = r;
12563}
12564
12565/*
12566 * "remote_send()" function
12567 */
12568/*ARGSUSED*/
12569 static void
12570f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T *argvars;
12572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573{
12574 rettv->v_type = VAR_STRING;
12575 rettv->vval.v_string = NULL;
12576#ifdef FEAT_CLIENTSERVER
12577 remote_common(argvars, rettv, FALSE);
12578#endif
12579}
12580
12581/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012582 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012583 */
12584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012585f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012586 typval_T *argvars;
12587 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012588{
Bram Moolenaar33570922005-01-25 22:26:29 +000012589 list_T *l;
12590 listitem_T *item, *item2;
12591 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012592 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012593 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012594 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 dict_T *d;
12596 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012597
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012598 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012599 if (argvars[0].v_type == VAR_DICT)
12600 {
12601 if (argvars[2].v_type != VAR_UNKNOWN)
12602 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012603 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012604 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 key = get_tv_string_chk(&argvars[1]);
12607 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 di = dict_find(d, key, -1);
12610 if (di == NULL)
12611 EMSG2(_(e_dictkey), key);
12612 else
12613 {
12614 *rettv = di->di_tv;
12615 init_tv(&di->di_tv);
12616 dictitem_remove(d, di);
12617 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012619 }
12620 }
12621 else if (argvars[0].v_type != VAR_LIST)
12622 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012623 else if ((l = argvars[0].vval.v_list) != NULL
12624 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012626 int error = FALSE;
12627
12628 idx = get_tv_number_chk(&argvars[1], &error);
12629 if (error)
12630 ; /* type error: do nothing, errmsg already given */
12631 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012632 EMSGN(_(e_listidx), idx);
12633 else
12634 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012635 if (argvars[2].v_type == VAR_UNKNOWN)
12636 {
12637 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012638 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012639 *rettv = item->li_tv;
12640 vim_free(item);
12641 }
12642 else
12643 {
12644 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645 end = get_tv_number_chk(&argvars[2], &error);
12646 if (error)
12647 ; /* type error: do nothing */
12648 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012649 EMSGN(_(e_listidx), end);
12650 else
12651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012652 int cnt = 0;
12653
12654 for (li = item; li != NULL; li = li->li_next)
12655 {
12656 ++cnt;
12657 if (li == item2)
12658 break;
12659 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012660 if (li == NULL) /* didn't find "item2" after "item" */
12661 EMSG(_(e_invrange));
12662 else
12663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012664 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012665 l = list_alloc();
12666 if (l != NULL)
12667 {
12668 rettv->v_type = VAR_LIST;
12669 rettv->vval.v_list = l;
12670 l->lv_first = item;
12671 l->lv_last = item2;
12672 l->lv_refcount = 1;
12673 item->li_prev = NULL;
12674 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012675 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012676 }
12677 }
12678 }
12679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012680 }
12681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682}
12683
12684/*
12685 * "rename({from}, {to})" function
12686 */
12687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012689 typval_T *argvars;
12690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691{
12692 char_u buf[NUMBUFLEN];
12693
12694 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12698 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699}
12700
12701/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012702 * "repeat()" function
12703 */
12704/*ARGSUSED*/
12705 static void
12706f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012707 typval_T *argvars;
12708 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012709{
12710 char_u *p;
12711 int n;
12712 int slen;
12713 int len;
12714 char_u *r;
12715 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012716 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012717
12718 n = get_tv_number(&argvars[1]);
12719 if (argvars[0].v_type == VAR_LIST)
12720 {
12721 l = list_alloc();
12722 if (l != NULL && argvars[0].vval.v_list != NULL)
12723 {
12724 l->lv_refcount = 1;
12725 while (n-- > 0)
12726 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12727 break;
12728 }
12729 rettv->v_type = VAR_LIST;
12730 rettv->vval.v_list = l;
12731 }
12732 else
12733 {
12734 p = get_tv_string(&argvars[0]);
12735 rettv->v_type = VAR_STRING;
12736 rettv->vval.v_string = NULL;
12737
12738 slen = (int)STRLEN(p);
12739 len = slen * n;
12740 if (len <= 0)
12741 return;
12742
12743 r = alloc(len + 1);
12744 if (r != NULL)
12745 {
12746 for (i = 0; i < n; i++)
12747 mch_memmove(r + i * slen, p, (size_t)slen);
12748 r[len] = NUL;
12749 }
12750
12751 rettv->vval.v_string = r;
12752 }
12753}
12754
12755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756 * "resolve()" function
12757 */
12758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012760 typval_T *argvars;
12761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762{
12763 char_u *p;
12764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766#ifdef FEAT_SHORTCUT
12767 {
12768 char_u *v = NULL;
12769
12770 v = mch_resolve_shortcut(p);
12771 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 }
12776#else
12777# ifdef HAVE_READLINK
12778 {
12779 char_u buf[MAXPATHL + 1];
12780 char_u *cpy;
12781 int len;
12782 char_u *remain = NULL;
12783 char_u *q;
12784 int is_relative_to_current = FALSE;
12785 int has_trailing_pathsep = FALSE;
12786 int limit = 100;
12787
12788 p = vim_strsave(p);
12789
12790 if (p[0] == '.' && (vim_ispathsep(p[1])
12791 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12792 is_relative_to_current = TRUE;
12793
12794 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012795 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 has_trailing_pathsep = TRUE;
12797
12798 q = getnextcomp(p);
12799 if (*q != NUL)
12800 {
12801 /* Separate the first path component in "p", and keep the
12802 * remainder (beginning with the path separator). */
12803 remain = vim_strsave(q - 1);
12804 q[-1] = NUL;
12805 }
12806
12807 for (;;)
12808 {
12809 for (;;)
12810 {
12811 len = readlink((char *)p, (char *)buf, MAXPATHL);
12812 if (len <= 0)
12813 break;
12814 buf[len] = NUL;
12815
12816 if (limit-- == 0)
12817 {
12818 vim_free(p);
12819 vim_free(remain);
12820 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 goto fail;
12823 }
12824
12825 /* Ensure that the result will have a trailing path separator
12826 * if the argument has one. */
12827 if (remain == NULL && has_trailing_pathsep)
12828 add_pathsep(buf);
12829
12830 /* Separate the first path component in the link value and
12831 * concatenate the remainders. */
12832 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12833 if (*q != NUL)
12834 {
12835 if (remain == NULL)
12836 remain = vim_strsave(q - 1);
12837 else
12838 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012839 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 if (cpy != NULL)
12841 {
12842 STRCAT(cpy, remain);
12843 vim_free(remain);
12844 remain = cpy;
12845 }
12846 }
12847 q[-1] = NUL;
12848 }
12849
12850 q = gettail(p);
12851 if (q > p && *q == NUL)
12852 {
12853 /* Ignore trailing path separator. */
12854 q[-1] = NUL;
12855 q = gettail(p);
12856 }
12857 if (q > p && !mch_isFullName(buf))
12858 {
12859 /* symlink is relative to directory of argument */
12860 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12861 if (cpy != NULL)
12862 {
12863 STRCPY(cpy, p);
12864 STRCPY(gettail(cpy), buf);
12865 vim_free(p);
12866 p = cpy;
12867 }
12868 }
12869 else
12870 {
12871 vim_free(p);
12872 p = vim_strsave(buf);
12873 }
12874 }
12875
12876 if (remain == NULL)
12877 break;
12878
12879 /* Append the first path component of "remain" to "p". */
12880 q = getnextcomp(remain + 1);
12881 len = q - remain - (*q != NUL);
12882 cpy = vim_strnsave(p, STRLEN(p) + len);
12883 if (cpy != NULL)
12884 {
12885 STRNCAT(cpy, remain, len);
12886 vim_free(p);
12887 p = cpy;
12888 }
12889 /* Shorten "remain". */
12890 if (*q != NUL)
12891 STRCPY(remain, q - 1);
12892 else
12893 {
12894 vim_free(remain);
12895 remain = NULL;
12896 }
12897 }
12898
12899 /* If the result is a relative path name, make it explicitly relative to
12900 * the current directory if and only if the argument had this form. */
12901 if (!vim_ispathsep(*p))
12902 {
12903 if (is_relative_to_current
12904 && *p != NUL
12905 && !(p[0] == '.'
12906 && (p[1] == NUL
12907 || vim_ispathsep(p[1])
12908 || (p[1] == '.'
12909 && (p[2] == NUL
12910 || vim_ispathsep(p[2]))))))
12911 {
12912 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012913 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 if (cpy != NULL)
12915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 vim_free(p);
12917 p = cpy;
12918 }
12919 }
12920 else if (!is_relative_to_current)
12921 {
12922 /* Strip leading "./". */
12923 q = p;
12924 while (q[0] == '.' && vim_ispathsep(q[1]))
12925 q += 2;
12926 if (q > p)
12927 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12928 }
12929 }
12930
12931 /* Ensure that the result will have no trailing path separator
12932 * if the argument had none. But keep "/" or "//". */
12933 if (!has_trailing_pathsep)
12934 {
12935 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012936 if (after_pathsep(p, q))
12937 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 }
12939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 }
12942# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944# endif
12945#endif
12946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948
12949#ifdef HAVE_READLINK
12950fail:
12951#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953}
12954
12955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 */
12958 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012959f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 typval_T *argvars;
12961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962{
Bram Moolenaar33570922005-01-25 22:26:29 +000012963 list_T *l;
12964 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965
Bram Moolenaar0d660222005-01-07 21:51:51 +000012966 rettv->vval.v_number = 0;
12967 if (argvars[0].v_type != VAR_LIST)
12968 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012969 else if ((l = argvars[0].vval.v_list) != NULL
12970 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971 {
12972 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012973 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012974 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012975 while (li != NULL)
12976 {
12977 ni = li->li_prev;
12978 list_append(l, li);
12979 li = ni;
12980 }
12981 rettv->vval.v_list = l;
12982 rettv->v_type = VAR_LIST;
12983 ++l->lv_refcount;
12984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012987#define SP_NOMOVE 1 /* don't move cursor */
12988#define SP_REPEAT 2 /* repeat to find outer pair */
12989#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012990#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012991
Bram Moolenaar33570922005-01-25 22:26:29 +000012992static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012993
12994/*
12995 * Get flags for a search function.
12996 * Possibly sets "p_ws".
12997 * Returns BACKWARD, FORWARD or zero (for an error).
12998 */
12999 static int
13000get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013001 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013002 int *flagsp;
13003{
13004 int dir = FORWARD;
13005 char_u *flags;
13006 char_u nbuf[NUMBUFLEN];
13007 int mask;
13008
13009 if (varp->v_type != VAR_UNKNOWN)
13010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013011 flags = get_tv_string_buf_chk(varp, nbuf);
13012 if (flags == NULL)
13013 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013014 while (*flags != NUL)
13015 {
13016 switch (*flags)
13017 {
13018 case 'b': dir = BACKWARD; break;
13019 case 'w': p_ws = TRUE; break;
13020 case 'W': p_ws = FALSE; break;
13021 default: mask = 0;
13022 if (flagsp != NULL)
13023 switch (*flags)
13024 {
13025 case 'n': mask = SP_NOMOVE; break;
13026 case 'r': mask = SP_REPEAT; break;
13027 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013028 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013029 }
13030 if (mask == 0)
13031 {
13032 EMSG2(_(e_invarg2), flags);
13033 dir = 0;
13034 }
13035 else
13036 *flagsp |= mask;
13037 }
13038 if (dir == 0)
13039 break;
13040 ++flags;
13041 }
13042 }
13043 return dir;
13044}
13045
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046/*
13047 * "search()" function
13048 */
13049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013051 typval_T *argvars;
13052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
13054 char_u *pat;
13055 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013056 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 int save_p_ws = p_ws;
13058 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013059 int flags = 0;
13060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013064 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13065 if (dir == 0)
13066 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013067 /*
13068 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13069 * Check to make sure only those flags are set.
13070 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13071 * flags cannot be set. Check for that condition also.
13072 */
13073 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13074 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013077 goto theend;
13078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013080 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13082 SEARCH_KEEP, RE_SEARCH) != FAIL)
13083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013084 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013085 if (flags & SP_SETPCMARK)
13086 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 curwin->w_cursor = pos;
13088 /* "/$" will put the cursor after the end of the line, may need to
13089 * correct that here */
13090 check_cursor();
13091 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013092
13093 /* If 'n' flag is used: restore cursor position. */
13094 if (flags & SP_NOMOVE)
13095 curwin->w_cursor = save_cursor;
13096theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097 p_ws = save_p_ws;
13098}
13099
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013101 * "searchdecl()" function
13102 */
13103 static void
13104f_searchdecl(argvars, rettv)
13105 typval_T *argvars;
13106 typval_T *rettv;
13107{
13108 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013109 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013110 int error = FALSE;
13111 char_u *name;
13112
13113 rettv->vval.v_number = 1; /* default: FAIL */
13114
13115 name = get_tv_string_chk(&argvars[0]);
13116 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013117 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013118 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013119 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13120 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13121 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013122 if (!error && name != NULL)
13123 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013124 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013125}
13126
13127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 * "searchpair()" function
13129 */
13130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013132 typval_T *argvars;
13133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134{
13135 char_u *spat, *mpat, *epat;
13136 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 int dir;
13139 int flags = 0;
13140 char_u nbuf1[NUMBUFLEN];
13141 char_u nbuf2[NUMBUFLEN];
13142 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013144 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013147 spat = get_tv_string_chk(&argvars[0]);
13148 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13149 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13150 if (spat == NULL || mpat == NULL || epat == NULL)
13151 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 /* Handle the optional fourth argument: flags */
13154 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013155 if (dir == 0)
13156 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013157 /*
13158 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13159 */
13160 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13161 {
13162 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13163 goto theend;
13164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165
13166 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 if (argvars[3].v_type == VAR_UNKNOWN
13168 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 skip = (char_u *)"";
13170 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013171 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13172 if (skip == NULL)
13173 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013175 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13176
13177theend:
13178 p_ws = save_p_ws;
13179}
13180
13181/*
13182 * Search for a start/middle/end thing.
13183 * Used by searchpair(), see its documentation for the details.
13184 * Returns 0 or -1 for no match,
13185 */
13186 long
13187do_searchpair(spat, mpat, epat, dir, skip, flags)
13188 char_u *spat; /* start pattern */
13189 char_u *mpat; /* middle pattern */
13190 char_u *epat; /* end pattern */
13191 int dir; /* BACKWARD or FORWARD */
13192 char_u *skip; /* skip expression */
13193 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13194{
13195 char_u *save_cpo;
13196 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13197 long retval = 0;
13198 pos_T pos;
13199 pos_T firstpos;
13200 pos_T foundpos;
13201 pos_T save_cursor;
13202 pos_T save_pos;
13203 int n;
13204 int r;
13205 int nest = 1;
13206 int err;
13207
13208 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13209 save_cpo = p_cpo;
13210 p_cpo = (char_u *)"";
13211
13212 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13213 * start/middle/end (pat3, for the top pair). */
13214 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13215 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13216 if (pat2 == NULL || pat3 == NULL)
13217 goto theend;
13218 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13219 if (*mpat == NUL)
13220 STRCPY(pat3, pat2);
13221 else
13222 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13223 spat, epat, mpat);
13224
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 save_cursor = curwin->w_cursor;
13226 pos = curwin->w_cursor;
13227 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013228 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 pat = pat3;
13230 for (;;)
13231 {
13232 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13233 SEARCH_KEEP, RE_SEARCH);
13234 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13235 /* didn't find it or found the first match again: FAIL */
13236 break;
13237
13238 if (firstpos.lnum == 0)
13239 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013240 if (equalpos(pos, foundpos))
13241 {
13242 /* Found the same position again. Can happen with a pattern that
13243 * has "\zs" at the end and searching backwards. Advance one
13244 * character and try again. */
13245 if (dir == BACKWARD)
13246 decl(&pos);
13247 else
13248 incl(&pos);
13249 }
13250 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251
13252 /* If the skip pattern matches, ignore this match. */
13253 if (*skip != NUL)
13254 {
13255 save_pos = curwin->w_cursor;
13256 curwin->w_cursor = pos;
13257 r = eval_to_bool(skip, &err, NULL, FALSE);
13258 curwin->w_cursor = save_pos;
13259 if (err)
13260 {
13261 /* Evaluating {skip} caused an error, break here. */
13262 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013263 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 break;
13265 }
13266 if (r)
13267 continue;
13268 }
13269
13270 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13271 {
13272 /* Found end when searching backwards or start when searching
13273 * forward: nested pair. */
13274 ++nest;
13275 pat = pat2; /* nested, don't search for middle */
13276 }
13277 else
13278 {
13279 /* Found end when searching forward or start when searching
13280 * backward: end of (nested) pair; or found middle in outer pair. */
13281 if (--nest == 1)
13282 pat = pat3; /* outer level, search for middle */
13283 }
13284
13285 if (nest == 0)
13286 {
13287 /* Found the match: return matchcount or line number. */
13288 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013289 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013291 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013292 if (flags & SP_SETPCMARK)
13293 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 curwin->w_cursor = pos;
13295 if (!(flags & SP_REPEAT))
13296 break;
13297 nest = 1; /* search for next unmatched */
13298 }
13299 }
13300
13301 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013302 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 curwin->w_cursor = save_cursor;
13304
13305theend:
13306 vim_free(pat2);
13307 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013309
13310 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311}
13312
Bram Moolenaar0d660222005-01-07 21:51:51 +000013313/*ARGSUSED*/
13314 static void
13315f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013316 typval_T *argvars;
13317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013319#ifdef FEAT_CLIENTSERVER
13320 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 char_u *server = get_tv_string_chk(&argvars[0]);
13322 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323
Bram Moolenaar0d660222005-01-07 21:51:51 +000013324 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013325 if (server == NULL || reply == NULL)
13326 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013327 if (check_restricted() || check_secure())
13328 return;
13329# ifdef FEAT_X11
13330 if (check_connection() == FAIL)
13331 return;
13332# endif
13333
13334 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013336 EMSG(_("E258: Unable to send to client"));
13337 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013339 rettv->vval.v_number = 0;
13340#else
13341 rettv->vval.v_number = -1;
13342#endif
13343}
13344
13345/*ARGSUSED*/
13346 static void
13347f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 typval_T *argvars;
13349 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013350{
13351 char_u *r = NULL;
13352
13353#ifdef FEAT_CLIENTSERVER
13354# ifdef WIN32
13355 r = serverGetVimNames();
13356# else
13357 make_connection();
13358 if (X_DISPLAY != NULL)
13359 r = serverGetVimNames(X_DISPLAY);
13360# endif
13361#endif
13362 rettv->v_type = VAR_STRING;
13363 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
13366/*
13367 * "setbufvar()" function
13368 */
13369/*ARGSUSED*/
13370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013372 typval_T *argvars;
13373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374{
13375 buf_T *buf;
13376#ifdef FEAT_AUTOCMD
13377 aco_save_T aco;
13378#else
13379 buf_T *save_curbuf;
13380#endif
13381 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 char_u nbuf[NUMBUFLEN];
13384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013385 rettv->vval.v_number = 0;
13386
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 if (check_restricted() || check_secure())
13388 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13390 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392 varp = &argvars[2];
13393
13394 if (buf != NULL && varname != NULL && varp != NULL)
13395 {
13396 /* set curbuf to be our buf, temporarily */
13397#ifdef FEAT_AUTOCMD
13398 aucmd_prepbuf(&aco, buf);
13399#else
13400 save_curbuf = curbuf;
13401 curbuf = buf;
13402#endif
13403
13404 if (*varname == '&')
13405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013406 long numval;
13407 char_u *strval;
13408 int error = FALSE;
13409
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 numval = get_tv_number_chk(varp, &error);
13412 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013413 if (!error && strval != NULL)
13414 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 }
13416 else
13417 {
13418 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13419 if (bufvarname != NULL)
13420 {
13421 STRCPY(bufvarname, "b:");
13422 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013423 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424 vim_free(bufvarname);
13425 }
13426 }
13427
13428 /* reset notion of buffer */
13429#ifdef FEAT_AUTOCMD
13430 aucmd_restbuf(&aco);
13431#else
13432 curbuf = save_curbuf;
13433#endif
13434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
13437/*
13438 * "setcmdpos()" function
13439 */
13440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 typval_T *argvars;
13443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013445 int pos = (int)get_tv_number(&argvars[0]) - 1;
13446
13447 if (pos >= 0)
13448 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449}
13450
13451/*
13452 * "setline()" function
13453 */
13454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
13459 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013460 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013461 list_T *l = NULL;
13462 listitem_T *li = NULL;
13463 long added = 0;
13464 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013466 lnum = get_tv_lnum(&argvars[0]);
13467 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013469 l = argvars[1].vval.v_list;
13470 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013472 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013473 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013474
13475 rettv->vval.v_number = 0; /* OK */
13476 for (;;)
13477 {
13478 if (l != NULL)
13479 {
13480 /* list argument, get next string */
13481 if (li == NULL)
13482 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013484 li = li->li_next;
13485 }
13486
13487 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013488 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013489 break;
13490 if (lnum <= curbuf->b_ml.ml_line_count)
13491 {
13492 /* existing line, replace it */
13493 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13494 {
13495 changed_bytes(lnum, 0);
13496 check_cursor_col();
13497 rettv->vval.v_number = 0; /* OK */
13498 }
13499 }
13500 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13501 {
13502 /* lnum is one past the last line, append the line */
13503 ++added;
13504 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13505 rettv->vval.v_number = 0; /* OK */
13506 }
13507
13508 if (l == NULL) /* only one string argument */
13509 break;
13510 ++lnum;
13511 }
13512
13513 if (added > 0)
13514 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515}
13516
13517/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013518 * "setqflist()" function
13519 */
13520/*ARGSUSED*/
13521 static void
13522f_setqflist(argvars, rettv)
13523 typval_T *argvars;
13524 typval_T *rettv;
13525{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013526 char_u *act;
13527 int action = ' ';
13528
Bram Moolenaar2641f772005-03-25 21:58:17 +000013529 rettv->vval.v_number = -1;
13530
13531#ifdef FEAT_QUICKFIX
13532 if (argvars[0].v_type != VAR_LIST)
13533 EMSG(_(e_listreq));
13534 else
13535 {
13536 list_T *l = argvars[0].vval.v_list;
13537
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013538 if (argvars[1].v_type == VAR_STRING)
13539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 act = get_tv_string_chk(&argvars[1]);
13541 if (act == NULL)
13542 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013543 if (*act == 'a' || *act == 'r')
13544 action = *act;
13545 }
13546
13547 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013548 rettv->vval.v_number = 0;
13549 }
13550#endif
13551}
13552
13553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 * "setreg()" function
13555 */
13556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 typval_T *argvars;
13559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560{
13561 int regname;
13562 char_u *strregname;
13563 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013564 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 int append;
13566 char_u yank_type;
13567 long block_len;
13568
13569 block_len = -1;
13570 yank_type = MAUTO;
13571 append = FALSE;
13572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013576 if (strregname == NULL)
13577 return; /* type error; errmsg already given */
13578 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 if (regname == 0 || regname == '@')
13580 regname = '"';
13581 else if (regname == '=')
13582 return;
13583
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 stropt = get_tv_string_chk(&argvars[2]);
13587 if (stropt == NULL)
13588 return; /* type error */
13589 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 switch (*stropt)
13591 {
13592 case 'a': case 'A': /* append */
13593 append = TRUE;
13594 break;
13595 case 'v': case 'c': /* character-wise selection */
13596 yank_type = MCHAR;
13597 break;
13598 case 'V': case 'l': /* line-wise selection */
13599 yank_type = MLINE;
13600 break;
13601#ifdef FEAT_VISUAL
13602 case 'b': case Ctrl_V: /* block-wise selection */
13603 yank_type = MBLOCK;
13604 if (VIM_ISDIGIT(stropt[1]))
13605 {
13606 ++stropt;
13607 block_len = getdigits(&stropt) - 1;
13608 --stropt;
13609 }
13610 break;
13611#endif
13612 }
13613 }
13614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 strval = get_tv_string_chk(&argvars[1]);
13616 if (strval != NULL)
13617 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620}
13621
13622
13623/*
13624 * "setwinvar(expr)" function
13625 */
13626/*ARGSUSED*/
13627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 typval_T *argvars;
13630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631{
13632 win_T *win;
13633#ifdef FEAT_WINDOWS
13634 win_T *save_curwin;
13635#endif
13636 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013637 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 char_u nbuf[NUMBUFLEN];
13639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013640 rettv->vval.v_number = 0;
13641
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 if (check_restricted() || check_secure())
13643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013645 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 varp = &argvars[2];
13647
13648 if (win != NULL && varname != NULL && varp != NULL)
13649 {
13650#ifdef FEAT_WINDOWS
13651 /* set curwin to be our win, temporarily */
13652 save_curwin = curwin;
13653 curwin = win;
13654 curbuf = curwin->w_buffer;
13655#endif
13656
13657 if (*varname == '&')
13658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 long numval;
13660 char_u *strval;
13661 int error = FALSE;
13662
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664 numval = get_tv_number_chk(varp, &error);
13665 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013666 if (!error && strval != NULL)
13667 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 }
13669 else
13670 {
13671 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13672 if (winvarname != NULL)
13673 {
13674 STRCPY(winvarname, "w:");
13675 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013676 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 vim_free(winvarname);
13678 }
13679 }
13680
13681#ifdef FEAT_WINDOWS
13682 /* Restore current window, if it's still valid (autocomands can make
13683 * it invalid). */
13684 if (win_valid(save_curwin))
13685 {
13686 curwin = save_curwin;
13687 curbuf = curwin->w_buffer;
13688 }
13689#endif
13690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691}
13692
13693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013694 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 */
13696 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013697f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 typval_T *argvars;
13699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
Bram Moolenaar0d660222005-01-07 21:51:51 +000013703 p = get_tv_string(&argvars[0]);
13704 rettv->vval.v_string = vim_strsave(p);
13705 simplify_filename(rettv->vval.v_string); /* simplify in place */
13706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707}
13708
Bram Moolenaar0d660222005-01-07 21:51:51 +000013709static int
13710#ifdef __BORLANDC__
13711 _RTLENTRYF
13712#endif
13713 item_compare __ARGS((const void *s1, const void *s2));
13714static int
13715#ifdef __BORLANDC__
13716 _RTLENTRYF
13717#endif
13718 item_compare2 __ARGS((const void *s1, const void *s2));
13719
13720static int item_compare_ic;
13721static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013722static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013723#define ITEM_COMPARE_FAIL 999
13724
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013726 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013728 static int
13729#ifdef __BORLANDC__
13730_RTLENTRYF
13731#endif
13732item_compare(s1, s2)
13733 const void *s1;
13734 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013736 char_u *p1, *p2;
13737 char_u *tofree1, *tofree2;
13738 int res;
13739 char_u numbuf1[NUMBUFLEN];
13740 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741
Bram Moolenaar33570922005-01-25 22:26:29 +000013742 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13743 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013744 if (item_compare_ic)
13745 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013747 res = STRCMP(p1, p2);
13748 vim_free(tofree1);
13749 vim_free(tofree2);
13750 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751}
13752
13753 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013754#ifdef __BORLANDC__
13755_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013757item_compare2(s1, s2)
13758 const void *s1;
13759 const void *s2;
13760{
13761 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013762 typval_T rettv;
13763 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013764 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013766 /* shortcut after failure in previous call; compare all items equal */
13767 if (item_compare_func_err)
13768 return 0;
13769
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013770 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13771 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013772 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13773 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013774
13775 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13776 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013777 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013778 clear_tv(&argv[0]);
13779 clear_tv(&argv[1]);
13780
13781 if (res == FAIL)
13782 res = ITEM_COMPARE_FAIL;
13783 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 /* return value has wrong type */
13785 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13786 if (item_compare_func_err)
13787 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013788 clear_tv(&rettv);
13789 return res;
13790}
13791
13792/*
13793 * "sort({list})" function
13794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013796f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799{
Bram Moolenaar33570922005-01-25 22:26:29 +000013800 list_T *l;
13801 listitem_T *li;
13802 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803 long len;
13804 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806 rettv->vval.v_number = 0;
13807 if (argvars[0].v_type != VAR_LIST)
13808 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 else
13810 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013811 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013812 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813 return;
13814 rettv->vval.v_list = l;
13815 rettv->v_type = VAR_LIST;
13816 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817
Bram Moolenaar0d660222005-01-07 21:51:51 +000013818 len = list_len(l);
13819 if (len <= 1)
13820 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821
Bram Moolenaar0d660222005-01-07 21:51:51 +000013822 item_compare_ic = FALSE;
13823 item_compare_func = NULL;
13824 if (argvars[1].v_type != VAR_UNKNOWN)
13825 {
13826 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013828 else
13829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 int error = FALSE;
13831
13832 i = get_tv_number_chk(&argvars[1], &error);
13833 if (error)
13834 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013835 if (i == 1)
13836 item_compare_ic = TRUE;
13837 else
13838 item_compare_func = get_tv_string(&argvars[1]);
13839 }
13840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841
Bram Moolenaar0d660222005-01-07 21:51:51 +000013842 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013844 if (ptrs == NULL)
13845 return;
13846 i = 0;
13847 for (li = l->lv_first; li != NULL; li = li->li_next)
13848 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013850 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013851 /* test the compare function */
13852 if (item_compare_func != NULL
13853 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13854 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013855 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013857 {
13858 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013859 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013860 item_compare_func == NULL ? item_compare : item_compare2);
13861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 if (!item_compare_func_err)
13863 {
13864 /* Clear the List and append the items in the sorted order. */
13865 l->lv_first = l->lv_last = NULL;
13866 l->lv_len = 0;
13867 for (i = 0; i < len; ++i)
13868 list_append(l, ptrs[i]);
13869 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013870 }
13871
13872 vim_free(ptrs);
13873 }
13874}
13875
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013876/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013877 * "soundfold({word})" function
13878 */
13879 static void
13880f_soundfold(argvars, rettv)
13881 typval_T *argvars;
13882 typval_T *rettv;
13883{
13884 char_u *s;
13885
13886 rettv->v_type = VAR_STRING;
13887 s = get_tv_string(&argvars[0]);
13888#ifdef FEAT_SYN_HL
13889 rettv->vval.v_string = eval_soundfold(s);
13890#else
13891 rettv->vval.v_string = vim_strsave(s);
13892#endif
13893}
13894
13895/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013896 * "spellbadword()" function
13897 */
13898/* ARGSUSED */
13899 static void
13900f_spellbadword(argvars, rettv)
13901 typval_T *argvars;
13902 typval_T *rettv;
13903{
Bram Moolenaar4463f292005-09-25 22:20:24 +000013904 char_u *word = (char_u *)"";
13905#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013906 int len = 0;
13907 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013908 list_T *l;
13909#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013910
Bram Moolenaar4463f292005-09-25 22:20:24 +000013911 l = list_alloc();
13912 if (l == NULL)
13913 return;
13914 rettv->v_type = VAR_LIST;
13915 rettv->vval.v_list = l;
13916 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013917
13918#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000013919 if (argvars[0].v_type == VAR_UNKNOWN)
13920 {
13921 /* Find the start and length of the badly spelled word. */
13922 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
13923 if (len != 0)
13924 word = ml_get_cursor();
13925 }
13926 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13927 {
13928 char_u *str = get_tv_string_chk(&argvars[0]);
13929 int capcol = -1;
13930
13931 if (str != NULL)
13932 {
13933 /* Check the argument for spelling. */
13934 while (*str != NUL)
13935 {
13936 len = spell_check(curwin, str, &attr, &capcol);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013937 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013938 {
13939 word = str;
13940 break;
13941 }
13942 str += len;
13943 }
13944 }
13945 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013946#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000013947
13948 list_append_string(l, word, len);
13949 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013950 attr == HLF_SPB ? "bad" :
13951 attr == HLF_SPR ? "rare" :
13952 attr == HLF_SPL ? "local" :
13953 attr == HLF_SPC ? "caps" :
13954 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013955}
13956
13957/*
13958 * "spellsuggest()" function
13959 */
13960 static void
13961f_spellsuggest(argvars, rettv)
13962 typval_T *argvars;
13963 typval_T *rettv;
13964{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013965 list_T *l;
13966#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013967 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013968 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013969 int maxcount;
13970 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013971 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013972 listitem_T *li;
13973 int need_capital = FALSE;
13974#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013975
13976 l = list_alloc();
13977 if (l == NULL)
13978 return;
13979 rettv->v_type = VAR_LIST;
13980 rettv->vval.v_list = l;
13981 ++l->lv_refcount;
13982
13983#ifdef FEAT_SYN_HL
13984 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13985 {
13986 str = get_tv_string(&argvars[0]);
13987 if (argvars[1].v_type != VAR_UNKNOWN)
13988 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013989 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013990 if (maxcount <= 0)
13991 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013992 if (argvars[2].v_type != VAR_UNKNOWN)
13993 {
13994 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
13995 if (typeerr)
13996 return;
13997 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013998 }
13999 else
14000 maxcount = 25;
14001
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014002 spell_suggest_list(&ga, str, maxcount, need_capital);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014003
14004 for (i = 0; i < ga.ga_len; ++i)
14005 {
14006 str = ((char_u **)ga.ga_data)[i];
14007
14008 li = listitem_alloc();
14009 if (li == NULL)
14010 vim_free(str);
14011 else
14012 {
14013 li->li_tv.v_type = VAR_STRING;
14014 li->li_tv.v_lock = 0;
14015 li->li_tv.vval.v_string = str;
14016 list_append(l, li);
14017 }
14018 }
14019 ga_clear(&ga);
14020 }
14021#endif
14022}
14023
Bram Moolenaar0d660222005-01-07 21:51:51 +000014024 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014025f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014026 typval_T *argvars;
14027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028{
14029 char_u *str;
14030 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014031 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014032 regmatch_T regmatch;
14033 char_u patbuf[NUMBUFLEN];
14034 char_u *save_cpo;
14035 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014036 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014038 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014039 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014040
14041 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14042 save_cpo = p_cpo;
14043 p_cpo = (char_u *)"";
14044
14045 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014046 if (argvars[1].v_type != VAR_UNKNOWN)
14047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014048 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14049 if (pat == NULL)
14050 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014051 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014053 }
14054 if (pat == NULL || *pat == NUL)
14055 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056
14057 l = list_alloc();
14058 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060 rettv->v_type = VAR_LIST;
14061 rettv->vval.v_list = l;
14062 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063 if (typeerr)
14064 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14067 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014069 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014070 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014072 if (*str == NUL)
14073 match = FALSE; /* empty item at the end */
14074 else
14075 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 if (match)
14077 end = regmatch.startp[0];
14078 else
14079 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014080 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14081 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014083 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014084 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014085 }
14086 if (!match)
14087 break;
14088 /* Advance to just after the match. */
14089 if (regmatch.endp[0] > str)
14090 col = 0;
14091 else
14092 {
14093 /* Don't get stuck at the same match. */
14094#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014095 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096#else
14097 col = 1;
14098#endif
14099 }
14100 str = regmatch.endp[0];
14101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105
Bram Moolenaar0d660222005-01-07 21:51:51 +000014106 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107}
14108
14109#ifdef HAVE_STRFTIME
14110/*
14111 * "strftime({format}[, {time}])" function
14112 */
14113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014114f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014115 typval_T *argvars;
14116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117{
14118 char_u result_buf[256];
14119 struct tm *curtime;
14120 time_t seconds;
14121 char_u *p;
14122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014126 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 seconds = time(NULL);
14128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 curtime = localtime(&seconds);
14131 /* MSVC returns NULL for an invalid value of seconds. */
14132 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 else
14135 {
14136# ifdef FEAT_MBYTE
14137 vimconv_T conv;
14138 char_u *enc;
14139
14140 conv.vc_type = CONV_NONE;
14141 enc = enc_locale();
14142 convert_setup(&conv, p_enc, enc);
14143 if (conv.vc_type != CONV_NONE)
14144 p = string_convert(&conv, p, NULL);
14145# endif
14146 if (p != NULL)
14147 (void)strftime((char *)result_buf, sizeof(result_buf),
14148 (char *)p, curtime);
14149 else
14150 result_buf[0] = NUL;
14151
14152# ifdef FEAT_MBYTE
14153 if (conv.vc_type != CONV_NONE)
14154 vim_free(p);
14155 convert_setup(&conv, enc, p_enc);
14156 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 else
14159# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161
14162# ifdef FEAT_MBYTE
14163 /* Release conversion descriptors */
14164 convert_setup(&conv, NULL, NULL);
14165 vim_free(enc);
14166# endif
14167 }
14168}
14169#endif
14170
14171/*
14172 * "stridx()" function
14173 */
14174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178{
14179 char_u buf[NUMBUFLEN];
14180 char_u *needle;
14181 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014182 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 needle = get_tv_string_chk(&argvars[1]);
14187 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 if (needle == NULL || haystack == NULL)
14190 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 if (argvars[2].v_type != VAR_UNKNOWN)
14193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 int error = FALSE;
14195
14196 start_idx = get_tv_number_chk(&argvars[2], &error);
14197 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014199 if (start_idx >= 0)
14200 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 }
14202
14203 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14204 if (pos != NULL)
14205 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206}
14207
14208/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014209 * "string()" function
14210 */
14211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014212f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 typval_T *argvars;
14214 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014215{
14216 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014217 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014220 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014221 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014222 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223}
14224
14225/*
14226 * "strlen()" function
14227 */
14228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014230 typval_T *argvars;
14231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 rettv->vval.v_number = (varnumber_T)(STRLEN(
14234 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235}
14236
14237/*
14238 * "strpart()" function
14239 */
14240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014242 typval_T *argvars;
14243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244{
14245 char_u *p;
14246 int n;
14247 int len;
14248 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 slen = (int)STRLEN(p);
14253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 n = get_tv_number_chk(&argvars[1], &error);
14255 if (error)
14256 len = 0;
14257 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 else
14260 len = slen - n; /* default len: all bytes that are available. */
14261
14262 /*
14263 * Only return the overlap between the specified part and the actual
14264 * string.
14265 */
14266 if (n < 0)
14267 {
14268 len += n;
14269 n = 0;
14270 }
14271 else if (n > slen)
14272 n = slen;
14273 if (len < 0)
14274 len = 0;
14275 else if (n + len > slen)
14276 len = slen - n;
14277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278 rettv->v_type = VAR_STRING;
14279 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280}
14281
14282/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014283 * "strridx()" function
14284 */
14285 static void
14286f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 typval_T *argvars;
14288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014289{
14290 char_u buf[NUMBUFLEN];
14291 char_u *needle;
14292 char_u *haystack;
14293 char_u *rest;
14294 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014295 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 needle = get_tv_string_chk(&argvars[1]);
14298 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014299 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300
14301 rettv->vval.v_number = -1;
14302 if (needle == NULL || haystack == NULL)
14303 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014304 if (argvars[2].v_type != VAR_UNKNOWN)
14305 {
14306 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014308 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014310 }
14311 else
14312 end_idx = haystack_len;
14313
Bram Moolenaar0d660222005-01-07 21:51:51 +000014314 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014315 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014317 lastmatch = haystack + end_idx;
14318 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014319 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014320 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014321 for (rest = haystack; *rest != '\0'; ++rest)
14322 {
14323 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014324 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014325 break;
14326 lastmatch = rest;
14327 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014328 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329
14330 if (lastmatch == NULL)
14331 rettv->vval.v_number = -1;
14332 else
14333 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14334}
14335
14336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 * "strtrans()" function
14338 */
14339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014341 typval_T *argvars;
14342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014344 rettv->v_type = VAR_STRING;
14345 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346}
14347
14348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349 * "submatch()" function
14350 */
14351 static void
14352f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 typval_T *argvars;
14354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355{
14356 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 rettv->vval.v_string =
14358 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014359}
14360
14361/*
14362 * "substitute()" function
14363 */
14364 static void
14365f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014366 typval_T *argvars;
14367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014368{
14369 char_u patbuf[NUMBUFLEN];
14370 char_u subbuf[NUMBUFLEN];
14371 char_u flagsbuf[NUMBUFLEN];
14372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 char_u *str = get_tv_string_chk(&argvars[0]);
14374 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14375 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14376 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14377
Bram Moolenaar0d660222005-01-07 21:51:51 +000014378 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14380 rettv->vval.v_string = NULL;
14381 else
14382 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383}
14384
14385/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014386 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 */
14388/*ARGSUSED*/
14389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014391 typval_T *argvars;
14392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393{
14394 int id = 0;
14395#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014396 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 long col;
14398 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014399 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014401 lnum = get_tv_lnum(argvars); /* -1 on type error */
14402 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14403 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014406 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014407 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408#endif
14409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411}
14412
14413/*
14414 * "synIDattr(id, what [, mode])" function
14415 */
14416/*ARGSUSED*/
14417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014419 typval_T *argvars;
14420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421{
14422 char_u *p = NULL;
14423#ifdef FEAT_SYN_HL
14424 int id;
14425 char_u *what;
14426 char_u *mode;
14427 char_u modebuf[NUMBUFLEN];
14428 int modec;
14429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 id = get_tv_number(&argvars[0]);
14431 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 modec = TOLOWER_ASC(mode[0]);
14436 if (modec != 't' && modec != 'c'
14437#ifdef FEAT_GUI
14438 && modec != 'g'
14439#endif
14440 )
14441 modec = 0; /* replace invalid with current */
14442 }
14443 else
14444 {
14445#ifdef FEAT_GUI
14446 if (gui.in_use)
14447 modec = 'g';
14448 else
14449#endif
14450 if (t_colors > 1)
14451 modec = 'c';
14452 else
14453 modec = 't';
14454 }
14455
14456
14457 switch (TOLOWER_ASC(what[0]))
14458 {
14459 case 'b':
14460 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14461 p = highlight_color(id, what, modec);
14462 else /* bold */
14463 p = highlight_has_attr(id, HL_BOLD, modec);
14464 break;
14465
14466 case 'f': /* fg[#] */
14467 p = highlight_color(id, what, modec);
14468 break;
14469
14470 case 'i':
14471 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14472 p = highlight_has_attr(id, HL_INVERSE, modec);
14473 else /* italic */
14474 p = highlight_has_attr(id, HL_ITALIC, modec);
14475 break;
14476
14477 case 'n': /* name */
14478 p = get_highlight_name(NULL, id - 1);
14479 break;
14480
14481 case 'r': /* reverse */
14482 p = highlight_has_attr(id, HL_INVERSE, modec);
14483 break;
14484
14485 case 's': /* standout */
14486 p = highlight_has_attr(id, HL_STANDOUT, modec);
14487 break;
14488
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014489 case 'u':
14490 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14491 /* underline */
14492 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14493 else
14494 /* undercurl */
14495 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 break;
14497 }
14498
14499 if (p != NULL)
14500 p = vim_strsave(p);
14501#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502 rettv->v_type = VAR_STRING;
14503 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504}
14505
14506/*
14507 * "synIDtrans(id)" function
14508 */
14509/*ARGSUSED*/
14510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014512 typval_T *argvars;
14513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514{
14515 int id;
14516
14517#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519
14520 if (id > 0)
14521 id = syn_get_final_id(id);
14522 else
14523#endif
14524 id = 0;
14525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527}
14528
14529/*
14530 * "system()" function
14531 */
14532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014534 typval_T *argvars;
14535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014537 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014539 char_u *infile = NULL;
14540 char_u buf[NUMBUFLEN];
14541 int err = FALSE;
14542 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014544 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014545 {
14546 /*
14547 * Write the string to a temp file, to be used for input of the shell
14548 * command.
14549 */
14550 if ((infile = vim_tempname('i')) == NULL)
14551 {
14552 EMSG(_(e_notmp));
14553 return;
14554 }
14555
14556 fd = mch_fopen((char *)infile, WRITEBIN);
14557 if (fd == NULL)
14558 {
14559 EMSG2(_(e_notopen), infile);
14560 goto done;
14561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 p = get_tv_string_buf_chk(&argvars[1], buf);
14563 if (p == NULL)
14564 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014565 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14566 err = TRUE;
14567 if (fclose(fd) != 0)
14568 err = TRUE;
14569 if (err)
14570 {
14571 EMSG(_("E677: Error writing temp file"));
14572 goto done;
14573 }
14574 }
14575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014577
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578#ifdef USE_CR
14579 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014580 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 {
14582 char_u *s;
14583
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014584 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 {
14586 if (*s == CAR)
14587 *s = NL;
14588 }
14589 }
14590#else
14591# ifdef USE_CRNL
14592 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014593 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 {
14595 char_u *s, *d;
14596
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014597 d = res;
14598 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 {
14600 if (s[0] == CAR && s[1] == NL)
14601 ++s;
14602 *d++ = *s;
14603 }
14604 *d = NUL;
14605 }
14606# endif
14607#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014608
14609done:
14610 if (infile != NULL)
14611 {
14612 mch_remove(infile);
14613 vim_free(infile);
14614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615 rettv->v_type = VAR_STRING;
14616 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617}
14618
14619/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014620 * "tagfiles()" function
14621 */
14622/*ARGSUSED*/
14623 static void
14624f_tagfiles(argvars, rettv)
14625 typval_T *argvars;
14626 typval_T *rettv;
14627{
14628 char_u fname[MAXPATHL + 1];
14629 list_T *l;
14630
14631 l = list_alloc();
14632 if (l == NULL)
14633 {
14634 rettv->vval.v_number = 0;
14635 return;
14636 }
14637 rettv->vval.v_list = l;
14638 rettv->v_type = VAR_LIST;
14639 ++l->lv_refcount;
14640
14641 get_tagfname(TRUE, NULL);
14642 for (;;)
14643 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014644 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014645 break;
14646}
14647
14648/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014649 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014650 */
14651 static void
14652f_taglist(argvars, rettv)
14653 typval_T *argvars;
14654 typval_T *rettv;
14655{
14656 char_u *tag_pattern;
14657 list_T *l;
14658
14659 tag_pattern = get_tv_string(&argvars[0]);
14660
14661 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 if (*tag_pattern == NUL)
14663 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014664
14665 l = list_alloc();
14666 if (l != NULL)
14667 {
14668 if (get_tags(l, tag_pattern) != FAIL)
14669 {
14670 rettv->vval.v_list = l;
14671 rettv->v_type = VAR_LIST;
14672 ++l->lv_refcount;
14673 }
14674 else
14675 list_free(l);
14676 }
14677}
14678
14679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 * "tempname()" function
14681 */
14682/*ARGSUSED*/
14683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014685 typval_T *argvars;
14686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687{
14688 static int x = 'A';
14689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014690 rettv->v_type = VAR_STRING;
14691 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692
14693 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14694 * names. Skip 'I' and 'O', they are used for shell redirection. */
14695 do
14696 {
14697 if (x == 'Z')
14698 x = '0';
14699 else if (x == '9')
14700 x = 'A';
14701 else
14702 {
14703#ifdef EBCDIC
14704 if (x == 'I')
14705 x = 'J';
14706 else if (x == 'R')
14707 x = 'S';
14708 else
14709#endif
14710 ++x;
14711 }
14712 } while (x == 'I' || x == 'O');
14713}
14714
14715/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014716 * "test(list)" function: Just checking the walls...
14717 */
14718/*ARGSUSED*/
14719 static void
14720f_test(argvars, rettv)
14721 typval_T *argvars;
14722 typval_T *rettv;
14723{
14724 /* Used for unit testing. Change the code below to your liking. */
14725#if 0
14726 listitem_T *li;
14727 list_T *l;
14728 char_u *bad, *good;
14729
14730 if (argvars[0].v_type != VAR_LIST)
14731 return;
14732 l = argvars[0].vval.v_list;
14733 if (l == NULL)
14734 return;
14735 li = l->lv_first;
14736 if (li == NULL)
14737 return;
14738 bad = get_tv_string(&li->li_tv);
14739 li = li->li_next;
14740 if (li == NULL)
14741 return;
14742 good = get_tv_string(&li->li_tv);
14743 rettv->vval.v_number = test_edit_score(bad, good);
14744#endif
14745}
14746
14747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 * "tolower(string)" function
14749 */
14750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014752 typval_T *argvars;
14753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754{
14755 char_u *p;
14756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757 p = vim_strsave(get_tv_string(&argvars[0]));
14758 rettv->v_type = VAR_STRING;
14759 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760
14761 if (p != NULL)
14762 while (*p != NUL)
14763 {
14764#ifdef FEAT_MBYTE
14765 int l;
14766
14767 if (enc_utf8)
14768 {
14769 int c, lc;
14770
14771 c = utf_ptr2char(p);
14772 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014773 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 /* TODO: reallocate string when byte count changes. */
14775 if (utf_char2len(lc) == l)
14776 utf_char2bytes(lc, p);
14777 p += l;
14778 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014779 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780 p += l; /* skip multi-byte character */
14781 else
14782#endif
14783 {
14784 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14785 ++p;
14786 }
14787 }
14788}
14789
14790/*
14791 * "toupper(string)" function
14792 */
14793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014794f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 typval_T *argvars;
14796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014798 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800}
14801
14802/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014803 * "tr(string, fromstr, tostr)" function
14804 */
14805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014806f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014807 typval_T *argvars;
14808 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014809{
14810 char_u *instr;
14811 char_u *fromstr;
14812 char_u *tostr;
14813 char_u *p;
14814#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014815 int inlen;
14816 int fromlen;
14817 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014818 int idx;
14819 char_u *cpstr;
14820 int cplen;
14821 int first = TRUE;
14822#endif
14823 char_u buf[NUMBUFLEN];
14824 char_u buf2[NUMBUFLEN];
14825 garray_T ga;
14826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014828 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14829 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014830
14831 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 rettv->v_type = VAR_STRING;
14833 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014834 if (fromstr == NULL || tostr == NULL)
14835 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014836 ga_init2(&ga, (int)sizeof(char), 80);
14837
14838#ifdef FEAT_MBYTE
14839 if (!has_mbyte)
14840#endif
14841 /* not multi-byte: fromstr and tostr must be the same length */
14842 if (STRLEN(fromstr) != STRLEN(tostr))
14843 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014844#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014845error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014846#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014847 EMSG2(_(e_invarg2), fromstr);
14848 ga_clear(&ga);
14849 return;
14850 }
14851
14852 /* fromstr and tostr have to contain the same number of chars */
14853 while (*instr != NUL)
14854 {
14855#ifdef FEAT_MBYTE
14856 if (has_mbyte)
14857 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014858 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014859 cpstr = instr;
14860 cplen = inlen;
14861 idx = 0;
14862 for (p = fromstr; *p != NUL; p += fromlen)
14863 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014864 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014865 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14866 {
14867 for (p = tostr; *p != NUL; p += tolen)
14868 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014869 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014870 if (idx-- == 0)
14871 {
14872 cplen = tolen;
14873 cpstr = p;
14874 break;
14875 }
14876 }
14877 if (*p == NUL) /* tostr is shorter than fromstr */
14878 goto error;
14879 break;
14880 }
14881 ++idx;
14882 }
14883
14884 if (first && cpstr == instr)
14885 {
14886 /* Check that fromstr and tostr have the same number of
14887 * (multi-byte) characters. Done only once when a character
14888 * of instr doesn't appear in fromstr. */
14889 first = FALSE;
14890 for (p = tostr; *p != NUL; p += tolen)
14891 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014892 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014893 --idx;
14894 }
14895 if (idx != 0)
14896 goto error;
14897 }
14898
14899 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014900 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014901 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014902
14903 instr += inlen;
14904 }
14905 else
14906#endif
14907 {
14908 /* When not using multi-byte chars we can do it faster. */
14909 p = vim_strchr(fromstr, *instr);
14910 if (p != NULL)
14911 ga_append(&ga, tostr[p - fromstr]);
14912 else
14913 ga_append(&ga, *instr);
14914 ++instr;
14915 }
14916 }
14917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014918 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014919}
14920
14921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 * "type(expr)" function
14923 */
14924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014926 typval_T *argvars;
14927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014929 int n;
14930
14931 switch (argvars[0].v_type)
14932 {
14933 case VAR_NUMBER: n = 0; break;
14934 case VAR_STRING: n = 1; break;
14935 case VAR_FUNC: n = 2; break;
14936 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014937 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014938 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14939 }
14940 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941}
14942
14943/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944 * "values(dict)" function
14945 */
14946 static void
14947f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014948 typval_T *argvars;
14949 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014950{
14951 dict_list(argvars, rettv, 1);
14952}
14953
14954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 * "virtcol(string)" function
14956 */
14957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014959 typval_T *argvars;
14960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961{
14962 colnr_T vcol = 0;
14963 pos_T *fp;
14964
14965 fp = var2fpos(&argvars[0], FALSE);
14966 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14967 {
14968 getvvcol(curwin, fp, NULL, NULL, &vcol);
14969 ++vcol;
14970 }
14971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014972 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973}
14974
14975/*
14976 * "visualmode()" function
14977 */
14978/*ARGSUSED*/
14979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014980f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014981 typval_T *argvars;
14982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983{
14984#ifdef FEAT_VISUAL
14985 char_u str[2];
14986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 str[0] = curbuf->b_visual_mode_eval;
14989 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991
14992 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014993 if ((argvars[0].v_type == VAR_NUMBER
14994 && argvars[0].vval.v_number != 0)
14995 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 curbuf->b_visual_mode_eval = NUL;
14998#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000#endif
15001}
15002
15003/*
15004 * "winbufnr(nr)" function
15005 */
15006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015007f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015008 typval_T *argvars;
15009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010{
15011 win_T *wp;
15012
15013 wp = find_win_by_nr(&argvars[0]);
15014 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015017 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018}
15019
15020/*
15021 * "wincol()" function
15022 */
15023/*ARGSUSED*/
15024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015025f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015026 typval_T *argvars;
15027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028{
15029 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031}
15032
15033/*
15034 * "winheight(nr)" function
15035 */
15036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015038 typval_T *argvars;
15039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040{
15041 win_T *wp;
15042
15043 wp = find_win_by_nr(&argvars[0]);
15044 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015047 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048}
15049
15050/*
15051 * "winline()" function
15052 */
15053/*ARGSUSED*/
15054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015056 typval_T *argvars;
15057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058{
15059 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015060 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061}
15062
15063/*
15064 * "winnr()" function
15065 */
15066/* ARGSUSED */
15067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 typval_T *argvars;
15070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071{
15072 int nr = 1;
15073#ifdef FEAT_WINDOWS
15074 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015075 win_T *twin = curwin;
15076 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015078 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015080 arg = get_tv_string_chk(&argvars[0]);
15081 if (arg == NULL)
15082 nr = 0; /* type error; errmsg already given */
15083 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015084 twin = lastwin;
15085 else if (STRCMP(arg, "#") == 0)
15086 {
15087 twin = prevwin;
15088 if (prevwin == NULL)
15089 nr = 0;
15090 }
15091 else
15092 {
15093 EMSG2(_(e_invexpr2), arg);
15094 nr = 0;
15095 }
15096 }
15097
15098 if (nr > 0)
15099 for (wp = firstwin; wp != twin; wp = wp->w_next)
15100 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015102 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103}
15104
15105/*
15106 * "winrestcmd()" function
15107 */
15108/* ARGSUSED */
15109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015110f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015111 typval_T *argvars;
15112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113{
15114#ifdef FEAT_WINDOWS
15115 win_T *wp;
15116 int winnr = 1;
15117 garray_T ga;
15118 char_u buf[50];
15119
15120 ga_init2(&ga, (int)sizeof(char), 70);
15121 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15122 {
15123 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15124 ga_concat(&ga, buf);
15125# ifdef FEAT_VERTSPLIT
15126 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15127 ga_concat(&ga, buf);
15128# endif
15129 ++winnr;
15130 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015131 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015133 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138}
15139
15140/*
15141 * "winwidth(nr)" function
15142 */
15143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 typval_T *argvars;
15146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147{
15148 win_T *wp;
15149
15150 wp = find_win_by_nr(&argvars[0]);
15151 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 else
15154#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015155 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015157 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158#endif
15159}
15160
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015162 * "writefile()" function
15163 */
15164 static void
15165f_writefile(argvars, rettv)
15166 typval_T *argvars;
15167 typval_T *rettv;
15168{
15169 int binary = FALSE;
15170 char_u *fname;
15171 FILE *fd;
15172 listitem_T *li;
15173 char_u *s;
15174 int ret = 0;
15175 int c;
15176
15177 if (argvars[0].v_type != VAR_LIST)
15178 {
15179 EMSG2(_(e_listarg), "writefile()");
15180 return;
15181 }
15182 if (argvars[0].vval.v_list == NULL)
15183 return;
15184
15185 if (argvars[2].v_type != VAR_UNKNOWN
15186 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15187 binary = TRUE;
15188
15189 /* Always open the file in binary mode, library functions have a mind of
15190 * their own about CR-LF conversion. */
15191 fname = get_tv_string(&argvars[1]);
15192 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15193 {
15194 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15195 ret = -1;
15196 }
15197 else
15198 {
15199 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15200 li = li->li_next)
15201 {
15202 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15203 {
15204 if (*s == '\n')
15205 c = putc(NUL, fd);
15206 else
15207 c = putc(*s, fd);
15208 if (c == EOF)
15209 {
15210 ret = -1;
15211 break;
15212 }
15213 }
15214 if (!binary || li->li_next != NULL)
15215 if (putc('\n', fd) == EOF)
15216 {
15217 ret = -1;
15218 break;
15219 }
15220 if (ret < 0)
15221 {
15222 EMSG(_(e_write));
15223 break;
15224 }
15225 }
15226 fclose(fd);
15227 }
15228
15229 rettv->vval.v_number = ret;
15230}
15231
15232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 * Translate a String variable into a position.
15234 */
15235 static pos_T *
15236var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015237 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 int lnum; /* TRUE when $ is last line */
15239{
15240 char_u *name;
15241 static pos_T pos;
15242 pos_T *pp;
15243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 name = get_tv_string_chk(varp);
15245 if (name == NULL)
15246 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 if (name[0] == '.') /* cursor */
15248 return &curwin->w_cursor;
15249 if (name[0] == '\'') /* mark */
15250 {
15251 pp = getmark(name[1], FALSE);
15252 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15253 return NULL;
15254 return pp;
15255 }
15256 if (name[0] == '$') /* last column or line */
15257 {
15258 if (lnum)
15259 {
15260 pos.lnum = curbuf->b_ml.ml_line_count;
15261 pos.col = 0;
15262 }
15263 else
15264 {
15265 pos.lnum = curwin->w_cursor.lnum;
15266 pos.col = (colnr_T)STRLEN(ml_get_curline());
15267 }
15268 return &pos;
15269 }
15270 return NULL;
15271}
15272
15273/*
15274 * Get the length of an environment variable name.
15275 * Advance "arg" to the first character after the name.
15276 * Return 0 for error.
15277 */
15278 static int
15279get_env_len(arg)
15280 char_u **arg;
15281{
15282 char_u *p;
15283 int len;
15284
15285 for (p = *arg; vim_isIDc(*p); ++p)
15286 ;
15287 if (p == *arg) /* no name found */
15288 return 0;
15289
15290 len = (int)(p - *arg);
15291 *arg = p;
15292 return len;
15293}
15294
15295/*
15296 * Get the length of the name of a function or internal variable.
15297 * "arg" is advanced to the first non-white character after the name.
15298 * Return 0 if something is wrong.
15299 */
15300 static int
15301get_id_len(arg)
15302 char_u **arg;
15303{
15304 char_u *p;
15305 int len;
15306
15307 /* Find the end of the name. */
15308 for (p = *arg; eval_isnamec(*p); ++p)
15309 ;
15310 if (p == *arg) /* no name found */
15311 return 0;
15312
15313 len = (int)(p - *arg);
15314 *arg = skipwhite(p);
15315
15316 return len;
15317}
15318
15319/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015320 * Get the length of the name of a variable or function.
15321 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015323 * Return -1 if curly braces expansion failed.
15324 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 * If the name contains 'magic' {}'s, expand them and return the
15326 * expanded name in an allocated string via 'alias' - caller must free.
15327 */
15328 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015329get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 char_u **arg;
15331 char_u **alias;
15332 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015333 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334{
15335 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 char_u *p;
15337 char_u *expr_start;
15338 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339
15340 *alias = NULL; /* default to no alias */
15341
15342 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15343 && (*arg)[2] == (int)KE_SNR)
15344 {
15345 /* hard coded <SNR>, already translated */
15346 *arg += 3;
15347 return get_id_len(arg) + 3;
15348 }
15349 len = eval_fname_script(*arg);
15350 if (len > 0)
15351 {
15352 /* literal "<SID>", "s:" or "<SNR>" */
15353 *arg += len;
15354 }
15355
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015359 p = find_name_end(*arg, &expr_start, &expr_end,
15360 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 if (expr_start != NULL)
15362 {
15363 char_u *temp_string;
15364
15365 if (!evaluate)
15366 {
15367 len += (int)(p - *arg);
15368 *arg = skipwhite(p);
15369 return len;
15370 }
15371
15372 /*
15373 * Include any <SID> etc in the expanded string:
15374 * Thus the -len here.
15375 */
15376 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15377 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 *alias = temp_string;
15380 *arg = skipwhite(p);
15381 return (int)STRLEN(temp_string);
15382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383
15384 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015385 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 EMSG2(_(e_invexpr2), *arg);
15387
15388 return len;
15389}
15390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391/*
15392 * Find the end of a variable or function name, taking care of magic braces.
15393 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15394 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015395 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396 * Return a pointer to just after the name. Equal to "arg" if there is no
15397 * valid name.
15398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015400find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 char_u *arg;
15402 char_u **expr_start;
15403 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015404 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 int mb_nest = 0;
15407 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 char_u *p;
15409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015410 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 *expr_start = NULL;
15413 *expr_end = NULL;
15414 }
15415
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015416 /* Quick check for valid starting character. */
15417 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15418 return arg;
15419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 for (p = arg; *p != NUL
15421 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015422 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015423 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015425 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015426 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015427 if (*p == '\'')
15428 {
15429 /* skip over 'string' to avoid counting [ and ] inside it. */
15430 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15431 ;
15432 if (*p == NUL)
15433 break;
15434 }
15435 else if (*p == '"')
15436 {
15437 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15438 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15439 if (*p == '\\' && p[1] != NUL)
15440 ++p;
15441 if (*p == NUL)
15442 break;
15443 }
15444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 if (*p == '[')
15448 ++br_nest;
15449 else if (*p == ']')
15450 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455 if (*p == '{')
15456 {
15457 mb_nest++;
15458 if (expr_start != NULL && *expr_start == NULL)
15459 *expr_start = p;
15460 }
15461 else if (*p == '}')
15462 {
15463 mb_nest--;
15464 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15465 *expr_end = p;
15466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 }
15469
15470 return p;
15471}
15472
15473/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015474 * Expands out the 'magic' {}'s in a variable/function name.
15475 * Note that this can call itself recursively, to deal with
15476 * constructs like foo{bar}{baz}{bam}
15477 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15478 * "in_start" ^
15479 * "expr_start" ^
15480 * "expr_end" ^
15481 * "in_end" ^
15482 *
15483 * Returns a new allocated string, which the caller must free.
15484 * Returns NULL for failure.
15485 */
15486 static char_u *
15487make_expanded_name(in_start, expr_start, expr_end, in_end)
15488 char_u *in_start;
15489 char_u *expr_start;
15490 char_u *expr_end;
15491 char_u *in_end;
15492{
15493 char_u c1;
15494 char_u *retval = NULL;
15495 char_u *temp_result;
15496 char_u *nextcmd = NULL;
15497
15498 if (expr_end == NULL || in_end == NULL)
15499 return NULL;
15500 *expr_start = NUL;
15501 *expr_end = NUL;
15502 c1 = *in_end;
15503 *in_end = NUL;
15504
15505 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15506 if (temp_result != NULL && nextcmd == NULL)
15507 {
15508 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15509 + (in_end - expr_end) + 1));
15510 if (retval != NULL)
15511 {
15512 STRCPY(retval, in_start);
15513 STRCAT(retval, temp_result);
15514 STRCAT(retval, expr_end + 1);
15515 }
15516 }
15517 vim_free(temp_result);
15518
15519 *in_end = c1; /* put char back for error messages */
15520 *expr_start = '{';
15521 *expr_end = '}';
15522
15523 if (retval != NULL)
15524 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015525 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015526 if (expr_start != NULL)
15527 {
15528 /* Further expansion! */
15529 temp_result = make_expanded_name(retval, expr_start,
15530 expr_end, temp_result);
15531 vim_free(retval);
15532 retval = temp_result;
15533 }
15534 }
15535
15536 return retval;
15537}
15538
15539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015541 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 */
15543 static int
15544eval_isnamec(c)
15545 int c;
15546{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015547 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15548}
15549
15550/*
15551 * Return TRUE if character "c" can be used as the first character in a
15552 * variable or function name (excluding '{' and '}').
15553 */
15554 static int
15555eval_isnamec1(c)
15556 int c;
15557{
15558 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559}
15560
15561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 * Set number v: variable to "val".
15563 */
15564 void
15565set_vim_var_nr(idx, val)
15566 int idx;
15567 long val;
15568{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015569 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570}
15571
15572/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015573 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 */
15575 long
15576get_vim_var_nr(idx)
15577 int idx;
15578{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015579 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580}
15581
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015582#if defined(FEAT_AUTOCMD) || defined(PROTO)
15583/*
15584 * Get string v: variable value. Uses a static buffer, can only be used once.
15585 */
15586 char_u *
15587get_vim_var_str(idx)
15588 int idx;
15589{
15590 return get_tv_string(&vimvars[idx].vv_tv);
15591}
15592#endif
15593
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594/*
15595 * Set v:count, v:count1 and v:prevcount.
15596 */
15597 void
15598set_vcount(count, count1)
15599 long count;
15600 long count1;
15601{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015602 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15603 vimvars[VV_COUNT].vv_nr = count;
15604 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605}
15606
15607/*
15608 * Set string v: variable to a copy of "val".
15609 */
15610 void
15611set_vim_var_string(idx, val, len)
15612 int idx;
15613 char_u *val;
15614 int len; /* length of "val" to use or -1 (whole string) */
15615{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015616 /* Need to do this (at least) once, since we can't initialize a union.
15617 * Will always be invoked when "v:progname" is set. */
15618 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15619
Bram Moolenaare9a41262005-01-15 22:18:47 +000015620 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015622 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015624 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015626 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627}
15628
15629/*
15630 * Set v:register if needed.
15631 */
15632 void
15633set_reg_var(c)
15634 int c;
15635{
15636 char_u regname;
15637
15638 if (c == 0 || c == ' ')
15639 regname = '"';
15640 else
15641 regname = c;
15642 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015643 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644 set_vim_var_string(VV_REG, &regname, 1);
15645}
15646
15647/*
15648 * Get or set v:exception. If "oldval" == NULL, return the current value.
15649 * Otherwise, restore the value to "oldval" and return NULL.
15650 * Must always be called in pairs to save and restore v:exception! Does not
15651 * take care of memory allocations.
15652 */
15653 char_u *
15654v_exception(oldval)
15655 char_u *oldval;
15656{
15657 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015658 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659
Bram Moolenaare9a41262005-01-15 22:18:47 +000015660 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 return NULL;
15662}
15663
15664/*
15665 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15666 * Otherwise, restore the value to "oldval" and return NULL.
15667 * Must always be called in pairs to save and restore v:throwpoint! Does not
15668 * take care of memory allocations.
15669 */
15670 char_u *
15671v_throwpoint(oldval)
15672 char_u *oldval;
15673{
15674 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015675 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676
Bram Moolenaare9a41262005-01-15 22:18:47 +000015677 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 return NULL;
15679}
15680
15681#if defined(FEAT_AUTOCMD) || defined(PROTO)
15682/*
15683 * Set v:cmdarg.
15684 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15685 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15686 * Must always be called in pairs!
15687 */
15688 char_u *
15689set_cmdarg(eap, oldarg)
15690 exarg_T *eap;
15691 char_u *oldarg;
15692{
15693 char_u *oldval;
15694 char_u *newval;
15695 unsigned len;
15696
Bram Moolenaare9a41262005-01-15 22:18:47 +000015697 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015698 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015700 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015701 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015702 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 }
15704
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015705 if (eap->force_bin == FORCE_BIN)
15706 len = 6;
15707 else if (eap->force_bin == FORCE_NOBIN)
15708 len = 8;
15709 else
15710 len = 0;
15711 if (eap->force_ff != 0)
15712 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15713# ifdef FEAT_MBYTE
15714 if (eap->force_enc != 0)
15715 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15716# endif
15717
15718 newval = alloc(len + 1);
15719 if (newval == NULL)
15720 return NULL;
15721
15722 if (eap->force_bin == FORCE_BIN)
15723 sprintf((char *)newval, " ++bin");
15724 else if (eap->force_bin == FORCE_NOBIN)
15725 sprintf((char *)newval, " ++nobin");
15726 else
15727 *newval = NUL;
15728 if (eap->force_ff != 0)
15729 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15730 eap->cmd + eap->force_ff);
15731# ifdef FEAT_MBYTE
15732 if (eap->force_enc != 0)
15733 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15734 eap->cmd + eap->force_enc);
15735# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015736 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015737 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738}
15739#endif
15740
15741/*
15742 * Get the value of internal variable "name".
15743 * Return OK or FAIL.
15744 */
15745 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015746get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 char_u *name;
15748 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015750 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751{
15752 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015753 typval_T *tv = NULL;
15754 typval_T atv;
15755 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757
15758 /* truncate the name, so that we can use strcmp() */
15759 cc = name[len];
15760 name[len] = NUL;
15761
15762 /*
15763 * Check for "b:changedtick".
15764 */
15765 if (STRCMP(name, "b:changedtick") == 0)
15766 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015767 atv.v_type = VAR_NUMBER;
15768 atv.vval.v_number = curbuf->b_changedtick;
15769 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 }
15771
15772 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 * Check for user-defined variables.
15774 */
15775 else
15776 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015777 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015779 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 }
15781
Bram Moolenaare9a41262005-01-15 22:18:47 +000015782 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015784 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 ret = FAIL;
15787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015789 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790
15791 name[len] = cc;
15792
15793 return ret;
15794}
15795
15796/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015797 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15798 * Also handle function call with Funcref variable: func(expr)
15799 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15800 */
15801 static int
15802handle_subscript(arg, rettv, evaluate, verbose)
15803 char_u **arg;
15804 typval_T *rettv;
15805 int evaluate; /* do more than finding the end */
15806 int verbose; /* give error messages */
15807{
15808 int ret = OK;
15809 dict_T *selfdict = NULL;
15810 char_u *s;
15811 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015812 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015813
15814 while (ret == OK
15815 && (**arg == '['
15816 || (**arg == '.' && rettv->v_type == VAR_DICT)
15817 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15818 && !vim_iswhite(*(*arg - 1)))
15819 {
15820 if (**arg == '(')
15821 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015822 /* need to copy the funcref so that we can clear rettv */
15823 functv = *rettv;
15824 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015825
15826 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015827 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015828 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015829 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15830 &len, evaluate, selfdict);
15831
15832 /* Clear the funcref afterwards, so that deleting it while
15833 * evaluating the arguments is possible (see test55). */
15834 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015835
15836 /* Stop the expression evaluation when immediately aborting on
15837 * error, or when an interrupt occurred or an exception was thrown
15838 * but not caught. */
15839 if (aborting())
15840 {
15841 if (ret == OK)
15842 clear_tv(rettv);
15843 ret = FAIL;
15844 }
15845 dict_unref(selfdict);
15846 selfdict = NULL;
15847 }
15848 else /* **arg == '[' || **arg == '.' */
15849 {
15850 dict_unref(selfdict);
15851 if (rettv->v_type == VAR_DICT)
15852 {
15853 selfdict = rettv->vval.v_dict;
15854 if (selfdict != NULL)
15855 ++selfdict->dv_refcount;
15856 }
15857 else
15858 selfdict = NULL;
15859 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15860 {
15861 clear_tv(rettv);
15862 ret = FAIL;
15863 }
15864 }
15865 }
15866 dict_unref(selfdict);
15867 return ret;
15868}
15869
15870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015871 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15872 * value).
15873 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015874 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015876{
Bram Moolenaar33570922005-01-25 22:26:29 +000015877 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015878}
15879
15880/*
15881 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 * The string "s" must have been allocated, it is consumed.
15883 * Return NULL for out of memory, the variable otherwise.
15884 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015885 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015886alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 char_u *s;
15888{
Bram Moolenaar33570922005-01-25 22:26:29 +000015889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891 rettv = alloc_tv();
15892 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015894 rettv->v_type = VAR_STRING;
15895 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 }
15897 else
15898 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015899 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900}
15901
15902/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015903 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 */
15905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908{
15909 if (varp != NULL)
15910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015911 switch (varp->v_type)
15912 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015914 func_unref(varp->vval.v_string);
15915 /*FALLTHROUGH*/
15916 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015917 vim_free(varp->vval.v_string);
15918 break;
15919 case VAR_LIST:
15920 list_unref(varp->vval.v_list);
15921 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015922 case VAR_DICT:
15923 dict_unref(varp->vval.v_dict);
15924 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015925 case VAR_NUMBER:
15926 case VAR_UNKNOWN:
15927 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015928 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015929 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 break;
15931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 vim_free(varp);
15933 }
15934}
15935
15936/*
15937 * Free the memory for a variable value and set the value to NULL or 0.
15938 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015939 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015940clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015941 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942{
15943 if (varp != NULL)
15944 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015945 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015947 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015948 func_unref(varp->vval.v_string);
15949 /*FALLTHROUGH*/
15950 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015951 vim_free(varp->vval.v_string);
15952 varp->vval.v_string = NULL;
15953 break;
15954 case VAR_LIST:
15955 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015956 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015957 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015958 case VAR_DICT:
15959 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015960 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015961 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015962 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015963 varp->vval.v_number = 0;
15964 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015965 case VAR_UNKNOWN:
15966 break;
15967 default:
15968 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015970 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971 }
15972}
15973
15974/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015975 * Set the value of a variable to NULL without freeing items.
15976 */
15977 static void
15978init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015980{
15981 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015982 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015983}
15984
15985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 * Get the number value of a variable.
15987 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015988 * For incompatible types, return 0.
15989 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15990 * caller of incompatible types: it sets *denote to TRUE if "denote"
15991 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 */
15993 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015994get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015997 int error = FALSE;
15998
15999 return get_tv_number_chk(varp, &error); /* return 0L on error */
16000}
16001
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016002 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016003get_tv_number_chk(varp, denote)
16004 typval_T *varp;
16005 int *denote;
16006{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016007 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016009 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016011 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016012 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016013 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016014 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016015 break;
16016 case VAR_STRING:
16017 if (varp->vval.v_string != NULL)
16018 vim_str2nr(varp->vval.v_string, NULL, NULL,
16019 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016021 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016022 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016023 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016024 case VAR_DICT:
16025 EMSG(_("E728: Using a Dictionary as a number"));
16026 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016027 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016028 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016029 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 if (denote == NULL) /* useful for values that must be unsigned */
16032 n = -1;
16033 else
16034 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016035 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036}
16037
16038/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016039 * Get the lnum from the first argument.
16040 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 */
16043 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016044get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046{
Bram Moolenaar33570922005-01-25 22:26:29 +000016047 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 linenr_T lnum;
16049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016050 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 if (lnum == 0) /* no valid number, try using line() */
16052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016053 rettv.v_type = VAR_NUMBER;
16054 f_line(argvars, &rettv);
16055 lnum = rettv.vval.v_number;
16056 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 }
16058 return lnum;
16059}
16060
16061/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016062 * Get the lnum from the first argument.
16063 * Also accepts "$", then "buf" is used.
16064 * Returns 0 on error.
16065 */
16066 static linenr_T
16067get_tv_lnum_buf(argvars, buf)
16068 typval_T *argvars;
16069 buf_T *buf;
16070{
16071 if (argvars[0].v_type == VAR_STRING
16072 && argvars[0].vval.v_string != NULL
16073 && argvars[0].vval.v_string[0] == '$'
16074 && buf != NULL)
16075 return buf->b_ml.ml_line_count;
16076 return get_tv_number_chk(&argvars[0], NULL);
16077}
16078
16079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 * Get the string value of a variable.
16081 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016082 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16083 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 * If the String variable has never been set, return an empty string.
16085 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16087 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 */
16089 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016090get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016091 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092{
16093 static char_u mybuf[NUMBUFLEN];
16094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016095 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096}
16097
16098 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016100 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 char_u *res = get_tv_string_buf_chk(varp, buf);
16104
16105 return res != NULL ? res : (char_u *)"";
16106}
16107
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016108 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016109get_tv_string_chk(varp)
16110 typval_T *varp;
16111{
16112 static char_u mybuf[NUMBUFLEN];
16113
16114 return get_tv_string_buf_chk(varp, mybuf);
16115}
16116
16117 static char_u *
16118get_tv_string_buf_chk(varp, buf)
16119 typval_T *varp;
16120 char_u *buf;
16121{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016122 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 case VAR_NUMBER:
16125 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16126 return buf;
16127 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016128 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016129 break;
16130 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016131 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016132 break;
16133 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016134 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016135 break;
16136 case VAR_STRING:
16137 if (varp->vval.v_string != NULL)
16138 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016139 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016140 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016141 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145}
16146
16147/*
16148 * Find variable "name" in the list of variables.
16149 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016150 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016151 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016155find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016157 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161
Bram Moolenaara7043832005-01-21 11:56:39 +000016162 ht = find_var_ht(name, &varname);
16163 if (htp != NULL)
16164 *htp = ht;
16165 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016167 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168}
16169
16170/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016172 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016174 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016175find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016177 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016178 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016179{
Bram Moolenaar33570922005-01-25 22:26:29 +000016180 hashitem_T *hi;
16181
16182 if (*varname == NUL)
16183 {
16184 /* Must be something like "s:", otherwise "ht" would be NULL. */
16185 switch (varname[-2])
16186 {
16187 case 's': return &SCRIPT_SV(current_SID).sv_var;
16188 case 'g': return &globvars_var;
16189 case 'v': return &vimvars_var;
16190 case 'b': return &curbuf->b_bufvar;
16191 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016192 case 'l': return current_funccal == NULL
16193 ? NULL : &current_funccal->l_vars_var;
16194 case 'a': return current_funccal == NULL
16195 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016196 }
16197 return NULL;
16198 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016199
16200 hi = hash_find(ht, varname);
16201 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016202 {
16203 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016204 * worked find the variable again. Don't auto-load a script if it was
16205 * loaded already, otherwise it would be loaded every time when
16206 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016207 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016208 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016209 hi = hash_find(ht, varname);
16210 if (HASHITEM_EMPTY(hi))
16211 return NULL;
16212 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016214}
16215
16216/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016217 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016218 * Set "varname" to the start of name without ':'.
16219 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016220 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016221find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 char_u *name;
16223 char_u **varname;
16224{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016225 hashitem_T *hi;
16226
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 if (name[1] != ':')
16228 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016229 /* The name must not start with a colon or #. */
16230 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 return NULL;
16232 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016233
16234 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016235 hi = hash_find(&compat_hashtab, name);
16236 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016237 return &compat_hashtab;
16238
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016240 return &globvarht; /* global variable */
16241 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 }
16243 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016244 if (*name == 'g') /* global variable */
16245 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016246 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16247 */
16248 if (vim_strchr(name + 2, ':') != NULL
16249 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016250 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 if (*name == 'v') /* v: variable */
16256 return &vimvarht;
16257 if (*name == 'a' && current_funccal != NULL) /* function argument */
16258 return &current_funccal->l_avars.dv_hashtab;
16259 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16260 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 if (*name == 's' /* script variable */
16262 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16263 return &SCRIPT_VARS(current_SID);
16264 return NULL;
16265}
16266
16267/*
16268 * Get the string value of a (global/local) variable.
16269 * Returns NULL when it doesn't exist.
16270 */
16271 char_u *
16272get_var_value(name)
16273 char_u *name;
16274{
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaara7043832005-01-21 11:56:39 +000016277 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278 if (v == NULL)
16279 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281}
16282
16283/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016284 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 * sourcing this script and when executing functions defined in the script.
16286 */
16287 void
16288new_script_vars(id)
16289 scid_T id;
16290{
Bram Moolenaara7043832005-01-21 11:56:39 +000016291 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 hashtab_T *ht;
16293 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016294
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16296 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016297 /* Re-allocating ga_data means that an ht_array pointing to
16298 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016300 for (i = 1; i <= ga_scripts.ga_len; ++i)
16301 {
16302 ht = &SCRIPT_VARS(i);
16303 if (ht->ht_mask == HT_INIT_SIZE - 1)
16304 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016305 sv = &SCRIPT_SV(i);
16306 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016307 }
16308
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 while (ga_scripts.ga_len < id)
16310 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16312 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 }
16315 }
16316}
16317
16318/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016319 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16320 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 */
16322 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016323init_var_dict(dict, dict_var)
16324 dict_T *dict;
16325 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326{
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 hash_init(&dict->dv_hashtab);
16328 dict->dv_refcount = 99999;
16329 dict_var->di_tv.vval.v_dict = dict;
16330 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016331 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16333 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334}
16335
16336/*
16337 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 * Frees all allocated variables and the value they contain.
16339 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 */
16341 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016342vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 hashtab_T *ht;
16344{
16345 vars_clear_ext(ht, TRUE);
16346}
16347
16348/*
16349 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16350 */
16351 static void
16352vars_clear_ext(ht, free_val)
16353 hashtab_T *ht;
16354 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355{
Bram Moolenaara7043832005-01-21 11:56:39 +000016356 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 hashitem_T *hi;
16358 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016361 todo = ht->ht_used;
16362 for (hi = ht->ht_array; todo > 0; ++hi)
16363 {
16364 if (!HASHITEM_EMPTY(hi))
16365 {
16366 --todo;
16367
Bram Moolenaar33570922005-01-25 22:26:29 +000016368 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016369 * ht_array might change then. hash_clear() takes care of it
16370 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 v = HI2DI(hi);
16372 if (free_val)
16373 clear_tv(&v->di_tv);
16374 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16375 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016376 }
16377 }
16378 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016379 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380}
16381
Bram Moolenaara7043832005-01-21 11:56:39 +000016382/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 * Delete a variable from hashtab "ht" at item "hi".
16384 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016387delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 hashtab_T *ht;
16389 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390{
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016392
16393 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 clear_tv(&di->di_tv);
16395 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396}
16397
16398/*
16399 * List the value of one internal variable.
16400 */
16401 static void
16402list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016403 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 char_u *prefix;
16405{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016406 char_u *tofree;
16407 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016408 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016409
Bram Moolenaar33570922005-01-25 22:26:29 +000016410 s = echo_string(&v->di_tv, &tofree, numbuf);
16411 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 s == NULL ? (char_u *)"" : s);
16413 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414}
16415
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 static void
16417list_one_var_a(prefix, name, type, string)
16418 char_u *prefix;
16419 char_u *name;
16420 int type;
16421 char_u *string;
16422{
16423 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16424 if (name != NULL) /* "a:" vars don't have a name stored */
16425 msg_puts(name);
16426 msg_putchar(' ');
16427 msg_advance(22);
16428 if (type == VAR_NUMBER)
16429 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016430 else if (type == VAR_FUNC)
16431 msg_putchar('*');
16432 else if (type == VAR_LIST)
16433 {
16434 msg_putchar('[');
16435 if (*string == '[')
16436 ++string;
16437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016438 else if (type == VAR_DICT)
16439 {
16440 msg_putchar('{');
16441 if (*string == '{')
16442 ++string;
16443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 else
16445 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016446
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016448
16449 if (type == VAR_FUNC)
16450 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451}
16452
16453/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016454 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 * If the variable already exists, the value is updated.
16456 * Otherwise the variable is created.
16457 */
16458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016462 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463{
Bram Moolenaar33570922005-01-25 22:26:29 +000016464 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016467 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016470 {
16471 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16472 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16473 ? name[2] : name[0]))
16474 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016475 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016476 return;
16477 }
16478 if (function_exists(name))
16479 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016480 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016481 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016482 return;
16483 }
16484 }
16485
Bram Moolenaara7043832005-01-21 11:56:39 +000016486 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016487 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016488 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016489 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016490 return;
16491 }
16492
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016493 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016494 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016496 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016497 if (var_check_ro(v->di_flags, name)
16498 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016499 return;
16500 if (v->di_tv.v_type != tv->v_type
16501 && !((v->di_tv.v_type == VAR_STRING
16502 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016503 && (tv->v_type == VAR_STRING
16504 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016505 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016506 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016507 return;
16508 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016509
16510 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016511 * Handle setting internal v: variables separately: we don't change
16512 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 */
16514 if (ht == &vimvarht)
16515 {
16516 if (v->di_tv.v_type == VAR_STRING)
16517 {
16518 vim_free(v->di_tv.vval.v_string);
16519 if (copy || tv->v_type != VAR_STRING)
16520 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16521 else
16522 {
16523 /* Take over the string to avoid an extra alloc/free. */
16524 v->di_tv.vval.v_string = tv->vval.v_string;
16525 tv->vval.v_string = NULL;
16526 }
16527 }
16528 else if (v->di_tv.v_type != VAR_NUMBER)
16529 EMSG2(_(e_intern2), "set_var()");
16530 else
16531 v->di_tv.vval.v_number = get_tv_number(tv);
16532 return;
16533 }
16534
16535 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 }
16537 else /* add a new variable */
16538 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016539 /* Make sure the variable name is valid. */
16540 for (p = varname; *p != NUL; ++p)
16541 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16542 {
16543 EMSG2(_(e_illvar), varname);
16544 return;
16545 }
16546
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016547 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16548 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016549 if (v == NULL)
16550 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016551 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016554 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 return;
16556 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016557 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016561 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016562 else
16563 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016564 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016565 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016566 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568}
16569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016570/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016571 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16572 * Also give an error message.
16573 */
16574 static int
16575var_check_ro(flags, name)
16576 int flags;
16577 char_u *name;
16578{
16579 if (flags & DI_FLAGS_RO)
16580 {
16581 EMSG2(_(e_readonlyvar), name);
16582 return TRUE;
16583 }
16584 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16585 {
16586 EMSG2(_(e_readonlysbx), name);
16587 return TRUE;
16588 }
16589 return FALSE;
16590}
16591
16592/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016593 * Return TRUE if typeval "tv" is set to be locked (immutable).
16594 * Also give an error message, using "name".
16595 */
16596 static int
16597tv_check_lock(lock, name)
16598 int lock;
16599 char_u *name;
16600{
16601 if (lock & VAR_LOCKED)
16602 {
16603 EMSG2(_("E741: Value is locked: %s"),
16604 name == NULL ? (char_u *)_("Unknown") : name);
16605 return TRUE;
16606 }
16607 if (lock & VAR_FIXED)
16608 {
16609 EMSG2(_("E742: Cannot change value of %s"),
16610 name == NULL ? (char_u *)_("Unknown") : name);
16611 return TRUE;
16612 }
16613 return FALSE;
16614}
16615
16616/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016617 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016618 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016619 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016622copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016623 typval_T *from;
16624 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016627 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016628 switch (from->v_type)
16629 {
16630 case VAR_NUMBER:
16631 to->vval.v_number = from->vval.v_number;
16632 break;
16633 case VAR_STRING:
16634 case VAR_FUNC:
16635 if (from->vval.v_string == NULL)
16636 to->vval.v_string = NULL;
16637 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016639 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016640 if (from->v_type == VAR_FUNC)
16641 func_ref(to->vval.v_string);
16642 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016643 break;
16644 case VAR_LIST:
16645 if (from->vval.v_list == NULL)
16646 to->vval.v_list = NULL;
16647 else
16648 {
16649 to->vval.v_list = from->vval.v_list;
16650 ++to->vval.v_list->lv_refcount;
16651 }
16652 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016653 case VAR_DICT:
16654 if (from->vval.v_dict == NULL)
16655 to->vval.v_dict = NULL;
16656 else
16657 {
16658 to->vval.v_dict = from->vval.v_dict;
16659 ++to->vval.v_dict->dv_refcount;
16660 }
16661 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016662 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016664 break;
16665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666}
16667
16668/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016669 * Make a copy of an item.
16670 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016671 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16672 * reference to an already copied list/dict can be used.
16673 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016674 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016675 static int
16676item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 typval_T *from;
16678 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016679 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016680 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016681{
16682 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016683 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016684
Bram Moolenaar33570922005-01-25 22:26:29 +000016685 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016686 {
16687 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016688 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016689 }
16690 ++recurse;
16691
16692 switch (from->v_type)
16693 {
16694 case VAR_NUMBER:
16695 case VAR_STRING:
16696 case VAR_FUNC:
16697 copy_tv(from, to);
16698 break;
16699 case VAR_LIST:
16700 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016701 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016702 if (from->vval.v_list == NULL)
16703 to->vval.v_list = NULL;
16704 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16705 {
16706 /* use the copy made earlier */
16707 to->vval.v_list = from->vval.v_list->lv_copylist;
16708 ++to->vval.v_list->lv_refcount;
16709 }
16710 else
16711 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16712 if (to->vval.v_list == NULL)
16713 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016714 break;
16715 case VAR_DICT:
16716 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016717 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016718 if (from->vval.v_dict == NULL)
16719 to->vval.v_dict = NULL;
16720 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16721 {
16722 /* use the copy made earlier */
16723 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16724 ++to->vval.v_dict->dv_refcount;
16725 }
16726 else
16727 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16728 if (to->vval.v_dict == NULL)
16729 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016730 break;
16731 default:
16732 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016733 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016734 }
16735 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016736 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016737}
16738
16739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 * ":echo expr1 ..." print each argument separated with a space, add a
16741 * newline at the end.
16742 * ":echon expr1 ..." print each argument plain.
16743 */
16744 void
16745ex_echo(eap)
16746 exarg_T *eap;
16747{
16748 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016749 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016750 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 char_u *p;
16752 int needclr = TRUE;
16753 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016754 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755
16756 if (eap->skip)
16757 ++emsg_skip;
16758 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16759 {
16760 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016761 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762 {
16763 /*
16764 * Report the invalid expression unless the expression evaluation
16765 * has been cancelled due to an aborting error, an interrupt, or an
16766 * exception.
16767 */
16768 if (!aborting())
16769 EMSG2(_(e_invexpr2), p);
16770 break;
16771 }
16772 if (!eap->skip)
16773 {
16774 if (atstart)
16775 {
16776 atstart = FALSE;
16777 /* Call msg_start() after eval1(), evaluating the expression
16778 * may cause a message to appear. */
16779 if (eap->cmdidx == CMD_echo)
16780 msg_start();
16781 }
16782 else if (eap->cmdidx == CMD_echo)
16783 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016784 p = echo_string(&rettv, &tofree, numbuf);
16785 if (p != NULL)
16786 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016788 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016790 if (*p != TAB && needclr)
16791 {
16792 /* remove any text still there from the command */
16793 msg_clr_eos();
16794 needclr = FALSE;
16795 }
16796 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 }
16798 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016799 {
16800#ifdef FEAT_MBYTE
16801 if (has_mbyte)
16802 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016803 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016804
16805 (void)msg_outtrans_len_attr(p, i, echo_attr);
16806 p += i - 1;
16807 }
16808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016810 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016813 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016815 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 arg = skipwhite(arg);
16817 }
16818 eap->nextcmd = check_nextcmd(arg);
16819
16820 if (eap->skip)
16821 --emsg_skip;
16822 else
16823 {
16824 /* remove text that may still be there from the command */
16825 if (needclr)
16826 msg_clr_eos();
16827 if (eap->cmdidx == CMD_echo)
16828 msg_end();
16829 }
16830}
16831
16832/*
16833 * ":echohl {name}".
16834 */
16835 void
16836ex_echohl(eap)
16837 exarg_T *eap;
16838{
16839 int id;
16840
16841 id = syn_name2id(eap->arg);
16842 if (id == 0)
16843 echo_attr = 0;
16844 else
16845 echo_attr = syn_id2attr(id);
16846}
16847
16848/*
16849 * ":execute expr1 ..." execute the result of an expression.
16850 * ":echomsg expr1 ..." Print a message
16851 * ":echoerr expr1 ..." Print an error
16852 * Each gets spaces around each argument and a newline at the end for
16853 * echo commands
16854 */
16855 void
16856ex_execute(eap)
16857 exarg_T *eap;
16858{
16859 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861 int ret = OK;
16862 char_u *p;
16863 garray_T ga;
16864 int len;
16865 int save_did_emsg;
16866
16867 ga_init2(&ga, 1, 80);
16868
16869 if (eap->skip)
16870 ++emsg_skip;
16871 while (*arg != NUL && *arg != '|' && *arg != '\n')
16872 {
16873 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016874 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 {
16876 /*
16877 * Report the invalid expression unless the expression evaluation
16878 * has been cancelled due to an aborting error, an interrupt, or an
16879 * exception.
16880 */
16881 if (!aborting())
16882 EMSG2(_(e_invexpr2), p);
16883 ret = FAIL;
16884 break;
16885 }
16886
16887 if (!eap->skip)
16888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 len = (int)STRLEN(p);
16891 if (ga_grow(&ga, len + 2) == FAIL)
16892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 ret = FAIL;
16895 break;
16896 }
16897 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 ga.ga_len += len;
16901 }
16902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904 arg = skipwhite(arg);
16905 }
16906
16907 if (ret != FAIL && ga.ga_data != NULL)
16908 {
16909 if (eap->cmdidx == CMD_echomsg)
16910 MSG_ATTR(ga.ga_data, echo_attr);
16911 else if (eap->cmdidx == CMD_echoerr)
16912 {
16913 /* We don't want to abort following commands, restore did_emsg. */
16914 save_did_emsg = did_emsg;
16915 EMSG((char_u *)ga.ga_data);
16916 if (!force_abort)
16917 did_emsg = save_did_emsg;
16918 }
16919 else if (eap->cmdidx == CMD_execute)
16920 do_cmdline((char_u *)ga.ga_data,
16921 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16922 }
16923
16924 ga_clear(&ga);
16925
16926 if (eap->skip)
16927 --emsg_skip;
16928
16929 eap->nextcmd = check_nextcmd(arg);
16930}
16931
16932/*
16933 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16934 * "arg" points to the "&" or '+' when called, to "option" when returning.
16935 * Returns NULL when no option name found. Otherwise pointer to the char
16936 * after the option name.
16937 */
16938 static char_u *
16939find_option_end(arg, opt_flags)
16940 char_u **arg;
16941 int *opt_flags;
16942{
16943 char_u *p = *arg;
16944
16945 ++p;
16946 if (*p == 'g' && p[1] == ':')
16947 {
16948 *opt_flags = OPT_GLOBAL;
16949 p += 2;
16950 }
16951 else if (*p == 'l' && p[1] == ':')
16952 {
16953 *opt_flags = OPT_LOCAL;
16954 p += 2;
16955 }
16956 else
16957 *opt_flags = 0;
16958
16959 if (!ASCII_ISALPHA(*p))
16960 return NULL;
16961 *arg = p;
16962
16963 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16964 p += 4; /* termcap option */
16965 else
16966 while (ASCII_ISALPHA(*p))
16967 ++p;
16968 return p;
16969}
16970
16971/*
16972 * ":function"
16973 */
16974 void
16975ex_function(eap)
16976 exarg_T *eap;
16977{
16978 char_u *theline;
16979 int j;
16980 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 char_u *name = NULL;
16983 char_u *p;
16984 char_u *arg;
16985 garray_T newargs;
16986 garray_T newlines;
16987 int varargs = FALSE;
16988 int mustend = FALSE;
16989 int flags = 0;
16990 ufunc_T *fp;
16991 int indent;
16992 int nesting;
16993 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016994 dictitem_T *v;
16995 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016996 static int func_nr = 0; /* number for nameless function */
16997 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016998 hashtab_T *ht;
16999 int todo;
17000 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001
17002 /*
17003 * ":function" without argument: list functions.
17004 */
17005 if (ends_excmd(*eap->arg))
17006 {
17007 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017008 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017009 todo = func_hashtab.ht_used;
17010 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017011 {
17012 if (!HASHITEM_EMPTY(hi))
17013 {
17014 --todo;
17015 fp = HI2UF(hi);
17016 if (!isdigit(*fp->uf_name))
17017 list_func_head(fp, FALSE);
17018 }
17019 }
17020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 eap->nextcmd = check_nextcmd(eap->arg);
17022 return;
17023 }
17024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017025 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017026 * ":function /pat": list functions matching pattern.
17027 */
17028 if (*eap->arg == '/')
17029 {
17030 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17031 if (!eap->skip)
17032 {
17033 regmatch_T regmatch;
17034
17035 c = *p;
17036 *p = NUL;
17037 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17038 *p = c;
17039 if (regmatch.regprog != NULL)
17040 {
17041 regmatch.rm_ic = p_ic;
17042
17043 todo = func_hashtab.ht_used;
17044 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17045 {
17046 if (!HASHITEM_EMPTY(hi))
17047 {
17048 --todo;
17049 fp = HI2UF(hi);
17050 if (!isdigit(*fp->uf_name)
17051 && vim_regexec(&regmatch, fp->uf_name, 0))
17052 list_func_head(fp, FALSE);
17053 }
17054 }
17055 }
17056 }
17057 if (*p == '/')
17058 ++p;
17059 eap->nextcmd = check_nextcmd(p);
17060 return;
17061 }
17062
17063 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017064 * Get the function name. There are these situations:
17065 * func normal function name
17066 * "name" == func, "fudi.fd_dict" == NULL
17067 * dict.func new dictionary entry
17068 * "name" == NULL, "fudi.fd_dict" set,
17069 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17070 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017071 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017072 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17073 * dict.func existing dict entry that's not a Funcref
17074 * "name" == NULL, "fudi.fd_dict" set,
17075 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017078 name = trans_function_name(&p, eap->skip, 0, &fudi);
17079 paren = (vim_strchr(p, '(') != NULL);
17080 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 {
17082 /*
17083 * Return on an invalid expression in braces, unless the expression
17084 * evaluation has been cancelled due to an aborting error, an
17085 * interrupt, or an exception.
17086 */
17087 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017089 if (!eap->skip && fudi.fd_newkey != NULL)
17090 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017091 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 else
17095 eap->skip = TRUE;
17096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 /* An error in a function call during evaluation of an expression in magic
17098 * braces should not cause the function not to be defined. */
17099 saved_did_emsg = did_emsg;
17100 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101
17102 /*
17103 * ":function func" with only function name: list function.
17104 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017105 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 {
17107 if (!ends_excmd(*skipwhite(p)))
17108 {
17109 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017110 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 }
17112 eap->nextcmd = check_nextcmd(p);
17113 if (eap->nextcmd != NULL)
17114 *p = NUL;
17115 if (!eap->skip && !got_int)
17116 {
17117 fp = find_func(name);
17118 if (fp != NULL)
17119 {
17120 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017121 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 {
17123 msg_putchar('\n');
17124 msg_outnum((long)(j + 1));
17125 if (j < 9)
17126 msg_putchar(' ');
17127 if (j < 99)
17128 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017129 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 out_flush(); /* show a line at a time */
17131 ui_breakcheck();
17132 }
17133 if (!got_int)
17134 {
17135 msg_putchar('\n');
17136 msg_puts((char_u *)" endfunction");
17137 }
17138 }
17139 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017140 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017142 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 }
17144
17145 /*
17146 * ":function name(arg1, arg2)" Define function.
17147 */
17148 p = skipwhite(p);
17149 if (*p != '(')
17150 {
17151 if (!eap->skip)
17152 {
17153 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017154 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 }
17156 /* attempt to continue by skipping some text */
17157 if (vim_strchr(p, '(') != NULL)
17158 p = vim_strchr(p, '(');
17159 }
17160 p = skipwhite(p + 1);
17161
17162 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17163 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17164
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017165 if (!eap->skip)
17166 {
17167 /* Check the name of the function. */
17168 if (name != NULL)
17169 arg = name;
17170 else
17171 arg = fudi.fd_newkey;
17172 if (arg != NULL)
17173 {
17174 if (*arg == K_SPECIAL)
17175 j = 3;
17176 else
17177 j = 0;
17178 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17179 : eval_isnamec(arg[j])))
17180 ++j;
17181 if (arg[j] != NUL)
17182 emsg_funcname(_(e_invarg2), arg);
17183 }
17184 }
17185
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 /*
17187 * Isolate the arguments: "arg1, arg2, ...)"
17188 */
17189 while (*p != ')')
17190 {
17191 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17192 {
17193 varargs = TRUE;
17194 p += 3;
17195 mustend = TRUE;
17196 }
17197 else
17198 {
17199 arg = p;
17200 while (ASCII_ISALNUM(*p) || *p == '_')
17201 ++p;
17202 if (arg == p || isdigit(*arg)
17203 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17204 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17205 {
17206 if (!eap->skip)
17207 EMSG2(_("E125: Illegal argument: %s"), arg);
17208 break;
17209 }
17210 if (ga_grow(&newargs, 1) == FAIL)
17211 goto erret;
17212 c = *p;
17213 *p = NUL;
17214 arg = vim_strsave(arg);
17215 if (arg == NULL)
17216 goto erret;
17217 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17218 *p = c;
17219 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 if (*p == ',')
17221 ++p;
17222 else
17223 mustend = TRUE;
17224 }
17225 p = skipwhite(p);
17226 if (mustend && *p != ')')
17227 {
17228 if (!eap->skip)
17229 EMSG2(_(e_invarg2), eap->arg);
17230 break;
17231 }
17232 }
17233 ++p; /* skip the ')' */
17234
Bram Moolenaare9a41262005-01-15 22:18:47 +000017235 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 for (;;)
17237 {
17238 p = skipwhite(p);
17239 if (STRNCMP(p, "range", 5) == 0)
17240 {
17241 flags |= FC_RANGE;
17242 p += 5;
17243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017244 else if (STRNCMP(p, "dict", 4) == 0)
17245 {
17246 flags |= FC_DICT;
17247 p += 4;
17248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 else if (STRNCMP(p, "abort", 5) == 0)
17250 {
17251 flags |= FC_ABORT;
17252 p += 5;
17253 }
17254 else
17255 break;
17256 }
17257
17258 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17259 EMSG(_(e_trailing));
17260
17261 /*
17262 * Read the body of the function, until ":endfunction" is found.
17263 */
17264 if (KeyTyped)
17265 {
17266 /* Check if the function already exists, don't let the user type the
17267 * whole function before telling him it doesn't work! For a script we
17268 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017269 if (!eap->skip && !eap->forceit)
17270 {
17271 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17272 EMSG(_(e_funcdict));
17273 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017274 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017277 if (!eap->skip && did_emsg)
17278 goto erret;
17279
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 msg_putchar('\n'); /* don't overwrite the function name */
17281 cmdline_row = msg_row;
17282 }
17283
17284 indent = 2;
17285 nesting = 0;
17286 for (;;)
17287 {
17288 msg_scroll = TRUE;
17289 need_wait_return = FALSE;
17290 if (eap->getline == NULL)
17291 theline = getcmdline(':', 0L, indent);
17292 else
17293 theline = eap->getline(':', eap->cookie, indent);
17294 if (KeyTyped)
17295 lines_left = Rows - 1;
17296 if (theline == NULL)
17297 {
17298 EMSG(_("E126: Missing :endfunction"));
17299 goto erret;
17300 }
17301
17302 if (skip_until != NULL)
17303 {
17304 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17305 * don't check for ":endfunc". */
17306 if (STRCMP(theline, skip_until) == 0)
17307 {
17308 vim_free(skip_until);
17309 skip_until = NULL;
17310 }
17311 }
17312 else
17313 {
17314 /* skip ':' and blanks*/
17315 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17316 ;
17317
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017318 /* Check for "endfunction". */
17319 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 {
17321 vim_free(theline);
17322 break;
17323 }
17324
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017325 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 * at "end". */
17327 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17328 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017329 else if (STRNCMP(p, "if", 2) == 0
17330 || STRNCMP(p, "wh", 2) == 0
17331 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 || STRNCMP(p, "try", 3) == 0)
17333 indent += 2;
17334
17335 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017336 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017338 if (*p == '!')
17339 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 p += eval_fname_script(p);
17341 if (ASCII_ISALPHA(*p))
17342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017343 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 if (*skipwhite(p) == '(')
17345 {
17346 ++nesting;
17347 indent += 2;
17348 }
17349 }
17350 }
17351
17352 /* Check for ":append" or ":insert". */
17353 p = skip_range(p, NULL);
17354 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17355 || (p[0] == 'i'
17356 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17357 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17358 skip_until = vim_strsave((char_u *)".");
17359
17360 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17361 arg = skipwhite(skiptowhite(p));
17362 if (arg[0] == '<' && arg[1] =='<'
17363 && ((p[0] == 'p' && p[1] == 'y'
17364 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17365 || (p[0] == 'p' && p[1] == 'e'
17366 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17367 || (p[0] == 't' && p[1] == 'c'
17368 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17369 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17370 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017371 || (p[0] == 'm' && p[1] == 'z'
17372 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 ))
17374 {
17375 /* ":python <<" continues until a dot, like ":append" */
17376 p = skipwhite(arg + 2);
17377 if (*p == NUL)
17378 skip_until = vim_strsave((char_u *)".");
17379 else
17380 skip_until = vim_strsave(p);
17381 }
17382 }
17383
17384 /* Add the line to the function. */
17385 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017386 {
17387 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017389 }
17390
17391 /* Copy the line to newly allocated memory. get_one_sourceline()
17392 * allocates 250 bytes per line, this saves 80% on average. The cost
17393 * is an extra alloc/free. */
17394 p = vim_strsave(theline);
17395 if (p != NULL)
17396 {
17397 vim_free(theline);
17398 theline = p;
17399 }
17400
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17402 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 }
17404
17405 /* Don't define the function when skipping commands or when an error was
17406 * detected. */
17407 if (eap->skip || did_emsg)
17408 goto erret;
17409
17410 /*
17411 * If there are no errors, add the function
17412 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017413 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017414 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017415 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017416 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017417 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017418 emsg_funcname("E707: Function name conflicts with variable: %s",
17419 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017420 goto erret;
17421 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017423 fp = find_func(name);
17424 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017426 if (!eap->forceit)
17427 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017428 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017429 goto erret;
17430 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017431 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017432 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017433 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017434 name);
17435 goto erret;
17436 }
17437 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017438 ga_clear_strings(&(fp->uf_args));
17439 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017440 vim_free(name);
17441 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 }
17444 else
17445 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017446 char numbuf[20];
17447
17448 fp = NULL;
17449 if (fudi.fd_newkey == NULL && !eap->forceit)
17450 {
17451 EMSG(_(e_funcdict));
17452 goto erret;
17453 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017454 if (fudi.fd_di == NULL)
17455 {
17456 /* Can't add a function to a locked dictionary */
17457 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17458 goto erret;
17459 }
17460 /* Can't change an existing function if it is locked */
17461 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17462 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017463
17464 /* Give the function a sequential number. Can only be used with a
17465 * Funcref! */
17466 vim_free(name);
17467 sprintf(numbuf, "%d", ++func_nr);
17468 name = vim_strsave((char_u *)numbuf);
17469 if (name == NULL)
17470 goto erret;
17471 }
17472
17473 if (fp == NULL)
17474 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017475 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017476 {
17477 int slen, plen;
17478 char_u *scriptname;
17479
17480 /* Check that the autoload name matches the script name. */
17481 j = FAIL;
17482 if (sourcing_name != NULL)
17483 {
17484 scriptname = autoload_name(name);
17485 if (scriptname != NULL)
17486 {
17487 p = vim_strchr(scriptname, '/');
17488 plen = STRLEN(p);
17489 slen = STRLEN(sourcing_name);
17490 if (slen > plen && fnamecmp(p,
17491 sourcing_name + slen - plen) == 0)
17492 j = OK;
17493 vim_free(scriptname);
17494 }
17495 }
17496 if (j == FAIL)
17497 {
17498 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17499 goto erret;
17500 }
17501 }
17502
17503 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 if (fp == NULL)
17505 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017506
17507 if (fudi.fd_dict != NULL)
17508 {
17509 if (fudi.fd_di == NULL)
17510 {
17511 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017512 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017513 if (fudi.fd_di == NULL)
17514 {
17515 vim_free(fp);
17516 goto erret;
17517 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017518 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17519 {
17520 vim_free(fudi.fd_di);
17521 goto erret;
17522 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017523 }
17524 else
17525 /* overwrite existing dict entry */
17526 clear_tv(&fudi.fd_di->di_tv);
17527 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017528 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017529 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017530 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017531 }
17532
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017534 STRCPY(fp->uf_name, name);
17535 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017537 fp->uf_args = newargs;
17538 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017539#ifdef FEAT_PROFILE
17540 fp->uf_tml_count = NULL;
17541 fp->uf_tml_total = NULL;
17542 fp->uf_tml_self = NULL;
17543 fp->uf_profiling = FALSE;
17544 if (prof_def_func())
17545 func_do_profile(fp);
17546#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017547 fp->uf_varargs = varargs;
17548 fp->uf_flags = flags;
17549 fp->uf_calls = 0;
17550 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017551 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552
17553erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 ga_clear_strings(&newargs);
17555 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017556ret_free:
17557 vim_free(skip_until);
17558 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561}
17562
17563/*
17564 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017565 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017567 * flags:
17568 * TFN_INT: internal function name OK
17569 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 * Advances "pp" to just after the function name (if no error).
17571 */
17572 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017573trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 char_u **pp;
17575 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017576 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017579 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 char_u *start;
17581 char_u *end;
17582 int lead;
17583 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017585 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017586
17587 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017590
17591 /* Check for hard coded <SNR>: already translated function ID (from a user
17592 * command). */
17593 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17594 && (*pp)[2] == (int)KE_SNR)
17595 {
17596 *pp += 3;
17597 len = get_id_len(pp) + 3;
17598 return vim_strnsave(start, len);
17599 }
17600
17601 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17602 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017604 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017606
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017607 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17608 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017609 if (end == start)
17610 {
17611 if (!skip)
17612 EMSG(_("E129: Function name required"));
17613 goto theend;
17614 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017615 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017616 {
17617 /*
17618 * Report an invalid expression in braces, unless the expression
17619 * evaluation has been cancelled due to an aborting error, an
17620 * interrupt, or an exception.
17621 */
17622 if (!aborting())
17623 {
17624 if (end != NULL)
17625 EMSG2(_(e_invarg2), start);
17626 }
17627 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017628 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017629 goto theend;
17630 }
17631
17632 if (lv.ll_tv != NULL)
17633 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017634 if (fdp != NULL)
17635 {
17636 fdp->fd_dict = lv.ll_dict;
17637 fdp->fd_newkey = lv.ll_newkey;
17638 lv.ll_newkey = NULL;
17639 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017640 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017641 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17642 {
17643 name = vim_strsave(lv.ll_tv->vval.v_string);
17644 *pp = end;
17645 }
17646 else
17647 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017648 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17649 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017650 EMSG(_(e_funcref));
17651 else
17652 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017653 name = NULL;
17654 }
17655 goto theend;
17656 }
17657
17658 if (lv.ll_name == NULL)
17659 {
17660 /* Error found, but continue after the function name. */
17661 *pp = end;
17662 goto theend;
17663 }
17664
17665 if (lv.ll_exp_name != NULL)
17666 len = STRLEN(lv.ll_exp_name);
17667 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017668 {
17669 if (lead == 2) /* skip over "s:" */
17670 lv.ll_name += 2;
17671 len = (int)(end - lv.ll_name);
17672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017673
17674 /*
17675 * Copy the function name to allocated memory.
17676 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17677 * Accept <SNR>123_name() outside a script.
17678 */
17679 if (skip)
17680 lead = 0; /* do nothing */
17681 else if (lead > 0)
17682 {
17683 lead = 3;
17684 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17685 {
17686 if (current_SID <= 0)
17687 {
17688 EMSG(_(e_usingsid));
17689 goto theend;
17690 }
17691 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17692 lead += (int)STRLEN(sid_buf);
17693 }
17694 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017695 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017696 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017697 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017698 goto theend;
17699 }
17700 name = alloc((unsigned)(len + lead + 1));
17701 if (name != NULL)
17702 {
17703 if (lead > 0)
17704 {
17705 name[0] = K_SPECIAL;
17706 name[1] = KS_EXTRA;
17707 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017708 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017709 STRCPY(name + 3, sid_buf);
17710 }
17711 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17712 name[len + lead] = NUL;
17713 }
17714 *pp = end;
17715
17716theend:
17717 clear_lval(&lv);
17718 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719}
17720
17721/*
17722 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17723 * Return 2 if "p" starts with "s:".
17724 * Return 0 otherwise.
17725 */
17726 static int
17727eval_fname_script(p)
17728 char_u *p;
17729{
17730 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17731 || STRNICMP(p + 1, "SNR>", 4) == 0))
17732 return 5;
17733 if (p[0] == 's' && p[1] == ':')
17734 return 2;
17735 return 0;
17736}
17737
17738/*
17739 * Return TRUE if "p" starts with "<SID>" or "s:".
17740 * Only works if eval_fname_script() returned non-zero for "p"!
17741 */
17742 static int
17743eval_fname_sid(p)
17744 char_u *p;
17745{
17746 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17747}
17748
17749/*
17750 * List the head of the function: "name(arg1, arg2)".
17751 */
17752 static void
17753list_func_head(fp, indent)
17754 ufunc_T *fp;
17755 int indent;
17756{
17757 int j;
17758
17759 msg_start();
17760 if (indent)
17761 MSG_PUTS(" ");
17762 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017763 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 {
17765 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017766 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 }
17768 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017769 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017771 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 {
17773 if (j)
17774 MSG_PUTS(", ");
17775 msg_puts(FUNCARG(fp, j));
17776 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017777 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 {
17779 if (j)
17780 MSG_PUTS(", ");
17781 MSG_PUTS("...");
17782 }
17783 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017784 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017785 if (p_verbose > 0)
17786 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787}
17788
17789/*
17790 * Find a function by name, return pointer to it in ufuncs.
17791 * Return NULL for unknown function.
17792 */
17793 static ufunc_T *
17794find_func(name)
17795 char_u *name;
17796{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017797 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017799 hi = hash_find(&func_hashtab, name);
17800 if (!HASHITEM_EMPTY(hi))
17801 return HI2UF(hi);
17802 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803}
17804
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017805#if defined(EXITFREE) || defined(PROTO)
17806 void
17807free_all_functions()
17808{
17809 hashitem_T *hi;
17810
17811 /* Need to start all over every time, because func_free() may change the
17812 * hash table. */
17813 while (func_hashtab.ht_used > 0)
17814 for (hi = func_hashtab.ht_array; ; ++hi)
17815 if (!HASHITEM_EMPTY(hi))
17816 {
17817 func_free(HI2UF(hi));
17818 break;
17819 }
17820}
17821#endif
17822
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017823/*
17824 * Return TRUE if a function "name" exists.
17825 */
17826 static int
17827function_exists(name)
17828 char_u *name;
17829{
17830 char_u *p = name;
17831 int n = FALSE;
17832
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017833 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017834 if (p != NULL)
17835 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017836 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017837 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017838 else
17839 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017840 vim_free(p);
17841 }
17842 return n;
17843}
17844
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017845/*
17846 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017847 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017848 */
17849 static int
17850builtin_function(name)
17851 char_u *name;
17852{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017853 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17854 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017855}
17856
Bram Moolenaar05159a02005-02-26 23:04:13 +000017857#if defined(FEAT_PROFILE) || defined(PROTO)
17858/*
17859 * Start profiling function "fp".
17860 */
17861 static void
17862func_do_profile(fp)
17863 ufunc_T *fp;
17864{
17865 fp->uf_tm_count = 0;
17866 profile_zero(&fp->uf_tm_self);
17867 profile_zero(&fp->uf_tm_total);
17868 if (fp->uf_tml_count == NULL)
17869 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17870 (sizeof(int) * fp->uf_lines.ga_len));
17871 if (fp->uf_tml_total == NULL)
17872 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17873 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17874 if (fp->uf_tml_self == NULL)
17875 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17876 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17877 fp->uf_tml_idx = -1;
17878 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17879 || fp->uf_tml_self == NULL)
17880 return; /* out of memory */
17881
17882 fp->uf_profiling = TRUE;
17883}
17884
17885/*
17886 * Dump the profiling results for all functions in file "fd".
17887 */
17888 void
17889func_dump_profile(fd)
17890 FILE *fd;
17891{
17892 hashitem_T *hi;
17893 int todo;
17894 ufunc_T *fp;
17895 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017896 ufunc_T **sorttab;
17897 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017898
17899 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017900 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17901
Bram Moolenaar05159a02005-02-26 23:04:13 +000017902 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17903 {
17904 if (!HASHITEM_EMPTY(hi))
17905 {
17906 --todo;
17907 fp = HI2UF(hi);
17908 if (fp->uf_profiling)
17909 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017910 if (sorttab != NULL)
17911 sorttab[st_len++] = fp;
17912
Bram Moolenaar05159a02005-02-26 23:04:13 +000017913 if (fp->uf_name[0] == K_SPECIAL)
17914 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17915 else
17916 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17917 if (fp->uf_tm_count == 1)
17918 fprintf(fd, "Called 1 time\n");
17919 else
17920 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17921 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17922 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17923 fprintf(fd, "\n");
17924 fprintf(fd, "count total (s) self (s)\n");
17925
17926 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17927 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017928 prof_func_line(fd, fp->uf_tml_count[i],
17929 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017930 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17931 }
17932 fprintf(fd, "\n");
17933 }
17934 }
17935 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017936
17937 if (sorttab != NULL && st_len > 0)
17938 {
17939 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17940 prof_total_cmp);
17941 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17942 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17943 prof_self_cmp);
17944 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17945 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017946}
Bram Moolenaar73830342005-02-28 22:48:19 +000017947
17948 static void
17949prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17950 FILE *fd;
17951 ufunc_T **sorttab;
17952 int st_len;
17953 char *title;
17954 int prefer_self; /* when equal print only self time */
17955{
17956 int i;
17957 ufunc_T *fp;
17958
17959 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17960 fprintf(fd, "count total (s) self (s) function\n");
17961 for (i = 0; i < 20 && i < st_len; ++i)
17962 {
17963 fp = sorttab[i];
17964 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17965 prefer_self);
17966 if (fp->uf_name[0] == K_SPECIAL)
17967 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17968 else
17969 fprintf(fd, " %s()\n", fp->uf_name);
17970 }
17971 fprintf(fd, "\n");
17972}
17973
17974/*
17975 * Print the count and times for one function or function line.
17976 */
17977 static void
17978prof_func_line(fd, count, total, self, prefer_self)
17979 FILE *fd;
17980 int count;
17981 proftime_T *total;
17982 proftime_T *self;
17983 int prefer_self; /* when equal print only self time */
17984{
17985 if (count > 0)
17986 {
17987 fprintf(fd, "%5d ", count);
17988 if (prefer_self && profile_equal(total, self))
17989 fprintf(fd, " ");
17990 else
17991 fprintf(fd, "%s ", profile_msg(total));
17992 if (!prefer_self && profile_equal(total, self))
17993 fprintf(fd, " ");
17994 else
17995 fprintf(fd, "%s ", profile_msg(self));
17996 }
17997 else
17998 fprintf(fd, " ");
17999}
18000
18001/*
18002 * Compare function for total time sorting.
18003 */
18004 static int
18005#ifdef __BORLANDC__
18006_RTLENTRYF
18007#endif
18008prof_total_cmp(s1, s2)
18009 const void *s1;
18010 const void *s2;
18011{
18012 ufunc_T *p1, *p2;
18013
18014 p1 = *(ufunc_T **)s1;
18015 p2 = *(ufunc_T **)s2;
18016 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18017}
18018
18019/*
18020 * Compare function for self time sorting.
18021 */
18022 static int
18023#ifdef __BORLANDC__
18024_RTLENTRYF
18025#endif
18026prof_self_cmp(s1, s2)
18027 const void *s1;
18028 const void *s2;
18029{
18030 ufunc_T *p1, *p2;
18031
18032 p1 = *(ufunc_T **)s1;
18033 p2 = *(ufunc_T **)s2;
18034 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18035}
18036
Bram Moolenaar05159a02005-02-26 23:04:13 +000018037#endif
18038
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018039/* The names of packages that once were loaded is remembered. */
18040static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18041
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018042/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018043 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018044 * Return TRUE if a package was loaded.
18045 */
18046 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018047script_autoload(name, reload)
18048 char_u *name;
18049 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018050{
18051 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018052 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018053 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018054 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018055
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018056 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018057 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018058 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018059 return FALSE;
18060
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018061 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018062
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018063 /* Find the name in the list of previously loaded package names. Skip
18064 * "autoload/", it's always the same. */
18065 for (i = 0; i < ga_loaded.ga_len; ++i)
18066 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18067 break;
18068 if (!reload && i < ga_loaded.ga_len)
18069 ret = FALSE; /* was loaded already */
18070 else
18071 {
18072 /* Remember the name if it wasn't loaded already. */
18073 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18074 {
18075 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18076 tofree = NULL;
18077 }
18078
18079 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018080 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018081 ret = TRUE;
18082 }
18083
18084 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018085 return ret;
18086}
18087
18088/*
18089 * Return the autoload script name for a function or variable name.
18090 * Returns NULL when out of memory.
18091 */
18092 static char_u *
18093autoload_name(name)
18094 char_u *name;
18095{
18096 char_u *p;
18097 char_u *scriptname;
18098
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018099 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018100 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18101 if (scriptname == NULL)
18102 return FALSE;
18103 STRCPY(scriptname, "autoload/");
18104 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018105 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018106 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018107 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018108 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018109 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018110}
18111
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18113
18114/*
18115 * Function given to ExpandGeneric() to obtain the list of user defined
18116 * function names.
18117 */
18118 char_u *
18119get_user_func_name(xp, idx)
18120 expand_T *xp;
18121 int idx;
18122{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018123 static long_u done;
18124 static hashitem_T *hi;
18125 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126
18127 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018129 done = 0;
18130 hi = func_hashtab.ht_array;
18131 }
18132 if (done < func_hashtab.ht_used)
18133 {
18134 if (done++ > 0)
18135 ++hi;
18136 while (HASHITEM_EMPTY(hi))
18137 ++hi;
18138 fp = HI2UF(hi);
18139
18140 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18141 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142
18143 cat_func_name(IObuff, fp);
18144 if (xp->xp_context != EXPAND_USER_FUNC)
18145 {
18146 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018147 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 STRCAT(IObuff, ")");
18149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 return IObuff;
18151 }
18152 return NULL;
18153}
18154
18155#endif /* FEAT_CMDL_COMPL */
18156
18157/*
18158 * Copy the function name of "fp" to buffer "buf".
18159 * "buf" must be able to hold the function name plus three bytes.
18160 * Takes care of script-local function names.
18161 */
18162 static void
18163cat_func_name(buf, fp)
18164 char_u *buf;
18165 ufunc_T *fp;
18166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018167 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 {
18169 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018170 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 }
18172 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018173 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174}
18175
18176/*
18177 * ":delfunction {name}"
18178 */
18179 void
18180ex_delfunction(eap)
18181 exarg_T *eap;
18182{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018183 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 char_u *p;
18185 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018186 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187
18188 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018189 name = trans_function_name(&p, eap->skip, 0, &fudi);
18190 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018192 {
18193 if (fudi.fd_dict != NULL && !eap->skip)
18194 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 if (!ends_excmd(*skipwhite(p)))
18198 {
18199 vim_free(name);
18200 EMSG(_(e_trailing));
18201 return;
18202 }
18203 eap->nextcmd = check_nextcmd(p);
18204 if (eap->nextcmd != NULL)
18205 *p = NUL;
18206
18207 if (!eap->skip)
18208 fp = find_func(name);
18209 vim_free(name);
18210
18211 if (!eap->skip)
18212 {
18213 if (fp == NULL)
18214 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018215 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 return;
18217 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018218 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 {
18220 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18221 return;
18222 }
18223
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018224 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018226 /* Delete the dict item that refers to the function, it will
18227 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018228 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018230 else
18231 func_free(fp);
18232 }
18233}
18234
18235/*
18236 * Free a function and remove it from the list of functions.
18237 */
18238 static void
18239func_free(fp)
18240 ufunc_T *fp;
18241{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018242 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018243
18244 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018245 ga_clear_strings(&(fp->uf_args));
18246 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018247#ifdef FEAT_PROFILE
18248 vim_free(fp->uf_tml_count);
18249 vim_free(fp->uf_tml_total);
18250 vim_free(fp->uf_tml_self);
18251#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018252
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018253 /* remove the function from the function hashtable */
18254 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18255 if (HASHITEM_EMPTY(hi))
18256 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018257 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018258 hash_remove(&func_hashtab, hi);
18259
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018260 vim_free(fp);
18261}
18262
18263/*
18264 * Unreference a Function: decrement the reference count and free it when it
18265 * becomes zero. Only for numbered functions.
18266 */
18267 static void
18268func_unref(name)
18269 char_u *name;
18270{
18271 ufunc_T *fp;
18272
18273 if (name != NULL && isdigit(*name))
18274 {
18275 fp = find_func(name);
18276 if (fp == NULL)
18277 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018278 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018279 {
18280 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018281 * when "uf_calls" becomes zero. */
18282 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018283 func_free(fp);
18284 }
18285 }
18286}
18287
18288/*
18289 * Count a reference to a Function.
18290 */
18291 static void
18292func_ref(name)
18293 char_u *name;
18294{
18295 ufunc_T *fp;
18296
18297 if (name != NULL && isdigit(*name))
18298 {
18299 fp = find_func(name);
18300 if (fp == NULL)
18301 EMSG2(_(e_intern2), "func_ref()");
18302 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018303 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 }
18305}
18306
18307/*
18308 * Call a user function.
18309 */
18310 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018311call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 ufunc_T *fp; /* pointer to function */
18313 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018314 typval_T *argvars; /* arguments */
18315 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 linenr_T firstline; /* first line of range */
18317 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319{
Bram Moolenaar33570922005-01-25 22:26:29 +000018320 char_u *save_sourcing_name;
18321 linenr_T save_sourcing_lnum;
18322 scid_T save_current_SID;
18323 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018324 int save_did_emsg;
18325 static int depth = 0;
18326 dictitem_T *v;
18327 int fixvar_idx = 0; /* index in fixvar[] */
18328 int i;
18329 int ai;
18330 char_u numbuf[NUMBUFLEN];
18331 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018332#ifdef FEAT_PROFILE
18333 proftime_T wait_start;
18334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335
18336 /* If depth of calling is getting too high, don't execute the function */
18337 if (depth >= p_mfd)
18338 {
18339 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 rettv->v_type = VAR_NUMBER;
18341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 return;
18343 }
18344 ++depth;
18345
18346 line_breakcheck(); /* check for CTRL-C hit */
18347
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018348 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018349 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018351 fc.rettv = rettv;
18352 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 fc.linenr = 0;
18354 fc.returned = FALSE;
18355 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018357 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 fc.dbg_tick = debug_tick;
18359
Bram Moolenaar33570922005-01-25 22:26:29 +000018360 /*
18361 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18362 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18363 * each argument variable and saves a lot of time.
18364 */
18365 /*
18366 * Init l: variables.
18367 */
18368 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018369 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018370 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 /* Set l:self to "selfdict". */
18372 v = &fc.fixvar[fixvar_idx++].var;
18373 STRCPY(v->di_key, "self");
18374 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18375 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18376 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018377 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018378 v->di_tv.vval.v_dict = selfdict;
18379 ++selfdict->dv_refcount;
18380 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018381
Bram Moolenaar33570922005-01-25 22:26:29 +000018382 /*
18383 * Init a: variables.
18384 * Set a:0 to "argcount".
18385 * Set a:000 to a list with room for the "..." arguments.
18386 */
18387 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18388 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018389 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018390 v = &fc.fixvar[fixvar_idx++].var;
18391 STRCPY(v->di_key, "000");
18392 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18393 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18394 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018395 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018396 v->di_tv.vval.v_list = &fc.l_varlist;
18397 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18398 fc.l_varlist.lv_refcount = 99999;
18399
18400 /*
18401 * Set a:firstline to "firstline" and a:lastline to "lastline".
18402 * Set a:name to named arguments.
18403 * Set a:N to the "..." arguments.
18404 */
18405 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18406 (varnumber_T)firstline);
18407 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18408 (varnumber_T)lastline);
18409 for (i = 0; i < argcount; ++i)
18410 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018411 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018412 if (ai < 0)
18413 /* named argument a:name */
18414 name = FUNCARG(fp, i);
18415 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018416 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018417 /* "..." argument a:1, a:2, etc. */
18418 sprintf((char *)numbuf, "%d", ai + 1);
18419 name = numbuf;
18420 }
18421 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18422 {
18423 v = &fc.fixvar[fixvar_idx++].var;
18424 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18425 }
18426 else
18427 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018428 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18429 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 if (v == NULL)
18431 break;
18432 v->di_flags = DI_FLAGS_RO;
18433 }
18434 STRCPY(v->di_key, name);
18435 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18436
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018437 /* Note: the values are copied directly to avoid alloc/free.
18438 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018439 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018440 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018441
18442 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18443 {
18444 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18445 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018446 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018447 }
18448 }
18449
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 /* Don't redraw while executing the function. */
18451 ++RedrawingDisabled;
18452 save_sourcing_name = sourcing_name;
18453 save_sourcing_lnum = sourcing_lnum;
18454 sourcing_lnum = 1;
18455 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018456 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 if (sourcing_name != NULL)
18458 {
18459 if (save_sourcing_name != NULL
18460 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18461 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18462 else
18463 STRCPY(sourcing_name, "function ");
18464 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18465
18466 if (p_verbose >= 12)
18467 {
18468 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018469 verbose_enter_scroll();
18470
Bram Moolenaar555b2802005-05-19 21:08:39 +000018471 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 if (p_verbose >= 14)
18473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018475 char_u numbuf[NUMBUFLEN];
18476 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477
18478 msg_puts((char_u *)"(");
18479 for (i = 0; i < argcount; ++i)
18480 {
18481 if (i > 0)
18482 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018483 if (argvars[i].v_type == VAR_NUMBER)
18484 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 else
18486 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018487 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018488 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018490 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 }
18492 }
18493 msg_puts((char_u *)")");
18494 }
18495 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018496
18497 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 --no_wait_return;
18499 }
18500 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018501#ifdef FEAT_PROFILE
18502 if (do_profiling)
18503 {
18504 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18505 func_do_profile(fp);
18506 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018507 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018508 {
18509 ++fp->uf_tm_count;
18510 profile_start(&fp->uf_tm_start);
18511 profile_zero(&fp->uf_tm_children);
18512 }
18513 script_prof_save(&wait_start);
18514 }
18515#endif
18516
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018518 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 save_did_emsg = did_emsg;
18520 did_emsg = FALSE;
18521
18522 /* call do_cmdline() to execute the lines */
18523 do_cmdline(NULL, get_func_line, (void *)&fc,
18524 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18525
18526 --RedrawingDisabled;
18527
18528 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018529 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018531 clear_tv(rettv);
18532 rettv->v_type = VAR_NUMBER;
18533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 }
18535
Bram Moolenaar05159a02005-02-26 23:04:13 +000018536#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018537 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018538 {
18539 profile_end(&fp->uf_tm_start);
18540 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18541 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18542 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18543 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018544 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018545 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018546 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18547 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018548 }
18549 }
18550#endif
18551
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 /* when being verbose, mention the return value */
18553 if (p_verbose >= 12)
18554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018556 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018559 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018560 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018561 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18562 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018565 char_u buf[MSG_BUF_LEN];
18566 char_u numbuf[NUMBUFLEN];
18567 char_u *tofree;
18568
Bram Moolenaar555b2802005-05-19 21:08:39 +000018569 /* The value may be very long. Skip the middle part, so that we
18570 * have some idea how it starts and ends. smsg() would always
18571 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018572 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018573 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018574 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018575 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 }
18577 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018578
18579 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 --no_wait_return;
18581 }
18582
18583 vim_free(sourcing_name);
18584 sourcing_name = save_sourcing_name;
18585 sourcing_lnum = save_sourcing_lnum;
18586 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018587#ifdef FEAT_PROFILE
18588 if (do_profiling)
18589 script_prof_restore(&wait_start);
18590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591
18592 if (p_verbose >= 12 && sourcing_name != NULL)
18593 {
18594 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018595 verbose_enter_scroll();
18596
Bram Moolenaar555b2802005-05-19 21:08:39 +000018597 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018599
18600 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 --no_wait_return;
18602 }
18603
18604 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018605 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606
Bram Moolenaar33570922005-01-25 22:26:29 +000018607 /* The a: variables typevals were not alloced, only free the allocated
18608 * variables. */
18609 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18610
18611 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 --depth;
18613}
18614
18615/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018616 * Add a number variable "name" to dict "dp" with value "nr".
18617 */
18618 static void
18619add_nr_var(dp, v, name, nr)
18620 dict_T *dp;
18621 dictitem_T *v;
18622 char *name;
18623 varnumber_T nr;
18624{
18625 STRCPY(v->di_key, name);
18626 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18627 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18628 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018629 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018630 v->di_tv.vval.v_number = nr;
18631}
18632
18633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 * ":return [expr]"
18635 */
18636 void
18637ex_return(eap)
18638 exarg_T *eap;
18639{
18640 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018641 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 int returning = FALSE;
18643
18644 if (current_funccal == NULL)
18645 {
18646 EMSG(_("E133: :return not inside a function"));
18647 return;
18648 }
18649
18650 if (eap->skip)
18651 ++emsg_skip;
18652
18653 eap->nextcmd = NULL;
18654 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 {
18657 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 }
18662 /* It's safer to return also on error. */
18663 else if (!eap->skip)
18664 {
18665 /*
18666 * Return unless the expression evaluation has been cancelled due to an
18667 * aborting error, an interrupt, or an exception.
18668 */
18669 if (!aborting())
18670 returning = do_return(eap, FALSE, TRUE, NULL);
18671 }
18672
18673 /* When skipping or the return gets pending, advance to the next command
18674 * in this line (!returning). Otherwise, ignore the rest of the line.
18675 * Following lines will be ignored by get_func_line(). */
18676 if (returning)
18677 eap->nextcmd = NULL;
18678 else if (eap->nextcmd == NULL) /* no argument */
18679 eap->nextcmd = check_nextcmd(arg);
18680
18681 if (eap->skip)
18682 --emsg_skip;
18683}
18684
18685/*
18686 * Return from a function. Possibly makes the return pending. Also called
18687 * for a pending return at the ":endtry" or after returning from an extra
18688 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 * FALSE when the return gets pending.
18692 */
18693 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 exarg_T *eap;
18696 int reanimate;
18697 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699{
18700 int idx;
18701 struct condstack *cstack = eap->cstack;
18702
18703 if (reanimate)
18704 /* Undo the return. */
18705 current_funccal->returned = FALSE;
18706
18707 /*
18708 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18709 * not in its finally clause (which then is to be executed next) is found.
18710 * In this case, make the ":return" pending for execution at the ":endtry".
18711 * Otherwise, return normally.
18712 */
18713 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18714 if (idx >= 0)
18715 {
18716 cstack->cs_pending[idx] = CSTP_RETURN;
18717
18718 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018719 /* A pending return again gets pending. "rettv" points to an
18720 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 else
18724 {
18725 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018730 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 {
18732 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018733 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018734 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 else
18736 EMSG(_(e_outofmem));
18737 }
18738 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740
18741 if (reanimate)
18742 {
18743 /* The pending return value could be overwritten by a ":return"
18744 * without argument in a finally clause; reset the default
18745 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018746 current_funccal->rettv->v_type = VAR_NUMBER;
18747 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 }
18749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 }
18752 else
18753 {
18754 current_funccal->returned = TRUE;
18755
18756 /* If the return is carried out now, store the return value. For
18757 * a return immediately after reanimation, the value is already
18758 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018761 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018762 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018764 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 }
18766 }
18767
18768 return idx < 0;
18769}
18770
18771/*
18772 * Free the variable with a pending return value.
18773 */
18774 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775discard_pending_return(rettv)
18776 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777{
Bram Moolenaar33570922005-01-25 22:26:29 +000018778 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779}
18780
18781/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018782 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 * is an allocated string. Used by report_pending() for verbose messages.
18784 */
18785 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786get_return_cmd(rettv)
18787 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018789 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018791 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018793 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018794 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018795 if (s == NULL)
18796 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797
18798 STRCPY(IObuff, ":return ");
18799 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18800 if (STRLEN(s) + 8 >= IOSIZE)
18801 STRCPY(IObuff + IOSIZE - 4, "...");
18802 vim_free(tofree);
18803 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804}
18805
18806/*
18807 * Get next function line.
18808 * Called by do_cmdline() to get the next line.
18809 * Returns allocated string, or NULL for end of function.
18810 */
18811/* ARGSUSED */
18812 char_u *
18813get_func_line(c, cookie, indent)
18814 int c; /* not used */
18815 void *cookie;
18816 int indent; /* not used */
18817{
Bram Moolenaar33570922005-01-25 22:26:29 +000018818 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018819 ufunc_T *fp = fcp->func;
18820 char_u *retval;
18821 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822
18823 /* If breakpoints have been added/deleted need to check for it. */
18824 if (fcp->dbg_tick != debug_tick)
18825 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018826 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 sourcing_lnum);
18828 fcp->dbg_tick = debug_tick;
18829 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018830#ifdef FEAT_PROFILE
18831 if (do_profiling)
18832 func_line_end(cookie);
18833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834
Bram Moolenaar05159a02005-02-26 23:04:13 +000018835 gap = &fp->uf_lines;
18836 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 retval = NULL;
18838 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18839 retval = NULL;
18840 else
18841 {
18842 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18843 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018844#ifdef FEAT_PROFILE
18845 if (do_profiling)
18846 func_line_start(cookie);
18847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 }
18849
18850 /* Did we encounter a breakpoint? */
18851 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18852 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018853 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018855 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 sourcing_lnum);
18857 fcp->dbg_tick = debug_tick;
18858 }
18859
18860 return retval;
18861}
18862
Bram Moolenaar05159a02005-02-26 23:04:13 +000018863#if defined(FEAT_PROFILE) || defined(PROTO)
18864/*
18865 * Called when starting to read a function line.
18866 * "sourcing_lnum" must be correct!
18867 * When skipping lines it may not actually be executed, but we won't find out
18868 * until later and we need to store the time now.
18869 */
18870 void
18871func_line_start(cookie)
18872 void *cookie;
18873{
18874 funccall_T *fcp = (funccall_T *)cookie;
18875 ufunc_T *fp = fcp->func;
18876
18877 if (fp->uf_profiling && sourcing_lnum >= 1
18878 && sourcing_lnum <= fp->uf_lines.ga_len)
18879 {
18880 fp->uf_tml_idx = sourcing_lnum - 1;
18881 fp->uf_tml_execed = FALSE;
18882 profile_start(&fp->uf_tml_start);
18883 profile_zero(&fp->uf_tml_children);
18884 profile_get_wait(&fp->uf_tml_wait);
18885 }
18886}
18887
18888/*
18889 * Called when actually executing a function line.
18890 */
18891 void
18892func_line_exec(cookie)
18893 void *cookie;
18894{
18895 funccall_T *fcp = (funccall_T *)cookie;
18896 ufunc_T *fp = fcp->func;
18897
18898 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18899 fp->uf_tml_execed = TRUE;
18900}
18901
18902/*
18903 * Called when done with a function line.
18904 */
18905 void
18906func_line_end(cookie)
18907 void *cookie;
18908{
18909 funccall_T *fcp = (funccall_T *)cookie;
18910 ufunc_T *fp = fcp->func;
18911
18912 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18913 {
18914 if (fp->uf_tml_execed)
18915 {
18916 ++fp->uf_tml_count[fp->uf_tml_idx];
18917 profile_end(&fp->uf_tml_start);
18918 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18919 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18920 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18921 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18922 }
18923 fp->uf_tml_idx = -1;
18924 }
18925}
18926#endif
18927
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928/*
18929 * Return TRUE if the currently active function should be ended, because a
18930 * return was encountered or an error occured. Used inside a ":while".
18931 */
18932 int
18933func_has_ended(cookie)
18934 void *cookie;
18935{
Bram Moolenaar33570922005-01-25 22:26:29 +000018936 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937
18938 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18939 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018940 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 || fcp->returned);
18942}
18943
18944/*
18945 * return TRUE if cookie indicates a function which "abort"s on errors.
18946 */
18947 int
18948func_has_abort(cookie)
18949 void *cookie;
18950{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018951 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952}
18953
18954#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18955typedef enum
18956{
18957 VAR_FLAVOUR_DEFAULT,
18958 VAR_FLAVOUR_SESSION,
18959 VAR_FLAVOUR_VIMINFO
18960} var_flavour_T;
18961
18962static var_flavour_T var_flavour __ARGS((char_u *varname));
18963
18964 static var_flavour_T
18965var_flavour(varname)
18966 char_u *varname;
18967{
18968 char_u *p = varname;
18969
18970 if (ASCII_ISUPPER(*p))
18971 {
18972 while (*(++p))
18973 if (ASCII_ISLOWER(*p))
18974 return VAR_FLAVOUR_SESSION;
18975 return VAR_FLAVOUR_VIMINFO;
18976 }
18977 else
18978 return VAR_FLAVOUR_DEFAULT;
18979}
18980#endif
18981
18982#if defined(FEAT_VIMINFO) || defined(PROTO)
18983/*
18984 * Restore global vars that start with a capital from the viminfo file
18985 */
18986 int
18987read_viminfo_varlist(virp, writing)
18988 vir_T *virp;
18989 int writing;
18990{
18991 char_u *tab;
18992 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018993 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994
18995 if (!writing && (find_viminfo_parameter('!') != NULL))
18996 {
18997 tab = vim_strchr(virp->vir_line + 1, '\t');
18998 if (tab != NULL)
18999 {
19000 *tab++ = '\0'; /* isolate the variable name */
19001 if (*tab == 'S') /* string var */
19002 is_string = TRUE;
19003
19004 tab = vim_strchr(tab, '\t');
19005 if (tab != NULL)
19006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 if (is_string)
19008 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019009 tv.v_type = VAR_STRING;
19010 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 }
19013 else
19014 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019015 tv.v_type = VAR_NUMBER;
19016 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019018 set_var(virp->vir_line + 1, &tv, FALSE);
19019 if (is_string)
19020 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 }
19022 }
19023 }
19024
19025 return viminfo_readline(virp);
19026}
19027
19028/*
19029 * Write global vars that start with a capital to the viminfo file
19030 */
19031 void
19032write_viminfo_varlist(fp)
19033 FILE *fp;
19034{
Bram Moolenaar33570922005-01-25 22:26:29 +000019035 hashitem_T *hi;
19036 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019037 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019039 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019040 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019041 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042
19043 if (find_viminfo_parameter('!') == NULL)
19044 return;
19045
19046 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019047
Bram Moolenaar33570922005-01-25 22:26:29 +000019048 todo = globvarht.ht_used;
19049 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019051 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019053 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019054 this_var = HI2DI(hi);
19055 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019056 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019057 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019058 {
19059 case VAR_STRING: s = "STR"; break;
19060 case VAR_NUMBER: s = "NUM"; break;
19061 default: continue;
19062 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019064 p = echo_string(&this_var->di_tv, &tofree, numbuf);
19065 if (p != NULL)
19066 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019067 vim_free(tofree);
19068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 }
19070 }
19071}
19072#endif
19073
19074#if defined(FEAT_SESSION) || defined(PROTO)
19075 int
19076store_session_globals(fd)
19077 FILE *fd;
19078{
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 hashitem_T *hi;
19080 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019081 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 char_u *p, *t;
19083
Bram Moolenaar33570922005-01-25 22:26:29 +000019084 todo = globvarht.ht_used;
19085 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019087 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019089 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019090 this_var = HI2DI(hi);
19091 if ((this_var->di_tv.v_type == VAR_NUMBER
19092 || this_var->di_tv.v_type == VAR_STRING)
19093 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019094 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019095 /* Escape special characters with a backslash. Turn a LF and
19096 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019097 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019098 (char_u *)"\\\"\n\r");
19099 if (p == NULL) /* out of memory */
19100 break;
19101 for (t = p; *t != NUL; ++t)
19102 if (*t == '\n')
19103 *t = 'n';
19104 else if (*t == '\r')
19105 *t = 'r';
19106 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 this_var->di_key,
19108 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19109 : ' ',
19110 p,
19111 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19112 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019113 || put_eol(fd) == FAIL)
19114 {
19115 vim_free(p);
19116 return FAIL;
19117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 vim_free(p);
19119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 }
19121 }
19122 return OK;
19123}
19124#endif
19125
Bram Moolenaar661b1822005-07-28 22:36:45 +000019126/*
19127 * Display script name where an item was last set.
19128 * Should only be invoked when 'verbose' is non-zero.
19129 */
19130 void
19131last_set_msg(scriptID)
19132 scid_T scriptID;
19133{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019134 char_u *p;
19135
Bram Moolenaar661b1822005-07-28 22:36:45 +000019136 if (scriptID != 0)
19137 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019138 p = home_replace_save(NULL, get_scriptname(scriptID));
19139 if (p != NULL)
19140 {
19141 verbose_enter();
19142 MSG_PUTS(_("\n\tLast set from "));
19143 MSG_PUTS(p);
19144 vim_free(p);
19145 verbose_leave();
19146 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019147 }
19148}
19149
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150#endif /* FEAT_EVAL */
19151
19152#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19153
19154
19155#ifdef WIN3264
19156/*
19157 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19158 */
19159static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19160static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19161static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19162
19163/*
19164 * Get the short pathname of a file.
19165 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19166 */
19167 static int
19168get_short_pathname(fnamep, bufp, fnamelen)
19169 char_u **fnamep;
19170 char_u **bufp;
19171 int *fnamelen;
19172{
19173 int l,len;
19174 char_u *newbuf;
19175
19176 len = *fnamelen;
19177
19178 l = GetShortPathName(*fnamep, *fnamep, len);
19179 if (l > len - 1)
19180 {
19181 /* If that doesn't work (not enough space), then save the string
19182 * and try again with a new buffer big enough
19183 */
19184 newbuf = vim_strnsave(*fnamep, l);
19185 if (newbuf == NULL)
19186 return 0;
19187
19188 vim_free(*bufp);
19189 *fnamep = *bufp = newbuf;
19190
19191 l = GetShortPathName(*fnamep,*fnamep,l+1);
19192
19193 /* Really should always succeed, as the buffer is big enough */
19194 }
19195
19196 *fnamelen = l;
19197 return 1;
19198}
19199
19200/*
19201 * Create a short path name. Returns the length of the buffer it needs.
19202 * Doesn't copy over the end of the buffer passed in.
19203 */
19204 static int
19205shortpath_for_invalid_fname(fname, bufp, fnamelen)
19206 char_u **fname;
19207 char_u **bufp;
19208 int *fnamelen;
19209{
19210 char_u *s, *p, *pbuf2, *pbuf3;
19211 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019212 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213
19214 /* Make a copy */
19215 len2 = *fnamelen;
19216 pbuf2 = vim_strnsave(*fname, len2);
19217 pbuf3 = NULL;
19218
19219 s = pbuf2 + len2 - 1; /* Find the end */
19220 slen = 1;
19221 plen = len2;
19222
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019223 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 {
19225 --s;
19226 ++slen;
19227 --plen;
19228 }
19229
19230 do
19231 {
19232 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019233 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 {
19235 --s;
19236 ++slen;
19237 --plen;
19238 }
19239 if (s <= pbuf2)
19240 break;
19241
19242 /* Remeber the character that is about to be blatted */
19243 ch = *s;
19244 *s = 0; /* get_short_pathname requires a null-terminated string */
19245
19246 /* Try it in situ */
19247 p = pbuf2;
19248 if (!get_short_pathname(&p, &pbuf3, &plen))
19249 {
19250 vim_free(pbuf2);
19251 return -1;
19252 }
19253 *s = ch; /* Preserve the string */
19254 } while (plen == 0);
19255
19256 if (plen > 0)
19257 {
19258 /* Remeber the length of the new string. */
19259 *fnamelen = len = plen + slen;
19260 vim_free(*bufp);
19261 if (len > len2)
19262 {
19263 /* If there's not enough space in the currently allocated string,
19264 * then copy it to a buffer big enough.
19265 */
19266 *fname= *bufp = vim_strnsave(p, len);
19267 if (*fname == NULL)
19268 return -1;
19269 }
19270 else
19271 {
19272 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19273 *fname = *bufp = pbuf2;
19274 if (p != pbuf2)
19275 strncpy(*fname, p, plen);
19276 pbuf2 = NULL;
19277 }
19278 /* Concat the next bit */
19279 strncpy(*fname + plen, s, slen);
19280 (*fname)[len] = '\0';
19281 }
19282 vim_free(pbuf3);
19283 vim_free(pbuf2);
19284 return 0;
19285}
19286
19287/*
19288 * Get a pathname for a partial path.
19289 */
19290 static int
19291shortpath_for_partial(fnamep, bufp, fnamelen)
19292 char_u **fnamep;
19293 char_u **bufp;
19294 int *fnamelen;
19295{
19296 int sepcount, len, tflen;
19297 char_u *p;
19298 char_u *pbuf, *tfname;
19299 int hasTilde;
19300
19301 /* Count up the path seperators from the RHS.. so we know which part
19302 * of the path to return.
19303 */
19304 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019305 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 if (vim_ispathsep(*p))
19307 ++sepcount;
19308
19309 /* Need full path first (use expand_env() to remove a "~/") */
19310 hasTilde = (**fnamep == '~');
19311 if (hasTilde)
19312 pbuf = tfname = expand_env_save(*fnamep);
19313 else
19314 pbuf = tfname = FullName_save(*fnamep, FALSE);
19315
19316 len = tflen = STRLEN(tfname);
19317
19318 if (!get_short_pathname(&tfname, &pbuf, &len))
19319 return -1;
19320
19321 if (len == 0)
19322 {
19323 /* Don't have a valid filename, so shorten the rest of the
19324 * path if we can. This CAN give us invalid 8.3 filenames, but
19325 * there's not a lot of point in guessing what it might be.
19326 */
19327 len = tflen;
19328 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19329 return -1;
19330 }
19331
19332 /* Count the paths backward to find the beginning of the desired string. */
19333 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019334 {
19335#ifdef FEAT_MBYTE
19336 if (has_mbyte)
19337 p -= mb_head_off(tfname, p);
19338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 if (vim_ispathsep(*p))
19340 {
19341 if (sepcount == 0 || (hasTilde && sepcount == 1))
19342 break;
19343 else
19344 sepcount --;
19345 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 if (hasTilde)
19348 {
19349 --p;
19350 if (p >= tfname)
19351 *p = '~';
19352 else
19353 return -1;
19354 }
19355 else
19356 ++p;
19357
19358 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19359 vim_free(*bufp);
19360 *fnamelen = (int)STRLEN(p);
19361 *bufp = pbuf;
19362 *fnamep = p;
19363
19364 return 0;
19365}
19366#endif /* WIN3264 */
19367
19368/*
19369 * Adjust a filename, according to a string of modifiers.
19370 * *fnamep must be NUL terminated when called. When returning, the length is
19371 * determined by *fnamelen.
19372 * Returns valid flags.
19373 * When there is an error, *fnamep is set to NULL.
19374 */
19375 int
19376modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19377 char_u *src; /* string with modifiers */
19378 int *usedlen; /* characters after src that are used */
19379 char_u **fnamep; /* file name so far */
19380 char_u **bufp; /* buffer for allocated file name or NULL */
19381 int *fnamelen; /* length of fnamep */
19382{
19383 int valid = 0;
19384 char_u *tail;
19385 char_u *s, *p, *pbuf;
19386 char_u dirname[MAXPATHL];
19387 int c;
19388 int has_fullname = 0;
19389#ifdef WIN3264
19390 int has_shortname = 0;
19391#endif
19392
19393repeat:
19394 /* ":p" - full path/file_name */
19395 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19396 {
19397 has_fullname = 1;
19398
19399 valid |= VALID_PATH;
19400 *usedlen += 2;
19401
19402 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19403 if ((*fnamep)[0] == '~'
19404#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19405 && ((*fnamep)[1] == '/'
19406# ifdef BACKSLASH_IN_FILENAME
19407 || (*fnamep)[1] == '\\'
19408# endif
19409 || (*fnamep)[1] == NUL)
19410
19411#endif
19412 )
19413 {
19414 *fnamep = expand_env_save(*fnamep);
19415 vim_free(*bufp); /* free any allocated file name */
19416 *bufp = *fnamep;
19417 if (*fnamep == NULL)
19418 return -1;
19419 }
19420
19421 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019422 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 {
19424 if (vim_ispathsep(*p)
19425 && p[1] == '.'
19426 && (p[2] == NUL
19427 || vim_ispathsep(p[2])
19428 || (p[2] == '.'
19429 && (p[3] == NUL || vim_ispathsep(p[3])))))
19430 break;
19431 }
19432
19433 /* FullName_save() is slow, don't use it when not needed. */
19434 if (*p != NUL || !vim_isAbsName(*fnamep))
19435 {
19436 *fnamep = FullName_save(*fnamep, *p != NUL);
19437 vim_free(*bufp); /* free any allocated file name */
19438 *bufp = *fnamep;
19439 if (*fnamep == NULL)
19440 return -1;
19441 }
19442
19443 /* Append a path separator to a directory. */
19444 if (mch_isdir(*fnamep))
19445 {
19446 /* Make room for one or two extra characters. */
19447 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19448 vim_free(*bufp); /* free any allocated file name */
19449 *bufp = *fnamep;
19450 if (*fnamep == NULL)
19451 return -1;
19452 add_pathsep(*fnamep);
19453 }
19454 }
19455
19456 /* ":." - path relative to the current directory */
19457 /* ":~" - path relative to the home directory */
19458 /* ":8" - shortname path - postponed till after */
19459 while (src[*usedlen] == ':'
19460 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19461 {
19462 *usedlen += 2;
19463 if (c == '8')
19464 {
19465#ifdef WIN3264
19466 has_shortname = 1; /* Postpone this. */
19467#endif
19468 continue;
19469 }
19470 pbuf = NULL;
19471 /* Need full path first (use expand_env() to remove a "~/") */
19472 if (!has_fullname)
19473 {
19474 if (c == '.' && **fnamep == '~')
19475 p = pbuf = expand_env_save(*fnamep);
19476 else
19477 p = pbuf = FullName_save(*fnamep, FALSE);
19478 }
19479 else
19480 p = *fnamep;
19481
19482 has_fullname = 0;
19483
19484 if (p != NULL)
19485 {
19486 if (c == '.')
19487 {
19488 mch_dirname(dirname, MAXPATHL);
19489 s = shorten_fname(p, dirname);
19490 if (s != NULL)
19491 {
19492 *fnamep = s;
19493 if (pbuf != NULL)
19494 {
19495 vim_free(*bufp); /* free any allocated file name */
19496 *bufp = pbuf;
19497 pbuf = NULL;
19498 }
19499 }
19500 }
19501 else
19502 {
19503 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19504 /* Only replace it when it starts with '~' */
19505 if (*dirname == '~')
19506 {
19507 s = vim_strsave(dirname);
19508 if (s != NULL)
19509 {
19510 *fnamep = s;
19511 vim_free(*bufp);
19512 *bufp = s;
19513 }
19514 }
19515 }
19516 vim_free(pbuf);
19517 }
19518 }
19519
19520 tail = gettail(*fnamep);
19521 *fnamelen = (int)STRLEN(*fnamep);
19522
19523 /* ":h" - head, remove "/file_name", can be repeated */
19524 /* Don't remove the first "/" or "c:\" */
19525 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19526 {
19527 valid |= VALID_HEAD;
19528 *usedlen += 2;
19529 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019530 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 --tail;
19532 *fnamelen = (int)(tail - *fnamep);
19533#ifdef VMS
19534 if (*fnamelen > 0)
19535 *fnamelen += 1; /* the path separator is part of the path */
19536#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019537 while (tail > s && !after_pathsep(s, tail))
19538 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 }
19540
19541 /* ":8" - shortname */
19542 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19543 {
19544 *usedlen += 2;
19545#ifdef WIN3264
19546 has_shortname = 1;
19547#endif
19548 }
19549
19550#ifdef WIN3264
19551 /* Check shortname after we have done 'heads' and before we do 'tails'
19552 */
19553 if (has_shortname)
19554 {
19555 pbuf = NULL;
19556 /* Copy the string if it is shortened by :h */
19557 if (*fnamelen < (int)STRLEN(*fnamep))
19558 {
19559 p = vim_strnsave(*fnamep, *fnamelen);
19560 if (p == 0)
19561 return -1;
19562 vim_free(*bufp);
19563 *bufp = *fnamep = p;
19564 }
19565
19566 /* Split into two implementations - makes it easier. First is where
19567 * there isn't a full name already, second is where there is.
19568 */
19569 if (!has_fullname && !vim_isAbsName(*fnamep))
19570 {
19571 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19572 return -1;
19573 }
19574 else
19575 {
19576 int l;
19577
19578 /* Simple case, already have the full-name
19579 * Nearly always shorter, so try first time. */
19580 l = *fnamelen;
19581 if (!get_short_pathname(fnamep, bufp, &l))
19582 return -1;
19583
19584 if (l == 0)
19585 {
19586 /* Couldn't find the filename.. search the paths.
19587 */
19588 l = *fnamelen;
19589 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19590 return -1;
19591 }
19592 *fnamelen = l;
19593 }
19594 }
19595#endif /* WIN3264 */
19596
19597 /* ":t" - tail, just the basename */
19598 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19599 {
19600 *usedlen += 2;
19601 *fnamelen -= (int)(tail - *fnamep);
19602 *fnamep = tail;
19603 }
19604
19605 /* ":e" - extension, can be repeated */
19606 /* ":r" - root, without extension, can be repeated */
19607 while (src[*usedlen] == ':'
19608 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19609 {
19610 /* find a '.' in the tail:
19611 * - for second :e: before the current fname
19612 * - otherwise: The last '.'
19613 */
19614 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19615 s = *fnamep - 2;
19616 else
19617 s = *fnamep + *fnamelen - 1;
19618 for ( ; s > tail; --s)
19619 if (s[0] == '.')
19620 break;
19621 if (src[*usedlen + 1] == 'e') /* :e */
19622 {
19623 if (s > tail)
19624 {
19625 *fnamelen += (int)(*fnamep - (s + 1));
19626 *fnamep = s + 1;
19627#ifdef VMS
19628 /* cut version from the extension */
19629 s = *fnamep + *fnamelen - 1;
19630 for ( ; s > *fnamep; --s)
19631 if (s[0] == ';')
19632 break;
19633 if (s > *fnamep)
19634 *fnamelen = s - *fnamep;
19635#endif
19636 }
19637 else if (*fnamep <= tail)
19638 *fnamelen = 0;
19639 }
19640 else /* :r */
19641 {
19642 if (s > tail) /* remove one extension */
19643 *fnamelen = (int)(s - *fnamep);
19644 }
19645 *usedlen += 2;
19646 }
19647
19648 /* ":s?pat?foo?" - substitute */
19649 /* ":gs?pat?foo?" - global substitute */
19650 if (src[*usedlen] == ':'
19651 && (src[*usedlen + 1] == 's'
19652 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19653 {
19654 char_u *str;
19655 char_u *pat;
19656 char_u *sub;
19657 int sep;
19658 char_u *flags;
19659 int didit = FALSE;
19660
19661 flags = (char_u *)"";
19662 s = src + *usedlen + 2;
19663 if (src[*usedlen + 1] == 'g')
19664 {
19665 flags = (char_u *)"g";
19666 ++s;
19667 }
19668
19669 sep = *s++;
19670 if (sep)
19671 {
19672 /* find end of pattern */
19673 p = vim_strchr(s, sep);
19674 if (p != NULL)
19675 {
19676 pat = vim_strnsave(s, (int)(p - s));
19677 if (pat != NULL)
19678 {
19679 s = p + 1;
19680 /* find end of substitution */
19681 p = vim_strchr(s, sep);
19682 if (p != NULL)
19683 {
19684 sub = vim_strnsave(s, (int)(p - s));
19685 str = vim_strnsave(*fnamep, *fnamelen);
19686 if (sub != NULL && str != NULL)
19687 {
19688 *usedlen = (int)(p + 1 - src);
19689 s = do_string_sub(str, pat, sub, flags);
19690 if (s != NULL)
19691 {
19692 *fnamep = s;
19693 *fnamelen = (int)STRLEN(s);
19694 vim_free(*bufp);
19695 *bufp = s;
19696 didit = TRUE;
19697 }
19698 }
19699 vim_free(sub);
19700 vim_free(str);
19701 }
19702 vim_free(pat);
19703 }
19704 }
19705 /* after using ":s", repeat all the modifiers */
19706 if (didit)
19707 goto repeat;
19708 }
19709 }
19710
19711 return valid;
19712}
19713
19714/*
19715 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19716 * "flags" can be "g" to do a global substitute.
19717 * Returns an allocated string, NULL for error.
19718 */
19719 char_u *
19720do_string_sub(str, pat, sub, flags)
19721 char_u *str;
19722 char_u *pat;
19723 char_u *sub;
19724 char_u *flags;
19725{
19726 int sublen;
19727 regmatch_T regmatch;
19728 int i;
19729 int do_all;
19730 char_u *tail;
19731 garray_T ga;
19732 char_u *ret;
19733 char_u *save_cpo;
19734
19735 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19736 save_cpo = p_cpo;
19737 p_cpo = (char_u *)"";
19738
19739 ga_init2(&ga, 1, 200);
19740
19741 do_all = (flags[0] == 'g');
19742
19743 regmatch.rm_ic = p_ic;
19744 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19745 if (regmatch.regprog != NULL)
19746 {
19747 tail = str;
19748 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19749 {
19750 /*
19751 * Get some space for a temporary buffer to do the substitution
19752 * into. It will contain:
19753 * - The text up to where the match is.
19754 * - The substituted text.
19755 * - The text after the match.
19756 */
19757 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19758 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19759 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19760 {
19761 ga_clear(&ga);
19762 break;
19763 }
19764
19765 /* copy the text up to where the match is */
19766 i = (int)(regmatch.startp[0] - tail);
19767 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19768 /* add the substituted text */
19769 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19770 + ga.ga_len + i, TRUE, TRUE, FALSE);
19771 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 /* avoid getting stuck on a match with an empty string */
19773 if (tail == regmatch.endp[0])
19774 {
19775 if (*tail == NUL)
19776 break;
19777 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19778 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 }
19780 else
19781 {
19782 tail = regmatch.endp[0];
19783 if (*tail == NUL)
19784 break;
19785 }
19786 if (!do_all)
19787 break;
19788 }
19789
19790 if (ga.ga_data != NULL)
19791 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19792
19793 vim_free(regmatch.regprog);
19794 }
19795
19796 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19797 ga_clear(&ga);
19798 p_cpo = save_cpo;
19799
19800 return ret;
19801}
19802
19803#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */