blob: e6e712bdf19fd41a06baa1b107db87b6b810d807 [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 Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static void list_free __ARGS((list_T *l));
405static listitem_T *listitem_alloc __ARGS((void));
406static void listitem_free __ARGS((listitem_T *item));
407static void listitem_remove __ARGS((list_T *l, listitem_T *item));
408static long list_len __ARGS((list_T *l));
409static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
410static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
411static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static listitem_T *list_find __ARGS((list_T *l, long n));
413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
423static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
438static char_u *dict2string __ARGS((typval_T *tv));
439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static 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));
447static 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 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
470static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
478static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000504static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000506static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000512static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000520static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000543static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000549static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000566static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000570#ifdef vim_mkdir
571static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
572#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000577static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000579static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000591static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000598static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000603static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000604static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
607#ifdef HAVE_STRFTIME
608static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
610static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000622static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000623static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000625static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
643static int get_env_len __ARGS((char_u **arg));
644static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000646static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
647#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
648#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
649 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000650static 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 +0000651static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000652static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000653static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
654static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static typval_T *alloc_tv __ARGS((void));
656static typval_T *alloc_string_tv __ARGS((char_u *string));
657static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void init_tv __ARGS((typval_T *varp));
659static long get_tv_number __ARGS((typval_T *varp));
660static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000661static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static char_u *get_tv_string __ARGS((typval_T *varp));
663static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000666static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
668static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
669static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
670static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
671static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
672static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
673static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000674static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000676static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
678static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
679static int eval_fname_script __ARGS((char_u *p));
680static int eval_fname_sid __ARGS((char_u *p));
681static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static ufunc_T *find_func __ARGS((char_u *name));
683static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000684static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000685#ifdef FEAT_PROFILE
686static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000687static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
688static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
689static int
690# ifdef __BORLANDC__
691 _RTLENTRYF
692# endif
693 prof_total_cmp __ARGS((const void *s1, const void *s2));
694static int
695# ifdef __BORLANDC__
696 _RTLENTRYF
697# endif
698 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000700static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000701static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000702static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void func_free __ARGS((ufunc_T *fp));
704static void func_unref __ARGS((char_u *name));
705static void func_ref __ARGS((char_u *name));
706static 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));
707static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
708
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000709/* Character used as separated in autoload function/variable names. */
710#define AUTOLOAD_CHAR '#'
711
Bram Moolenaar33570922005-01-25 22:26:29 +0000712/*
713 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000714 */
715 void
716eval_init()
717{
Bram Moolenaar33570922005-01-25 22:26:29 +0000718 int i;
719 struct vimvar *p;
720
721 init_var_dict(&globvardict, &globvars_var);
722 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000723 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000724 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000725
726 for (i = 0; i < VV_LEN; ++i)
727 {
728 p = &vimvars[i];
729 STRCPY(p->vv_di.di_key, p->vv_name);
730 if (p->vv_flags & VV_RO)
731 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
732 else if (p->vv_flags & VV_RO_SBX)
733 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
734 else
735 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000736
737 /* add to v: scope dict, unless the value is not always available */
738 if (p->vv_type != VAR_UNKNOWN)
739 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000740 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000741 /* add to compat scope dict */
742 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000743 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000744}
745
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000746#if defined(EXITFREE) || defined(PROTO)
747 void
748eval_clear()
749{
750 int i;
751 struct vimvar *p;
752
753 for (i = 0; i < VV_LEN; ++i)
754 {
755 p = &vimvars[i];
756 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000757 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000758 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000759 p->vv_di.di_tv.vval.v_string = NULL;
760 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000761 }
762 hash_clear(&vimvarht);
763 hash_clear(&compat_hashtab);
764
765 /* script-local variables */
766 for (i = 1; i <= ga_scripts.ga_len; ++i)
767 vars_clear(&SCRIPT_VARS(i));
768 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000769 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000770
771 /* global variables */
772 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000773
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000774 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000775 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000776 hash_clear(&func_hashtab);
777
778 /* unreferenced lists and dicts */
779 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000780}
781#endif
782
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * Return the name of the executed function.
785 */
786 char_u *
787func_name(cookie)
788 void *cookie;
789{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000790 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
793/*
794 * Return the address holding the next breakpoint line for a funccall cookie.
795 */
796 linenr_T *
797func_breakpoint(cookie)
798 void *cookie;
799{
Bram Moolenaar33570922005-01-25 22:26:29 +0000800 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
803/*
804 * Return the address holding the debug tick for a funccall cookie.
805 */
806 int *
807func_dbg_tick(cookie)
808 void *cookie;
809{
Bram Moolenaar33570922005-01-25 22:26:29 +0000810 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811}
812
813/*
814 * Return the nesting level for a funccall cookie.
815 */
816 int
817func_level(cookie)
818 void *cookie;
819{
Bram Moolenaar33570922005-01-25 22:26:29 +0000820 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
823/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000824funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826/*
827 * Return TRUE when a function was ended by a ":return" command.
828 */
829 int
830current_func_returned()
831{
832 return current_funccal->returned;
833}
834
835
836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 * Set an internal variable to a string value. Creates the variable if it does
838 * not already exist.
839 */
840 void
841set_internal_string_var(name, value)
842 char_u *name;
843 char_u *value;
844{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000845 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
848 val = vim_strsave(value);
849 if (val != NULL)
850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000852 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000854 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857 }
858}
859
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000860static lval_T *redir_lval = NULL;
861static char_u *redir_endp = NULL;
862static char_u *redir_varname = NULL;
863
864/*
865 * Start recording command output to a variable
866 * Returns OK if successfully completed the setup. FAIL otherwise.
867 */
868 int
869var_redir_start(name, append)
870 char_u *name;
871 int append; /* append to an existing variable */
872{
873 int save_emsg;
874 int err;
875 typval_T tv;
876
877 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000878 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000879 {
880 EMSG(_(e_invarg));
881 return FAIL;
882 }
883
884 redir_varname = vim_strsave(name);
885 if (redir_varname == NULL)
886 return FAIL;
887
888 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
889 if (redir_lval == NULL)
890 {
891 var_redir_stop();
892 return FAIL;
893 }
894
895 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000896 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
897 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
899 {
900 if (redir_endp != NULL && *redir_endp != NUL)
901 /* Trailing characters are present after the variable name */
902 EMSG(_(e_trailing));
903 else
904 EMSG(_(e_invarg));
905 var_redir_stop();
906 return FAIL;
907 }
908
909 /* check if we can write to the variable: set it to or append an empty
910 * string */
911 save_emsg = did_emsg;
912 did_emsg = FALSE;
913 tv.v_type = VAR_STRING;
914 tv.vval.v_string = (char_u *)"";
915 if (append)
916 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
917 else
918 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
919 err = did_emsg;
920 did_emsg += save_emsg;
921 if (err)
922 {
923 var_redir_stop();
924 return FAIL;
925 }
926 if (redir_lval->ll_newkey != NULL)
927 {
928 /* Dictionary item was created, don't do it again. */
929 vim_free(redir_lval->ll_newkey);
930 redir_lval->ll_newkey = NULL;
931 }
932
933 return OK;
934}
935
936/*
937 * Append "value[len]" to the variable set by var_redir_start().
938 */
939 void
940var_redir_str(value, len)
941 char_u *value;
942 int len;
943{
944 char_u *val;
945 typval_T tv;
946 int save_emsg;
947 int err;
948
949 if (redir_lval == NULL)
950 return;
951
952 if (len == -1)
953 /* Append the entire string */
954 val = vim_strsave(value);
955 else
956 /* Append only the specified number of characters */
957 val = vim_strnsave(value, len);
958 if (val == NULL)
959 return;
960
961 tv.v_type = VAR_STRING;
962 tv.vval.v_string = val;
963
964 save_emsg = did_emsg;
965 did_emsg = FALSE;
966 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
967 err = did_emsg;
968 did_emsg += save_emsg;
969 if (err)
970 var_redir_stop();
971
972 vim_free(tv.vval.v_string);
973}
974
975/*
976 * Stop redirecting command output to a variable.
977 */
978 void
979var_redir_stop()
980{
981 if (redir_lval != NULL)
982 {
983 clear_lval(redir_lval);
984 vim_free(redir_lval);
985 redir_lval = NULL;
986 }
987 vim_free(redir_varname);
988 redir_varname = NULL;
989}
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991# if defined(FEAT_MBYTE) || defined(PROTO)
992 int
993eval_charconvert(enc_from, enc_to, fname_from, fname_to)
994 char_u *enc_from;
995 char_u *enc_to;
996 char_u *fname_from;
997 char_u *fname_to;
998{
999 int err = FALSE;
1000
1001 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1002 set_vim_var_string(VV_CC_TO, enc_to, -1);
1003 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1004 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1005 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1006 err = TRUE;
1007 set_vim_var_string(VV_CC_FROM, NULL, -1);
1008 set_vim_var_string(VV_CC_TO, NULL, -1);
1009 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1010 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1011
1012 if (err)
1013 return FAIL;
1014 return OK;
1015}
1016# endif
1017
1018# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1019 int
1020eval_printexpr(fname, args)
1021 char_u *fname;
1022 char_u *args;
1023{
1024 int err = FALSE;
1025
1026 set_vim_var_string(VV_FNAME_IN, fname, -1);
1027 set_vim_var_string(VV_CMDARG, args, -1);
1028 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1029 err = TRUE;
1030 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1031 set_vim_var_string(VV_CMDARG, NULL, -1);
1032
1033 if (err)
1034 {
1035 mch_remove(fname);
1036 return FAIL;
1037 }
1038 return OK;
1039}
1040# endif
1041
1042# if defined(FEAT_DIFF) || defined(PROTO)
1043 void
1044eval_diff(origfile, newfile, outfile)
1045 char_u *origfile;
1046 char_u *newfile;
1047 char_u *outfile;
1048{
1049 int err = FALSE;
1050
1051 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1052 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1053 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1054 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1055 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1056 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1057 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1058}
1059
1060 void
1061eval_patch(origfile, difffile, outfile)
1062 char_u *origfile;
1063 char_u *difffile;
1064 char_u *outfile;
1065{
1066 int err;
1067
1068 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1069 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1070 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1071 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1072 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1073 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1074 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1075}
1076# endif
1077
1078/*
1079 * Top level evaluation function, returning a boolean.
1080 * Sets "error" to TRUE if there was an error.
1081 * Return TRUE or FALSE.
1082 */
1083 int
1084eval_to_bool(arg, error, nextcmd, skip)
1085 char_u *arg;
1086 int *error;
1087 char_u **nextcmd;
1088 int skip; /* only parse, don't execute */
1089{
Bram Moolenaar33570922005-01-25 22:26:29 +00001090 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 int retval = FALSE;
1092
1093 if (skip)
1094 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001095 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 else
1098 {
1099 *error = FALSE;
1100 if (!skip)
1101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001102 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001103 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105 }
1106 if (skip)
1107 --emsg_skip;
1108
1109 return retval;
1110}
1111
1112/*
1113 * Top level evaluation function, returning a string. If "skip" is TRUE,
1114 * only parsing to "nextcmd" is done, without reporting errors. Return
1115 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1116 */
1117 char_u *
1118eval_to_string_skip(arg, nextcmd, skip)
1119 char_u *arg;
1120 char_u **nextcmd;
1121 int skip; /* only parse, don't execute */
1122{
Bram Moolenaar33570922005-01-25 22:26:29 +00001123 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 char_u *retval;
1125
1126 if (skip)
1127 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 retval = NULL;
1130 else
1131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001132 retval = vim_strsave(get_tv_string(&tv));
1133 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 if (skip)
1136 --emsg_skip;
1137
1138 return retval;
1139}
1140
1141/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001142 * Skip over an expression at "*pp".
1143 * Return FAIL for an error, OK otherwise.
1144 */
1145 int
1146skip_expr(pp)
1147 char_u **pp;
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001150
1151 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001153}
1154
1155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * Top level evaluation function, returning a string.
1157 * Return pointer to allocated memory, or NULL for failure.
1158 */
1159 char_u *
1160eval_to_string(arg, nextcmd)
1161 char_u *arg;
1162 char_u **nextcmd;
1163{
Bram Moolenaar33570922005-01-25 22:26:29 +00001164 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 char_u *retval;
1166
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 retval = NULL;
1169 else
1170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 retval = vim_strsave(get_tv_string(&tv));
1172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174
1175 return retval;
1176}
1177
1178/*
1179 * Call eval_to_string() with "sandbox" set and not using local variables.
1180 */
1181 char_u *
1182eval_to_string_safe(arg, nextcmd)
1183 char_u *arg;
1184 char_u **nextcmd;
1185{
1186 char_u *retval;
1187 void *save_funccalp;
1188
1189 save_funccalp = save_funccal();
1190 ++sandbox;
1191 retval = eval_to_string(arg, nextcmd);
1192 --sandbox;
1193 restore_funccal(save_funccalp);
1194 return retval;
1195}
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197/*
1198 * Top level evaluation function, returning a number.
1199 * Evaluates "expr" silently.
1200 * Returns -1 for an error.
1201 */
1202 int
1203eval_to_number(expr)
1204 char_u *expr;
1205{
Bram Moolenaar33570922005-01-25 22:26:29 +00001206 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001208 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
1210 ++emsg_off;
1211
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 retval = -1;
1214 else
1215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001216 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 --emsg_off;
1220
1221 return retval;
1222}
1223
Bram Moolenaara40058a2005-07-11 22:42:07 +00001224/*
1225 * Prepare v: variable "idx" to be used.
1226 * Save the current typeval in "save_tv".
1227 * When not used yet add the variable to the v: hashtable.
1228 */
1229 static void
1230prepare_vimvar(idx, save_tv)
1231 int idx;
1232 typval_T *save_tv;
1233{
1234 *save_tv = vimvars[idx].vv_tv;
1235 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1236 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1237}
1238
1239/*
1240 * Restore v: variable "idx" to typeval "save_tv".
1241 * When no longer defined, remove the variable from the v: hashtable.
1242 */
1243 static void
1244restore_vimvar(idx, save_tv)
1245 int idx;
1246 typval_T *save_tv;
1247{
1248 hashitem_T *hi;
1249
1250 clear_tv(&vimvars[idx].vv_tv);
1251 vimvars[idx].vv_tv = *save_tv;
1252 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1253 {
1254 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1255 if (HASHITEM_EMPTY(hi))
1256 EMSG2(_(e_intern2), "restore_vimvar()");
1257 else
1258 hash_remove(&vimvarht, hi);
1259 }
1260}
1261
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001262#if defined(FEAT_SYN_HL) || defined(PROTO)
1263/*
1264 * Evaluate an expression to a list with suggestions.
1265 * For the "expr:" part of 'spellsuggest'.
1266 */
1267 list_T *
1268eval_spell_expr(badword, expr)
1269 char_u *badword;
1270 char_u *expr;
1271{
1272 typval_T save_val;
1273 typval_T rettv;
1274 list_T *list = NULL;
1275 char_u *p = skipwhite(expr);
1276
1277 /* Set "v:val" to the bad word. */
1278 prepare_vimvar(VV_VAL, &save_val);
1279 vimvars[VV_VAL].vv_type = VAR_STRING;
1280 vimvars[VV_VAL].vv_str = badword;
1281 if (p_verbose == 0)
1282 ++emsg_off;
1283
1284 if (eval1(&p, &rettv, TRUE) == OK)
1285 {
1286 if (rettv.v_type != VAR_LIST)
1287 clear_tv(&rettv);
1288 else
1289 list = rettv.vval.v_list;
1290 }
1291
1292 if (p_verbose == 0)
1293 --emsg_off;
1294 vimvars[VV_VAL].vv_str = NULL;
1295 restore_vimvar(VV_VAL, &save_val);
1296
1297 return list;
1298}
1299
1300/*
1301 * "list" is supposed to contain two items: a word and a number. Return the
1302 * word in "pp" and the number as the return value.
1303 * Return -1 if anything isn't right.
1304 * Used to get the good word and score from the eval_spell_expr() result.
1305 */
1306 int
1307get_spellword(list, pp)
1308 list_T *list;
1309 char_u **pp;
1310{
1311 listitem_T *li;
1312
1313 li = list->lv_first;
1314 if (li == NULL)
1315 return -1;
1316 *pp = get_tv_string(&li->li_tv);
1317
1318 li = li->li_next;
1319 if (li == NULL)
1320 return -1;
1321 return get_tv_number(&li->li_tv);
1322}
1323#endif
1324
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001325/*
1326 * Top level evaluation function,
1327 */
1328 typval_T *
1329eval_expr(arg, nextcmd)
1330 char_u *arg;
1331 char_u **nextcmd;
1332{
1333 typval_T *tv;
1334
1335 tv = (typval_T *)alloc(sizeof(typval_T));
1336 if (!tv)
1337 return NULL;
1338
1339 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1340 {
1341 vim_free(tv);
1342 return NULL;
1343 }
1344
1345 return tv;
1346}
1347
1348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1350/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001351 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001353 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001355 static int
1356call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *func;
1358 int argc;
1359 char_u **argv;
1360 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
Bram Moolenaar33570922005-01-25 22:26:29 +00001363 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 long n;
1365 int len;
1366 int i;
1367 int doesrange;
1368 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001369 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001373 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 for (i = 0; i < argc; i++)
1376 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001377 /* Pass a NULL or empty argument as an empty string */
1378 if (argv[i] == NULL || *argv[i] == NUL)
1379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001380 argvars[i].v_type = VAR_STRING;
1381 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001382 continue;
1383 }
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /* Recognize a number argument, the others must be strings. */
1386 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1387 if (len != 0 && len == (int)STRLEN(argv[i]))
1388 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001389 argvars[i].v_type = VAR_NUMBER;
1390 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 else
1393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001394 argvars[i].v_type = VAR_STRING;
1395 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 }
1398
1399 if (safe)
1400 {
1401 save_funccalp = save_funccal();
1402 ++sandbox;
1403 }
1404
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1406 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (safe)
1410 {
1411 --sandbox;
1412 restore_funccal(save_funccalp);
1413 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 vim_free(argvars);
1415
1416 if (ret == FAIL)
1417 clear_tv(rettv);
1418
1419 return ret;
1420}
1421
1422/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001423 * Call vimL function "func" and return the result as a string.
1424 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 * Uses argv[argc] for the function arguments.
1426 */
1427 void *
1428call_func_retstr(func, argc, argv, safe)
1429 char_u *func;
1430 int argc;
1431 char_u **argv;
1432 int safe; /* use the sandbox */
1433{
1434 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001435 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001436
1437 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1438 return NULL;
1439
1440 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 return retval;
1443}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001445#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001446/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001447 * Call vimL function "func" and return the result as a number.
1448 * Returns -1 when calling the function fails.
1449 * Uses argv[argc] for the function arguments.
1450 */
1451 long
1452call_func_retnr(func, argc, argv, safe)
1453 char_u *func;
1454 int argc;
1455 char_u **argv;
1456 int safe; /* use the sandbox */
1457{
1458 typval_T rettv;
1459 long retval;
1460
1461 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1462 return -1;
1463
1464 retval = get_tv_number_chk(&rettv, NULL);
1465 clear_tv(&rettv);
1466 return retval;
1467}
1468#endif
1469
1470/*
1471 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472 * Uses argv[argc] for the function arguments.
1473 */
1474 void *
1475call_func_retlist(func, argc, argv, safe)
1476 char_u *func;
1477 int argc;
1478 char_u **argv;
1479 int safe; /* use the sandbox */
1480{
1481 typval_T rettv;
1482
1483 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1484 return NULL;
1485
1486 if (rettv.v_type != VAR_LIST)
1487 {
1488 clear_tv(&rettv);
1489 return NULL;
1490 }
1491
1492 return rettv.vval.v_list;
1493}
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#endif
1496
1497/*
1498 * Save the current function call pointer, and set it to NULL.
1499 * Used when executing autocommands and for ":source".
1500 */
1501 void *
1502save_funccal()
1503{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001504 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 current_funccal = NULL;
1507 return (void *)fc;
1508}
1509
1510 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001511restore_funccal(vfc)
1512 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001514 funccall_T *fc = (funccall_T *)vfc;
1515
1516 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517}
1518
Bram Moolenaar05159a02005-02-26 23:04:13 +00001519#if defined(FEAT_PROFILE) || defined(PROTO)
1520/*
1521 * Prepare profiling for entering a child or something else that is not
1522 * counted for the script/function itself.
1523 * Should always be called in pair with prof_child_exit().
1524 */
1525 void
1526prof_child_enter(tm)
1527 proftime_T *tm; /* place to store waittime */
1528{
1529 funccall_T *fc = current_funccal;
1530
1531 if (fc != NULL && fc->func->uf_profiling)
1532 profile_start(&fc->prof_child);
1533 script_prof_save(tm);
1534}
1535
1536/*
1537 * Take care of time spent in a child.
1538 * Should always be called after prof_child_enter().
1539 */
1540 void
1541prof_child_exit(tm)
1542 proftime_T *tm; /* where waittime was stored */
1543{
1544 funccall_T *fc = current_funccal;
1545
1546 if (fc != NULL && fc->func->uf_profiling)
1547 {
1548 profile_end(&fc->prof_child);
1549 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1550 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1551 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1552 }
1553 script_prof_restore(tm);
1554}
1555#endif
1556
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558#ifdef FEAT_FOLDING
1559/*
1560 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1561 * it in "*cp". Doesn't give error messages.
1562 */
1563 int
1564eval_foldexpr(arg, cp)
1565 char_u *arg;
1566 int *cp;
1567{
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 int retval;
1570 char_u *s;
1571
1572 ++emsg_off;
1573 ++sandbox;
1574 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 retval = 0;
1577 else
1578 {
1579 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001580 if (tv.v_type == VAR_NUMBER)
1581 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001582 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 retval = 0;
1584 else
1585 {
1586 /* If the result is a string, check if there is a non-digit before
1587 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (!VIM_ISDIGIT(*s) && *s != '-')
1590 *cp = *s++;
1591 retval = atol((char *)s);
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 --emsg_off;
1596 --sandbox;
1597
1598 return retval;
1599}
1600#endif
1601
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603 * ":let" list all variable values
1604 * ":let var1 var2" list variable values
1605 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 * ":let var += expr" assignment command.
1607 * ":let var -= expr" assignment command.
1608 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 */
1611 void
1612ex_let(eap)
1613 exarg_T *eap;
1614{
1615 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001617 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001619 int var_count = 0;
1620 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 expr = skip_var_list(arg, &var_count, &semicolon);
1624 if (expr == NULL)
1625 return;
1626 expr = vim_strchr(expr, '=');
1627 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001629 /*
1630 * ":let" without "=": list variables
1631 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 if (*arg == '[')
1633 EMSG(_(e_invarg));
1634 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 /* ":let var1 var2" */
1636 arg = list_arg_vars(eap, arg);
1637 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001640 list_glob_vars();
1641 list_buf_vars();
1642 list_win_vars();
1643 list_vim_vars();
1644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 eap->nextcmd = check_nextcmd(arg);
1646 }
1647 else
1648 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001649 op[0] = '=';
1650 op[1] = NUL;
1651 if (expr > arg)
1652 {
1653 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1654 op[0] = expr[-1]; /* +=, -= or .= */
1655 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (eap->skip)
1659 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (eap->skip)
1662 {
1663 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 --emsg_skip;
1666 }
1667 else if (i != FAIL)
1668 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673 }
1674}
1675
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676/*
1677 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1678 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 * When "nextchars" is not NULL it points to a string with characters that
1680 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1681 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 * Returns OK or FAIL;
1683 */
1684 static int
1685ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1686 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 int copy; /* copy values from "tv", don't move */
1689 int semicolon; /* from skip_var_list() */
1690 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692{
1693 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 listitem_T *item;
1697 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698
1699 if (*arg != '[')
1700 {
1701 /*
1702 * ":let var = expr" or ":for var in list"
1703 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 return FAIL;
1706 return OK;
1707 }
1708
1709 /*
1710 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1711 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001712 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 {
1714 EMSG(_(e_listreq));
1715 return FAIL;
1716 }
1717
1718 i = list_len(l);
1719 if (semicolon == 0 && var_count < i)
1720 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001721 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 return FAIL;
1723 }
1724 if (var_count - semicolon > i)
1725 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001726 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 return FAIL;
1728 }
1729
1730 item = l->lv_first;
1731 while (*arg != ']')
1732 {
1733 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001734 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 item = item->li_next;
1736 if (arg == NULL)
1737 return FAIL;
1738
1739 arg = skipwhite(arg);
1740 if (*arg == ';')
1741 {
1742 /* Put the rest of the list (may be empty) in the var after ';'.
1743 * Create a new list for this. */
1744 l = list_alloc();
1745 if (l == NULL)
1746 return FAIL;
1747 while (item != NULL)
1748 {
1749 list_append_tv(l, &item->li_tv);
1750 item = item->li_next;
1751 }
1752
1753 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001754 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 ltv.vval.v_list = l;
1756 l->lv_refcount = 1;
1757
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1759 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 clear_tv(&ltv);
1761 if (arg == NULL)
1762 return FAIL;
1763 break;
1764 }
1765 else if (*arg != ',' && *arg != ']')
1766 {
1767 EMSG2(_(e_intern2), "ex_let_vars()");
1768 return FAIL;
1769 }
1770 }
1771
1772 return OK;
1773}
1774
1775/*
1776 * Skip over assignable variable "var" or list of variables "[var, var]".
1777 * Used for ":let varvar = expr" and ":for varvar in expr".
1778 * For "[var, var]" increment "*var_count" for each variable.
1779 * for "[var, var; var]" set "semicolon".
1780 * Return NULL for an error.
1781 */
1782 static char_u *
1783skip_var_list(arg, var_count, semicolon)
1784 char_u *arg;
1785 int *var_count;
1786 int *semicolon;
1787{
1788 char_u *p, *s;
1789
1790 if (*arg == '[')
1791 {
1792 /* "[var, var]": find the matching ']'. */
1793 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001794 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 {
1796 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1797 s = skip_var_one(p);
1798 if (s == p)
1799 {
1800 EMSG2(_(e_invarg2), p);
1801 return NULL;
1802 }
1803 ++*var_count;
1804
1805 p = skipwhite(s);
1806 if (*p == ']')
1807 break;
1808 else if (*p == ';')
1809 {
1810 if (*semicolon == 1)
1811 {
1812 EMSG(_("Double ; in list of variables"));
1813 return NULL;
1814 }
1815 *semicolon = 1;
1816 }
1817 else if (*p != ',')
1818 {
1819 EMSG2(_(e_invarg2), p);
1820 return NULL;
1821 }
1822 }
1823 return p + 1;
1824 }
1825 else
1826 return skip_var_one(arg);
1827}
1828
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001829/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001830 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1831 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001832 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 static char_u *
1834skip_var_one(arg)
1835 char_u *arg;
1836{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001837 if (*arg == '@' && arg[1] != NUL)
1838 return arg + 2;
1839 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1840 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841}
1842
Bram Moolenaara7043832005-01-21 11:56:39 +00001843/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 * List variables for hashtab "ht" with prefix "prefix".
1845 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001846 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001848list_hashtable_vars(ht, prefix, empty)
1849 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001850 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001852{
Bram Moolenaar33570922005-01-25 22:26:29 +00001853 hashitem_T *hi;
1854 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001855 int todo;
1856
1857 todo = ht->ht_used;
1858 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1859 {
1860 if (!HASHITEM_EMPTY(hi))
1861 {
1862 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001863 di = HI2DI(hi);
1864 if (empty || di->di_tv.v_type != VAR_STRING
1865 || di->di_tv.vval.v_string != NULL)
1866 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 }
1868 }
1869}
1870
1871/*
1872 * List global variables.
1873 */
1874 static void
1875list_glob_vars()
1876{
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001878}
1879
1880/*
1881 * List buffer variables.
1882 */
1883 static void
1884list_buf_vars()
1885{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001886 char_u numbuf[NUMBUFLEN];
1887
Bram Moolenaar33570922005-01-25 22:26:29 +00001888 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001889
1890 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1891 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001892}
1893
1894/*
1895 * List window variables.
1896 */
1897 static void
1898list_win_vars()
1899{
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001901}
1902
1903/*
1904 * List Vim variables.
1905 */
1906 static void
1907list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908{
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910}
1911
1912/*
1913 * List variables in "arg".
1914 */
1915 static char_u *
1916list_arg_vars(eap, arg)
1917 exarg_T *eap;
1918 char_u *arg;
1919{
1920 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001923 char_u *name_start;
1924 char_u *arg_subsc;
1925 char_u *tofree;
1926 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927
1928 while (!ends_excmd(*arg) && !got_int)
1929 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001932 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1934 {
1935 emsg_severe = TRUE;
1936 EMSG(_(e_trailing));
1937 break;
1938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 /* get_name_len() takes care of expanding curly braces */
1943 name_start = name = arg;
1944 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1945 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001947 /* This is mainly to keep test 49 working: when expanding
1948 * curly braces fails overrule the exception error message. */
1949 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001951 emsg_severe = TRUE;
1952 EMSG2(_(e_invarg2), arg);
1953 break;
1954 }
1955 error = TRUE;
1956 }
1957 else
1958 {
1959 if (tofree != NULL)
1960 name = tofree;
1961 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 else
1964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 /* handle d.key, l[idx], f(expr) */
1966 arg_subsc = arg;
1967 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001971 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001972 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001973 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001975 case 'g': list_glob_vars(); break;
1976 case 'b': list_buf_vars(); break;
1977 case 'w': list_win_vars(); break;
1978 case 'v': list_vim_vars(); break;
1979 default:
1980 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001982 }
1983 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 {
1985 char_u numbuf[NUMBUFLEN];
1986 char_u *tf;
1987 int c;
1988 char_u *s;
1989
1990 s = echo_string(&tv, &tf, numbuf);
1991 c = *arg;
1992 *arg = NUL;
1993 list_one_var_a((char_u *)"",
1994 arg == arg_subsc ? name : name_start,
1995 tv.v_type, s == NULL ? (char_u *)"" : s);
1996 *arg = c;
1997 vim_free(tf);
1998 }
1999 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 }
2002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003
2004 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002006
2007 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002008 }
2009
2010 return arg;
2011}
2012
2013/*
2014 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2015 * Returns a pointer to the char just after the var name.
2016 * Returns NULL if there is an error.
2017 */
2018 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002021 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025{
2026 int c1;
2027 char_u *name;
2028 char_u *p;
2029 char_u *arg_end = NULL;
2030 int len;
2031 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033
2034 /*
2035 * ":let $VAR = expr": Set environment variable.
2036 */
2037 if (*arg == '$')
2038 {
2039 /* Find the end of the name. */
2040 ++arg;
2041 name = arg;
2042 len = get_env_len(&arg);
2043 if (len == 0)
2044 EMSG2(_(e_invarg2), name - 1);
2045 else
2046 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 if (op != NULL && (*op == '+' || *op == '-'))
2048 EMSG2(_(e_letwrong), op);
2049 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 EMSG(_(e_letunexp));
2052 else
2053 {
2054 c1 = name[len];
2055 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002056 p = get_tv_string_chk(tv);
2057 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002058 {
2059 int mustfree = FALSE;
2060 char_u *s = vim_getenv(name, &mustfree);
2061
2062 if (s != NULL)
2063 {
2064 p = tofree = concat_str(s, p);
2065 if (mustfree)
2066 vim_free(s);
2067 }
2068 }
2069 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002070 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002071 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002072 if (STRICMP(name, "HOME") == 0)
2073 init_homedir();
2074 else if (didset_vim && STRICMP(name, "VIM") == 0)
2075 didset_vim = FALSE;
2076 else if (didset_vimruntime
2077 && STRICMP(name, "VIMRUNTIME") == 0)
2078 didset_vimruntime = FALSE;
2079 arg_end = arg;
2080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002082 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 }
2084 }
2085 }
2086
2087 /*
2088 * ":let &option = expr": Set option value.
2089 * ":let &l:option = expr": Set local option value.
2090 * ":let &g:option = expr": Set global option value.
2091 */
2092 else if (*arg == '&')
2093 {
2094 /* Find the end of the name. */
2095 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002096 if (p == NULL || (endchars != NULL
2097 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 EMSG(_(e_letunexp));
2099 else
2100 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 long n;
2102 int opt_type;
2103 long numval;
2104 char_u *stringval = NULL;
2105 char_u *s;
2106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 c1 = *p;
2108 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002109
2110 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002111 s = get_tv_string_chk(tv); /* != NULL if number or string */
2112 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002113 {
2114 opt_type = get_option_value(arg, &numval,
2115 &stringval, opt_flags);
2116 if ((opt_type == 1 && *op == '.')
2117 || (opt_type == 0 && *op != '.'))
2118 EMSG2(_(e_letwrong), op);
2119 else
2120 {
2121 if (opt_type == 1) /* number */
2122 {
2123 if (*op == '+')
2124 n = numval + n;
2125 else
2126 n = numval - n;
2127 }
2128 else if (opt_type == 0 && stringval != NULL) /* string */
2129 {
2130 s = concat_str(stringval, s);
2131 vim_free(stringval);
2132 stringval = s;
2133 }
2134 }
2135 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 if (s != NULL)
2137 {
2138 set_option_value(arg, n, s, opt_flags);
2139 arg_end = p;
2140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 }
2144 }
2145
2146 /*
2147 * ":let @r = expr": Set register contents.
2148 */
2149 else if (*arg == '@')
2150 {
2151 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 if (op != NULL && (*op == '+' || *op == '-'))
2153 EMSG2(_(e_letwrong), op);
2154 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002155 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156 EMSG(_(e_letunexp));
2157 else
2158 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 char_u *tofree = NULL;
2160 char_u *s;
2161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 p = get_tv_string_chk(tv);
2163 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002165 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 if (s != NULL)
2167 {
2168 p = tofree = concat_str(s, p);
2169 vim_free(s);
2170 }
2171 }
2172 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002173 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002175 arg_end = arg + 1;
2176 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 }
2179 }
2180
2181 /*
2182 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002185 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002187 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002189 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2193 EMSG(_(e_letunexp));
2194 else
2195 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002196 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 arg_end = p;
2198 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 }
2202
2203 else
2204 EMSG2(_(e_invarg2), arg);
2205
2206 return arg_end;
2207}
2208
2209/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002210 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2211 */
2212 static int
2213check_changedtick(arg)
2214 char_u *arg;
2215{
2216 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2217 {
2218 EMSG2(_(e_readonlyvar), arg);
2219 return TRUE;
2220 }
2221 return FALSE;
2222}
2223
2224/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 * Get an lval: variable, Dict item or List item that can be assigned a value
2226 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2227 * "name.key", "name.key[expr]" etc.
2228 * Indexing only works if "name" is an existing List or Dictionary.
2229 * "name" points to the start of the name.
2230 * If "rettv" is not NULL it points to the value to be assigned.
2231 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2232 * wrong; must end in space or cmd separator.
2233 *
2234 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002235 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 */
2238 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002241 typval_T *rettv;
2242 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 int unlet;
2244 int skip;
2245 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002246 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 char_u *expr_start, *expr_end;
2250 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 dictitem_T *v;
2252 typval_T var1;
2253 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002256 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002258 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002261 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262
2263 if (skip)
2264 {
2265 /* When skipping just find the end of the name. */
2266 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002267 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 }
2269
2270 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002271 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002272 if (expr_start != NULL)
2273 {
2274 /* Don't expand the name when we already know there is an error. */
2275 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2276 && *p != '[' && *p != '.')
2277 {
2278 EMSG(_(e_trailing));
2279 return NULL;
2280 }
2281
2282 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2283 if (lp->ll_exp_name == NULL)
2284 {
2285 /* Report an invalid expression in braces, unless the
2286 * expression evaluation has been cancelled due to an
2287 * aborting error, an interrupt, or an exception. */
2288 if (!aborting() && !quiet)
2289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002290 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 EMSG2(_(e_invarg2), name);
2292 return NULL;
2293 }
2294 }
2295 lp->ll_name = lp->ll_exp_name;
2296 }
2297 else
2298 lp->ll_name = name;
2299
2300 /* Without [idx] or .key we are done. */
2301 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2302 return p;
2303
2304 cc = *p;
2305 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 if (v == NULL && !quiet)
2308 EMSG2(_(e_undefvar), lp->ll_name);
2309 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 if (v == NULL)
2311 return NULL;
2312
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 /*
2314 * Loop until no more [idx] or .key is following.
2315 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2320 && !(lp->ll_tv->v_type == VAR_DICT
2321 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 if (!quiet)
2324 EMSG(_("E689: Can only index a List or Dictionary"));
2325 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 if (!quiet)
2330 EMSG(_("E708: [:] must come last"));
2331 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002333
Bram Moolenaar8c711452005-01-14 21:53:12 +00002334 len = -1;
2335 if (*p == '.')
2336 {
2337 key = p + 1;
2338 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2339 ;
2340 if (len == 0)
2341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (!quiet)
2343 EMSG(_(e_emptykey));
2344 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 }
2346 p = key + len;
2347 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002348 else
2349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 if (*p == ':')
2353 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002354 else
2355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 empty1 = FALSE;
2357 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002359 if (get_tv_string_chk(&var1) == NULL)
2360 {
2361 /* not a number or string */
2362 clear_tv(&var1);
2363 return NULL;
2364 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 }
2366
2367 /* Optionally get the second index [ :expr]. */
2368 if (*p == ':')
2369 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374 if (!empty1)
2375 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002377 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (rettv != NULL && (rettv->v_type != VAR_LIST
2379 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 if (!quiet)
2382 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 if (!empty1)
2384 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 }
2387 p = skipwhite(p + 1);
2388 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 else
2391 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002393 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 if (!empty1)
2396 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002399 if (get_tv_string_chk(&var2) == NULL)
2400 {
2401 /* not a number or string */
2402 if (!empty1)
2403 clear_tv(&var1);
2404 clear_tv(&var2);
2405 return NULL;
2406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002409 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002412
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (!quiet)
2416 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 if (!empty1)
2418 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 }
2423
2424 /* Skip to past ']'. */
2425 ++p;
2426 }
2427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 {
2430 if (len == -1)
2431 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 if (*key == NUL)
2435 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (!quiet)
2437 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 }
2441 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 lp->ll_list = NULL;
2443 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002444 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002447 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 if (len == -1)
2453 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 }
2456 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 if (len == -1)
2461 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 p = NULL;
2464 break;
2465 }
2466 if (len == -1)
2467 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470 else
2471 {
2472 /*
2473 * Get the number and item for the only or first index of the List.
2474 */
2475 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 else
2478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 clear_tv(&var1);
2481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 lp->ll_list = lp->ll_tv->vval.v_list;
2484 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2485 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (!quiet)
2488 EMSGN(_(e_listidx), lp->ll_n1);
2489 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493
2494 /*
2495 * May need to find the item or absolute index for the second
2496 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 * When no index given: "lp->ll_empty2" is TRUE.
2498 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 if (ni == NULL)
2508 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (!quiet)
2510 EMSGN(_(e_listidx), lp->ll_n2);
2511 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 }
2515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2517 if (lp->ll_n1 < 0)
2518 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2519 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSGN(_(e_listidx), lp->ll_n2);
2523 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002524 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002525 }
2526
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
2530
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 return p;
2532}
2533
2534/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 */
2537 static void
2538clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540{
2541 vim_free(lp->ll_exp_name);
2542 vim_free(lp->ll_newkey);
2543}
2544
2545/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002546 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 */
2550 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002556 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557{
2558 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 listitem_T *ri;
2560 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561
2562 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 cc = *endp;
2567 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568 if (op != NULL && *op != '=')
2569 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002571
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002572 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002573 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2574 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002575 {
2576 if (tv_op(&tv, rettv, op) == OK)
2577 set_var(lp->ll_name, &tv, FALSE);
2578 clear_tv(&tv);
2579 }
2580 }
2581 else
2582 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002586 else if (tv_check_lock(lp->ll_newkey == NULL
2587 ? lp->ll_tv->v_lock
2588 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2589 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 else if (lp->ll_range)
2591 {
2592 /*
2593 * Assign the List values to the list items.
2594 */
2595 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002597 if (op != NULL && *op != '=')
2598 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2599 else
2600 {
2601 clear_tv(&lp->ll_li->li_tv);
2602 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 ri = ri->li_next;
2605 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2606 break;
2607 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002610 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 ri = NULL;
2613 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 lp->ll_li = lp->ll_li->li_next;
2617 ++lp->ll_n1;
2618 }
2619 if (ri != NULL)
2620 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 else if (lp->ll_empty2
2622 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 : lp->ll_n1 != lp->ll_n2)
2624 EMSG(_("E711: List value has not enough items"));
2625 }
2626 else
2627 {
2628 /*
2629 * Assign to a List or Dictionary item.
2630 */
2631 if (lp->ll_newkey != NULL)
2632 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 if (op != NULL && *op != '=')
2634 {
2635 EMSG2(_(e_letwrong), op);
2636 return;
2637 }
2638
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002640 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (di == NULL)
2642 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002643 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2644 {
2645 vim_free(di);
2646 return;
2647 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 else if (op != NULL && *op != '=')
2651 {
2652 tv_op(lp->ll_tv, rettv, op);
2653 return;
2654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /*
2659 * Assign the value to the variable or list item.
2660 */
2661 if (copy)
2662 copy_tv(rettv, lp->ll_tv);
2663 else
2664 {
2665 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002666 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
2669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670}
2671
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2674 * Returns OK or FAIL.
2675 */
2676 static int
2677tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002678 typval_T *tv1;
2679 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002680 char_u *op;
2681{
2682 long n;
2683 char_u numbuf[NUMBUFLEN];
2684 char_u *s;
2685
2686 /* Can't do anything with a Funcref or a Dict on the right. */
2687 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2688 {
2689 switch (tv1->v_type)
2690 {
2691 case VAR_DICT:
2692 case VAR_FUNC:
2693 break;
2694
2695 case VAR_LIST:
2696 if (*op != '+' || tv2->v_type != VAR_LIST)
2697 break;
2698 /* List += List */
2699 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2700 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2701 return OK;
2702
2703 case VAR_NUMBER:
2704 case VAR_STRING:
2705 if (tv2->v_type == VAR_LIST)
2706 break;
2707 if (*op == '+' || *op == '-')
2708 {
2709 /* nr += nr or nr -= nr*/
2710 n = get_tv_number(tv1);
2711 if (*op == '+')
2712 n += get_tv_number(tv2);
2713 else
2714 n -= get_tv_number(tv2);
2715 clear_tv(tv1);
2716 tv1->v_type = VAR_NUMBER;
2717 tv1->vval.v_number = n;
2718 }
2719 else
2720 {
2721 /* str .= str */
2722 s = get_tv_string(tv1);
2723 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2724 clear_tv(tv1);
2725 tv1->v_type = VAR_STRING;
2726 tv1->vval.v_string = s;
2727 }
2728 return OK;
2729 }
2730 }
2731
2732 EMSG2(_(e_letwrong), op);
2733 return FAIL;
2734}
2735
2736/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737 * Add a watcher to a list.
2738 */
2739 static void
2740list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002741 list_T *l;
2742 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002743{
2744 lw->lw_next = l->lv_watch;
2745 l->lv_watch = lw;
2746}
2747
2748/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002749 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002750 * No warning when it isn't found...
2751 */
2752 static void
2753list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 list_T *l;
2755 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756{
Bram Moolenaar33570922005-01-25 22:26:29 +00002757 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002758
2759 lwp = &l->lv_watch;
2760 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2761 {
2762 if (lw == lwrem)
2763 {
2764 *lwp = lw->lw_next;
2765 break;
2766 }
2767 lwp = &lw->lw_next;
2768 }
2769}
2770
2771/*
2772 * Just before removing an item from a list: advance watchers to the next
2773 * item.
2774 */
2775 static void
2776list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 list_T *l;
2778 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779{
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002781
2782 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2783 if (lw->lw_item == item)
2784 lw->lw_item = item->li_next;
2785}
2786
2787/*
2788 * Evaluate the expression used in a ":for var in expr" command.
2789 * "arg" points to "var".
2790 * Set "*errp" to TRUE for an error, FALSE otherwise;
2791 * Return a pointer that holds the info. Null when there is an error.
2792 */
2793 void *
2794eval_for_line(arg, errp, nextcmdp, skip)
2795 char_u *arg;
2796 int *errp;
2797 char_u **nextcmdp;
2798 int skip;
2799{
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002801 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002802 typval_T tv;
2803 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804
2805 *errp = TRUE; /* default: there is an error */
2806
Bram Moolenaar33570922005-01-25 22:26:29 +00002807 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002808 if (fi == NULL)
2809 return NULL;
2810
2811 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2812 if (expr == NULL)
2813 return fi;
2814
2815 expr = skipwhite(expr);
2816 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2817 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002818 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002819 return fi;
2820 }
2821
2822 if (skip)
2823 ++emsg_skip;
2824 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2825 {
2826 *errp = FALSE;
2827 if (!skip)
2828 {
2829 l = tv.vval.v_list;
2830 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002831 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002833 clear_tv(&tv);
2834 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002835 else
2836 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002837 /* No need to increment the refcount, it's already set for the
2838 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839 fi->fi_list = l;
2840 list_add_watch(l, &fi->fi_lw);
2841 fi->fi_lw.lw_item = l->lv_first;
2842 }
2843 }
2844 }
2845 if (skip)
2846 --emsg_skip;
2847
2848 return fi;
2849}
2850
2851/*
2852 * Use the first item in a ":for" list. Advance to the next.
2853 * Assign the values to the variable (list). "arg" points to the first one.
2854 * Return TRUE when a valid item was found, FALSE when at end of list or
2855 * something wrong.
2856 */
2857 int
2858next_for_item(fi_void, arg)
2859 void *fi_void;
2860 char_u *arg;
2861{
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865
2866 item = fi->fi_lw.lw_item;
2867 if (item == NULL)
2868 result = FALSE;
2869 else
2870 {
2871 fi->fi_lw.lw_item = item->li_next;
2872 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2873 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2874 }
2875 return result;
2876}
2877
2878/*
2879 * Free the structure used to store info used by ":for".
2880 */
2881 void
2882free_for_info(fi_void)
2883 void *fi_void;
2884{
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002887 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002890 list_unref(fi->fi_list);
2891 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002892 vim_free(fi);
2893}
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2896
2897 void
2898set_context_for_expression(xp, arg, cmdidx)
2899 expand_T *xp;
2900 char_u *arg;
2901 cmdidx_T cmdidx;
2902{
2903 int got_eq = FALSE;
2904 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 if (cmdidx == CMD_let)
2908 {
2909 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002910 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 {
2912 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 {
2915 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917 if (vim_iswhite(*p))
2918 break;
2919 }
2920 return;
2921 }
2922 }
2923 else
2924 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2925 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 while ((xp->xp_pattern = vim_strpbrk(arg,
2927 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2928 {
2929 c = *xp->xp_pattern;
2930 if (c == '&')
2931 {
2932 c = xp->xp_pattern[1];
2933 if (c == '&')
2934 {
2935 ++xp->xp_pattern;
2936 xp->xp_context = cmdidx != CMD_let || got_eq
2937 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2938 }
2939 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002942 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2943 xp->xp_pattern += 2;
2944
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 else if (c == '$')
2948 {
2949 /* environment variable */
2950 xp->xp_context = EXPAND_ENV_VARS;
2951 }
2952 else if (c == '=')
2953 {
2954 got_eq = TRUE;
2955 xp->xp_context = EXPAND_EXPRESSION;
2956 }
2957 else if (c == '<'
2958 && xp->xp_context == EXPAND_FUNCTIONS
2959 && vim_strchr(xp->xp_pattern, '(') == NULL)
2960 {
2961 /* Function name can start with "<SNR>" */
2962 break;
2963 }
2964 else if (cmdidx != CMD_let || got_eq)
2965 {
2966 if (c == '"') /* string */
2967 {
2968 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2969 if (c == '\\' && xp->xp_pattern[1] != NUL)
2970 ++xp->xp_pattern;
2971 xp->xp_context = EXPAND_NOTHING;
2972 }
2973 else if (c == '\'') /* literal string */
2974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002975 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2977 /* skip */ ;
2978 xp->xp_context = EXPAND_NOTHING;
2979 }
2980 else if (c == '|')
2981 {
2982 if (xp->xp_pattern[1] == '|')
2983 {
2984 ++xp->xp_pattern;
2985 xp->xp_context = EXPAND_EXPRESSION;
2986 }
2987 else
2988 xp->xp_context = EXPAND_COMMANDS;
2989 }
2990 else
2991 xp->xp_context = EXPAND_EXPRESSION;
2992 }
2993 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 /* Doesn't look like something valid, expand as an expression
2995 * anyway. */
2996 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 arg = xp->xp_pattern;
2998 if (*arg != NUL)
2999 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3000 /* skip */ ;
3001 }
3002 xp->xp_pattern = arg;
3003}
3004
3005#endif /* FEAT_CMDL_COMPL */
3006
3007/*
3008 * ":1,25call func(arg1, arg2)" function call.
3009 */
3010 void
3011ex_call(eap)
3012 exarg_T *eap;
3013{
3014 char_u *arg = eap->arg;
3015 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 linenr_T lnum;
3021 int doesrange;
3022 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003025 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3026 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (tofree == NULL)
3028 return;
3029
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003030 /* Increase refcount on dictionary, it could get deleted when evaluating
3031 * the arguments. */
3032 if (fudi.fd_dict != NULL)
3033 ++fudi.fd_dict->dv_refcount;
3034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3036 len = STRLEN(tofree);
3037 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaar532c7802005-01-27 14:44:31 +00003039 /* Skip white space to allow ":call func ()". Not good, but required for
3040 * backward compatibility. */
3041 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003042 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 if (*startarg != '(')
3045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003046 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 goto end;
3048 }
3049
3050 /*
3051 * When skipping, evaluate the function once, to find the end of the
3052 * arguments.
3053 * When the function takes a range, this is discovered after the first
3054 * call, and the loop is broken.
3055 */
3056 if (eap->skip)
3057 {
3058 ++emsg_skip;
3059 lnum = eap->line2; /* do it once, also with an invalid range */
3060 }
3061 else
3062 lnum = eap->line1;
3063 for ( ; lnum <= eap->line2; ++lnum)
3064 {
3065 if (!eap->skip && eap->addr_count > 0)
3066 {
3067 curwin->w_cursor.lnum = lnum;
3068 curwin->w_cursor.col = 0;
3069 }
3070 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003072 eap->line1, eap->line2, &doesrange,
3073 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
3075 failed = TRUE;
3076 break;
3077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (doesrange || eap->skip)
3080 break;
3081 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003082 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003083 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003084 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (aborting())
3086 break;
3087 }
3088 if (eap->skip)
3089 --emsg_skip;
3090
3091 if (!failed)
3092 {
3093 /* Check for trailing illegal characters and a following command. */
3094 if (!ends_excmd(*arg))
3095 {
3096 emsg_severe = TRUE;
3097 EMSG(_(e_trailing));
3098 }
3099 else
3100 eap->nextcmd = check_nextcmd(arg);
3101 }
3102
3103end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003104 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107
3108/*
3109 * ":unlet[!] var1 ... " command.
3110 */
3111 void
3112ex_unlet(eap)
3113 exarg_T *eap;
3114{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 ex_unletlock(eap, eap->arg, 0);
3116}
3117
3118/*
3119 * ":lockvar" and ":unlockvar" commands
3120 */
3121 void
3122ex_lockvar(eap)
3123 exarg_T *eap;
3124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003126 int deep = 2;
3127
3128 if (eap->forceit)
3129 deep = -1;
3130 else if (vim_isdigit(*arg))
3131 {
3132 deep = getdigits(&arg);
3133 arg = skipwhite(arg);
3134 }
3135
3136 ex_unletlock(eap, arg, deep);
3137}
3138
3139/*
3140 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3141 */
3142 static void
3143ex_unletlock(eap, argstart, deep)
3144 exarg_T *eap;
3145 char_u *argstart;
3146 int deep;
3147{
3148 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003151 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 do
3154 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003155 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003156 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3157 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003158 if (lv.ll_name == NULL)
3159 error = TRUE; /* error but continue parsing */
3160 if (name_end == NULL || (!vim_iswhite(*name_end)
3161 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003163 if (name_end != NULL)
3164 {
3165 emsg_severe = TRUE;
3166 EMSG(_(e_trailing));
3167 }
3168 if (!(eap->skip || error))
3169 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 break;
3171 }
3172
3173 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003174 {
3175 if (eap->cmdidx == CMD_unlet)
3176 {
3177 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3178 error = TRUE;
3179 }
3180 else
3181 {
3182 if (do_lock_var(&lv, name_end, deep,
3183 eap->cmdidx == CMD_lockvar) == FAIL)
3184 error = TRUE;
3185 }
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003188 if (!eap->skip)
3189 clear_lval(&lv);
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 arg = skipwhite(name_end);
3192 } while (!ends_excmd(*arg));
3193
3194 eap->nextcmd = check_nextcmd(arg);
3195}
3196
Bram Moolenaar8c711452005-01-14 21:53:12 +00003197 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003198do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 int forceit;
3202{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003203 int ret = OK;
3204 int cc;
3205
3206 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003208 cc = *name_end;
3209 *name_end = NUL;
3210
3211 /* Normal name or expanded name. */
3212 if (check_changedtick(lp->ll_name))
3213 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003214 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003217 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3219 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 else if (lp->ll_range)
3221 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223
3224 /* Delete a range of List items. */
3225 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3226 {
3227 li = lp->ll_li->li_next;
3228 listitem_remove(lp->ll_list, lp->ll_li);
3229 lp->ll_li = li;
3230 ++lp->ll_n1;
3231 }
3232 }
3233 else
3234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 /* unlet a List item. */
3237 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003240 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003241 }
3242
3243 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003244}
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
3247 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003248 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
3250 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003251do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 hashtab_T *ht;
3256 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003257 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 ht = find_var_ht(name, &varname);
3260 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003262 hi = hash_find(ht, varname);
3263 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003264 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265 if (var_check_ro(HI2DI(hi)->di_flags, name))
3266 return FAIL;
3267 delete_var(ht, hi);
3268 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271 if (forceit)
3272 return OK;
3273 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 return FAIL;
3275}
3276
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277/*
3278 * Lock or unlock variable indicated by "lp".
3279 * "deep" is the levels to go (-1 for unlimited);
3280 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3281 */
3282 static int
3283do_lock_var(lp, name_end, deep, lock)
3284 lval_T *lp;
3285 char_u *name_end;
3286 int deep;
3287 int lock;
3288{
3289 int ret = OK;
3290 int cc;
3291 dictitem_T *di;
3292
3293 if (deep == 0) /* nothing to do */
3294 return OK;
3295
3296 if (lp->ll_tv == NULL)
3297 {
3298 cc = *name_end;
3299 *name_end = NUL;
3300
3301 /* Normal name or expanded name. */
3302 if (check_changedtick(lp->ll_name))
3303 ret = FAIL;
3304 else
3305 {
3306 di = find_var(lp->ll_name, NULL);
3307 if (di == NULL)
3308 ret = FAIL;
3309 else
3310 {
3311 if (lock)
3312 di->di_flags |= DI_FLAGS_LOCK;
3313 else
3314 di->di_flags &= ~DI_FLAGS_LOCK;
3315 item_lock(&di->di_tv, deep, lock);
3316 }
3317 }
3318 *name_end = cc;
3319 }
3320 else if (lp->ll_range)
3321 {
3322 listitem_T *li = lp->ll_li;
3323
3324 /* (un)lock a range of List items. */
3325 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3326 {
3327 item_lock(&li->li_tv, deep, lock);
3328 li = li->li_next;
3329 ++lp->ll_n1;
3330 }
3331 }
3332 else if (lp->ll_list != NULL)
3333 /* (un)lock a List item. */
3334 item_lock(&lp->ll_li->li_tv, deep, lock);
3335 else
3336 /* un(lock) a Dictionary item. */
3337 item_lock(&lp->ll_di->di_tv, deep, lock);
3338
3339 return ret;
3340}
3341
3342/*
3343 * Lock or unlock an item. "deep" is nr of levels to go.
3344 */
3345 static void
3346item_lock(tv, deep, lock)
3347 typval_T *tv;
3348 int deep;
3349 int lock;
3350{
3351 static int recurse = 0;
3352 list_T *l;
3353 listitem_T *li;
3354 dict_T *d;
3355 hashitem_T *hi;
3356 int todo;
3357
3358 if (recurse >= DICT_MAXNEST)
3359 {
3360 EMSG(_("E743: variable nested too deep for (un)lock"));
3361 return;
3362 }
3363 if (deep == 0)
3364 return;
3365 ++recurse;
3366
3367 /* lock/unlock the item itself */
3368 if (lock)
3369 tv->v_lock |= VAR_LOCKED;
3370 else
3371 tv->v_lock &= ~VAR_LOCKED;
3372
3373 switch (tv->v_type)
3374 {
3375 case VAR_LIST:
3376 if ((l = tv->vval.v_list) != NULL)
3377 {
3378 if (lock)
3379 l->lv_lock |= VAR_LOCKED;
3380 else
3381 l->lv_lock &= ~VAR_LOCKED;
3382 if (deep < 0 || deep > 1)
3383 /* recursive: lock/unlock the items the List contains */
3384 for (li = l->lv_first; li != NULL; li = li->li_next)
3385 item_lock(&li->li_tv, deep - 1, lock);
3386 }
3387 break;
3388 case VAR_DICT:
3389 if ((d = tv->vval.v_dict) != NULL)
3390 {
3391 if (lock)
3392 d->dv_lock |= VAR_LOCKED;
3393 else
3394 d->dv_lock &= ~VAR_LOCKED;
3395 if (deep < 0 || deep > 1)
3396 {
3397 /* recursive: lock/unlock the items the List contains */
3398 todo = d->dv_hashtab.ht_used;
3399 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3400 {
3401 if (!HASHITEM_EMPTY(hi))
3402 {
3403 --todo;
3404 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3405 }
3406 }
3407 }
3408 }
3409 }
3410 --recurse;
3411}
3412
Bram Moolenaara40058a2005-07-11 22:42:07 +00003413/*
3414 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3415 * it refers to a List or Dictionary that is locked.
3416 */
3417 static int
3418tv_islocked(tv)
3419 typval_T *tv;
3420{
3421 return (tv->v_lock & VAR_LOCKED)
3422 || (tv->v_type == VAR_LIST
3423 && tv->vval.v_list != NULL
3424 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3425 || (tv->v_type == VAR_DICT
3426 && tv->vval.v_dict != NULL
3427 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3428}
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3431/*
3432 * Delete all "menutrans_" variables.
3433 */
3434 void
3435del_menutrans_vars()
3436{
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003438 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 hash_lock(&globvarht);
3441 todo = globvarht.ht_used;
3442 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003443 {
3444 if (!HASHITEM_EMPTY(hi))
3445 {
3446 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003447 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3448 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003449 }
3450 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452}
3453#endif
3454
3455#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3456
3457/*
3458 * Local string buffer for the next two functions to store a variable name
3459 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3460 * get_user_var_name().
3461 */
3462
3463static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3464
3465static char_u *varnamebuf = NULL;
3466static int varnamebuflen = 0;
3467
3468/*
3469 * Function to concatenate a prefix and a variable name.
3470 */
3471 static char_u *
3472cat_prefix_varname(prefix, name)
3473 int prefix;
3474 char_u *name;
3475{
3476 int len;
3477
3478 len = (int)STRLEN(name) + 3;
3479 if (len > varnamebuflen)
3480 {
3481 vim_free(varnamebuf);
3482 len += 10; /* some additional space */
3483 varnamebuf = alloc(len);
3484 if (varnamebuf == NULL)
3485 {
3486 varnamebuflen = 0;
3487 return NULL;
3488 }
3489 varnamebuflen = len;
3490 }
3491 *varnamebuf = prefix;
3492 varnamebuf[1] = ':';
3493 STRCPY(varnamebuf + 2, name);
3494 return varnamebuf;
3495}
3496
3497/*
3498 * Function given to ExpandGeneric() to obtain the list of user defined
3499 * (global/buffer/window/built-in) variable names.
3500 */
3501/*ARGSUSED*/
3502 char_u *
3503get_user_var_name(xp, idx)
3504 expand_T *xp;
3505 int idx;
3506{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003507 static long_u gdone;
3508 static long_u bdone;
3509 static long_u wdone;
3510 static int vidx;
3511 static hashitem_T *hi;
3512 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003515 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003516
3517 /* Global variables */
3518 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003520 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003521 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003522 else
3523 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003524 while (HASHITEM_EMPTY(hi))
3525 ++hi;
3526 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3527 return cat_prefix_varname('g', hi->hi_key);
3528 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003530
3531 /* b: variables */
3532 ht = &curbuf->b_vars.dv_hashtab;
3533 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003535 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003537 else
3538 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 while (HASHITEM_EMPTY(hi))
3540 ++hi;
3541 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return (char_u *)"b:changedtick";
3547 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003548
3549 /* w: variables */
3550 ht = &curwin->w_vars.dv_hashtab;
3551 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003553 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003554 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003555 else
3556 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003557 while (HASHITEM_EMPTY(hi))
3558 ++hi;
3559 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003561
3562 /* v: variables */
3563 if (vidx < VV_LEN)
3564 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566 vim_free(varnamebuf);
3567 varnamebuf = NULL;
3568 varnamebuflen = 0;
3569 return NULL;
3570}
3571
3572#endif /* FEAT_CMDL_COMPL */
3573
3574/*
3575 * types for expressions.
3576 */
3577typedef enum
3578{
3579 TYPE_UNKNOWN = 0
3580 , TYPE_EQUAL /* == */
3581 , TYPE_NEQUAL /* != */
3582 , TYPE_GREATER /* > */
3583 , TYPE_GEQUAL /* >= */
3584 , TYPE_SMALLER /* < */
3585 , TYPE_SEQUAL /* <= */
3586 , TYPE_MATCH /* =~ */
3587 , TYPE_NOMATCH /* !~ */
3588} exptype_T;
3589
3590/*
3591 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3594 */
3595
3596/*
3597 * Handle zero level expression.
3598 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003600 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 * Return OK or FAIL.
3602 */
3603 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u **nextcmd;
3608 int evaluate;
3609{
3610 int ret;
3611 char_u *p;
3612
3613 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 if (ret == FAIL || !ends_excmd(*p))
3616 {
3617 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003618 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /*
3620 * Report the invalid expression unless the expression evaluation has
3621 * been cancelled due to an aborting error, an interrupt, or an
3622 * exception.
3623 */
3624 if (!aborting())
3625 EMSG2(_(e_invexpr2), arg);
3626 ret = FAIL;
3627 }
3628 if (nextcmd != NULL)
3629 *nextcmd = check_nextcmd(p);
3630
3631 return ret;
3632}
3633
3634/*
3635 * Handle top level expression:
3636 * expr1 ? expr0 : expr0
3637 *
3638 * "arg" must point to the first non-white of the expression.
3639 * "arg" is advanced to the next non-white after the recognized expression.
3640 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003641 * Note: "rettv.v_lock" is not set.
3642 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * Return OK or FAIL.
3644 */
3645 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003646eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 int evaluate;
3650{
3651 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003652 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 /*
3655 * Get the first variable.
3656 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return FAIL;
3659
3660 if ((*arg)[0] == '?')
3661 {
3662 result = FALSE;
3663 if (evaluate)
3664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003665 int error = FALSE;
3666
3667 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003670 if (error)
3671 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673
3674 /*
3675 * Get the second variable.
3676 */
3677 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return FAIL;
3680
3681 /*
3682 * Check for the ":".
3683 */
3684 if ((*arg)[0] != ':')
3685 {
3686 EMSG(_("E109: Missing ':' after '?'"));
3687 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 return FAIL;
3690 }
3691
3692 /*
3693 * Get the third variable.
3694 */
3695 *arg = skipwhite(*arg + 1);
3696 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3697 {
3698 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return FAIL;
3701 }
3702 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003703 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705
3706 return OK;
3707}
3708
3709/*
3710 * Handle first level expression:
3711 * expr2 || expr2 || expr2 logical OR
3712 *
3713 * "arg" must point to the first non-white of the expression.
3714 * "arg" is advanced to the next non-white after the recognized expression.
3715 *
3716 * Return OK or FAIL.
3717 */
3718 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 int evaluate;
3723{
Bram Moolenaar33570922005-01-25 22:26:29 +00003724 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 long result;
3726 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003727 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 /*
3730 * Get the first variable.
3731 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003732 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 return FAIL;
3734
3735 /*
3736 * Repeat until there is no following "||".
3737 */
3738 first = TRUE;
3739 result = FALSE;
3740 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3741 {
3742 if (evaluate && first)
3743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003747 if (error)
3748 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 first = FALSE;
3750 }
3751
3752 /*
3753 * Get the second variable.
3754 */
3755 *arg = skipwhite(*arg + 2);
3756 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3757 return FAIL;
3758
3759 /*
3760 * Compute the result.
3761 */
3762 if (evaluate && !result)
3763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003766 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003767 if (error)
3768 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 if (evaluate)
3771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 rettv->v_type = VAR_NUMBER;
3773 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775 }
3776
3777 return OK;
3778}
3779
3780/*
3781 * Handle second level expression:
3782 * expr3 && expr3 && expr3 logical AND
3783 *
3784 * "arg" must point to the first non-white of the expression.
3785 * "arg" is advanced to the next non-white after the recognized expression.
3786 *
3787 * Return OK or FAIL.
3788 */
3789 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 int evaluate;
3794{
Bram Moolenaar33570922005-01-25 22:26:29 +00003795 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 long result;
3797 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 /*
3801 * Get the first variable.
3802 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805
3806 /*
3807 * Repeat until there is no following "&&".
3808 */
3809 first = TRUE;
3810 result = TRUE;
3811 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3812 {
3813 if (evaluate && first)
3814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003815 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003817 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003818 if (error)
3819 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 first = FALSE;
3821 }
3822
3823 /*
3824 * Get the second variable.
3825 */
3826 *arg = skipwhite(*arg + 2);
3827 if (eval4(arg, &var2, evaluate && result) == FAIL)
3828 return FAIL;
3829
3830 /*
3831 * Compute the result.
3832 */
3833 if (evaluate && result)
3834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003838 if (error)
3839 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 if (evaluate)
3842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003843 rettv->v_type = VAR_NUMBER;
3844 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 }
3847
3848 return OK;
3849}
3850
3851/*
3852 * Handle third level expression:
3853 * var1 == var2
3854 * var1 =~ var2
3855 * var1 != var2
3856 * var1 !~ var2
3857 * var1 > var2
3858 * var1 >= var2
3859 * var1 < var2
3860 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003861 * var1 is var2
3862 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 *
3864 * "arg" must point to the first non-white of the expression.
3865 * "arg" is advanced to the next non-white after the recognized expression.
3866 *
3867 * Return OK or FAIL.
3868 */
3869 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003870eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 int evaluate;
3874{
Bram Moolenaar33570922005-01-25 22:26:29 +00003875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 char_u *p;
3877 int i;
3878 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003879 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 int len = 2;
3881 long n1, n2;
3882 char_u *s1, *s2;
3883 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3884 regmatch_T regmatch;
3885 int ic;
3886 char_u *save_cpo;
3887
3888 /*
3889 * Get the first variable.
3890 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 return FAIL;
3893
3894 p = *arg;
3895 switch (p[0])
3896 {
3897 case '=': if (p[1] == '=')
3898 type = TYPE_EQUAL;
3899 else if (p[1] == '~')
3900 type = TYPE_MATCH;
3901 break;
3902 case '!': if (p[1] == '=')
3903 type = TYPE_NEQUAL;
3904 else if (p[1] == '~')
3905 type = TYPE_NOMATCH;
3906 break;
3907 case '>': if (p[1] != '=')
3908 {
3909 type = TYPE_GREATER;
3910 len = 1;
3911 }
3912 else
3913 type = TYPE_GEQUAL;
3914 break;
3915 case '<': if (p[1] != '=')
3916 {
3917 type = TYPE_SMALLER;
3918 len = 1;
3919 }
3920 else
3921 type = TYPE_SEQUAL;
3922 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003923 case 'i': if (p[1] == 's')
3924 {
3925 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3926 len = 5;
3927 if (!vim_isIDc(p[len]))
3928 {
3929 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3930 type_is = TRUE;
3931 }
3932 }
3933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935
3936 /*
3937 * If there is a comparitive operator, use it.
3938 */
3939 if (type != TYPE_UNKNOWN)
3940 {
3941 /* extra question mark appended: ignore case */
3942 if (p[len] == '?')
3943 {
3944 ic = TRUE;
3945 ++len;
3946 }
3947 /* extra '#' appended: match case */
3948 else if (p[len] == '#')
3949 {
3950 ic = FALSE;
3951 ++len;
3952 }
3953 /* nothing appened: use 'ignorecase' */
3954 else
3955 ic = p_ic;
3956
3957 /*
3958 * Get the second variable.
3959 */
3960 *arg = skipwhite(p + len);
3961 if (eval5(arg, &var2, evaluate) == FAIL)
3962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 return FAIL;
3965 }
3966
3967 if (evaluate)
3968 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003969 if (type_is && rettv->v_type != var2.v_type)
3970 {
3971 /* For "is" a different type always means FALSE, for "notis"
3972 * it means TRUE. */
3973 n1 = (type == TYPE_NEQUAL);
3974 }
3975 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3976 {
3977 if (type_is)
3978 {
3979 n1 = (rettv->v_type == var2.v_type
3980 && rettv->vval.v_list == var2.vval.v_list);
3981 if (type == TYPE_NEQUAL)
3982 n1 = !n1;
3983 }
3984 else if (rettv->v_type != var2.v_type
3985 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3986 {
3987 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003988 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003989 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003990 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003991 clear_tv(rettv);
3992 clear_tv(&var2);
3993 return FAIL;
3994 }
3995 else
3996 {
3997 /* Compare two Lists for being equal or unequal. */
3998 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3999 if (type == TYPE_NEQUAL)
4000 n1 = !n1;
4001 }
4002 }
4003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004004 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4005 {
4006 if (type_is)
4007 {
4008 n1 = (rettv->v_type == var2.v_type
4009 && rettv->vval.v_dict == var2.vval.v_dict);
4010 if (type == TYPE_NEQUAL)
4011 n1 = !n1;
4012 }
4013 else if (rettv->v_type != var2.v_type
4014 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4015 {
4016 if (rettv->v_type != var2.v_type)
4017 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4018 else
4019 EMSG(_("E736: Invalid operation for Dictionary"));
4020 clear_tv(rettv);
4021 clear_tv(&var2);
4022 return FAIL;
4023 }
4024 else
4025 {
4026 /* Compare two Dictionaries for being equal or unequal. */
4027 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4028 if (type == TYPE_NEQUAL)
4029 n1 = !n1;
4030 }
4031 }
4032
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004033 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4034 {
4035 if (rettv->v_type != var2.v_type
4036 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4037 {
4038 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004039 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004040 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004041 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 clear_tv(rettv);
4043 clear_tv(&var2);
4044 return FAIL;
4045 }
4046 else
4047 {
4048 /* Compare two Funcrefs for being equal or unequal. */
4049 if (rettv->vval.v_string == NULL
4050 || var2.vval.v_string == NULL)
4051 n1 = FALSE;
4052 else
4053 n1 = STRCMP(rettv->vval.v_string,
4054 var2.vval.v_string) == 0;
4055 if (type == TYPE_NEQUAL)
4056 n1 = !n1;
4057 }
4058 }
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /*
4061 * If one of the two variables is a number, compare as a number.
4062 * When using "=~" or "!~", always compare as string.
4063 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004064 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 n1 = get_tv_number(rettv);
4068 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 switch (type)
4070 {
4071 case TYPE_EQUAL: n1 = (n1 == n2); break;
4072 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4073 case TYPE_GREATER: n1 = (n1 > n2); break;
4074 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4075 case TYPE_SMALLER: n1 = (n1 < n2); break;
4076 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4077 case TYPE_UNKNOWN:
4078 case TYPE_MATCH:
4079 case TYPE_NOMATCH: break; /* avoid gcc warning */
4080 }
4081 }
4082 else
4083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 s1 = get_tv_string_buf(rettv, buf1);
4085 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4087 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4088 else
4089 i = 0;
4090 n1 = FALSE;
4091 switch (type)
4092 {
4093 case TYPE_EQUAL: n1 = (i == 0); break;
4094 case TYPE_NEQUAL: n1 = (i != 0); break;
4095 case TYPE_GREATER: n1 = (i > 0); break;
4096 case TYPE_GEQUAL: n1 = (i >= 0); break;
4097 case TYPE_SMALLER: n1 = (i < 0); break;
4098 case TYPE_SEQUAL: n1 = (i <= 0); break;
4099
4100 case TYPE_MATCH:
4101 case TYPE_NOMATCH:
4102 /* avoid 'l' flag in 'cpoptions' */
4103 save_cpo = p_cpo;
4104 p_cpo = (char_u *)"";
4105 regmatch.regprog = vim_regcomp(s2,
4106 RE_MAGIC + RE_STRING);
4107 regmatch.rm_ic = ic;
4108 if (regmatch.regprog != NULL)
4109 {
4110 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4111 vim_free(regmatch.regprog);
4112 if (type == TYPE_NOMATCH)
4113 n1 = !n1;
4114 }
4115 p_cpo = save_cpo;
4116 break;
4117
4118 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4119 }
4120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(rettv);
4122 clear_tv(&var2);
4123 rettv->v_type = VAR_NUMBER;
4124 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 }
4127
4128 return OK;
4129}
4130
4131/*
4132 * Handle fourth level expression:
4133 * + number addition
4134 * - number subtraction
4135 * . string concatenation
4136 *
4137 * "arg" must point to the first non-white of the expression.
4138 * "arg" is advanced to the next non-white after the recognized expression.
4139 *
4140 * Return OK or FAIL.
4141 */
4142 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int evaluate;
4147{
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 typval_T var2;
4149 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int op;
4151 long n1, n2;
4152 char_u *s1, *s2;
4153 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4154 char_u *p;
4155
4156 /*
4157 * Get the first variable.
4158 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return FAIL;
4161
4162 /*
4163 * Repeat computing, until no '+', '-' or '.' is following.
4164 */
4165 for (;;)
4166 {
4167 op = **arg;
4168 if (op != '+' && op != '-' && op != '.')
4169 break;
4170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 if (op != '+' || rettv->v_type != VAR_LIST)
4172 {
4173 /* For "list + ...", an illegal use of the first operand as
4174 * a number cannot be determined before evaluating the 2nd
4175 * operand: if this is also a list, all is ok.
4176 * For "something . ...", "something - ..." or "non-list + ...",
4177 * we know that the first operand needs to be a string or number
4178 * without evaluating the 2nd operand. So check before to avoid
4179 * side effects after an error. */
4180 if (evaluate && get_tv_string_chk(rettv) == NULL)
4181 {
4182 clear_tv(rettv);
4183 return FAIL;
4184 }
4185 }
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 1);
4191 if (eval6(arg, &var2, evaluate) == FAIL)
4192 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return FAIL;
4195 }
4196
4197 if (evaluate)
4198 {
4199 /*
4200 * Compute the result.
4201 */
4202 if (op == '.')
4203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4205 s2 = get_tv_string_buf_chk(&var2, buf2);
4206 if (s2 == NULL) /* type error ? */
4207 {
4208 clear_tv(rettv);
4209 clear_tv(&var2);
4210 return FAIL;
4211 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004212 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
4214 rettv->v_type = VAR_STRING;
4215 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004217 else if (op == '+' && rettv->v_type == VAR_LIST
4218 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004219 {
4220 /* concatenate Lists */
4221 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4222 &var3) == FAIL)
4223 {
4224 clear_tv(rettv);
4225 clear_tv(&var2);
4226 return FAIL;
4227 }
4228 clear_tv(rettv);
4229 *rettv = var3;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 else
4232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 int error = FALSE;
4234
4235 n1 = get_tv_number_chk(rettv, &error);
4236 if (error)
4237 {
4238 /* This can only happen for "list + non-list".
4239 * For "non-list + ..." or "something - ...", we returned
4240 * before evaluating the 2nd operand. */
4241 clear_tv(rettv);
4242 return FAIL;
4243 }
4244 n2 = get_tv_number_chk(&var2, &error);
4245 if (error)
4246 {
4247 clear_tv(rettv);
4248 clear_tv(&var2);
4249 return FAIL;
4250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (op == '+')
4253 n1 = n1 + n2;
4254 else
4255 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 rettv->v_type = VAR_NUMBER;
4257 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 }
4262 return OK;
4263}
4264
4265/*
4266 * Handle fifth level expression:
4267 * * number multiplication
4268 * / number division
4269 * % number modulo
4270 *
4271 * "arg" must point to the first non-white of the expression.
4272 * "arg" is advanced to the next non-white after the recognized expression.
4273 *
4274 * Return OK or FAIL.
4275 */
4276 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int evaluate;
4281{
Bram Moolenaar33570922005-01-25 22:26:29 +00004282 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 int op;
4284 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 /*
4288 * Get the first variable.
4289 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 return FAIL;
4292
4293 /*
4294 * Repeat computing, until no '*', '/' or '%' is following.
4295 */
4296 for (;;)
4297 {
4298 op = **arg;
4299 if (op != '*' && op != '/' && op != '%')
4300 break;
4301
4302 if (evaluate)
4303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 if (error)
4307 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 else
4310 n1 = 0;
4311
4312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(*arg + 1);
4316 if (eval7(arg, &var2, evaluate) == FAIL)
4317 return FAIL;
4318
4319 if (evaluate)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (error)
4324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 /*
4327 * Compute the result.
4328 */
4329 if (op == '*')
4330 n1 = n1 * n2;
4331 else if (op == '/')
4332 {
4333 if (n2 == 0) /* give an error message? */
4334 n1 = 0x7fffffffL;
4335 else
4336 n1 = n1 / n2;
4337 }
4338 else
4339 {
4340 if (n2 == 0) /* give an error message? */
4341 n1 = 0;
4342 else
4343 n1 = n1 % n2;
4344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 rettv->v_type = VAR_NUMBER;
4346 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 }
4349
4350 return OK;
4351}
4352
4353/*
4354 * Handle sixth level expression:
4355 * number number constant
4356 * "string" string contstant
4357 * 'string' literal string contstant
4358 * &option-name option value
4359 * @r register contents
4360 * identifier variable value
4361 * function() function call
4362 * $VAR environment variable
4363 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004364 * [expr, expr] List
4365 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 *
4367 * Also handle:
4368 * ! in front logical NOT
4369 * - in front unary minus
4370 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004371 * trailing [] subscript in String or List
4372 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 *
4374 * "arg" must point to the first non-white of the expression.
4375 * "arg" is advanced to the next non-white after the recognized expression.
4376 *
4377 * Return OK or FAIL.
4378 */
4379 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int evaluate;
4384{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 long n;
4386 int len;
4387 char_u *s;
4388 int val;
4389 char_u *start_leader, *end_leader;
4390 int ret = OK;
4391 char_u *alias;
4392
4393 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004395 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 /*
4400 * Skip '!' and '-' characters. They are handled later.
4401 */
4402 start_leader = *arg;
4403 while (**arg == '!' || **arg == '-' || **arg == '+')
4404 *arg = skipwhite(*arg + 1);
4405 end_leader = *arg;
4406
4407 switch (**arg)
4408 {
4409 /*
4410 * Number constant.
4411 */
4412 case '0':
4413 case '1':
4414 case '2':
4415 case '3':
4416 case '4':
4417 case '5':
4418 case '6':
4419 case '7':
4420 case '8':
4421 case '9':
4422 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4423 *arg += len;
4424 if (evaluate)
4425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 rettv->v_type = VAR_NUMBER;
4427 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 break;
4430
4431 /*
4432 * String constant: "string".
4433 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 break;
4436
4437 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 break;
4442
4443 /*
4444 * List: [expr, expr]
4445 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 break;
4448
4449 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 * Dictionary: {key: val, key: val}
4451 */
4452 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4453 break;
4454
4455 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004456 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004458 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 break;
4460
4461 /*
4462 * Environment variable: $VAR.
4463 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 break;
4466
4467 /*
4468 * Register contents: @r.
4469 */
4470 case '@': ++*arg;
4471 if (evaluate)
4472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004474 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 if (**arg != NUL)
4477 ++*arg;
4478 break;
4479
4480 /*
4481 * nested expression: (expression).
4482 */
4483 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (**arg == ')')
4486 ++*arg;
4487 else if (ret == OK)
4488 {
4489 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 ret = FAIL;
4492 }
4493 break;
4494
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 break;
4497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498
4499 if (ret == NOTDONE)
4500 {
4501 /*
4502 * Must be a variable or function name.
4503 * Can also be a curly-braces kind of name: {expr}.
4504 */
4505 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004506 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 if (alias != NULL)
4508 s = alias;
4509
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004510 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004511 ret = FAIL;
4512 else
4513 {
4514 if (**arg == '(') /* recursive! */
4515 {
4516 /* If "s" is the name of a variable of type VAR_FUNC
4517 * use its contents. */
4518 s = deref_func_name(s, &len);
4519
4520 /* Invoke the function. */
4521 ret = get_func_tv(s, len, rettv, arg,
4522 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004523 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 /* Stop the expression evaluation when immediately
4525 * aborting on error, or when an interrupt occurred or
4526 * an exception was thrown but not caught. */
4527 if (aborting())
4528 {
4529 if (ret == OK)
4530 clear_tv(rettv);
4531 ret = FAIL;
4532 }
4533 }
4534 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004535 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004536 else
4537 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004538 }
4539
4540 if (alias != NULL)
4541 vim_free(alias);
4542 }
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 *arg = skipwhite(*arg);
4545
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004546 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4547 * expr(expr). */
4548 if (ret == OK)
4549 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 /*
4552 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4553 */
4554 if (ret == OK && evaluate && end_leader > start_leader)
4555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004556 int error = FALSE;
4557
4558 val = get_tv_number_chk(rettv, &error);
4559 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561 clear_tv(rettv);
4562 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004564 else
4565 {
4566 while (end_leader > start_leader)
4567 {
4568 --end_leader;
4569 if (*end_leader == '!')
4570 val = !val;
4571 else if (*end_leader == '-')
4572 val = -val;
4573 }
4574 clear_tv(rettv);
4575 rettv->v_type = VAR_NUMBER;
4576 rettv->vval.v_number = val;
4577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579
4580 return ret;
4581}
4582
4583/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004584 * Evaluate an "[expr]" or "[expr:expr]" index.
4585 * "*arg" points to the '['.
4586 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4587 */
4588 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004589eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004591 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004593 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594{
4595 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004596 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 long len = -1;
4599 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004605 if (verbose)
4606 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 return FAIL;
4608 }
4609
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004612 /*
4613 * dict.name
4614 */
4615 key = *arg + 1;
4616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4617 ;
4618 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 }
4622 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 /*
4625 * something[idx]
4626 *
4627 * Get the (first) variable from inside the [].
4628 */
4629 *arg = skipwhite(*arg + 1);
4630 if (**arg == ':')
4631 empty1 = TRUE;
4632 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4633 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4635 {
4636 /* not a number or string */
4637 clear_tv(&var1);
4638 return FAIL;
4639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004640
4641 /*
4642 * Get the second variable from inside the [:].
4643 */
4644 if (**arg == ':')
4645 {
4646 range = TRUE;
4647 *arg = skipwhite(*arg + 1);
4648 if (**arg == ']')
4649 empty2 = TRUE;
4650 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 if (!empty1)
4653 clear_tv(&var1);
4654 return FAIL;
4655 }
4656 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4657 {
4658 /* not a number or string */
4659 if (!empty1)
4660 clear_tv(&var1);
4661 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 return FAIL;
4663 }
4664 }
4665
4666 /* Check for the ']'. */
4667 if (**arg != ']')
4668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004669 if (verbose)
4670 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 clear_tv(&var1);
4672 if (range)
4673 clear_tv(&var2);
4674 return FAIL;
4675 }
4676 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 }
4678
4679 if (evaluate)
4680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 n1 = 0;
4682 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 n1 = get_tv_number(&var1);
4685 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 }
4687 if (range)
4688 {
4689 if (empty2)
4690 n2 = -1;
4691 else
4692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 n2 = get_tv_number(&var2);
4694 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 }
4696 }
4697
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 {
4700 case VAR_NUMBER:
4701 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 len = (long)STRLEN(s);
4704 if (range)
4705 {
4706 /* The resulting variable is a substring. If the indexes
4707 * are out of range the result is empty. */
4708 if (n1 < 0)
4709 {
4710 n1 = len + n1;
4711 if (n1 < 0)
4712 n1 = 0;
4713 }
4714 if (n2 < 0)
4715 n2 = len + n2;
4716 else if (n2 >= len)
4717 n2 = len;
4718 if (n1 >= len || n2 < 0 || n1 > n2)
4719 s = NULL;
4720 else
4721 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4722 }
4723 else
4724 {
4725 /* The resulting variable is a string of a single
4726 * character. If the index is too big or negative the
4727 * result is empty. */
4728 if (n1 >= len || n1 < 0)
4729 s = NULL;
4730 else
4731 s = vim_strnsave(s + n1, 1);
4732 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 clear_tv(rettv);
4734 rettv->v_type = VAR_STRING;
4735 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 break;
4737
4738 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 if (n1 < 0)
4741 n1 = len + n1;
4742 if (!empty1 && (n1 < 0 || n1 >= len))
4743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004744 if (verbose)
4745 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 return FAIL;
4747 }
4748 if (range)
4749 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004750 list_T *l;
4751 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752
4753 if (n2 < 0)
4754 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004757 if (verbose)
4758 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 return FAIL;
4760 }
4761 l = list_alloc();
4762 if (l == NULL)
4763 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 n1 <= n2; ++n1)
4766 {
4767 if (list_append_tv(l, &item->li_tv) == FAIL)
4768 {
4769 list_free(l);
4770 return FAIL;
4771 }
4772 item = item->li_next;
4773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(rettv);
4775 rettv->v_type = VAR_LIST;
4776 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004777 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 }
4779 else
4780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 clear_tv(rettv);
4784 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 }
4786 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787
4788 case VAR_DICT:
4789 if (range)
4790 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004791 if (verbose)
4792 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793 if (len == -1)
4794 clear_tv(&var1);
4795 return FAIL;
4796 }
4797 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004798 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799
4800 if (len == -1)
4801 {
4802 key = get_tv_string(&var1);
4803 if (*key == NUL)
4804 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004805 if (verbose)
4806 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004807 clear_tv(&var1);
4808 return FAIL;
4809 }
4810 }
4811
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004812 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004814 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004815 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 if (len == -1)
4817 clear_tv(&var1);
4818 if (item == NULL)
4819 return FAIL;
4820
4821 copy_tv(&item->di_tv, &var1);
4822 clear_tv(rettv);
4823 *rettv = var1;
4824 }
4825 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 }
4827 }
4828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 return OK;
4830}
4831
4832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * Get an option value.
4834 * "arg" points to the '&' or '+' before the option name.
4835 * "arg" is advanced to character after the option name.
4836 * Return OK or FAIL.
4837 */
4838 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004841 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 int evaluate;
4843{
4844 char_u *option_end;
4845 long numval;
4846 char_u *stringval;
4847 int opt_type;
4848 int c;
4849 int working = (**arg == '+'); /* has("+option") */
4850 int ret = OK;
4851 int opt_flags;
4852
4853 /*
4854 * Isolate the option name and find its value.
4855 */
4856 option_end = find_option_end(arg, &opt_flags);
4857 if (option_end == NULL)
4858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 EMSG2(_("E112: Option name missing: %s"), *arg);
4861 return FAIL;
4862 }
4863
4864 if (!evaluate)
4865 {
4866 *arg = option_end;
4867 return OK;
4868 }
4869
4870 c = *option_end;
4871 *option_end = NUL;
4872 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 if (opt_type == -3) /* invalid name */
4876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 EMSG2(_("E113: Unknown option: %s"), *arg);
4879 ret = FAIL;
4880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 if (opt_type == -2) /* hidden string option */
4884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 rettv->v_type = VAR_STRING;
4886 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 else if (opt_type == -1) /* hidden number option */
4889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 rettv->v_type = VAR_NUMBER;
4891 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 else if (opt_type == 1) /* number option */
4894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 rettv->v_type = VAR_NUMBER;
4896 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898 else /* string option */
4899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 rettv->v_type = VAR_STRING;
4901 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 }
4904 else if (working && (opt_type == -2 || opt_type == -1))
4905 ret = FAIL;
4906
4907 *option_end = c; /* put back for error messages */
4908 *arg = option_end;
4909
4910 return ret;
4911}
4912
4913/*
4914 * Allocate a variable for a string constant.
4915 * Return OK or FAIL.
4916 */
4917 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 int evaluate;
4922{
4923 char_u *p;
4924 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 int extra = 0;
4926
4927 /*
4928 * Find the end of the string, skipping backslashed characters.
4929 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004930 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
4932 if (*p == '\\' && p[1] != NUL)
4933 {
4934 ++p;
4935 /* A "\<x>" form occupies at least 4 characters, and produces up
4936 * to 6 characters: reserve space for 2 extra */
4937 if (*p == '<')
4938 extra += 2;
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941
4942 if (*p != '"')
4943 {
4944 EMSG2(_("E114: Missing quote: %s"), *arg);
4945 return FAIL;
4946 }
4947
4948 /* If only parsing, set *arg and return here */
4949 if (!evaluate)
4950 {
4951 *arg = p + 1;
4952 return OK;
4953 }
4954
4955 /*
4956 * Copy the string into allocated memory, handling backslashed
4957 * characters.
4958 */
4959 name = alloc((unsigned)(p - *arg + extra));
4960 if (name == NULL)
4961 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 rettv->v_type = VAR_STRING;
4963 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 if (*p == '\\')
4968 {
4969 switch (*++p)
4970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 case 'b': *name++ = BS; ++p; break;
4972 case 'e': *name++ = ESC; ++p; break;
4973 case 'f': *name++ = FF; ++p; break;
4974 case 'n': *name++ = NL; ++p; break;
4975 case 'r': *name++ = CAR; ++p; break;
4976 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 case 'X': /* hex: "\x1", "\x12" */
4979 case 'x':
4980 case 'u': /* Unicode: "\u0023" */
4981 case 'U':
4982 if (vim_isxdigit(p[1]))
4983 {
4984 int n, nr;
4985 int c = toupper(*p);
4986
4987 if (c == 'X')
4988 n = 2;
4989 else
4990 n = 4;
4991 nr = 0;
4992 while (--n >= 0 && vim_isxdigit(p[1]))
4993 {
4994 ++p;
4995 nr = (nr << 4) + hex2nr(*p);
4996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#ifdef FEAT_MBYTE
4999 /* For "\u" store the number according to
5000 * 'encoding'. */
5001 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
5004#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 break;
5008
5009 /* octal: "\1", "\12", "\123" */
5010 case '0':
5011 case '1':
5012 case '2':
5013 case '3':
5014 case '4':
5015 case '5':
5016 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 case '7': *name = *p++ - '0';
5018 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 *name = (*name << 3) + *p++ - '0';
5021 if (*p >= '0' && *p <= '7')
5022 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026
5027 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (extra != 0)
5030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 break;
5033 }
5034 /* FALLTHROUGH */
5035
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038 }
5039 }
5040 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *arg = p + 1;
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 return OK;
5048}
5049
5050/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 * Return OK or FAIL.
5053 */
5054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 int evaluate;
5059{
5060 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 int reduce = 0;
5063
5064 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 */
5067 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 break;
5073 ++reduce;
5074 ++p;
5075 }
5076 }
5077
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 return FAIL;
5082 }
5083
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005085 if (!evaluate)
5086 {
5087 *arg = p + 1;
5088 return OK;
5089 }
5090
5091 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093 */
5094 str = alloc((unsigned)((p - *arg) - reduce));
5095 if (str == NULL)
5096 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 rettv->v_type = VAR_STRING;
5098 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 break;
5106 ++p;
5107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 *arg = p + 1;
5112
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005113 return OK;
5114}
5115
5116/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 * Allocate a variable for a List and fill it from "*arg".
5118 * Return OK or FAIL.
5119 */
5120 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005123 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124 int evaluate;
5125{
Bram Moolenaar33570922005-01-25 22:26:29 +00005126 list_T *l = NULL;
5127 typval_T tv;
5128 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129
5130 if (evaluate)
5131 {
5132 l = list_alloc();
5133 if (l == NULL)
5134 return FAIL;
5135 }
5136
5137 *arg = skipwhite(*arg + 1);
5138 while (**arg != ']' && **arg != NUL)
5139 {
5140 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5141 goto failret;
5142 if (evaluate)
5143 {
5144 item = listitem_alloc();
5145 if (item != NULL)
5146 {
5147 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005148 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 list_append(l, item);
5150 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005151 else
5152 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 }
5154
5155 if (**arg == ']')
5156 break;
5157 if (**arg != ',')
5158 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 goto failret;
5161 }
5162 *arg = skipwhite(*arg + 1);
5163 }
5164
5165 if (**arg != ']')
5166 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168failret:
5169 if (evaluate)
5170 list_free(l);
5171 return FAIL;
5172 }
5173
5174 *arg = skipwhite(*arg + 1);
5175 if (evaluate)
5176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 rettv->v_type = VAR_LIST;
5178 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 ++l->lv_refcount;
5180 }
5181
5182 return OK;
5183}
5184
5185/*
5186 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005187 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005189 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190list_alloc()
5191{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192 list_T *l;
5193
5194 l = (list_T *)alloc_clear(sizeof(list_T));
5195 if (l != NULL)
5196 {
5197 /* Prepend the list to the list of lists for garbage collection. */
5198 if (first_list != NULL)
5199 first_list->lv_used_prev = l;
5200 l->lv_used_prev = NULL;
5201 l->lv_used_next = first_list;
5202 first_list = l;
5203 }
5204 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205}
5206
5207/*
5208 * Unreference a list: decrement the reference count and free it when it
5209 * becomes zero.
5210 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005211 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5216 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217}
5218
5219/*
5220 * Free a list, including all items it points to.
5221 * Ignores the reference count.
5222 */
5223 static void
5224list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226{
Bram Moolenaar33570922005-01-25 22:26:29 +00005227 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228
Bram Moolenaard9fba312005-06-26 22:34:35 +00005229 /* Avoid that recursive reference to the list frees us again. */
5230 l->lv_refcount = DEL_REFCOUNT;
5231
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232 /* Remove the list from the list of lists for garbage collection. */
5233 if (l->lv_used_prev == NULL)
5234 first_list = l->lv_used_next;
5235 else
5236 l->lv_used_prev->lv_used_next = l->lv_used_next;
5237 if (l->lv_used_next != NULL)
5238 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5239
Bram Moolenaard9fba312005-06-26 22:34:35 +00005240 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005242 /* Remove the item before deleting it. */
5243 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 listitem_free(item);
5245 }
5246 vim_free(l);
5247}
5248
5249/*
5250 * Allocate a list item.
5251 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005252 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253listitem_alloc()
5254{
Bram Moolenaar33570922005-01-25 22:26:29 +00005255 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256}
5257
5258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005259 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 */
5261 static void
5262listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005265 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 vim_free(item);
5267}
5268
5269/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005270 * Remove a list item from a List and free it. Also clears the value.
5271 */
5272 static void
5273listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 list_T *l;
5275 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005276{
5277 list_remove(l, item, item);
5278 listitem_free(item);
5279}
5280
5281/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 * Get the number of items in a list.
5283 */
5284 static long
5285list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005286 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 if (l == NULL)
5289 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005290 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291}
5292
5293/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005294 * Return TRUE when two lists have exactly the same values.
5295 */
5296 static int
5297list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005298 list_T *l1;
5299 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 int ic; /* ignore case for strings */
5301{
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005304 if (list_len(l1) != list_len(l2))
5305 return FALSE;
5306
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005307 for (item1 = l1->lv_first, item2 = l2->lv_first;
5308 item1 != NULL && item2 != NULL;
5309 item1 = item1->li_next, item2 = item2->li_next)
5310 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5311 return FALSE;
5312 return item1 == NULL && item2 == NULL;
5313}
5314
5315/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 * Return TRUE when two dictionaries have exactly the same key/values.
5317 */
5318 static int
5319dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005320 dict_T *d1;
5321 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005322 int ic; /* ignore case for strings */
5323{
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 hashitem_T *hi;
5325 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005326 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005327
5328 if (dict_len(d1) != dict_len(d2))
5329 return FALSE;
5330
Bram Moolenaar33570922005-01-25 22:26:29 +00005331 todo = d1->dv_hashtab.ht_used;
5332 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005334 if (!HASHITEM_EMPTY(hi))
5335 {
5336 item2 = dict_find(d2, hi->hi_key, -1);
5337 if (item2 == NULL)
5338 return FALSE;
5339 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5340 return FALSE;
5341 --todo;
5342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005343 }
5344 return TRUE;
5345}
5346
5347/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005349 * Compares the items just like "==" would compare them, but strings and
5350 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005351 */
5352 static int
5353tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T *tv1;
5355 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005356 int ic; /* ignore case */
5357{
5358 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005360
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005361 if (tv1->v_type != tv2->v_type)
5362 return FALSE;
5363
5364 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005365 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005366 case VAR_LIST:
5367 /* recursive! */
5368 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5369
5370 case VAR_DICT:
5371 /* recursive! */
5372 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5373
5374 case VAR_FUNC:
5375 return (tv1->vval.v_string != NULL
5376 && tv2->vval.v_string != NULL
5377 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5378
5379 case VAR_NUMBER:
5380 return tv1->vval.v_number == tv2->vval.v_number;
5381
5382 case VAR_STRING:
5383 s1 = get_tv_string_buf(tv1, buf1);
5384 s2 = get_tv_string_buf(tv2, buf2);
5385 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005387
5388 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005389 return TRUE;
5390}
5391
5392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 * Locate item with index "n" in list "l" and return it.
5394 * A negative index is counted from the end; -1 is the last item.
5395 * Returns NULL when "n" is out of range.
5396 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 long n;
5401{
Bram Moolenaar33570922005-01-25 22:26:29 +00005402 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 long idx;
5404
5405 if (l == NULL)
5406 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005407
5408 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005410 n = l->lv_len + n;
5411
5412 /* Check for index out of range. */
5413 if (n < 0 || n >= l->lv_len)
5414 return NULL;
5415
5416 /* When there is a cached index may start search from there. */
5417 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005419 if (n < l->lv_idx / 2)
5420 {
5421 /* closest to the start of the list */
5422 item = l->lv_first;
5423 idx = 0;
5424 }
5425 else if (n > (l->lv_idx + l->lv_len) / 2)
5426 {
5427 /* closest to the end of the list */
5428 item = l->lv_last;
5429 idx = l->lv_len - 1;
5430 }
5431 else
5432 {
5433 /* closest to the cached index */
5434 item = l->lv_idx_item;
5435 idx = l->lv_idx;
5436 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
5438 else
5439 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 if (n < l->lv_len / 2)
5441 {
5442 /* closest to the start of the list */
5443 item = l->lv_first;
5444 idx = 0;
5445 }
5446 else
5447 {
5448 /* closest to the end of the list */
5449 item = l->lv_last;
5450 idx = l->lv_len - 1;
5451 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005453
5454 while (n > idx)
5455 {
5456 /* search forward */
5457 item = item->li_next;
5458 ++idx;
5459 }
5460 while (n < idx)
5461 {
5462 /* search backward */
5463 item = item->li_prev;
5464 --idx;
5465 }
5466
5467 /* cache the used index */
5468 l->lv_idx = idx;
5469 l->lv_idx_item = item;
5470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 return item;
5472}
5473
5474/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005475 * Locate "item" list "l" and return its index.
5476 * Returns -1 when "item" is not in the list.
5477 */
5478 static long
5479list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 list_T *l;
5481 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005482{
5483 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005484 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005485
5486 if (l == NULL)
5487 return -1;
5488 idx = 0;
5489 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5490 ++idx;
5491 if (li == NULL)
5492 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005493 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005494}
5495
5496/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 * Append item "item" to the end of list "l".
5498 */
5499 static void
5500list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005501 list_T *l;
5502 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503{
5504 if (l->lv_last == NULL)
5505 {
5506 /* empty list */
5507 l->lv_first = item;
5508 l->lv_last = item;
5509 item->li_prev = NULL;
5510 }
5511 else
5512 {
5513 l->lv_last->li_next = item;
5514 item->li_prev = l->lv_last;
5515 l->lv_last = item;
5516 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005517 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 item->li_next = NULL;
5519}
5520
5521/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 */
5525 static int
5526list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005527 list_T *l;
5528 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531
Bram Moolenaar05159a02005-02-26 23:04:13 +00005532 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005534 copy_tv(tv, &li->li_tv);
5535 list_append(l, li);
5536 return OK;
5537}
5538
5539/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005540 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005541 * Return FAIL when out of memory.
5542 */
5543 int
5544list_append_dict(list, dict)
5545 list_T *list;
5546 dict_T *dict;
5547{
5548 listitem_T *li = listitem_alloc();
5549
5550 if (li == NULL)
5551 return FAIL;
5552 li->li_tv.v_type = VAR_DICT;
5553 li->li_tv.v_lock = 0;
5554 li->li_tv.vval.v_dict = dict;
5555 list_append(list, li);
5556 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return OK;
5558}
5559
5560/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005561 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005562 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005563 * Returns FAIL when out of memory.
5564 */
5565 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005566list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005567 list_T *l;
5568 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005569 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005570{
5571 listitem_T *li = listitem_alloc();
5572
5573 if (li == NULL)
5574 return FAIL;
5575 list_append(l, li);
5576 li->li_tv.v_type = VAR_STRING;
5577 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005578 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5579 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005580 return FAIL;
5581 return OK;
5582}
5583
5584/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005585 * Append "n" to list "l".
5586 * Returns FAIL when out of memory.
5587 */
5588 static int
5589list_append_number(l, n)
5590 list_T *l;
5591 varnumber_T n;
5592{
5593 listitem_T *li;
5594
5595 li = listitem_alloc();
5596 if (li == NULL)
5597 return FAIL;
5598 li->li_tv.v_type = VAR_NUMBER;
5599 li->li_tv.v_lock = 0;
5600 li->li_tv.vval.v_number = n;
5601 list_append(l, li);
5602 return OK;
5603}
5604
5605/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005606 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005607 * If "item" is NULL append at the end.
5608 * Return FAIL when out of memory.
5609 */
5610 static int
5611list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005612 list_T *l;
5613 typval_T *tv;
5614 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615{
Bram Moolenaar33570922005-01-25 22:26:29 +00005616 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005617
5618 if (ni == NULL)
5619 return FAIL;
5620 copy_tv(tv, &ni->li_tv);
5621 if (item == NULL)
5622 /* Append new item at end of list. */
5623 list_append(l, ni);
5624 else
5625 {
5626 /* Insert new item before existing item. */
5627 ni->li_prev = item->li_prev;
5628 ni->li_next = item;
5629 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005630 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005631 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632 ++l->lv_idx;
5633 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005634 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005635 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005636 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 l->lv_idx_item = NULL;
5638 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005639 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005640 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005641 }
5642 return OK;
5643}
5644
5645/*
5646 * Extend "l1" with "l2".
5647 * If "bef" is NULL append at the end, otherwise insert before this item.
5648 * Returns FAIL when out of memory.
5649 */
5650 static int
5651list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005652 list_T *l1;
5653 list_T *l2;
5654 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005655{
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657
5658 for (item = l2->lv_first; item != NULL; item = item->li_next)
5659 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5660 return FAIL;
5661 return OK;
5662}
5663
5664/*
5665 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5666 * Return FAIL when out of memory.
5667 */
5668 static int
5669list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005670 list_T *l1;
5671 list_T *l2;
5672 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005673{
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005675
5676 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005677 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005678 if (l == NULL)
5679 return FAIL;
5680 tv->v_type = VAR_LIST;
5681 tv->vval.v_list = l;
5682
5683 /* append all items from the second list */
5684 return list_extend(l, l2, NULL);
5685}
5686
5687/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005688 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005690 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 * Returns NULL when out of memory.
5692 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005694list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005697 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698{
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 list_T *copy;
5700 listitem_T *item;
5701 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702
5703 if (orig == NULL)
5704 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705
5706 copy = list_alloc();
5707 if (copy != NULL)
5708 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005709 if (copyID != 0)
5710 {
5711 /* Do this before adding the items, because one of the items may
5712 * refer back to this list. */
5713 orig->lv_copyID = copyID;
5714 orig->lv_copylist = copy;
5715 }
5716 for (item = orig->lv_first; item != NULL && !got_int;
5717 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 {
5719 ni = listitem_alloc();
5720 if (ni == NULL)
5721 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005722 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005723 {
5724 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5725 {
5726 vim_free(ni);
5727 break;
5728 }
5729 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 list_append(copy, ni);
5733 }
5734 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005735 if (item != NULL)
5736 {
5737 list_unref(copy);
5738 copy = NULL;
5739 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740 }
5741
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 return copy;
5743}
5744
5745/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005746 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005747 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005749 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005750list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 list_T *l;
5752 listitem_T *item;
5753 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005754{
Bram Moolenaar33570922005-01-25 22:26:29 +00005755 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005757 /* notify watchers */
5758 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005760 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005761 list_fix_watch(l, ip);
5762 if (ip == item2)
5763 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005765
5766 if (item2->li_next == NULL)
5767 l->lv_last = item->li_prev;
5768 else
5769 item2->li_next->li_prev = item->li_prev;
5770 if (item->li_prev == NULL)
5771 l->lv_first = item2->li_next;
5772 else
5773 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005774 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775}
5776
5777/*
5778 * Return an allocated string with the string representation of a list.
5779 * May return NULL.
5780 */
5781 static char_u *
5782list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005783 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784{
5785 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786
5787 if (tv->vval.v_list == NULL)
5788 return NULL;
5789 ga_init2(&ga, (int)sizeof(char), 80);
5790 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005791 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5792 {
5793 vim_free(ga.ga_data);
5794 return NULL;
5795 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 ga_append(&ga, ']');
5797 ga_append(&ga, NUL);
5798 return (char_u *)ga.ga_data;
5799}
5800
5801/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 * Join list "l" into a string in "*gap", using separator "sep".
5803 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005804 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005806 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807list_join(gap, l, sep, echo)
5808 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810 char_u *sep;
5811 int echo;
5812{
5813 int first = TRUE;
5814 char_u *tofree;
5815 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005816 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 char_u *s;
5818
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005819 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 {
5821 if (first)
5822 first = FALSE;
5823 else
5824 ga_concat(gap, sep);
5825
5826 if (echo)
5827 s = echo_string(&item->li_tv, &tofree, numbuf);
5828 else
5829 s = tv2string(&item->li_tv, &tofree, numbuf);
5830 if (s != NULL)
5831 ga_concat(gap, s);
5832 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005833 if (s == NULL)
5834 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005836 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837}
5838
5839/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005840 * Garbage collection for lists and dictionaries.
5841 *
5842 * We use reference counts to be able to free most items right away when they
5843 * are no longer used. But for composite items it's possible that it becomes
5844 * unused while the reference count is > 0: When there is a recursive
5845 * reference. Example:
5846 * :let l = [1, 2, 3]
5847 * :let d = {9: l}
5848 * :let l[1] = d
5849 *
5850 * Since this is quite unusual we handle this with garbage collection: every
5851 * once in a while find out which lists and dicts are not referenced from any
5852 * variable.
5853 *
5854 * Here is a good reference text about garbage collection (refers to Python
5855 * but it applies to all reference-counting mechanisms):
5856 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005857 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005858
5859/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005860 * Do garbage collection for lists and dicts.
5861 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005862 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005863 int
5864garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005865{
5866 dict_T *dd;
5867 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005868 int copyID = ++current_copyID;
5869 buf_T *buf;
5870 win_T *wp;
5871 int i;
5872 funccall_T *fc;
5873 int did_free = FALSE;
5874
5875 /*
5876 * 1. Go through all accessible variables and mark all lists and dicts
5877 * with copyID.
5878 */
5879 /* script-local variables */
5880 for (i = 1; i <= ga_scripts.ga_len; ++i)
5881 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5882
5883 /* buffer-local variables */
5884 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5885 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5886
5887 /* window-local variables */
5888 FOR_ALL_WINDOWS(wp)
5889 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5890
5891 /* global variables */
5892 set_ref_in_ht(&globvarht, copyID);
5893
5894 /* function-local variables */
5895 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5896 {
5897 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5898 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5899 }
5900
5901 /*
5902 * 2. Go through the list of dicts and free items without the copyID.
5903 */
5904 for (dd = first_dict; dd != NULL; )
5905 if (dd->dv_copyID != copyID)
5906 {
5907 dict_free(dd);
5908 did_free = TRUE;
5909
5910 /* restart, next dict may also have been freed */
5911 dd = first_dict;
5912 }
5913 else
5914 dd = dd->dv_used_next;
5915
5916 /*
5917 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005918 * But don't free a list that has a watcher (used in a for loop), these
5919 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005920 */
5921 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005922 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005923 {
5924 list_free(ll);
5925 did_free = TRUE;
5926
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005927 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005928 ll = first_list;
5929 }
5930 else
5931 ll = ll->lv_used_next;
5932
5933 return did_free;
5934}
5935
5936/*
5937 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5938 */
5939 static void
5940set_ref_in_ht(ht, copyID)
5941 hashtab_T *ht;
5942 int copyID;
5943{
5944 int todo;
5945 hashitem_T *hi;
5946
5947 todo = ht->ht_used;
5948 for (hi = ht->ht_array; todo > 0; ++hi)
5949 if (!HASHITEM_EMPTY(hi))
5950 {
5951 --todo;
5952 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5953 }
5954}
5955
5956/*
5957 * Mark all lists and dicts referenced through list "l" with "copyID".
5958 */
5959 static void
5960set_ref_in_list(l, copyID)
5961 list_T *l;
5962 int copyID;
5963{
5964 listitem_T *li;
5965
5966 for (li = l->lv_first; li != NULL; li = li->li_next)
5967 set_ref_in_item(&li->li_tv, copyID);
5968}
5969
5970/*
5971 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5972 */
5973 static void
5974set_ref_in_item(tv, copyID)
5975 typval_T *tv;
5976 int copyID;
5977{
5978 dict_T *dd;
5979 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005980
5981 switch (tv->v_type)
5982 {
5983 case VAR_DICT:
5984 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005985 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005986 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 /* Didn't see this dict yet. */
5988 dd->dv_copyID = copyID;
5989 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005990 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005991 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005992
5993 case VAR_LIST:
5994 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005995 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005996 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005997 /* Didn't see this list yet. */
5998 ll->lv_copyID = copyID;
5999 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006000 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006001 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006002 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006003 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006004}
6005
6006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006007 * Allocate an empty header for a dictionary.
6008 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006009 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006010dict_alloc()
6011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006013
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006016 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017 /* Add the list to the hashtable for garbage collection. */
6018 if (first_dict != NULL)
6019 first_dict->dv_used_prev = d;
6020 d->dv_used_next = first_dict;
6021 d->dv_used_prev = NULL;
6022
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006024 d->dv_lock = 0;
6025 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006026 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006027 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006028 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006029}
6030
6031/*
6032 * Unreference a Dictionary: decrement the reference count and free it when it
6033 * becomes zero.
6034 */
6035 static void
6036dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6040 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006041}
6042
6043/*
6044 * Free a Dictionary, including all items it contains.
6045 * Ignores the reference count.
6046 */
6047 static void
6048dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006050{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006053 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006054
Bram Moolenaard9fba312005-06-26 22:34:35 +00006055 /* Avoid that recursive reference to the dict frees us again. */
6056 d->dv_refcount = DEL_REFCOUNT;
6057
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006058 /* Remove the dict from the list of dicts for garbage collection. */
6059 if (d->dv_used_prev == NULL)
6060 first_dict = d->dv_used_next;
6061 else
6062 d->dv_used_prev->dv_used_next = d->dv_used_next;
6063 if (d->dv_used_next != NULL)
6064 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6065
6066 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 todo = d->dv_hashtab.ht_used;
6069 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (!HASHITEM_EMPTY(hi))
6072 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006073 /* Remove the item before deleting it, just in case there is
6074 * something recursive causing trouble. */
6075 di = HI2DI(hi);
6076 hash_remove(&d->dv_hashtab, hi);
6077 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006078 --todo;
6079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006080 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006082 vim_free(d);
6083}
6084
6085/*
6086 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 * The "key" is copied to the new item.
6088 * Note that the value of the item "di_tv" still needs to be initialized!
6089 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006090 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092dictitem_alloc(key)
6093 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006094{
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006096
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006098 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006101 di->di_flags = 0;
6102 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006103 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006104}
6105
6106/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107 * Make a copy of a Dictionary item.
6108 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006110dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112{
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006114
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006116 if (di != NULL)
6117 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006118 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006119 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006120 copy_tv(&org->di_tv, &di->di_tv);
6121 }
6122 return di;
6123}
6124
6125/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006126 * Remove item "item" from Dictionary "dict" and free it.
6127 */
6128 static void
6129dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 dict_T *dict;
6131 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006134
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006136 if (HASHITEM_EMPTY(hi))
6137 EMSG2(_(e_intern2), "dictitem_remove()");
6138 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 dictitem_free(item);
6141}
6142
6143/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006144 * Free a dict item. Also clears the value.
6145 */
6146 static void
6147dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006149{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006150 clear_tv(&item->di_tv);
6151 vim_free(item);
6152}
6153
6154/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006155 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6156 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006157 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006158 * Returns NULL when out of memory.
6159 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006161dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006163 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006164 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006165{
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 dict_T *copy;
6167 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170
6171 if (orig == NULL)
6172 return NULL;
6173
6174 copy = dict_alloc();
6175 if (copy != NULL)
6176 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006177 if (copyID != 0)
6178 {
6179 orig->dv_copyID = copyID;
6180 orig->dv_copydict = copy;
6181 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006183 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006185 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006187 --todo;
6188
6189 di = dictitem_alloc(hi->hi_key);
6190 if (di == NULL)
6191 break;
6192 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006193 {
6194 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6195 copyID) == FAIL)
6196 {
6197 vim_free(di);
6198 break;
6199 }
6200 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006201 else
6202 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6203 if (dict_add(copy, di) == FAIL)
6204 {
6205 dictitem_free(di);
6206 break;
6207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006210
Bram Moolenaare9a41262005-01-15 22:18:47 +00006211 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006212 if (todo > 0)
6213 {
6214 dict_unref(copy);
6215 copy = NULL;
6216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006217 }
6218
6219 return copy;
6220}
6221
6222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006225 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006226 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006227dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 dict_T *d;
6229 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006230{
Bram Moolenaar33570922005-01-25 22:26:29 +00006231 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006232}
6233
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006235 * Add a number or string entry to dictionary "d".
6236 * When "str" is NULL use number "nr", otherwise use "str".
6237 * Returns FAIL when out of memory and when key already exists.
6238 */
6239 int
6240dict_add_nr_str(d, key, nr, str)
6241 dict_T *d;
6242 char *key;
6243 long nr;
6244 char_u *str;
6245{
6246 dictitem_T *item;
6247
6248 item = dictitem_alloc((char_u *)key);
6249 if (item == NULL)
6250 return FAIL;
6251 item->di_tv.v_lock = 0;
6252 if (str == NULL)
6253 {
6254 item->di_tv.v_type = VAR_NUMBER;
6255 item->di_tv.vval.v_number = nr;
6256 }
6257 else
6258 {
6259 item->di_tv.v_type = VAR_STRING;
6260 item->di_tv.vval.v_string = vim_strsave(str);
6261 }
6262 if (dict_add(d, item) == FAIL)
6263 {
6264 dictitem_free(item);
6265 return FAIL;
6266 }
6267 return OK;
6268}
6269
6270/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006271 * Get the number of items in a Dictionary.
6272 */
6273 static long
6274dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006276{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277 if (d == NULL)
6278 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006280}
6281
6282/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283 * Find item "key[len]" in Dictionary "d".
6284 * If "len" is negative use strlen(key).
6285 * Returns NULL when not found.
6286 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290 char_u *key;
6291 int len;
6292{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006293#define AKEYLEN 200
6294 char_u buf[AKEYLEN];
6295 char_u *akey;
6296 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006298
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 if (len < 0)
6300 akey = key;
6301 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 tofree = akey = vim_strnsave(key, len);
6304 if (akey == NULL)
6305 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006307 else
6308 {
6309 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006310 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311 akey = buf;
6312 }
6313
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006315 vim_free(tofree);
6316 if (HASHITEM_EMPTY(hi))
6317 return NULL;
6318 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319}
6320
6321/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006322 * Get a string item from a dictionary in allocated memory.
6323 * Returns NULL if the entry doesn't exist or out of memory.
6324 */
6325 char_u *
6326get_dict_string(d, key)
6327 dict_T *d;
6328 char_u *key;
6329{
6330 dictitem_T *di;
6331
6332 di = dict_find(d, key, -1);
6333 if (di == NULL)
6334 return NULL;
6335 return vim_strsave(get_tv_string(&di->di_tv));
6336}
6337
6338/*
6339 * Get a number item from a dictionary.
6340 * Returns 0 if the entry doesn't exist or out of memory.
6341 */
6342 long
6343get_dict_number(d, key)
6344 dict_T *d;
6345 char_u *key;
6346{
6347 dictitem_T *di;
6348
6349 di = dict_find(d, key, -1);
6350 if (di == NULL)
6351 return 0;
6352 return get_tv_number(&di->di_tv);
6353}
6354
6355/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356 * Return an allocated string with the string representation of a Dictionary.
6357 * May return NULL.
6358 */
6359 static char_u *
6360dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006362{
6363 garray_T ga;
6364 int first = TRUE;
6365 char_u *tofree;
6366 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 return NULL;
6374 ga_init2(&ga, (int)sizeof(char), 80);
6375 ga_append(&ga, '{');
6376
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006382 --todo;
6383
6384 if (first)
6385 first = FALSE;
6386 else
6387 ga_concat(&ga, (char_u *)", ");
6388
6389 tofree = string_quote(hi->hi_key, FALSE);
6390 if (tofree != NULL)
6391 {
6392 ga_concat(&ga, tofree);
6393 vim_free(tofree);
6394 }
6395 ga_concat(&ga, (char_u *)": ");
6396 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6397 if (s != NULL)
6398 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 if (s == NULL)
6401 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006403 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006404 if (todo > 0)
6405 {
6406 vim_free(ga.ga_data);
6407 return NULL;
6408 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409
6410 ga_append(&ga, '}');
6411 ga_append(&ga, NUL);
6412 return (char_u *)ga.ga_data;
6413}
6414
6415/*
6416 * Allocate a variable for a Dictionary and fill it from "*arg".
6417 * Return OK or FAIL. Returns NOTDONE for {expr}.
6418 */
6419 static int
6420get_dict_tv(arg, rettv, evaluate)
6421 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423 int evaluate;
6424{
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 dict_T *d = NULL;
6426 typval_T tvkey;
6427 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006431 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432
6433 /*
6434 * First check if it's not a curly-braces thing: {expr}.
6435 * Must do this without evaluating, otherwise a function may be called
6436 * twice. Unfortunately this means we need to call eval1() twice for the
6437 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006440 if (*start != '}')
6441 {
6442 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6443 return FAIL;
6444 if (*start == '}')
6445 return NOTDONE;
6446 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006447
6448 if (evaluate)
6449 {
6450 d = dict_alloc();
6451 if (d == NULL)
6452 return FAIL;
6453 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006454 tvkey.v_type = VAR_UNKNOWN;
6455 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456
6457 *arg = skipwhite(*arg + 1);
6458 while (**arg != '}' && **arg != NUL)
6459 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006460 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461 goto failret;
6462 if (**arg != ':')
6463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006464 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006465 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466 goto failret;
6467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006468 key = get_tv_string_buf_chk(&tvkey, buf);
6469 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006471 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6472 if (key != NULL)
6473 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006474 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006475 goto failret;
6476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477
6478 *arg = skipwhite(*arg + 1);
6479 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006481 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482 goto failret;
6483 }
6484 if (evaluate)
6485 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006486 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 if (item != NULL)
6488 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006489 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491 clear_tv(&tv);
6492 goto failret;
6493 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006494 item = dictitem_alloc(key);
6495 clear_tv(&tvkey);
6496 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006499 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 if (dict_add(d, item) == FAIL)
6501 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502 }
6503 }
6504
6505 if (**arg == '}')
6506 break;
6507 if (**arg != ',')
6508 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006509 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510 goto failret;
6511 }
6512 *arg = skipwhite(*arg + 1);
6513 }
6514
6515 if (**arg != '}')
6516 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006517 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006518failret:
6519 if (evaluate)
6520 dict_free(d);
6521 return FAIL;
6522 }
6523
6524 *arg = skipwhite(*arg + 1);
6525 if (evaluate)
6526 {
6527 rettv->v_type = VAR_DICT;
6528 rettv->vval.v_dict = d;
6529 ++d->dv_refcount;
6530 }
6531
6532 return OK;
6533}
6534
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 * Return a string with the string representation of a variable.
6537 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006539 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 * May return NULL;
6541 */
6542 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006545 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 static int recurse = 0;
6549 char_u *r = NULL;
6550
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006553 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 *tofree = NULL;
6555 return NULL;
6556 }
6557 ++recurse;
6558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 switch (tv->v_type)
6560 {
6561 case VAR_FUNC:
6562 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 r = tv->vval.v_string;
6564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 case VAR_LIST:
6566 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006567 r = *tofree;
6568 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006569 case VAR_DICT:
6570 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 r = *tofree;
6572 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006573 case VAR_STRING:
6574 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 *tofree = NULL;
6576 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006579 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006582
6583 --recurse;
6584 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006585}
6586
6587/*
6588 * Return a string with the string representation of a variable.
6589 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6590 * "numbuf" is used for a number.
6591 * Puts quotes around strings, so that they can be parsed back by eval().
6592 * May return NULL;
6593 */
6594 static char_u *
6595tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006597 char_u **tofree;
6598 char_u *numbuf;
6599{
6600 switch (tv->v_type)
6601 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006602 case VAR_FUNC:
6603 *tofree = string_quote(tv->vval.v_string, TRUE);
6604 return *tofree;
6605 case VAR_STRING:
6606 *tofree = string_quote(tv->vval.v_string, FALSE);
6607 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006609 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006612 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006613 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616}
6617
6618/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 * Return string "str" in ' quotes, doubling ' characters.
6620 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006621 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006622 */
6623 static char_u *
6624string_quote(str, function)
6625 char_u *str;
6626 int function;
6627{
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006629 char_u *p, *r, *s;
6630
Bram Moolenaar33570922005-01-25 22:26:29 +00006631 len = (function ? 13 : 3);
6632 if (str != NULL)
6633 {
6634 len += STRLEN(str);
6635 for (p = str; *p != NUL; mb_ptr_adv(p))
6636 if (*p == '\'')
6637 ++len;
6638 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006639 s = r = alloc(len);
6640 if (r != NULL)
6641 {
6642 if (function)
6643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006645 r += 10;
6646 }
6647 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006648 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 if (str != NULL)
6650 for (p = str; *p != NUL; )
6651 {
6652 if (*p == '\'')
6653 *r++ = '\'';
6654 MB_COPY_CHAR(p, r);
6655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006657 if (function)
6658 *r++ = ')';
6659 *r++ = NUL;
6660 }
6661 return s;
6662}
6663
6664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 * Get the value of an environment variable.
6666 * "arg" is pointing to the '$'. It is advanced to after the name.
6667 * If the environment variable was not set, silently assume it is empty.
6668 * Always return OK.
6669 */
6670 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006671get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 int evaluate;
6675{
6676 char_u *string = NULL;
6677 int len;
6678 int cc;
6679 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006680 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
6682 ++*arg;
6683 name = *arg;
6684 len = get_env_len(arg);
6685 if (evaluate)
6686 {
6687 if (len != 0)
6688 {
6689 cc = name[len];
6690 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006691 /* first try vim_getenv(), fast for normal environment vars */
6692 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006694 {
6695 if (!mustfree)
6696 string = vim_strsave(string);
6697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 else
6699 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006700 if (mustfree)
6701 vim_free(string);
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 /* next try expanding things like $VIM and ${HOME} */
6704 string = expand_env_save(name - 1);
6705 if (string != NULL && *string == '$')
6706 {
6707 vim_free(string);
6708 string = NULL;
6709 }
6710 }
6711 name[len] = cc;
6712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713 rettv->v_type = VAR_STRING;
6714 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716
6717 return OK;
6718}
6719
6720/*
6721 * Array with names and number of arguments of all internal functions
6722 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6723 */
6724static struct fst
6725{
6726 char *f_name; /* function name */
6727 char f_min_argc; /* minimal number of arguments */
6728 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006729 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731} functions[] =
6732{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006733 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {"append", 2, 2, f_append},
6735 {"argc", 0, 0, f_argc},
6736 {"argidx", 0, 0, f_argidx},
6737 {"argv", 1, 1, f_argv},
6738 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006739 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {"bufexists", 1, 1, f_bufexists},
6741 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6742 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6743 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6744 {"buflisted", 1, 1, f_buflisted},
6745 {"bufloaded", 1, 1, f_bufloaded},
6746 {"bufname", 1, 1, f_bufname},
6747 {"bufnr", 1, 1, f_bufnr},
6748 {"bufwinnr", 1, 1, f_bufwinnr},
6749 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006750 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006751 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {"char2nr", 1, 1, f_char2nr},
6753 {"cindent", 1, 1, f_cindent},
6754 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006755#if defined(FEAT_INS_EXPAND)
6756 {"complete_add", 1, 1, f_complete_add},
6757 {"complete_check", 0, 0, f_complete_check},
6758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006760 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {"cscope_connection",0,3, f_cscope_connection},
6763 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {"delete", 1, 1, f_delete},
6766 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006767 {"diff_filler", 1, 1, f_diff_filler},
6768 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006769 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {"eventhandler", 0, 0, f_eventhandler},
6773 {"executable", 1, 1, f_executable},
6774 {"exists", 1, 1, f_exists},
6775 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006776 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6778 {"filereadable", 1, 1, f_filereadable},
6779 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006781 {"finddir", 1, 3, f_finddir},
6782 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"fnamemodify", 2, 2, f_fnamemodify},
6784 {"foldclosed", 1, 1, f_foldclosed},
6785 {"foldclosedend", 1, 1, f_foldclosedend},
6786 {"foldlevel", 1, 1, f_foldlevel},
6787 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006788 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006790 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006792 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006793 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {"getbufvar", 2, 2, f_getbufvar},
6795 {"getchar", 0, 1, f_getchar},
6796 {"getcharmod", 0, 0, f_getcharmod},
6797 {"getcmdline", 0, 0, f_getcmdline},
6798 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006799 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006801 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006802 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {"getfsize", 1, 1, f_getfsize},
6804 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006805 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006806 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006807 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006808 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {"getregtype", 0, 1, f_getregtype},
6810 {"getwinposx", 0, 0, f_getwinposx},
6811 {"getwinposy", 0, 0, f_getwinposy},
6812 {"getwinvar", 2, 2, f_getwinvar},
6813 {"glob", 1, 1, f_glob},
6814 {"globpath", 2, 2, f_globpath},
6815 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006816 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 {"hasmapto", 1, 2, f_hasmapto},
6818 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6819 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6820 {"histadd", 2, 2, f_histadd},
6821 {"histdel", 1, 2, f_histdel},
6822 {"histget", 1, 2, f_histget},
6823 {"histnr", 1, 1, f_histnr},
6824 {"hlID", 1, 1, f_hlID},
6825 {"hlexists", 1, 1, f_hlexists},
6826 {"hostname", 0, 0, f_hostname},
6827 {"iconv", 3, 3, f_iconv},
6828 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006829 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006830 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006832 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {"inputrestore", 0, 0, f_inputrestore},
6834 {"inputsave", 0, 0, f_inputsave},
6835 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006838 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006840 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006841 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006843 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {"libcall", 3, 3, f_libcall},
6845 {"libcallnr", 3, 3, f_libcallnr},
6846 {"line", 1, 1, f_line},
6847 {"line2byte", 1, 1, f_line2byte},
6848 {"lispindent", 1, 1, f_lispindent},
6849 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"maparg", 1, 2, f_maparg},
6852 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006853 {"match", 2, 4, f_match},
6854 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006855 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006856 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006857 {"max", 1, 1, f_max},
6858 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006859#ifdef vim_mkdir
6860 {"mkdir", 1, 3, f_mkdir},
6861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {"mode", 0, 0, f_mode},
6863 {"nextnonblank", 1, 1, f_nextnonblank},
6864 {"nr2char", 1, 1, f_nr2char},
6865 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006866 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006867 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006868 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 {"remote_expr", 2, 3, f_remote_expr},
6870 {"remote_foreground", 1, 1, f_remote_foreground},
6871 {"remote_peek", 1, 2, f_remote_peek},
6872 {"remote_read", 1, 1, f_remote_read},
6873 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006874 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006876 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006878 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006880 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {"searchpair", 3, 5, f_searchpair},
6882 {"server2client", 2, 2, f_server2client},
6883 {"serverlist", 0, 0, f_serverlist},
6884 {"setbufvar", 3, 3, f_setbufvar},
6885 {"setcmdpos", 1, 1, f_setcmdpos},
6886 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006887 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {"setreg", 2, 3, f_setreg},
6889 {"setwinvar", 3, 3, f_setwinvar},
6890 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006891 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006892 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006893 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006894 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006895 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896#ifdef HAVE_STRFTIME
6897 {"strftime", 1, 2, f_strftime},
6898#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006899 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006900 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"strlen", 1, 1, f_strlen},
6902 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006903 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {"strtrans", 1, 1, f_strtrans},
6905 {"submatch", 1, 1, f_submatch},
6906 {"substitute", 4, 4, f_substitute},
6907 {"synID", 3, 3, f_synID},
6908 {"synIDattr", 2, 3, f_synIDattr},
6909 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006910 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006911 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006912 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006914 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {"tolower", 1, 1, f_tolower},
6916 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006917 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006919 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {"virtcol", 1, 1, f_virtcol},
6921 {"visualmode", 0, 1, f_visualmode},
6922 {"winbufnr", 1, 1, f_winbufnr},
6923 {"wincol", 0, 0, f_wincol},
6924 {"winheight", 1, 1, f_winheight},
6925 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006926 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {"winrestcmd", 0, 0, f_winrestcmd},
6928 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006929 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930};
6931
6932#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6933
6934/*
6935 * Function given to ExpandGeneric() to obtain the list of internal
6936 * or user defined function names.
6937 */
6938 char_u *
6939get_function_name(xp, idx)
6940 expand_T *xp;
6941 int idx;
6942{
6943 static int intidx = -1;
6944 char_u *name;
6945
6946 if (idx == 0)
6947 intidx = -1;
6948 if (intidx < 0)
6949 {
6950 name = get_user_func_name(xp, idx);
6951 if (name != NULL)
6952 return name;
6953 }
6954 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6955 {
6956 STRCPY(IObuff, functions[intidx].f_name);
6957 STRCAT(IObuff, "(");
6958 if (functions[intidx].f_max_argc == 0)
6959 STRCAT(IObuff, ")");
6960 return IObuff;
6961 }
6962
6963 return NULL;
6964}
6965
6966/*
6967 * Function given to ExpandGeneric() to obtain the list of internal or
6968 * user defined variable or function names.
6969 */
6970/*ARGSUSED*/
6971 char_u *
6972get_expr_name(xp, idx)
6973 expand_T *xp;
6974 int idx;
6975{
6976 static int intidx = -1;
6977 char_u *name;
6978
6979 if (idx == 0)
6980 intidx = -1;
6981 if (intidx < 0)
6982 {
6983 name = get_function_name(xp, idx);
6984 if (name != NULL)
6985 return name;
6986 }
6987 return get_user_var_name(xp, ++intidx);
6988}
6989
6990#endif /* FEAT_CMDL_COMPL */
6991
6992/*
6993 * Find internal function in table above.
6994 * Return index, or -1 if not found
6995 */
6996 static int
6997find_internal_func(name)
6998 char_u *name; /* name of the function */
6999{
7000 int first = 0;
7001 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7002 int cmp;
7003 int x;
7004
7005 /*
7006 * Find the function name in the table. Binary search.
7007 */
7008 while (first <= last)
7009 {
7010 x = first + ((unsigned)(last - first) >> 1);
7011 cmp = STRCMP(name, functions[x].f_name);
7012 if (cmp < 0)
7013 last = x - 1;
7014 else if (cmp > 0)
7015 first = x + 1;
7016 else
7017 return x;
7018 }
7019 return -1;
7020}
7021
7022/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007023 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7024 * name it contains, otherwise return "name".
7025 */
7026 static char_u *
7027deref_func_name(name, lenp)
7028 char_u *name;
7029 int *lenp;
7030{
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007032 int cc;
7033
7034 cc = name[*lenp];
7035 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007036 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007039 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007040 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007041 {
7042 *lenp = 0;
7043 return (char_u *)""; /* just in case */
7044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 *lenp = STRLEN(v->di_tv.vval.v_string);
7046 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007047 }
7048
7049 return name;
7050}
7051
7052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 * Allocate a variable for the result of a function.
7054 * Return OK or FAIL.
7055 */
7056 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007057get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7058 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 char_u *name; /* name of the function */
7060 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 char_u **arg; /* argument, pointing to the '(' */
7063 linenr_T firstline; /* first line of range */
7064 linenr_T lastline; /* last line of range */
7065 int *doesrange; /* return: function handled range */
7066 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 char_u *argp;
7070 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 int argcount = 0; /* number of arguments found */
7073
7074 /*
7075 * Get the arguments.
7076 */
7077 argp = *arg;
7078 while (argcount < MAX_FUNC_ARGS)
7079 {
7080 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7081 if (*argp == ')' || *argp == ',' || *argp == NUL)
7082 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7084 {
7085 ret = FAIL;
7086 break;
7087 }
7088 ++argcount;
7089 if (*argp != ',')
7090 break;
7091 }
7092 if (*argp == ')')
7093 ++argp;
7094 else
7095 ret = FAIL;
7096
7097 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007098 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 {
7102 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007103 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007105 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007109 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 *arg = skipwhite(argp);
7112 return ret;
7113}
7114
7115
7116/*
7117 * Call a function with its resolved parameters
7118 * Return OK or FAIL.
7119 */
7120 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007121call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007122 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 char_u *name; /* name of the function */
7124 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 linenr_T firstline; /* first line of range */
7129 linenr_T lastline; /* last line of range */
7130 int *doesrange; /* return: function handled range */
7131 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135#define ERROR_UNKNOWN 0
7136#define ERROR_TOOMANY 1
7137#define ERROR_TOOFEW 2
7138#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007139#define ERROR_DICT 4
7140#define ERROR_NONE 5
7141#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 int error = ERROR_NONE;
7143 int i;
7144 int llen;
7145 ufunc_T *fp;
7146 int cc;
7147#define FLEN_FIXED 40
7148 char_u fname_buf[FLEN_FIXED + 1];
7149 char_u *fname;
7150
7151 /*
7152 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7153 * Change <SNR>123_name() to K_SNR 123_name().
7154 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7155 */
7156 cc = name[len];
7157 name[len] = NUL;
7158 llen = eval_fname_script(name);
7159 if (llen > 0)
7160 {
7161 fname_buf[0] = K_SPECIAL;
7162 fname_buf[1] = KS_EXTRA;
7163 fname_buf[2] = (int)KE_SNR;
7164 i = 3;
7165 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7166 {
7167 if (current_SID <= 0)
7168 error = ERROR_SCRIPT;
7169 else
7170 {
7171 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7172 i = (int)STRLEN(fname_buf);
7173 }
7174 }
7175 if (i + STRLEN(name + llen) < FLEN_FIXED)
7176 {
7177 STRCPY(fname_buf + i, name + llen);
7178 fname = fname_buf;
7179 }
7180 else
7181 {
7182 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7183 if (fname == NULL)
7184 error = ERROR_OTHER;
7185 else
7186 {
7187 mch_memmove(fname, fname_buf, (size_t)i);
7188 STRCPY(fname + i, name + llen);
7189 }
7190 }
7191 }
7192 else
7193 fname = name;
7194
7195 *doesrange = FALSE;
7196
7197
7198 /* execute the function if no errors detected and executing */
7199 if (evaluate && error == ERROR_NONE)
7200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007201 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 error = ERROR_UNKNOWN;
7203
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007204 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
7206 /*
7207 * User defined function.
7208 */
7209 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007210
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007212 /* Trigger FuncUndefined event, may load the function. */
7213 if (fp == NULL
7214 && apply_autocmds(EVENT_FUNCUNDEFINED,
7215 fname, fname, TRUE, NULL)
7216 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007218 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 fp = find_func(fname);
7220 }
7221#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007222 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007223 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007224 {
7225 /* loaded a package, search for the function again */
7226 fp = find_func(fname);
7227 }
7228
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 if (fp != NULL)
7230 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007231 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007233 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007235 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007237 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 else
7240 {
7241 /*
7242 * Call the user function.
7243 * Save and restore search patterns, script variables and
7244 * redo buffer.
7245 */
7246 save_search_patterns();
7247 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007248 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007249 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007250 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007251 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7252 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7253 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007254 /* Function was unreferenced while being used, free it
7255 * now. */
7256 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 restoreRedobuff();
7258 restore_search_patterns();
7259 error = ERROR_NONE;
7260 }
7261 }
7262 }
7263 else
7264 {
7265 /*
7266 * Find the function name in the table, call its implementation.
7267 */
7268 i = find_internal_func(fname);
7269 if (i >= 0)
7270 {
7271 if (argcount < functions[i].f_min_argc)
7272 error = ERROR_TOOFEW;
7273 else if (argcount > functions[i].f_max_argc)
7274 error = ERROR_TOOMANY;
7275 else
7276 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007277 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 error = ERROR_NONE;
7280 }
7281 }
7282 }
7283 /*
7284 * The function call (or "FuncUndefined" autocommand sequence) might
7285 * have been aborted by an error, an interrupt, or an explicitly thrown
7286 * exception that has not been caught so far. This situation can be
7287 * tested for by calling aborting(). For an error in an internal
7288 * function or for the "E132" error in call_user_func(), however, the
7289 * throw point at which the "force_abort" flag (temporarily reset by
7290 * emsg()) is normally updated has not been reached yet. We need to
7291 * update that flag first to make aborting() reliable.
7292 */
7293 update_force_abort();
7294 }
7295 if (error == ERROR_NONE)
7296 ret = OK;
7297
7298 /*
7299 * Report an error unless the argument evaluation or function call has been
7300 * cancelled due to an aborting error, an interrupt, or an exception.
7301 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 if (!aborting())
7303 {
7304 switch (error)
7305 {
7306 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007307 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 break;
7309 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 break;
7312 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007313 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 name);
7315 break;
7316 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007317 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 name);
7319 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007321 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 name);
7323 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 }
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326
7327 name[len] = cc;
7328 if (fname != name && fname != fname_buf)
7329 vim_free(fname);
7330
7331 return ret;
7332}
7333
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007334/*
7335 * Give an error message with a function name. Handle <SNR> things.
7336 */
7337 static void
7338emsg_funcname(msg, name)
7339 char *msg;
7340 char_u *name;
7341{
7342 char_u *p;
7343
7344 if (*name == K_SPECIAL)
7345 p = concat_str((char_u *)"<SNR>", name + 3);
7346 else
7347 p = name;
7348 EMSG2(_(msg), p);
7349 if (p != name)
7350 vim_free(p);
7351}
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353/*********************************************
7354 * Implementation of the built-in functions
7355 */
7356
7357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007358 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 */
7360 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007361f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 typval_T *argvars;
7363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007367 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007368 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007370 if ((l = argvars[0].vval.v_list) != NULL
7371 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7372 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007373 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007374 }
7375 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007376 EMSG(_(e_listreq));
7377}
7378
7379/*
7380 * "append(lnum, string/list)" function
7381 */
7382 static void
7383f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 typval_T *argvars;
7385 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007386{
7387 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007388 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 list_T *l = NULL;
7390 listitem_T *li = NULL;
7391 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007392 long added = 0;
7393
Bram Moolenaar0d660222005-01-07 21:51:51 +00007394 lnum = get_tv_lnum(argvars);
7395 if (lnum >= 0
7396 && lnum <= curbuf->b_ml.ml_line_count
7397 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007398 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007399 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007400 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007401 l = argvars[1].vval.v_list;
7402 if (l == NULL)
7403 return;
7404 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007407 for (;;)
7408 {
7409 if (l == NULL)
7410 tv = &argvars[1]; /* append a string */
7411 else if (li == NULL)
7412 break; /* end of list */
7413 else
7414 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007415 line = get_tv_string_chk(tv);
7416 if (line == NULL) /* type error */
7417 {
7418 rettv->vval.v_number = 1; /* Failed */
7419 break;
7420 }
7421 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007422 ++added;
7423 if (l == NULL)
7424 break;
7425 li = li->li_next;
7426 }
7427
7428 appended_lines_mark(lnum, added);
7429 if (curwin->w_cursor.lnum > lnum)
7430 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007432 else
7433 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
7436/*
7437 * "argc()" function
7438 */
7439/* ARGSUSED */
7440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 typval_T *argvars;
7443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446}
7447
7448/*
7449 * "argidx()" function
7450 */
7451/* ARGSUSED */
7452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 typval_T *argvars;
7455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007457 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458}
7459
7460/*
7461 * "argv(nr)" function
7462 */
7463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 typval_T *argvars;
7466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468 int idx;
7469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007474 rettv->vval.v_string = NULL;
7475 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
7478/*
7479 * "browse(save, title, initdir, default)" function
7480 */
7481/* ARGSUSED */
7482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007483f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 typval_T *argvars;
7485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487#ifdef FEAT_BROWSE
7488 int save;
7489 char_u *title;
7490 char_u *initdir;
7491 char_u *defname;
7492 char_u buf[NUMBUFLEN];
7493 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007494 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007496 save = get_tv_number_chk(&argvars[0], &error);
7497 title = get_tv_string_chk(&argvars[1]);
7498 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7499 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007501 if (error || title == NULL || initdir == NULL || defname == NULL)
7502 rettv->vval.v_string = NULL;
7503 else
7504 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007505 do_browse(save ? BROWSE_SAVE : 0,
7506 title, defname, NULL, initdir, NULL, curbuf);
7507#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007509#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007511}
7512
7513/*
7514 * "browsedir(title, initdir)" function
7515 */
7516/* ARGSUSED */
7517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007518f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 typval_T *argvars;
7520 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007521{
7522#ifdef FEAT_BROWSE
7523 char_u *title;
7524 char_u *initdir;
7525 char_u buf[NUMBUFLEN];
7526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 title = get_tv_string_chk(&argvars[0]);
7528 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007530 if (title == NULL || initdir == NULL)
7531 rettv->vval.v_string = NULL;
7532 else
7533 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007534 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539}
7540
Bram Moolenaar33570922005-01-25 22:26:29 +00007541static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543/*
7544 * Find a buffer by number or exact name.
7545 */
7546 static buf_T *
7547find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
7550 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007552 if (avar->v_type == VAR_NUMBER)
7553 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007554 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007556 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007557 if (buf == NULL)
7558 {
7559 /* No full path name match, try a match with a URL or a "nofile"
7560 * buffer, these don't use the full path. */
7561 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7562 if (buf->b_fname != NULL
7563 && (path_with_url(buf->b_fname)
7564#ifdef FEAT_QUICKFIX
7565 || bt_nofile(buf)
7566#endif
7567 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007569 break;
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 }
7572 return buf;
7573}
7574
7575/*
7576 * "bufexists(expr)" function
7577 */
7578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 typval_T *argvars;
7581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007583 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584}
7585
7586/*
7587 * "buflisted(expr)" function
7588 */
7589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007590f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 typval_T *argvars;
7592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
7594 buf_T *buf;
7595
7596 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598}
7599
7600/*
7601 * "bufloaded(expr)" function
7602 */
7603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 typval_T *argvars;
7606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608 buf_T *buf;
7609
7610 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007611 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612}
7613
Bram Moolenaar33570922005-01-25 22:26:29 +00007614static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007615
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616/*
7617 * Get buffer by number or pattern.
7618 */
7619 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 int save_magic;
7625 char_u *save_cpo;
7626 buf_T *buf;
7627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 if (tv->v_type == VAR_NUMBER)
7629 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007630 if (tv->v_type != VAR_STRING)
7631 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (name == NULL || *name == NUL)
7633 return curbuf;
7634 if (name[0] == '$' && name[1] == NUL)
7635 return lastbuf;
7636
7637 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7638 save_magic = p_magic;
7639 p_magic = TRUE;
7640 save_cpo = p_cpo;
7641 p_cpo = (char_u *)"";
7642
7643 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7644 TRUE, FALSE));
7645
7646 p_magic = save_magic;
7647 p_cpo = save_cpo;
7648
7649 /* If not found, try expanding the name, like done for bufexists(). */
7650 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
7653 return buf;
7654}
7655
7656/*
7657 * "bufname(expr)" function
7658 */
7659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *argvars;
7662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664 buf_T *buf;
7665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007666 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 buf = get_buf_tv(&argvars[0]);
7669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 --emsg_off;
7675}
7676
7677/*
7678 * "bufnr(expr)" function
7679 */
7680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *argvars;
7683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 buf_T *buf;
7686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007687 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 --emsg_off;
7695}
7696
7697/*
7698 * "bufwinnr(nr)" function
7699 */
7700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 typval_T *argvars;
7703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
7705#ifdef FEAT_WINDOWS
7706 win_T *wp;
7707 int winnr = 0;
7708#endif
7709 buf_T *buf;
7710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007711 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#ifdef FEAT_WINDOWS
7715 for (wp = firstwin; wp; wp = wp->w_next)
7716 {
7717 ++winnr;
7718 if (wp->w_buffer == buf)
7719 break;
7720 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724#endif
7725 --emsg_off;
7726}
7727
7728/*
7729 * "byte2line(byte)" function
7730 */
7731/*ARGSUSED*/
7732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 typval_T *argvars;
7735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736{
7737#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739#else
7740 long boff = 0;
7741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007746 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 (linenr_T)0, &boff);
7748#endif
7749}
7750
7751/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007752 * "byteidx()" function
7753 */
7754/*ARGSUSED*/
7755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007756f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 typval_T *argvars;
7758 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007759{
7760#ifdef FEAT_MBYTE
7761 char_u *t;
7762#endif
7763 char_u *str;
7764 long idx;
7765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 str = get_tv_string_chk(&argvars[0]);
7767 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007770 return;
7771
7772#ifdef FEAT_MBYTE
7773 t = str;
7774 for ( ; idx > 0; idx--)
7775 {
7776 if (*t == NUL) /* EOL reached */
7777 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007778 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007781#else
7782 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007784#endif
7785}
7786
7787/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007788 * "call(func, arglist)" function
7789 */
7790 static void
7791f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 typval_T *argvars;
7793 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007794{
7795 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007799 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007801
7802 rettv->vval.v_number = 0;
7803 if (argvars[1].v_type != VAR_LIST)
7804 {
7805 EMSG(_(e_listreq));
7806 return;
7807 }
7808 if (argvars[1].vval.v_list == NULL)
7809 return;
7810
7811 if (argvars[0].v_type == VAR_FUNC)
7812 func = argvars[0].vval.v_string;
7813 else
7814 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007815 if (*func == NUL)
7816 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007817
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 if (argvars[2].v_type != VAR_UNKNOWN)
7819 {
7820 if (argvars[2].v_type != VAR_DICT)
7821 {
7822 EMSG(_(e_dictreq));
7823 return;
7824 }
7825 selfdict = argvars[2].vval.v_dict;
7826 }
7827
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007828 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7829 item = item->li_next)
7830 {
7831 if (argc == MAX_FUNC_ARGS)
7832 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007833 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007834 break;
7835 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007836 /* Make a copy of each argument. This is needed to be able to set
7837 * v_lock to VAR_FIXED in the copy without changing the original list.
7838 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007839 copy_tv(&item->li_tv, &argv[argc++]);
7840 }
7841
7842 if (item == NULL)
7843 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7845 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007846
7847 /* Free the arguments. */
7848 while (argc > 0)
7849 clear_tv(&argv[--argc]);
7850}
7851
7852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 * "char2nr(string)" function
7854 */
7855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 typval_T *argvars;
7858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860#ifdef FEAT_MBYTE
7861 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007862 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 else
7864#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868/*
7869 * "cindent(lnum)" function
7870 */
7871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 typval_T *argvars;
7874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876#ifdef FEAT_CINDENT
7877 pos_T pos;
7878 linenr_T lnum;
7879
7880 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7883 {
7884 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 curwin->w_cursor = pos;
7887 }
7888 else
7889#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891}
7892
7893/*
7894 * "col(string)" function
7895 */
7896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007898 typval_T *argvars;
7899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900{
7901 colnr_T col = 0;
7902 pos_T *fp;
7903
7904 fp = var2fpos(&argvars[0], FALSE);
7905 if (fp != NULL)
7906 {
7907 if (fp->col == MAXCOL)
7908 {
7909 /* '> can be MAXCOL, get the length of the line then */
7910 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7911 col = STRLEN(ml_get(fp->lnum)) + 1;
7912 else
7913 col = MAXCOL;
7914 }
7915 else
7916 {
7917 col = fp->col + 1;
7918#ifdef FEAT_VIRTUALEDIT
7919 /* col(".") when the cursor is on the NUL at the end of the line
7920 * because of "coladd" can be seen as an extra column. */
7921 if (virtual_active() && fp == &curwin->w_cursor)
7922 {
7923 char_u *p = ml_get_cursor();
7924
7925 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7926 curwin->w_virtcol - curwin->w_cursor.coladd))
7927 {
7928# ifdef FEAT_MBYTE
7929 int l;
7930
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007931 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 col += l;
7933# else
7934 if (*p != NUL && p[1] == NUL)
7935 ++col;
7936# endif
7937 }
7938 }
7939#endif
7940 }
7941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
Bram Moolenaar572cb562005-08-05 21:35:02 +00007945#if defined(FEAT_INS_EXPAND)
7946/*
7947 * "complete_add()" function
7948 */
7949/*ARGSUSED*/
7950 static void
7951f_complete_add(argvars, rettv)
7952 typval_T *argvars;
7953 typval_T *rettv;
7954{
7955 char_u *s;
7956
7957 s = get_tv_string_chk(&argvars[0]);
7958 if (s != NULL)
7959 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7960}
7961
7962/*
7963 * "complete_check()" function
7964 */
7965/*ARGSUSED*/
7966 static void
7967f_complete_check(argvars, rettv)
7968 typval_T *argvars;
7969 typval_T *rettv;
7970{
7971 int saved = RedrawingDisabled;
7972
7973 RedrawingDisabled = 0;
7974 ins_compl_check_keys(0);
7975 rettv->vval.v_number = compl_interrupted;
7976 RedrawingDisabled = saved;
7977}
7978#endif
7979
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980/*
7981 * "confirm(message, buttons[, default [, type]])" function
7982 */
7983/*ARGSUSED*/
7984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T *argvars;
7987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7990 char_u *message;
7991 char_u *buttons = NULL;
7992 char_u buf[NUMBUFLEN];
7993 char_u buf2[NUMBUFLEN];
7994 int def = 1;
7995 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007996 char_u *typestr;
7997 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007999 message = get_tv_string_chk(&argvars[0]);
8000 if (message == NULL)
8001 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008002 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008004 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8005 if (buttons == NULL)
8006 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008009 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008010 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008012 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8013 if (typestr == NULL)
8014 error = TRUE;
8015 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008017 switch (TOUPPER_ASC(*typestr))
8018 {
8019 case 'E': type = VIM_ERROR; break;
8020 case 'Q': type = VIM_QUESTION; break;
8021 case 'I': type = VIM_INFO; break;
8022 case 'W': type = VIM_WARNING; break;
8023 case 'G': type = VIM_GENERIC; break;
8024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026 }
8027 }
8028 }
8029
8030 if (buttons == NULL || *buttons == NUL)
8031 buttons = (char_u *)_("&Ok");
8032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008033 if (error)
8034 rettv->vval.v_number = 0;
8035 else
8036 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 def, NULL);
8038#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040#endif
8041}
8042
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008043/*
8044 * "copy()" function
8045 */
8046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 typval_T *argvars;
8049 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008051 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
8054/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008055 * "count()" function
8056 */
8057 static void
8058f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 typval_T *argvars;
8060 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008061{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008062 long n = 0;
8063 int ic = FALSE;
8064
Bram Moolenaare9a41262005-01-15 22:18:47 +00008065 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008066 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 listitem_T *li;
8068 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008069 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070
Bram Moolenaare9a41262005-01-15 22:18:47 +00008071 if ((l = argvars[0].vval.v_list) != NULL)
8072 {
8073 li = l->lv_first;
8074 if (argvars[2].v_type != VAR_UNKNOWN)
8075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 int error = FALSE;
8077
8078 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 if (argvars[3].v_type != VAR_UNKNOWN)
8080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 idx = get_tv_number_chk(&argvars[3], &error);
8082 if (!error)
8083 {
8084 li = list_find(l, idx);
8085 if (li == NULL)
8086 EMSGN(_(e_listidx), idx);
8087 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 if (error)
8090 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008091 }
8092
8093 for ( ; li != NULL; li = li->li_next)
8094 if (tv_equal(&li->li_tv, &argvars[1], ic))
8095 ++n;
8096 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008098 else if (argvars[0].v_type == VAR_DICT)
8099 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 int todo;
8101 dict_T *d;
8102 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008104 if ((d = argvars[0].vval.v_dict) != NULL)
8105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008106 int error = FALSE;
8107
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 if (argvars[2].v_type != VAR_UNKNOWN)
8109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008110 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008111 if (argvars[3].v_type != VAR_UNKNOWN)
8112 EMSG(_(e_invarg));
8113 }
8114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008115 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008116 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008117 {
8118 if (!HASHITEM_EMPTY(hi))
8119 {
8120 --todo;
8121 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8122 ++n;
8123 }
8124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008125 }
8126 }
8127 else
8128 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129 rettv->vval.v_number = n;
8130}
8131
8132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8134 *
8135 * Checks the existence of a cscope connection.
8136 */
8137/*ARGSUSED*/
8138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 typval_T *argvars;
8141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143#ifdef FEAT_CSCOPE
8144 int num = 0;
8145 char_u *dbpath = NULL;
8146 char_u *prepend = NULL;
8147 char_u buf[NUMBUFLEN];
8148
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 if (argvars[0].v_type != VAR_UNKNOWN
8150 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 num = (int)get_tv_number(&argvars[0]);
8153 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008154 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 }
8157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161#endif
8162}
8163
8164/*
8165 * "cursor(lnum, col)" function
8166 *
8167 * Moves the cursor to the specified line and column
8168 */
8169/*ARGSUSED*/
8170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008171f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 typval_T *argvars;
8173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175 long line, col;
8176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008177 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178 col = get_tv_number_chk(&argvars[1], NULL);
8179 if (line < 0 || col < 0)
8180 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (line > 0)
8182 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (col > 0)
8184 curwin->w_cursor.col = col - 1;
8185#ifdef FEAT_VIRTUALEDIT
8186 curwin->w_cursor.coladd = 0;
8187#endif
8188
8189 /* Make sure the cursor is in a valid position. */
8190 check_cursor();
8191#ifdef FEAT_MBYTE
8192 /* Correct cursor for multi-byte character. */
8193 if (has_mbyte)
8194 mb_adjust_cursor();
8195#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008196
8197 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198}
8199
8200/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008201 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 */
8203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008204f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 typval_T *argvars;
8206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008208 int noref = 0;
8209
8210 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008212 if (noref < 0 || noref > 1)
8213 EMSG(_(e_invarg));
8214 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008215 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216}
8217
8218/*
8219 * "delete()" function
8220 */
8221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008222f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008223 typval_T *argvars;
8224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
8226 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230}
8231
8232/*
8233 * "did_filetype()" function
8234 */
8235/*ARGSUSED*/
8236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 typval_T *argvars;
8239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240{
8241#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#endif
8246}
8247
8248/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008249 * "diff_filler()" function
8250 */
8251/*ARGSUSED*/
8252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008254 typval_T *argvars;
8255 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008256{
8257#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008258 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008259#endif
8260}
8261
8262/*
8263 * "diff_hlID()" function
8264 */
8265/*ARGSUSED*/
8266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008268 typval_T *argvars;
8269 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008270{
8271#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008273 static linenr_T prev_lnum = 0;
8274 static int changedtick = 0;
8275 static int fnum = 0;
8276 static int change_start = 0;
8277 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008278 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008279 int filler_lines;
8280 int col;
8281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008282 if (lnum < 0) /* ignore type error in {lnum} arg */
8283 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008284 if (lnum != prev_lnum
8285 || changedtick != curbuf->b_changedtick
8286 || fnum != curbuf->b_fnum)
8287 {
8288 /* New line, buffer, change: need to get the values. */
8289 filler_lines = diff_check(curwin, lnum);
8290 if (filler_lines < 0)
8291 {
8292 if (filler_lines == -1)
8293 {
8294 change_start = MAXCOL;
8295 change_end = -1;
8296 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8297 hlID = HLF_ADD; /* added line */
8298 else
8299 hlID = HLF_CHD; /* changed line */
8300 }
8301 else
8302 hlID = HLF_ADD; /* added line */
8303 }
8304 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008305 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008306 prev_lnum = lnum;
8307 changedtick = curbuf->b_changedtick;
8308 fnum = curbuf->b_fnum;
8309 }
8310
8311 if (hlID == HLF_CHD || hlID == HLF_TXD)
8312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008313 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008314 if (col >= change_start && col <= change_end)
8315 hlID = HLF_TXD; /* changed text */
8316 else
8317 hlID = HLF_CHD; /* changed line */
8318 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008319 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008320#endif
8321}
8322
8323/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008324 * "empty({expr})" function
8325 */
8326 static void
8327f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 typval_T *argvars;
8329 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008330{
8331 int n;
8332
8333 switch (argvars[0].v_type)
8334 {
8335 case VAR_STRING:
8336 case VAR_FUNC:
8337 n = argvars[0].vval.v_string == NULL
8338 || *argvars[0].vval.v_string == NUL;
8339 break;
8340 case VAR_NUMBER:
8341 n = argvars[0].vval.v_number == 0;
8342 break;
8343 case VAR_LIST:
8344 n = argvars[0].vval.v_list == NULL
8345 || argvars[0].vval.v_list->lv_first == NULL;
8346 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 case VAR_DICT:
8348 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008351 default:
8352 EMSG2(_(e_intern2), "f_empty()");
8353 n = 0;
8354 }
8355
8356 rettv->vval.v_number = n;
8357}
8358
8359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 * "escape({string}, {chars})" function
8361 */
8362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 typval_T *argvars;
8365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
8367 char_u buf[NUMBUFLEN];
8368
Bram Moolenaar758711c2005-02-02 23:11:38 +00008369 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8370 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372}
8373
8374/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008375 * "eval()" function
8376 */
8377/*ARGSUSED*/
8378 static void
8379f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 typval_T *argvars;
8381 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008382{
8383 char_u *s;
8384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 s = get_tv_string_chk(&argvars[0]);
8386 if (s != NULL)
8387 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008389 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8390 {
8391 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008392 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008393 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008394 else if (*s != NUL)
8395 EMSG(_(e_trailing));
8396}
8397
8398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 * "eventhandler()" function
8400 */
8401/*ARGSUSED*/
8402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008403f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008404 typval_T *argvars;
8405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008407 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408}
8409
8410/*
8411 * "executable()" function
8412 */
8413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008414f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 typval_T *argvars;
8416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419}
8420
8421/*
8422 * "exists()" function
8423 */
8424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 typval_T *argvars;
8427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 char_u *p;
8430 char_u *name;
8431 int n = FALSE;
8432 int len = 0;
8433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (*p == '$') /* environment variable */
8436 {
8437 /* first try "normal" environment variables (fast) */
8438 if (mch_getenv(p + 1) != NULL)
8439 n = TRUE;
8440 else
8441 {
8442 /* try expanding things like $VIM and ${HOME} */
8443 p = expand_env_save(p);
8444 if (p != NULL && *p != '$')
8445 n = TRUE;
8446 vim_free(p);
8447 }
8448 }
8449 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 else if (*p == '*') /* internal or user defined function */
8452 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008453 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 else if (*p == ':')
8456 {
8457 n = cmd_exists(p + 1);
8458 }
8459 else if (*p == '#')
8460 {
8461#ifdef FEAT_AUTOCMD
Bram Moolenaarbb1004e2005-12-19 22:15:59 +00008462 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#endif
8464 }
8465 else /* internal variable */
8466 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008467 char_u *tofree;
8468 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008470 /* get_name_len() takes care of expanding curly braces */
8471 name = p;
8472 len = get_name_len(&p, &tofree, TRUE, FALSE);
8473 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008475 if (tofree != NULL)
8476 name = tofree;
8477 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8478 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008480 /* handle d.key, l[idx], f(expr) */
8481 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8482 if (n)
8483 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 }
8485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008487 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491}
8492
8493/*
8494 * "expand()" function
8495 */
8496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 typval_T *argvars;
8499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 char_u *s;
8502 int len;
8503 char_u *errormsg;
8504 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8505 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008506 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 rettv->v_type = VAR_STRING;
8509 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 if (*s == '%' || *s == '#' || *s == '<')
8511 {
8512 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 --emsg_off;
8515 }
8516 else
8517 {
8518 /* When the optional second argument is non-zero, don't remove matches
8519 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008520 if (argvars[1].v_type != VAR_UNKNOWN
8521 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 if (!error)
8524 {
8525 ExpandInit(&xpc);
8526 xpc.xp_context = EXPAND_FILES;
8527 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8528 ExpandCleanup(&xpc);
8529 }
8530 else
8531 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533}
8534
8535/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008536 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008538 */
8539 static void
8540f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 typval_T *argvars;
8542 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008543{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008544 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008545 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008546 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008547 list_T *l1, *l2;
8548 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008549 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008550 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008551
Bram Moolenaare9a41262005-01-15 22:18:47 +00008552 l1 = argvars[0].vval.v_list;
8553 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008554 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8555 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008556 {
8557 if (argvars[2].v_type != VAR_UNKNOWN)
8558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008559 before = get_tv_number_chk(&argvars[2], &error);
8560 if (error)
8561 return; /* type error; errmsg already given */
8562
Bram Moolenaar758711c2005-02-02 23:11:38 +00008563 if (before == l1->lv_len)
8564 item = NULL;
8565 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008566 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008567 item = list_find(l1, before);
8568 if (item == NULL)
8569 {
8570 EMSGN(_(e_listidx), before);
8571 return;
8572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008573 }
8574 }
8575 else
8576 item = NULL;
8577 list_extend(l1, l2, item);
8578
Bram Moolenaare9a41262005-01-15 22:18:47 +00008579 copy_tv(&argvars[0], rettv);
8580 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008582 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008584 dict_T *d1, *d2;
8585 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008586 char_u *action;
8587 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008589 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008590
8591 d1 = argvars[0].vval.v_dict;
8592 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008593 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8594 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 {
8596 /* Check the third argument. */
8597 if (argvars[2].v_type != VAR_UNKNOWN)
8598 {
8599 static char *(av[]) = {"keep", "force", "error"};
8600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008601 action = get_tv_string_chk(&argvars[2]);
8602 if (action == NULL)
8603 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008604 for (i = 0; i < 3; ++i)
8605 if (STRCMP(action, av[i]) == 0)
8606 break;
8607 if (i == 3)
8608 {
8609 EMSGN(_(e_invarg2), action);
8610 return;
8611 }
8612 }
8613 else
8614 action = (char_u *)"force";
8615
8616 /* Go over all entries in the second dict and add them to the
8617 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 todo = d2->dv_hashtab.ht_used;
8619 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008620 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008621 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008623 --todo;
8624 di1 = dict_find(d1, hi2->hi_key, -1);
8625 if (di1 == NULL)
8626 {
8627 di1 = dictitem_copy(HI2DI(hi2));
8628 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8629 dictitem_free(di1);
8630 }
8631 else if (*action == 'e')
8632 {
8633 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8634 break;
8635 }
8636 else if (*action == 'f')
8637 {
8638 clear_tv(&di1->di_tv);
8639 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008641 }
8642 }
8643
Bram Moolenaare9a41262005-01-15 22:18:47 +00008644 copy_tv(&argvars[0], rettv);
8645 }
8646 }
8647 else
8648 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008649}
8650
8651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 * "filereadable()" function
8653 */
8654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008655f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 typval_T *argvars;
8657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
8659 FILE *fd;
8660 char_u *p;
8661 int n;
8662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8665 {
8666 n = TRUE;
8667 fclose(fd);
8668 }
8669 else
8670 n = FALSE;
8671
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673}
8674
8675/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008676 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 * rights to write into.
8678 */
8679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 typval_T *argvars;
8682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008684 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685}
8686
Bram Moolenaar33570922005-01-25 22:26:29 +00008687static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008688
8689 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008690findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008691 typval_T *argvars;
8692 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008693 int dir;
8694{
8695#ifdef FEAT_SEARCHPATH
8696 char_u *fname;
8697 char_u *fresult = NULL;
8698 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8699 char_u *p;
8700 char_u pathbuf[NUMBUFLEN];
8701 int count = 1;
8702 int first = TRUE;
8703
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008705
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008706 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008708 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8709 if (p == NULL)
8710 count = -1; /* error */
8711 else
8712 {
8713 if (*p != NUL)
8714 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 if (argvars[2].v_type != VAR_UNKNOWN)
8717 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8718 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008719 }
8720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008721 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008722 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008723 do
8724 {
8725 vim_free(fresult);
8726 fresult = find_file_in_path_option(first ? fname : NULL,
8727 first ? (int)STRLEN(fname) : 0,
8728 0, first, path, dir, NULL);
8729 first = FALSE;
8730 } while (--count > 0 && fresult != NULL);
8731 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008734#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008736#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008738}
8739
Bram Moolenaar33570922005-01-25 22:26:29 +00008740static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8741static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008742
8743/*
8744 * Implementation of map() and filter().
8745 */
8746 static void
8747filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008750 int map;
8751{
8752 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008753 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 listitem_T *li, *nli;
8755 list_T *l = NULL;
8756 dictitem_T *di;
8757 hashtab_T *ht;
8758 hashitem_T *hi;
8759 dict_T *d = NULL;
8760 typval_T save_val;
8761 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008762 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008763 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008764 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008766
8767 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008769 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008770 if ((l = argvars[0].vval.v_list) == NULL
8771 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008772 return;
8773 }
8774 else if (argvars[0].v_type == VAR_DICT)
8775 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008776 if ((d = argvars[0].vval.v_dict) == NULL
8777 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008778 return;
8779 }
8780 else
8781 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008782 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008783 return;
8784 }
8785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008786 expr = get_tv_string_buf_chk(&argvars[1], buf);
8787 /* On type errors, the preceding call has already displayed an error
8788 * message. Avoid a misleading error message for an empty string that
8789 * was not passed as argument. */
8790 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008792 prepare_vimvar(VV_VAL, &save_val);
8793 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008795 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008797 prepare_vimvar(VV_KEY, &save_key);
8798 vimvars[VV_KEY].vv_type = VAR_STRING;
8799
8800 ht = &d->dv_hashtab;
8801 hash_lock(ht);
8802 todo = ht->ht_used;
8803 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008805 if (!HASHITEM_EMPTY(hi))
8806 {
8807 --todo;
8808 di = HI2DI(hi);
8809 if (tv_check_lock(di->di_tv.v_lock, msg))
8810 break;
8811 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8812 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8813 break;
8814 if (!map && rem)
8815 dictitem_remove(d, di);
8816 clear_tv(&vimvars[VV_KEY].vv_tv);
8817 }
8818 }
8819 hash_unlock(ht);
8820
8821 restore_vimvar(VV_KEY, &save_key);
8822 }
8823 else
8824 {
8825 for (li = l->lv_first; li != NULL; li = nli)
8826 {
8827 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008828 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008829 nli = li->li_next;
8830 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008831 break;
8832 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008833 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008834 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008837 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008839
8840 copy_tv(&argvars[0], rettv);
8841}
8842
8843 static int
8844filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008846 char_u *expr;
8847 int map;
8848 int *remp;
8849{
Bram Moolenaar33570922005-01-25 22:26:29 +00008850 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008851 char_u *s;
8852
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854 s = expr;
8855 if (eval1(&s, &rettv, TRUE) == FAIL)
8856 return FAIL;
8857 if (*s != NUL) /* check for trailing chars after expr */
8858 {
8859 EMSG2(_(e_invexpr2), s);
8860 return FAIL;
8861 }
8862 if (map)
8863 {
8864 /* map(): replace the list item value */
8865 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008866 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008867 *tv = rettv;
8868 }
8869 else
8870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008871 int error = FALSE;
8872
Bram Moolenaare9a41262005-01-15 22:18:47 +00008873 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 /* On type error, nothing has been removed; return FAIL to stop the
8877 * loop. The error message was given by get_tv_number_chk(). */
8878 if (error)
8879 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008880 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008881 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008882 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008883}
8884
8885/*
8886 * "filter()" function
8887 */
8888 static void
8889f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008890 typval_T *argvars;
8891 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008892{
8893 filter_map(argvars, rettv, FALSE);
8894}
8895
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008896/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008897 * "finddir({fname}[, {path}[, {count}]])" function
8898 */
8899 static void
8900f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *argvars;
8902 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008903{
8904 findfilendir(argvars, rettv, TRUE);
8905}
8906
8907/*
8908 * "findfile({fname}[, {path}[, {count}]])" function
8909 */
8910 static void
8911f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *argvars;
8913 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008914{
8915 findfilendir(argvars, rettv, FALSE);
8916}
8917
8918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 * "fnamemodify({fname}, {mods})" function
8920 */
8921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008923 typval_T *argvars;
8924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 char_u *fname;
8927 char_u *mods;
8928 int usedlen = 0;
8929 int len;
8930 char_u *fbuf = NULL;
8931 char_u buf[NUMBUFLEN];
8932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 fname = get_tv_string_chk(&argvars[0]);
8934 mods = get_tv_string_buf_chk(&argvars[1], buf);
8935 if (fname == NULL || mods == NULL)
8936 fname = NULL;
8937 else
8938 {
8939 len = (int)STRLEN(fname);
8940 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 vim_free(fbuf);
8949}
8950
Bram Moolenaar33570922005-01-25 22:26:29 +00008951static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
8953/*
8954 * "foldclosed()" function
8955 */
8956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *argvars;
8959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 int end;
8961{
8962#ifdef FEAT_FOLDING
8963 linenr_T lnum;
8964 linenr_T first, last;
8965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8968 {
8969 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8970 {
8971 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 return;
8976 }
8977 }
8978#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
8981
8982/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008983 * "foldclosed()" function
8984 */
8985 static void
8986f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989{
8990 foldclosed_both(argvars, rettv, FALSE);
8991}
8992
8993/*
8994 * "foldclosedend()" function
8995 */
8996 static void
8997f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *argvars;
8999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009000{
9001 foldclosed_both(argvars, rettv, TRUE);
9002}
9003
9004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 * "foldlevel()" function
9006 */
9007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 typval_T *argvars;
9010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
9012#ifdef FEAT_FOLDING
9013 linenr_T lnum;
9014
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 else
9019#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021}
9022
9023/*
9024 * "foldtext()" function
9025 */
9026/*ARGSUSED*/
9027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032#ifdef FEAT_FOLDING
9033 linenr_T lnum;
9034 char_u *s;
9035 char_u *r;
9036 int len;
9037 char *txt;
9038#endif
9039
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 rettv->v_type = VAR_STRING;
9041 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9044 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9045 <= curbuf->b_ml.ml_line_count
9046 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 {
9048 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9050 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 {
9052 if (!linewhite(lnum))
9053 break;
9054 ++lnum;
9055 }
9056
9057 /* Find interesting text in this line. */
9058 s = skipwhite(ml_get(lnum));
9059 /* skip C comment-start */
9060 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009063 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009064 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009065 {
9066 s = skipwhite(ml_get(lnum + 1));
9067 if (*s == '*')
9068 s = skipwhite(s + 1);
9069 }
9070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 txt = _("+-%s%3ld lines: ");
9072 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 + 20 /* for %3ld */
9075 + STRLEN(s))); /* concatenated */
9076 if (r != NULL)
9077 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009078 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9079 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9080 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 len = (int)STRLEN(r);
9082 STRCAT(r, s);
9083 /* remove 'foldmarker' and 'commentstring' */
9084 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
9087 }
9088#endif
9089}
9090
9091/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009092 * "foldtextresult(lnum)" function
9093 */
9094/*ARGSUSED*/
9095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009097 typval_T *argvars;
9098 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009099{
9100#ifdef FEAT_FOLDING
9101 linenr_T lnum;
9102 char_u *text;
9103 char_u buf[51];
9104 foldinfo_T foldinfo;
9105 int fold_count;
9106#endif
9107
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->v_type = VAR_STRING;
9109 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009110#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009112 /* treat illegal types and illegal string values for {lnum} the same */
9113 if (lnum < 0)
9114 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009115 fold_count = foldedCount(curwin, lnum, &foldinfo);
9116 if (fold_count > 0)
9117 {
9118 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9119 &foldinfo, buf);
9120 if (text == buf)
9121 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009123 }
9124#endif
9125}
9126
9127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 * "foreground()" function
9129 */
9130/*ARGSUSED*/
9131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009133 typval_T *argvars;
9134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137#ifdef FEAT_GUI
9138 if (gui.in_use)
9139 gui_mch_set_foreground();
9140#else
9141# ifdef WIN32
9142 win32_set_foreground();
9143# endif
9144#endif
9145}
9146
9147/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009148 * "function()" function
9149 */
9150/*ARGSUSED*/
9151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *argvars;
9154 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009155{
9156 char_u *s;
9157
Bram Moolenaara7043832005-01-21 11:56:39 +00009158 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009160 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009161 EMSG2(_(e_invarg2), s);
9162 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009163 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009164 else
9165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_string = vim_strsave(s);
9167 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009168 }
9169}
9170
9171/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009172 * "garbagecollect()" function
9173 */
9174/*ARGSUSED*/
9175 static void
9176f_garbagecollect(argvars, rettv)
9177 typval_T *argvars;
9178 typval_T *rettv;
9179{
9180 garbage_collect();
9181}
9182
9183/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009184 * "get()" function
9185 */
9186 static void
9187f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009188 typval_T *argvars;
9189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009190{
Bram Moolenaar33570922005-01-25 22:26:29 +00009191 listitem_T *li;
9192 list_T *l;
9193 dictitem_T *di;
9194 dict_T *d;
9195 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009196
Bram Moolenaare9a41262005-01-15 22:18:47 +00009197 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009198 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009199 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009201 int error = FALSE;
9202
9203 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9204 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009205 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009206 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009208 else if (argvars[0].v_type == VAR_DICT)
9209 {
9210 if ((d = argvars[0].vval.v_dict) != NULL)
9211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009212 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009213 if (di != NULL)
9214 tv = &di->di_tv;
9215 }
9216 }
9217 else
9218 EMSG2(_(e_listdictarg), "get()");
9219
9220 if (tv == NULL)
9221 {
9222 if (argvars[2].v_type == VAR_UNKNOWN)
9223 rettv->vval.v_number = 0;
9224 else
9225 copy_tv(&argvars[2], rettv);
9226 }
9227 else
9228 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009229}
9230
Bram Moolenaar342337a2005-07-21 21:11:17 +00009231static 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 +00009232
9233/*
9234 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009235 * Return a range (from start to end) of lines in rettv from the specified
9236 * buffer.
9237 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009238 */
9239 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009240get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009241 buf_T *buf;
9242 linenr_T start;
9243 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009244 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009245 typval_T *rettv;
9246{
9247 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009248 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009249
Bram Moolenaar342337a2005-07-21 21:11:17 +00009250 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009251 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009252 l = list_alloc();
9253 if (l == NULL)
9254 return;
9255
9256 rettv->vval.v_list = l;
9257 rettv->v_type = VAR_LIST;
9258 ++l->lv_refcount;
9259 }
9260 else
9261 rettv->vval.v_number = 0;
9262
9263 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9264 return;
9265
9266 if (!retlist)
9267 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009268 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9269 p = ml_get_buf(buf, start, FALSE);
9270 else
9271 p = (char_u *)"";
9272
9273 rettv->v_type = VAR_STRING;
9274 rettv->vval.v_string = vim_strsave(p);
9275 }
9276 else
9277 {
9278 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009279 return;
9280
9281 if (start < 1)
9282 start = 1;
9283 if (end > buf->b_ml.ml_line_count)
9284 end = buf->b_ml.ml_line_count;
9285 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009286 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9287 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009288 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009289 }
9290}
9291
9292/*
9293 * "getbufline()" function
9294 */
9295 static void
9296f_getbufline(argvars, rettv)
9297 typval_T *argvars;
9298 typval_T *rettv;
9299{
9300 linenr_T lnum;
9301 linenr_T end;
9302 buf_T *buf;
9303
9304 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9305 ++emsg_off;
9306 buf = get_buf_tv(&argvars[0]);
9307 --emsg_off;
9308
Bram Moolenaar661b1822005-07-28 22:36:45 +00009309 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009310 if (argvars[2].v_type == VAR_UNKNOWN)
9311 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009312 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009313 end = get_tv_lnum_buf(&argvars[2], buf);
9314
Bram Moolenaar342337a2005-07-21 21:11:17 +00009315 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009316}
9317
Bram Moolenaar0d660222005-01-07 21:51:51 +00009318/*
9319 * "getbufvar()" function
9320 */
9321 static void
9322f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009323 typval_T *argvars;
9324 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009325{
9326 buf_T *buf;
9327 buf_T *save_curbuf;
9328 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9332 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009333 ++emsg_off;
9334 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335
9336 rettv->v_type = VAR_STRING;
9337 rettv->vval.v_string = NULL;
9338
9339 if (buf != NULL && varname != NULL)
9340 {
9341 if (*varname == '&') /* buffer-local-option */
9342 {
9343 /* set curbuf to be our buf, temporarily */
9344 save_curbuf = curbuf;
9345 curbuf = buf;
9346
9347 get_option_tv(&varname, rettv, TRUE);
9348
9349 /* restore previous notion of curbuf */
9350 curbuf = save_curbuf;
9351 }
9352 else
9353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 if (*varname == NUL)
9355 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9356 * scope prefix before the NUL byte is required by
9357 * find_var_in_ht(). */
9358 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009359 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009360 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009361 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009362 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009363 }
9364 }
9365
9366 --emsg_off;
9367}
9368
9369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 * "getchar()" function
9371 */
9372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009374 typval_T *argvars;
9375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
9377 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379
9380 ++no_mapping;
9381 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009382 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 /* getchar(): blocking wait. */
9384 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009385 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 /* getchar(1): only check if char avail */
9387 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 else if (error || vpeekc() == NUL)
9389 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 n = 0;
9391 else
9392 /* getchar(0) and char avail: return char */
9393 n = safe_vgetc();
9394 --no_mapping;
9395 --allow_keys;
9396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 if (IS_SPECIAL(n) || mod_mask != 0)
9399 {
9400 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9401 int i = 0;
9402
9403 /* Turn a special key into three bytes, plus modifier. */
9404 if (mod_mask != 0)
9405 {
9406 temp[i++] = K_SPECIAL;
9407 temp[i++] = KS_MODIFIER;
9408 temp[i++] = mod_mask;
9409 }
9410 if (IS_SPECIAL(n))
9411 {
9412 temp[i++] = K_SPECIAL;
9413 temp[i++] = K_SECOND(n);
9414 temp[i++] = K_THIRD(n);
9415 }
9416#ifdef FEAT_MBYTE
9417 else if (has_mbyte)
9418 i += (*mb_char2bytes)(n, temp + i);
9419#endif
9420 else
9421 temp[i++] = n;
9422 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->v_type = VAR_STRING;
9424 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 }
9426}
9427
9428/*
9429 * "getcharmod()" function
9430 */
9431/*ARGSUSED*/
9432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009434 typval_T *argvars;
9435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438}
9439
9440/*
9441 * "getcmdline()" function
9442 */
9443/*ARGSUSED*/
9444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->v_type = VAR_STRING;
9450 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453/*
9454 * "getcmdpos()" function
9455 */
9456/*ARGSUSED*/
9457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463}
9464
9465/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009466 * "getcmdtype()" function
9467 */
9468/*ARGSUSED*/
9469 static void
9470f_getcmdtype(argvars, rettv)
9471 typval_T *argvars;
9472 typval_T *rettv;
9473{
9474 rettv->v_type = VAR_STRING;
9475 rettv->vval.v_string = alloc(2);
9476 if (rettv->vval.v_string != NULL)
9477 {
9478 rettv->vval.v_string[0] = get_cmdline_type();
9479 rettv->vval.v_string[1] = NUL;
9480 }
9481}
9482
9483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 * "getcwd()" function
9485 */
9486/*ARGSUSED*/
9487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009489 typval_T *argvars;
9490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492 char_u cwd[MAXPATHL];
9493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009494 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 else
9498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009501 if (rettv->vval.v_string != NULL)
9502 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#endif
9504 }
9505}
9506
9507/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009508 * "getfontname()" function
9509 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009510/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 typval_T *argvars;
9514 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->v_type = VAR_STRING;
9517 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009518#ifdef FEAT_GUI
9519 if (gui.in_use)
9520 {
9521 GuiFont font;
9522 char_u *name = NULL;
9523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009525 {
9526 /* Get the "Normal" font. Either the name saved by
9527 * hl_set_font_name() or from the font ID. */
9528 font = gui.norm_font;
9529 name = hl_get_font_name();
9530 }
9531 else
9532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009534 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9535 return;
9536 font = gui_mch_get_font(name, FALSE);
9537 if (font == NOFONT)
9538 return; /* Invalid font name, return empty string. */
9539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009541 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009542 gui_mch_free_font(font);
9543 }
9544#endif
9545}
9546
9547/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009548 * "getfperm({fname})" function
9549 */
9550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009552 typval_T *argvars;
9553 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009554{
9555 char_u *fname;
9556 struct stat st;
9557 char_u *perm = NULL;
9558 char_u flags[] = "rwx";
9559 int i;
9560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009562
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009564 if (mch_stat((char *)fname, &st) >= 0)
9565 {
9566 perm = vim_strsave((char_u *)"---------");
9567 if (perm != NULL)
9568 {
9569 for (i = 0; i < 9; i++)
9570 {
9571 if (st.st_mode & (1 << (8 - i)))
9572 perm[i] = flags[i % 3];
9573 }
9574 }
9575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009577}
9578
9579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 * "getfsize({fname})" function
9581 */
9582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009584 typval_T *argvars;
9585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 char_u *fname;
9588 struct stat st;
9589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593
9594 if (mch_stat((char *)fname, &st) >= 0)
9595 {
9596 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 }
9601 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "getftime({fname})" function
9607 */
9608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 char_u *fname;
9614 struct stat st;
9615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617
9618 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622}
9623
9624/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009625 * "getftype({fname})" function
9626 */
9627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009631{
9632 char_u *fname;
9633 struct stat st;
9634 char_u *type = NULL;
9635 char *t;
9636
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009640 if (mch_lstat((char *)fname, &st) >= 0)
9641 {
9642#ifdef S_ISREG
9643 if (S_ISREG(st.st_mode))
9644 t = "file";
9645 else if (S_ISDIR(st.st_mode))
9646 t = "dir";
9647# ifdef S_ISLNK
9648 else if (S_ISLNK(st.st_mode))
9649 t = "link";
9650# endif
9651# ifdef S_ISBLK
9652 else if (S_ISBLK(st.st_mode))
9653 t = "bdev";
9654# endif
9655# ifdef S_ISCHR
9656 else if (S_ISCHR(st.st_mode))
9657 t = "cdev";
9658# endif
9659# ifdef S_ISFIFO
9660 else if (S_ISFIFO(st.st_mode))
9661 t = "fifo";
9662# endif
9663# ifdef S_ISSOCK
9664 else if (S_ISSOCK(st.st_mode))
9665 t = "fifo";
9666# endif
9667 else
9668 t = "other";
9669#else
9670# ifdef S_IFMT
9671 switch (st.st_mode & S_IFMT)
9672 {
9673 case S_IFREG: t = "file"; break;
9674 case S_IFDIR: t = "dir"; break;
9675# ifdef S_IFLNK
9676 case S_IFLNK: t = "link"; break;
9677# endif
9678# ifdef S_IFBLK
9679 case S_IFBLK: t = "bdev"; break;
9680# endif
9681# ifdef S_IFCHR
9682 case S_IFCHR: t = "cdev"; break;
9683# endif
9684# ifdef S_IFIFO
9685 case S_IFIFO: t = "fifo"; break;
9686# endif
9687# ifdef S_IFSOCK
9688 case S_IFSOCK: t = "socket"; break;
9689# endif
9690 default: t = "other";
9691 }
9692# else
9693 if (mch_isdir(fname))
9694 t = "dir";
9695 else
9696 t = "file";
9697# endif
9698#endif
9699 type = vim_strsave((char_u *)t);
9700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009702}
9703
9704/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009705 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009706 */
9707 static void
9708f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 typval_T *argvars;
9710 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009711{
9712 linenr_T lnum;
9713 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009714 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009715
9716 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009717 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009718 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009719 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009720 retlist = FALSE;
9721 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009722 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009723 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009724 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009725 retlist = TRUE;
9726 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009727
Bram Moolenaar342337a2005-07-21 21:11:17 +00009728 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009729}
9730
9731/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009732 * "getqflist()" function
9733 */
9734/*ARGSUSED*/
9735 static void
9736f_getqflist(argvars, rettv)
9737 typval_T *argvars;
9738 typval_T *rettv;
9739{
9740#ifdef FEAT_QUICKFIX
9741 list_T *l;
9742#endif
9743
9744 rettv->vval.v_number = FALSE;
9745#ifdef FEAT_QUICKFIX
9746 l = list_alloc();
9747 if (l != NULL)
9748 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009749 rettv->vval.v_list = l;
9750 rettv->v_type = VAR_LIST;
9751 ++l->lv_refcount;
9752 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009753 }
9754#endif
9755}
9756
9757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 * "getreg()" function
9759 */
9760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009762 typval_T *argvars;
9763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
9765 char_u *strregname;
9766 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009767 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009768 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009770 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 strregname = get_tv_string_chk(&argvars[0]);
9773 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009774 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009778 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 regname = (strregname == NULL ? '"' : *strregname);
9780 if (regname == 0)
9781 regname = '"';
9782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 rettv->vval.v_string = error ? NULL :
9785 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786}
9787
9788/*
9789 * "getregtype()" function
9790 */
9791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796 char_u *strregname;
9797 int regname;
9798 char_u buf[NUMBUFLEN + 2];
9799 long reglen = 0;
9800
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009801 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 {
9803 strregname = get_tv_string_chk(&argvars[0]);
9804 if (strregname == NULL) /* type error; errmsg already given */
9805 {
9806 rettv->v_type = VAR_STRING;
9807 rettv->vval.v_string = NULL;
9808 return;
9809 }
9810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 else
9812 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009813 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814
9815 regname = (strregname == NULL ? '"' : *strregname);
9816 if (regname == 0)
9817 regname = '"';
9818
9819 buf[0] = NUL;
9820 buf[1] = NUL;
9821 switch (get_reg_type(regname, &reglen))
9822 {
9823 case MLINE: buf[0] = 'V'; break;
9824 case MCHAR: buf[0] = 'v'; break;
9825#ifdef FEAT_VISUAL
9826 case MBLOCK:
9827 buf[0] = Ctrl_V;
9828 sprintf((char *)buf + 1, "%ld", reglen + 1);
9829 break;
9830#endif
9831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->v_type = VAR_STRING;
9833 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834}
9835
9836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 * "getwinposx()" function
9838 */
9839/*ARGSUSED*/
9840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 typval_T *argvars;
9843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846#ifdef FEAT_GUI
9847 if (gui.in_use)
9848 {
9849 int x, y;
9850
9851 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 }
9854#endif
9855}
9856
9857/*
9858 * "getwinposy()" function
9859 */
9860/*ARGSUSED*/
9861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 typval_T *argvars;
9864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867#ifdef FEAT_GUI
9868 if (gui.in_use)
9869 {
9870 int x, y;
9871
9872 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 }
9875#endif
9876}
9877
Bram Moolenaara40058a2005-07-11 22:42:07 +00009878static win_T *find_win_by_nr __ARGS((typval_T *vp));
9879
9880 static win_T *
9881find_win_by_nr(vp)
9882 typval_T *vp;
9883{
9884#ifdef FEAT_WINDOWS
9885 win_T *wp;
9886#endif
9887 int nr;
9888
9889 nr = get_tv_number_chk(vp, NULL);
9890
9891#ifdef FEAT_WINDOWS
9892 if (nr < 0)
9893 return NULL;
9894 if (nr == 0)
9895 return curwin;
9896
9897 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9898 if (--nr <= 0)
9899 break;
9900 return wp;
9901#else
9902 if (nr == 0 || nr == 1)
9903 return curwin;
9904 return NULL;
9905#endif
9906}
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908/*
9909 * "getwinvar()" function
9910 */
9911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 typval_T *argvars;
9914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 win_T *win, *oldcurwin;
9917 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009921 varname = get_tv_string_chk(&argvars[1]);
9922 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009924 rettv->v_type = VAR_STRING;
9925 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
9927 if (win != NULL && varname != NULL)
9928 {
9929 if (*varname == '&') /* window-local-option */
9930 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009931 /* Set curwin to be our win, temporarily. Also set curbuf, so
9932 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 oldcurwin = curwin;
9934 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009935 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938
9939 /* restore previous notion of curwin */
9940 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009941 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 }
9943 else
9944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 if (*varname == NUL)
9946 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9947 * scope prefix before the NUL byte is required by
9948 * find_var_in_ht(). */
9949 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009951 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009953 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 }
9955 }
9956
9957 --emsg_off;
9958}
9959
9960/*
9961 * "glob()" function
9962 */
9963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 typval_T *argvars;
9966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
9968 expand_T xpc;
9969
9970 ExpandInit(&xpc);
9971 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972 rettv->v_type = VAR_STRING;
9973 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9975 ExpandCleanup(&xpc);
9976}
9977
9978/*
9979 * "globpath()" function
9980 */
9981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009983 typval_T *argvars;
9984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
9986 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009987 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 if (file == NULL)
9991 rettv->vval.v_string = NULL;
9992 else
9993 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994}
9995
9996/*
9997 * "has()" function
9998 */
9999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 typval_T *argvars;
10002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 int i;
10005 char_u *name;
10006 int n = FALSE;
10007 static char *(has_list[]) =
10008 {
10009#ifdef AMIGA
10010 "amiga",
10011# ifdef FEAT_ARP
10012 "arp",
10013# endif
10014#endif
10015#ifdef __BEOS__
10016 "beos",
10017#endif
10018#ifdef MSDOS
10019# ifdef DJGPP
10020 "dos32",
10021# else
10022 "dos16",
10023# endif
10024#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010025#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 "mac",
10027#endif
10028#if defined(MACOS_X_UNIX)
10029 "macunix",
10030#endif
10031#ifdef OS2
10032 "os2",
10033#endif
10034#ifdef __QNX__
10035 "qnx",
10036#endif
10037#ifdef RISCOS
10038 "riscos",
10039#endif
10040#ifdef UNIX
10041 "unix",
10042#endif
10043#ifdef VMS
10044 "vms",
10045#endif
10046#ifdef WIN16
10047 "win16",
10048#endif
10049#ifdef WIN32
10050 "win32",
10051#endif
10052#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10053 "win32unix",
10054#endif
10055#ifdef WIN64
10056 "win64",
10057#endif
10058#ifdef EBCDIC
10059 "ebcdic",
10060#endif
10061#ifndef CASE_INSENSITIVE_FILENAME
10062 "fname_case",
10063#endif
10064#ifdef FEAT_ARABIC
10065 "arabic",
10066#endif
10067#ifdef FEAT_AUTOCMD
10068 "autocmd",
10069#endif
10070#ifdef FEAT_BEVAL
10071 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010072# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10073 "balloon_multiline",
10074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075#endif
10076#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10077 "builtin_terms",
10078# ifdef ALL_BUILTIN_TCAPS
10079 "all_builtin_terms",
10080# endif
10081#endif
10082#ifdef FEAT_BYTEOFF
10083 "byte_offset",
10084#endif
10085#ifdef FEAT_CINDENT
10086 "cindent",
10087#endif
10088#ifdef FEAT_CLIENTSERVER
10089 "clientserver",
10090#endif
10091#ifdef FEAT_CLIPBOARD
10092 "clipboard",
10093#endif
10094#ifdef FEAT_CMDL_COMPL
10095 "cmdline_compl",
10096#endif
10097#ifdef FEAT_CMDHIST
10098 "cmdline_hist",
10099#endif
10100#ifdef FEAT_COMMENTS
10101 "comments",
10102#endif
10103#ifdef FEAT_CRYPT
10104 "cryptv",
10105#endif
10106#ifdef FEAT_CSCOPE
10107 "cscope",
10108#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010109#ifdef CURSOR_SHAPE
10110 "cursorshape",
10111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112#ifdef DEBUG
10113 "debug",
10114#endif
10115#ifdef FEAT_CON_DIALOG
10116 "dialog_con",
10117#endif
10118#ifdef FEAT_GUI_DIALOG
10119 "dialog_gui",
10120#endif
10121#ifdef FEAT_DIFF
10122 "diff",
10123#endif
10124#ifdef FEAT_DIGRAPHS
10125 "digraphs",
10126#endif
10127#ifdef FEAT_DND
10128 "dnd",
10129#endif
10130#ifdef FEAT_EMACS_TAGS
10131 "emacs_tags",
10132#endif
10133 "eval", /* always present, of course! */
10134#ifdef FEAT_EX_EXTRA
10135 "ex_extra",
10136#endif
10137#ifdef FEAT_SEARCH_EXTRA
10138 "extra_search",
10139#endif
10140#ifdef FEAT_FKMAP
10141 "farsi",
10142#endif
10143#ifdef FEAT_SEARCHPATH
10144 "file_in_path",
10145#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010146#if defined(UNIX) && !defined(USE_SYSTEM)
10147 "filterpipe",
10148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149#ifdef FEAT_FIND_ID
10150 "find_in_path",
10151#endif
10152#ifdef FEAT_FOLDING
10153 "folding",
10154#endif
10155#ifdef FEAT_FOOTER
10156 "footer",
10157#endif
10158#if !defined(USE_SYSTEM) && defined(UNIX)
10159 "fork",
10160#endif
10161#ifdef FEAT_GETTEXT
10162 "gettext",
10163#endif
10164#ifdef FEAT_GUI
10165 "gui",
10166#endif
10167#ifdef FEAT_GUI_ATHENA
10168# ifdef FEAT_GUI_NEXTAW
10169 "gui_neXtaw",
10170# else
10171 "gui_athena",
10172# endif
10173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174#ifdef FEAT_GUI_GTK
10175 "gui_gtk",
10176# ifdef HAVE_GTK2
10177 "gui_gtk2",
10178# endif
10179#endif
10180#ifdef FEAT_GUI_MAC
10181 "gui_mac",
10182#endif
10183#ifdef FEAT_GUI_MOTIF
10184 "gui_motif",
10185#endif
10186#ifdef FEAT_GUI_PHOTON
10187 "gui_photon",
10188#endif
10189#ifdef FEAT_GUI_W16
10190 "gui_win16",
10191#endif
10192#ifdef FEAT_GUI_W32
10193 "gui_win32",
10194#endif
10195#ifdef FEAT_HANGULIN
10196 "hangul_input",
10197#endif
10198#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10199 "iconv",
10200#endif
10201#ifdef FEAT_INS_EXPAND
10202 "insert_expand",
10203#endif
10204#ifdef FEAT_JUMPLIST
10205 "jumplist",
10206#endif
10207#ifdef FEAT_KEYMAP
10208 "keymap",
10209#endif
10210#ifdef FEAT_LANGMAP
10211 "langmap",
10212#endif
10213#ifdef FEAT_LIBCALL
10214 "libcall",
10215#endif
10216#ifdef FEAT_LINEBREAK
10217 "linebreak",
10218#endif
10219#ifdef FEAT_LISP
10220 "lispindent",
10221#endif
10222#ifdef FEAT_LISTCMDS
10223 "listcmds",
10224#endif
10225#ifdef FEAT_LOCALMAP
10226 "localmap",
10227#endif
10228#ifdef FEAT_MENU
10229 "menu",
10230#endif
10231#ifdef FEAT_SESSION
10232 "mksession",
10233#endif
10234#ifdef FEAT_MODIFY_FNAME
10235 "modify_fname",
10236#endif
10237#ifdef FEAT_MOUSE
10238 "mouse",
10239#endif
10240#ifdef FEAT_MOUSESHAPE
10241 "mouseshape",
10242#endif
10243#if defined(UNIX) || defined(VMS)
10244# ifdef FEAT_MOUSE_DEC
10245 "mouse_dec",
10246# endif
10247# ifdef FEAT_MOUSE_GPM
10248 "mouse_gpm",
10249# endif
10250# ifdef FEAT_MOUSE_JSB
10251 "mouse_jsbterm",
10252# endif
10253# ifdef FEAT_MOUSE_NET
10254 "mouse_netterm",
10255# endif
10256# ifdef FEAT_MOUSE_PTERM
10257 "mouse_pterm",
10258# endif
10259# ifdef FEAT_MOUSE_XTERM
10260 "mouse_xterm",
10261# endif
10262#endif
10263#ifdef FEAT_MBYTE
10264 "multi_byte",
10265#endif
10266#ifdef FEAT_MBYTE_IME
10267 "multi_byte_ime",
10268#endif
10269#ifdef FEAT_MULTI_LANG
10270 "multi_lang",
10271#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010272#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010273#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010274 "mzscheme",
10275#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277#ifdef FEAT_OLE
10278 "ole",
10279#endif
10280#ifdef FEAT_OSFILETYPE
10281 "osfiletype",
10282#endif
10283#ifdef FEAT_PATH_EXTRA
10284 "path_extra",
10285#endif
10286#ifdef FEAT_PERL
10287#ifndef DYNAMIC_PERL
10288 "perl",
10289#endif
10290#endif
10291#ifdef FEAT_PYTHON
10292#ifndef DYNAMIC_PYTHON
10293 "python",
10294#endif
10295#endif
10296#ifdef FEAT_POSTSCRIPT
10297 "postscript",
10298#endif
10299#ifdef FEAT_PRINTER
10300 "printer",
10301#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010302#ifdef FEAT_PROFILE
10303 "profile",
10304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#ifdef FEAT_QUICKFIX
10306 "quickfix",
10307#endif
10308#ifdef FEAT_RIGHTLEFT
10309 "rightleft",
10310#endif
10311#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10312 "ruby",
10313#endif
10314#ifdef FEAT_SCROLLBIND
10315 "scrollbind",
10316#endif
10317#ifdef FEAT_CMDL_INFO
10318 "showcmd",
10319 "cmdline_info",
10320#endif
10321#ifdef FEAT_SIGNS
10322 "signs",
10323#endif
10324#ifdef FEAT_SMARTINDENT
10325 "smartindent",
10326#endif
10327#ifdef FEAT_SNIFF
10328 "sniff",
10329#endif
10330#ifdef FEAT_STL_OPT
10331 "statusline",
10332#endif
10333#ifdef FEAT_SUN_WORKSHOP
10334 "sun_workshop",
10335#endif
10336#ifdef FEAT_NETBEANS_INTG
10337 "netbeans_intg",
10338#endif
10339#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010340 "spell",
10341#endif
10342#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 "syntax",
10344#endif
10345#if defined(USE_SYSTEM) || !defined(UNIX)
10346 "system",
10347#endif
10348#ifdef FEAT_TAG_BINS
10349 "tag_binary",
10350#endif
10351#ifdef FEAT_TAG_OLDSTATIC
10352 "tag_old_static",
10353#endif
10354#ifdef FEAT_TAG_ANYWHITE
10355 "tag_any_white",
10356#endif
10357#ifdef FEAT_TCL
10358# ifndef DYNAMIC_TCL
10359 "tcl",
10360# endif
10361#endif
10362#ifdef TERMINFO
10363 "terminfo",
10364#endif
10365#ifdef FEAT_TERMRESPONSE
10366 "termresponse",
10367#endif
10368#ifdef FEAT_TEXTOBJ
10369 "textobjects",
10370#endif
10371#ifdef HAVE_TGETENT
10372 "tgetent",
10373#endif
10374#ifdef FEAT_TITLE
10375 "title",
10376#endif
10377#ifdef FEAT_TOOLBAR
10378 "toolbar",
10379#endif
10380#ifdef FEAT_USR_CMDS
10381 "user-commands", /* was accidentally included in 5.4 */
10382 "user_commands",
10383#endif
10384#ifdef FEAT_VIMINFO
10385 "viminfo",
10386#endif
10387#ifdef FEAT_VERTSPLIT
10388 "vertsplit",
10389#endif
10390#ifdef FEAT_VIRTUALEDIT
10391 "virtualedit",
10392#endif
10393#ifdef FEAT_VISUAL
10394 "visual",
10395#endif
10396#ifdef FEAT_VISUALEXTRA
10397 "visualextra",
10398#endif
10399#ifdef FEAT_VREPLACE
10400 "vreplace",
10401#endif
10402#ifdef FEAT_WILDIGN
10403 "wildignore",
10404#endif
10405#ifdef FEAT_WILDMENU
10406 "wildmenu",
10407#endif
10408#ifdef FEAT_WINDOWS
10409 "windows",
10410#endif
10411#ifdef FEAT_WAK
10412 "winaltkeys",
10413#endif
10414#ifdef FEAT_WRITEBACKUP
10415 "writebackup",
10416#endif
10417#ifdef FEAT_XIM
10418 "xim",
10419#endif
10420#ifdef FEAT_XFONTSET
10421 "xfontset",
10422#endif
10423#ifdef USE_XSMP
10424 "xsmp",
10425#endif
10426#ifdef USE_XSMP_INTERACT
10427 "xsmp_interact",
10428#endif
10429#ifdef FEAT_XCLIPBOARD
10430 "xterm_clipboard",
10431#endif
10432#ifdef FEAT_XTERM_SAVE
10433 "xterm_save",
10434#endif
10435#if defined(UNIX) && defined(FEAT_X11)
10436 "X11",
10437#endif
10438 NULL
10439 };
10440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 for (i = 0; has_list[i] != NULL; ++i)
10443 if (STRICMP(name, has_list[i]) == 0)
10444 {
10445 n = TRUE;
10446 break;
10447 }
10448
10449 if (n == FALSE)
10450 {
10451 if (STRNICMP(name, "patch", 5) == 0)
10452 n = has_patch(atoi((char *)name + 5));
10453 else if (STRICMP(name, "vim_starting") == 0)
10454 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010455#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10456 else if (STRICMP(name, "balloon_multiline") == 0)
10457 n = multiline_balloon_available();
10458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459#ifdef DYNAMIC_TCL
10460 else if (STRICMP(name, "tcl") == 0)
10461 n = tcl_enabled(FALSE);
10462#endif
10463#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10464 else if (STRICMP(name, "iconv") == 0)
10465 n = iconv_enabled(FALSE);
10466#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010467#ifdef DYNAMIC_MZSCHEME
10468 else if (STRICMP(name, "mzscheme") == 0)
10469 n = mzscheme_enabled(FALSE);
10470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471#ifdef DYNAMIC_RUBY
10472 else if (STRICMP(name, "ruby") == 0)
10473 n = ruby_enabled(FALSE);
10474#endif
10475#ifdef DYNAMIC_PYTHON
10476 else if (STRICMP(name, "python") == 0)
10477 n = python_enabled(FALSE);
10478#endif
10479#ifdef DYNAMIC_PERL
10480 else if (STRICMP(name, "perl") == 0)
10481 n = perl_enabled(FALSE);
10482#endif
10483#ifdef FEAT_GUI
10484 else if (STRICMP(name, "gui_running") == 0)
10485 n = (gui.in_use || gui.starting);
10486# ifdef FEAT_GUI_W32
10487 else if (STRICMP(name, "gui_win32s") == 0)
10488 n = gui_is_win32s();
10489# endif
10490# ifdef FEAT_BROWSE
10491 else if (STRICMP(name, "browse") == 0)
10492 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10493# endif
10494#endif
10495#ifdef FEAT_SYN_HL
10496 else if (STRICMP(name, "syntax_items") == 0)
10497 n = syntax_present(curbuf);
10498#endif
10499#if defined(WIN3264)
10500 else if (STRICMP(name, "win95") == 0)
10501 n = mch_windows95();
10502#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010503#ifdef FEAT_NETBEANS_INTG
10504 else if (STRICMP(name, "netbeans_enabled") == 0)
10505 n = usingNetbeans;
10506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 }
10508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510}
10511
10512/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010513 * "has_key()" function
10514 */
10515 static void
10516f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 typval_T *argvars;
10518 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010519{
10520 rettv->vval.v_number = 0;
10521 if (argvars[0].v_type != VAR_DICT)
10522 {
10523 EMSG(_(e_dictreq));
10524 return;
10525 }
10526 if (argvars[0].vval.v_dict == NULL)
10527 return;
10528
10529 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010530 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010531}
10532
10533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 * "hasmapto()" function
10535 */
10536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010538 typval_T *argvars;
10539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540{
10541 char_u *name;
10542 char_u *mode;
10543 char_u buf[NUMBUFLEN];
10544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010546 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 mode = (char_u *)"nvo";
10548 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550
10551 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555}
10556
10557/*
10558 * "histadd()" function
10559 */
10560/*ARGSUSED*/
10561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010562f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010563 typval_T *argvars;
10564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565{
10566#ifdef FEAT_CMDHIST
10567 int histype;
10568 char_u *str;
10569 char_u buf[NUMBUFLEN];
10570#endif
10571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 if (check_restricted() || check_secure())
10574 return;
10575#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10577 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 if (histype >= 0)
10579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 if (*str != NUL)
10582 {
10583 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 return;
10586 }
10587 }
10588#endif
10589}
10590
10591/*
10592 * "histdel()" function
10593 */
10594/*ARGSUSED*/
10595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010597 typval_T *argvars;
10598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599{
10600#ifdef FEAT_CMDHIST
10601 int n;
10602 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010603 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10606 if (str == NULL)
10607 n = 0;
10608 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010611 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010613 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 else
10616 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 get_tv_string_buf(&argvars[1], buf));
10619 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622#endif
10623}
10624
10625/*
10626 * "histget()" function
10627 */
10628/*ARGSUSED*/
10629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634#ifdef FEAT_CMDHIST
10635 int type;
10636 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010637 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010639 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10640 if (str == NULL)
10641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 {
10644 type = get_histtype(str);
10645 if (argvars[1].v_type == VAR_UNKNOWN)
10646 idx = get_history_idx(type);
10647 else
10648 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10649 /* -1 on type error */
10650 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010655 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656}
10657
10658/*
10659 * "histnr()" function
10660 */
10661/*ARGSUSED*/
10662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010664 typval_T *argvars;
10665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666{
10667 int i;
10668
10669#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 char_u *history = get_tv_string_chk(&argvars[0]);
10671
10672 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 if (i >= HIST_CMD && i < HIST_COUNT)
10674 i = get_history_idx(i);
10675 else
10676#endif
10677 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
10681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 * "highlightID(name)" function
10683 */
10684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010686 typval_T *argvars;
10687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690}
10691
10692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010693 * "highlight_exists()" function
10694 */
10695 static void
10696f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 typval_T *argvars;
10698 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010699{
10700 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10701}
10702
10703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 * "hostname()" function
10705 */
10706/*ARGSUSED*/
10707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010709 typval_T *argvars;
10710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
10712 char_u hostname[256];
10713
10714 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 rettv->v_type = VAR_STRING;
10716 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717}
10718
10719/*
10720 * iconv() function
10721 */
10722/*ARGSUSED*/
10723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T *argvars;
10726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727{
10728#ifdef FEAT_MBYTE
10729 char_u buf1[NUMBUFLEN];
10730 char_u buf2[NUMBUFLEN];
10731 char_u *from, *to, *str;
10732 vimconv_T vimconv;
10733#endif
10734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->v_type = VAR_STRING;
10736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737
10738#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739 str = get_tv_string(&argvars[0]);
10740 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10741 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 vimconv.vc_type = CONV_NONE;
10743 convert_setup(&vimconv, from, to);
10744
10745 /* If the encodings are equal, no conversion needed. */
10746 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750
10751 convert_setup(&vimconv, NULL, NULL);
10752 vim_free(from);
10753 vim_free(to);
10754#endif
10755}
10756
10757/*
10758 * "indent()" function
10759 */
10760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 typval_T *argvars;
10763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764{
10765 linenr_T lnum;
10766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010771 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772}
10773
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010774/*
10775 * "index()" function
10776 */
10777 static void
10778f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 typval_T *argvars;
10780 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010781{
Bram Moolenaar33570922005-01-25 22:26:29 +000010782 list_T *l;
10783 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010784 long idx = 0;
10785 int ic = FALSE;
10786
10787 rettv->vval.v_number = -1;
10788 if (argvars[0].v_type != VAR_LIST)
10789 {
10790 EMSG(_(e_listreq));
10791 return;
10792 }
10793 l = argvars[0].vval.v_list;
10794 if (l != NULL)
10795 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010796 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010799 int error = FALSE;
10800
Bram Moolenaar758711c2005-02-02 23:11:38 +000010801 /* Start at specified item. Use the cached index that list_find()
10802 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010804 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010805 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 ic = get_tv_number_chk(&argvars[3], &error);
10807 if (error)
10808 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010809 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010810
Bram Moolenaar758711c2005-02-02 23:11:38 +000010811 for ( ; item != NULL; item = item->li_next, ++idx)
10812 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010813 {
10814 rettv->vval.v_number = idx;
10815 break;
10816 }
10817 }
10818}
10819
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820static int inputsecret_flag = 0;
10821
10822/*
10823 * "input()" function
10824 * Also handles inputsecret() when inputsecret is set.
10825 */
10826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 typval_T *argvars;
10829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010831 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 char_u *p = NULL;
10833 int c;
10834 char_u buf[NUMBUFLEN];
10835 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010837 int xp_type = EXPAND_NOTHING;
10838 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841
10842#ifdef NO_CONSOLE_INPUT
10843 /* While starting up, there is no place to enter text. */
10844 if (no_console_input())
10845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 return;
10848 }
10849#endif
10850
10851 cmd_silent = FALSE; /* Want to see the prompt. */
10852 if (prompt != NULL)
10853 {
10854 /* Only the part of the message after the last NL is considered as
10855 * prompt for the command line */
10856 p = vim_strrchr(prompt, '\n');
10857 if (p == NULL)
10858 p = prompt;
10859 else
10860 {
10861 ++p;
10862 c = *p;
10863 *p = NUL;
10864 msg_start();
10865 msg_clr_eos();
10866 msg_puts_attr(prompt, echo_attr);
10867 msg_didout = FALSE;
10868 msg_starthere();
10869 *p = c;
10870 }
10871 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 if (argvars[1].v_type != VAR_UNKNOWN)
10874 {
10875 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10876 if (defstr != NULL)
10877 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878
Bram Moolenaar4463f292005-09-25 22:20:24 +000010879 if (argvars[2].v_type != VAR_UNKNOWN)
10880 {
10881 char_u *xp_name;
10882 int xp_namelen;
10883 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010884
Bram Moolenaar4463f292005-09-25 22:20:24 +000010885 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010886
Bram Moolenaar4463f292005-09-25 22:20:24 +000010887 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10888 if (xp_name == NULL)
10889 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010890
Bram Moolenaar4463f292005-09-25 22:20:24 +000010891 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010892
Bram Moolenaar4463f292005-09-25 22:20:24 +000010893 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10894 &xp_arg) == FAIL)
10895 return;
10896 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010897 }
10898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 if (defstr != NULL)
10900 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010901 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10902 xp_type, xp_arg);
10903
10904 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 /* since the user typed this, no need to wait for return */
10907 need_wait_return = FALSE;
10908 msg_didout = FALSE;
10909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 cmd_silent = cmd_silent_save;
10911}
10912
10913/*
10914 * "inputdialog()" function
10915 */
10916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010918 typval_T *argvars;
10919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921#if defined(FEAT_GUI_TEXTDIALOG)
10922 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10923 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10924 {
10925 char_u *message;
10926 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 message = get_tv_string_chk(&argvars[0]);
10930 if (argvars[1].v_type != VAR_UNKNOWN
10931 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010932 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 else
10934 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010935 if (message != NULL && defstr != NULL
10936 && do_dialog(VIM_QUESTION, NULL, message,
10937 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 else
10940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 if (message != NULL && defstr != NULL
10942 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->vval.v_string = vim_strsave(
10945 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 }
10951 else
10952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954}
10955
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010956/*
10957 * "inputlist()" function
10958 */
10959 static void
10960f_inputlist(argvars, rettv)
10961 typval_T *argvars;
10962 typval_T *rettv;
10963{
10964 listitem_T *li;
10965 int selected;
10966 int mouse_used;
10967
10968 rettv->vval.v_number = 0;
10969#ifdef NO_CONSOLE_INPUT
10970 /* While starting up, there is no place to enter text. */
10971 if (no_console_input())
10972 return;
10973#endif
10974 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10975 {
10976 EMSG2(_(e_listarg), "inputlist()");
10977 return;
10978 }
10979
10980 msg_start();
10981 lines_left = Rows; /* avoid more prompt */
10982 msg_scroll = TRUE;
10983 msg_clr_eos();
10984
10985 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10986 {
10987 msg_puts(get_tv_string(&li->li_tv));
10988 msg_putchar('\n');
10989 }
10990
10991 /* Ask for choice. */
10992 selected = prompt_for_number(&mouse_used);
10993 if (mouse_used)
10994 selected -= lines_left;
10995
10996 rettv->vval.v_number = selected;
10997}
10998
10999
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11001
11002/*
11003 * "inputrestore()" function
11004 */
11005/*ARGSUSED*/
11006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 typval_T *argvars;
11009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
11011 if (ga_userinput.ga_len > 0)
11012 {
11013 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11015 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 }
11018 else if (p_verbose > 1)
11019 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011020 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 }
11023}
11024
11025/*
11026 * "inputsave()" function
11027 */
11028/*ARGSUSED*/
11029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 typval_T *argvars;
11032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
11034 /* Add an entry to the stack of typehead storage. */
11035 if (ga_grow(&ga_userinput, 1) == OK)
11036 {
11037 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11038 + ga_userinput.ga_len);
11039 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 }
11042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011043 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044}
11045
11046/*
11047 * "inputsecret()" function
11048 */
11049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011051 typval_T *argvars;
11052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053{
11054 ++cmdline_star;
11055 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 --cmdline_star;
11058 --inputsecret_flag;
11059}
11060
11061/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011062 * "insert()" function
11063 */
11064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011066 typval_T *argvars;
11067 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011068{
11069 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011070 listitem_T *item;
11071 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011073
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011074 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011075 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011076 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011077 else if ((l = argvars[0].vval.v_list) != NULL
11078 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011079 {
11080 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 before = get_tv_number_chk(&argvars[2], &error);
11082 if (error)
11083 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011084
Bram Moolenaar758711c2005-02-02 23:11:38 +000011085 if (before == l->lv_len)
11086 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011087 else
11088 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011089 item = list_find(l, before);
11090 if (item == NULL)
11091 {
11092 EMSGN(_(e_listidx), before);
11093 l = NULL;
11094 }
11095 }
11096 if (l != NULL)
11097 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011098 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011099 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011100 }
11101 }
11102}
11103
11104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 * "isdirectory()" function
11106 */
11107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011109 typval_T *argvars;
11110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113}
11114
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011115/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011116 * "islocked()" function
11117 */
11118 static void
11119f_islocked(argvars, rettv)
11120 typval_T *argvars;
11121 typval_T *rettv;
11122{
11123 lval_T lv;
11124 char_u *end;
11125 dictitem_T *di;
11126
11127 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011128 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11129 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011130 if (end != NULL && lv.ll_name != NULL)
11131 {
11132 if (*end != NUL)
11133 EMSG(_(e_trailing));
11134 else
11135 {
11136 if (lv.ll_tv == NULL)
11137 {
11138 if (check_changedtick(lv.ll_name))
11139 rettv->vval.v_number = 1; /* always locked */
11140 else
11141 {
11142 di = find_var(lv.ll_name, NULL);
11143 if (di != NULL)
11144 {
11145 /* Consider a variable locked when:
11146 * 1. the variable itself is locked
11147 * 2. the value of the variable is locked.
11148 * 3. the List or Dict value is locked.
11149 */
11150 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11151 || tv_islocked(&di->di_tv));
11152 }
11153 }
11154 }
11155 else if (lv.ll_range)
11156 EMSG(_("E745: Range not allowed"));
11157 else if (lv.ll_newkey != NULL)
11158 EMSG2(_(e_dictkey), lv.ll_newkey);
11159 else if (lv.ll_list != NULL)
11160 /* List item. */
11161 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11162 else
11163 /* Dictionary item. */
11164 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11165 }
11166 }
11167
11168 clear_lval(&lv);
11169}
11170
Bram Moolenaar33570922005-01-25 22:26:29 +000011171static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011172
11173/*
11174 * Turn a dict into a list:
11175 * "what" == 0: list of keys
11176 * "what" == 1: list of values
11177 * "what" == 2: list of items
11178 */
11179 static void
11180dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 typval_T *argvars;
11182 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011183 int what;
11184{
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 list_T *l;
11186 list_T *l2;
11187 dictitem_T *di;
11188 hashitem_T *hi;
11189 listitem_T *li;
11190 listitem_T *li2;
11191 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011192 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011193
11194 rettv->vval.v_number = 0;
11195 if (argvars[0].v_type != VAR_DICT)
11196 {
11197 EMSG(_(e_dictreq));
11198 return;
11199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011200 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011201 return;
11202
11203 l = list_alloc();
11204 if (l == NULL)
11205 return;
11206 rettv->v_type = VAR_LIST;
11207 rettv->vval.v_list = l;
11208 ++l->lv_refcount;
11209
Bram Moolenaar33570922005-01-25 22:26:29 +000011210 todo = d->dv_hashtab.ht_used;
11211 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011213 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011215 --todo;
11216 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011217
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011218 li = listitem_alloc();
11219 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011220 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011221 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011222
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011223 if (what == 0)
11224 {
11225 /* keys() */
11226 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011227 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011228 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11229 }
11230 else if (what == 1)
11231 {
11232 /* values() */
11233 copy_tv(&di->di_tv, &li->li_tv);
11234 }
11235 else
11236 {
11237 /* items() */
11238 l2 = list_alloc();
11239 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011240 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011241 li->li_tv.vval.v_list = l2;
11242 if (l2 == NULL)
11243 break;
11244 ++l2->lv_refcount;
11245
11246 li2 = listitem_alloc();
11247 if (li2 == NULL)
11248 break;
11249 list_append(l2, li2);
11250 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011251 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011252 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11253
11254 li2 = listitem_alloc();
11255 if (li2 == NULL)
11256 break;
11257 list_append(l2, li2);
11258 copy_tv(&di->di_tv, &li2->li_tv);
11259 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011260 }
11261 }
11262}
11263
11264/*
11265 * "items(dict)" function
11266 */
11267 static void
11268f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011269 typval_T *argvars;
11270 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011271{
11272 dict_list(argvars, rettv, 2);
11273}
11274
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011276 * "join()" function
11277 */
11278 static void
11279f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011280 typval_T *argvars;
11281 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011282{
11283 garray_T ga;
11284 char_u *sep;
11285
11286 rettv->vval.v_number = 0;
11287 if (argvars[0].v_type != VAR_LIST)
11288 {
11289 EMSG(_(e_listreq));
11290 return;
11291 }
11292 if (argvars[0].vval.v_list == NULL)
11293 return;
11294 if (argvars[1].v_type == VAR_UNKNOWN)
11295 sep = (char_u *)" ";
11296 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011298
11299 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300
11301 if (sep != NULL)
11302 {
11303 ga_init2(&ga, (int)sizeof(char), 80);
11304 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11305 ga_append(&ga, NUL);
11306 rettv->vval.v_string = (char_u *)ga.ga_data;
11307 }
11308 else
11309 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011310}
11311
11312/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011313 * "keys()" function
11314 */
11315 static void
11316f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 typval_T *argvars;
11318 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011319{
11320 dict_list(argvars, rettv, 0);
11321}
11322
11323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 * "last_buffer_nr()" function.
11325 */
11326/*ARGSUSED*/
11327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 typval_T *argvars;
11330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
11332 int n = 0;
11333 buf_T *buf;
11334
11335 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11336 if (n < buf->b_fnum)
11337 n = buf->b_fnum;
11338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011340}
11341
11342/*
11343 * "len()" function
11344 */
11345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *argvars;
11348 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011349{
11350 switch (argvars[0].v_type)
11351 {
11352 case VAR_STRING:
11353 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->vval.v_number = (varnumber_T)STRLEN(
11355 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011356 break;
11357 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011359 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011360 case VAR_DICT:
11361 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11362 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011363 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011364 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011365 break;
11366 }
11367}
11368
Bram Moolenaar33570922005-01-25 22:26:29 +000011369static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011370
11371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *argvars;
11374 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011375 int type;
11376{
11377#ifdef FEAT_LIBCALL
11378 char_u *string_in;
11379 char_u **string_result;
11380 int nr_result;
11381#endif
11382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011384 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011388
11389 if (check_restricted() || check_secure())
11390 return;
11391
11392#ifdef FEAT_LIBCALL
11393 /* The first two args must be strings, otherwise its meaningless */
11394 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11395 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011396 string_in = NULL;
11397 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011398 string_in = argvars[2].vval.v_string;
11399 if (type == VAR_NUMBER)
11400 string_result = NULL;
11401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011403 if (mch_libcall(argvars[0].vval.v_string,
11404 argvars[1].vval.v_string,
11405 string_in,
11406 argvars[2].vval.v_number,
11407 string_result,
11408 &nr_result) == OK
11409 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011411 }
11412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413}
11414
11415/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011416 * "libcall()" function
11417 */
11418 static void
11419f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 typval_T *argvars;
11421 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011422{
11423 libcall_common(argvars, rettv, VAR_STRING);
11424}
11425
11426/*
11427 * "libcallnr()" function
11428 */
11429 static void
11430f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T *argvars;
11432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011433{
11434 libcall_common(argvars, rettv, VAR_NUMBER);
11435}
11436
11437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 * "line(string)" function
11439 */
11440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011442 typval_T *argvars;
11443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444{
11445 linenr_T lnum = 0;
11446 pos_T *fp;
11447
11448 fp = var2fpos(&argvars[0], TRUE);
11449 if (fp != NULL)
11450 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452}
11453
11454/*
11455 * "line2byte(lnum)" function
11456 */
11457/*ARGSUSED*/
11458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011460 typval_T *argvars;
11461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462{
11463#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465#else
11466 linenr_T lnum;
11467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11473 if (rettv->vval.v_number >= 0)
11474 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475#endif
11476}
11477
11478/*
11479 * "lispindent(lnum)" function
11480 */
11481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011483 typval_T *argvars;
11484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485{
11486#ifdef FEAT_LISP
11487 pos_T pos;
11488 linenr_T lnum;
11489
11490 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11493 {
11494 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 curwin->w_cursor = pos;
11497 }
11498 else
11499#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501}
11502
11503/*
11504 * "localtime()" function
11505 */
11506/*ARGSUSED*/
11507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 typval_T *argvars;
11510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513}
11514
Bram Moolenaar33570922005-01-25 22:26:29 +000011515static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516
11517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011519 typval_T *argvars;
11520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 int exact;
11522{
11523 char_u *keys;
11524 char_u *which;
11525 char_u buf[NUMBUFLEN];
11526 char_u *keys_buf = NULL;
11527 char_u *rhs;
11528 int mode;
11529 garray_T ga;
11530
11531 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011532 rettv->v_type = VAR_STRING;
11533 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 if (*keys == NUL)
11537 return;
11538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011539 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011540 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 else
11542 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 if (which == NULL)
11544 return;
11545
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 mode = get_map_mode(&which, 0);
11547
11548 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011549 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 vim_free(keys_buf);
11551 if (rhs != NULL)
11552 {
11553 ga_init(&ga);
11554 ga.ga_itemsize = 1;
11555 ga.ga_growsize = 40;
11556
11557 while (*rhs != NUL)
11558 ga_concat(&ga, str2special(&rhs, FALSE));
11559
11560 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 }
11563}
11564
11565/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011566 * "map()" function
11567 */
11568 static void
11569f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011570 typval_T *argvars;
11571 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011572{
11573 filter_map(argvars, rettv, TRUE);
11574}
11575
11576/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 */
11579 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T *argvars;
11582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585}
11586
11587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011588 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 */
11590 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011592 typval_T *argvars;
11593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596}
11597
Bram Moolenaar33570922005-01-25 22:26:29 +000011598static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599
11600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011601find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011602 typval_T *argvars;
11603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 int type;
11605{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011606 char_u *str = NULL;
11607 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 char_u *pat;
11609 regmatch_T regmatch;
11610 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011611 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 char_u *save_cpo;
11613 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011614 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011615 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 list_T *l = NULL;
11617 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011618 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011619 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620
11621 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11622 save_cpo = p_cpo;
11623 p_cpo = (char_u *)"";
11624
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011625 rettv->vval.v_number = -1;
11626 if (type == 3)
11627 {
11628 /* return empty list when there are no matches */
11629 if ((rettv->vval.v_list = list_alloc()) == NULL)
11630 goto theend;
11631 rettv->v_type = VAR_LIST;
11632 ++rettv->vval.v_list->lv_refcount;
11633 }
11634 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 rettv->v_type = VAR_STRING;
11637 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011640 if (argvars[0].v_type == VAR_LIST)
11641 {
11642 if ((l = argvars[0].vval.v_list) == NULL)
11643 goto theend;
11644 li = l->lv_first;
11645 }
11646 else
11647 expr = str = get_tv_string(&argvars[0]);
11648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011649 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11650 if (pat == NULL)
11651 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011652
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011653 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011655 int error = FALSE;
11656
11657 start = get_tv_number_chk(&argvars[2], &error);
11658 if (error)
11659 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011660 if (l != NULL)
11661 {
11662 li = list_find(l, start);
11663 if (li == NULL)
11664 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011665 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011666 }
11667 else
11668 {
11669 if (start < 0)
11670 start = 0;
11671 if (start > (long)STRLEN(str))
11672 goto theend;
11673 str += start;
11674 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011675
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011676 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 nth = get_tv_number_chk(&argvars[3], &error);
11678 if (error)
11679 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 }
11681
11682 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11683 if (regmatch.regprog != NULL)
11684 {
11685 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011686
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011687 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011688 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011689 if (l != NULL)
11690 {
11691 if (li == NULL)
11692 {
11693 match = FALSE;
11694 break;
11695 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011696 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011697 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011698 if (str == NULL)
11699 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011700 }
11701
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011702 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011703
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011704 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011705 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011706 if (l == NULL && !match)
11707 break;
11708
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011709 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011710 if (l != NULL)
11711 {
11712 li = li->li_next;
11713 ++idx;
11714 }
11715 else
11716 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011717#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011718 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011719#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011720 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011721#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011722 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011723 }
11724
11725 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011727 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011729 int i;
11730
11731 /* return list with matched string and submatches */
11732 for (i = 0; i < NSUBEXP; ++i)
11733 {
11734 if (regmatch.endp[i] == NULL)
11735 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011736 if (list_append_string(rettv->vval.v_list,
11737 regmatch.startp[i],
11738 (int)(regmatch.endp[i] - regmatch.startp[i]))
11739 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011740 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011741 }
11742 }
11743 else if (type == 2)
11744 {
11745 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011746 if (l != NULL)
11747 copy_tv(&li->li_tv, rettv);
11748 else
11749 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011751 }
11752 else if (l != NULL)
11753 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 else
11755 {
11756 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 (varnumber_T)(regmatch.startp[0] - str);
11759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 }
11764 }
11765 vim_free(regmatch.regprog);
11766 }
11767
11768theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011769 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 p_cpo = save_cpo;
11771}
11772
11773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011774 * "match()" function
11775 */
11776 static void
11777f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011778 typval_T *argvars;
11779 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011780{
11781 find_some_match(argvars, rettv, 1);
11782}
11783
11784/*
11785 * "matchend()" function
11786 */
11787 static void
11788f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011791{
11792 find_some_match(argvars, rettv, 0);
11793}
11794
11795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011796 * "matchlist()" function
11797 */
11798 static void
11799f_matchlist(argvars, rettv)
11800 typval_T *argvars;
11801 typval_T *rettv;
11802{
11803 find_some_match(argvars, rettv, 3);
11804}
11805
11806/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011807 * "matchstr()" function
11808 */
11809 static void
11810f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 typval_T *argvars;
11812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011813{
11814 find_some_match(argvars, rettv, 2);
11815}
11816
Bram Moolenaar33570922005-01-25 22:26:29 +000011817static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011818
11819 static void
11820max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011821 typval_T *argvars;
11822 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011823 int domax;
11824{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011825 long n = 0;
11826 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011828
11829 if (argvars[0].v_type == VAR_LIST)
11830 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011831 list_T *l;
11832 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011833
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011834 l = argvars[0].vval.v_list;
11835 if (l != NULL)
11836 {
11837 li = l->lv_first;
11838 if (li != NULL)
11839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011841 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011842 {
11843 li = li->li_next;
11844 if (li == NULL)
11845 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011846 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011847 if (domax ? i > n : i < n)
11848 n = i;
11849 }
11850 }
11851 }
11852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011853 else if (argvars[0].v_type == VAR_DICT)
11854 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011856 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011858 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011859
11860 d = argvars[0].vval.v_dict;
11861 if (d != NULL)
11862 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 todo = d->dv_hashtab.ht_used;
11864 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011866 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011868 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011869 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011870 if (first)
11871 {
11872 n = i;
11873 first = FALSE;
11874 }
11875 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876 n = i;
11877 }
11878 }
11879 }
11880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011881 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011882 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011884}
11885
11886/*
11887 * "max()" function
11888 */
11889 static void
11890f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 typval_T *argvars;
11892 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011893{
11894 max_min(argvars, rettv, TRUE);
11895}
11896
11897/*
11898 * "min()" function
11899 */
11900 static void
11901f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011904{
11905 max_min(argvars, rettv, FALSE);
11906}
11907
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011908static int mkdir_recurse __ARGS((char_u *dir, int prot));
11909
11910/*
11911 * Create the directory in which "dir" is located, and higher levels when
11912 * needed.
11913 */
11914 static int
11915mkdir_recurse(dir, prot)
11916 char_u *dir;
11917 int prot;
11918{
11919 char_u *p;
11920 char_u *updir;
11921 int r = FAIL;
11922
11923 /* Get end of directory name in "dir".
11924 * We're done when it's "/" or "c:/". */
11925 p = gettail_sep(dir);
11926 if (p <= get_past_head(dir))
11927 return OK;
11928
11929 /* If the directory exists we're done. Otherwise: create it.*/
11930 updir = vim_strnsave(dir, (int)(p - dir));
11931 if (updir == NULL)
11932 return FAIL;
11933 if (mch_isdir(updir))
11934 r = OK;
11935 else if (mkdir_recurse(updir, prot) == OK)
11936 r = vim_mkdir_emsg(updir, prot);
11937 vim_free(updir);
11938 return r;
11939}
11940
11941#ifdef vim_mkdir
11942/*
11943 * "mkdir()" function
11944 */
11945 static void
11946f_mkdir(argvars, rettv)
11947 typval_T *argvars;
11948 typval_T *rettv;
11949{
11950 char_u *dir;
11951 char_u buf[NUMBUFLEN];
11952 int prot = 0755;
11953
11954 rettv->vval.v_number = FAIL;
11955 if (check_restricted() || check_secure())
11956 return;
11957
11958 dir = get_tv_string_buf(&argvars[0], buf);
11959 if (argvars[1].v_type != VAR_UNKNOWN)
11960 {
11961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011962 prot = get_tv_number_chk(&argvars[2], NULL);
11963 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011964 mkdir_recurse(dir, prot);
11965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011967}
11968#endif
11969
Bram Moolenaar0d660222005-01-07 21:51:51 +000011970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 * "mode()" function
11972 */
11973/*ARGSUSED*/
11974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978{
11979 char_u buf[2];
11980
11981#ifdef FEAT_VISUAL
11982 if (VIsual_active)
11983 {
11984 if (VIsual_select)
11985 buf[0] = VIsual_mode + 's' - 'v';
11986 else
11987 buf[0] = VIsual_mode;
11988 }
11989 else
11990#endif
11991 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11992 buf[0] = 'r';
11993 else if (State & INSERT)
11994 {
11995 if (State & REPLACE_FLAG)
11996 buf[0] = 'R';
11997 else
11998 buf[0] = 'i';
11999 }
12000 else if (State & CMDLINE)
12001 buf[0] = 'c';
12002 else
12003 buf[0] = 'n';
12004
12005 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012006 rettv->vval.v_string = vim_strsave(buf);
12007 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008}
12009
12010/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012011 * "nextnonblank()" function
12012 */
12013 static void
12014f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012015 typval_T *argvars;
12016 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012017{
12018 linenr_T lnum;
12019
12020 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012022 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012023 {
12024 lnum = 0;
12025 break;
12026 }
12027 if (*skipwhite(ml_get(lnum)) != NUL)
12028 break;
12029 }
12030 rettv->vval.v_number = lnum;
12031}
12032
12033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 * "nr2char()" function
12035 */
12036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T *argvars;
12039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040{
12041 char_u buf[NUMBUFLEN];
12042
12043#ifdef FEAT_MBYTE
12044 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 else
12047#endif
12048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 buf[1] = NUL;
12051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 rettv->v_type = VAR_STRING;
12053 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012054}
12055
12056/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012057 * "prevnonblank()" function
12058 */
12059 static void
12060f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012061 typval_T *argvars;
12062 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063{
12064 linenr_T lnum;
12065
12066 lnum = get_tv_lnum(argvars);
12067 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12068 lnum = 0;
12069 else
12070 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12071 --lnum;
12072 rettv->vval.v_number = lnum;
12073}
12074
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012075#ifdef HAVE_STDARG_H
12076/* This dummy va_list is here because:
12077 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12078 * - locally in the function results in a "used before set" warning
12079 * - using va_start() to initialize it gives "function with fixed args" error */
12080static va_list ap;
12081#endif
12082
Bram Moolenaar8c711452005-01-14 21:53:12 +000012083/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012084 * "printf()" function
12085 */
12086 static void
12087f_printf(argvars, rettv)
12088 typval_T *argvars;
12089 typval_T *rettv;
12090{
12091 rettv->v_type = VAR_STRING;
12092 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012093#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012094 {
12095 char_u buf[NUMBUFLEN];
12096 int len;
12097 char_u *s;
12098 int saved_did_emsg = did_emsg;
12099 char *fmt;
12100
12101 /* Get the required length, allocate the buffer and do it for real. */
12102 did_emsg = FALSE;
12103 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012104 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012105 if (!did_emsg)
12106 {
12107 s = alloc(len + 1);
12108 if (s != NULL)
12109 {
12110 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012111 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012112 }
12113 }
12114 did_emsg |= saved_did_emsg;
12115 }
12116#endif
12117}
12118
12119/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012120 * "range()" function
12121 */
12122 static void
12123f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T *argvars;
12125 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012126{
12127 long start;
12128 long end;
12129 long stride = 1;
12130 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012132 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012134 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012135 if (argvars[1].v_type == VAR_UNKNOWN)
12136 {
12137 end = start - 1;
12138 start = 0;
12139 }
12140 else
12141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012142 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012143 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012145 }
12146
12147 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012148 if (error)
12149 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012150 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012151 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012152 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012153 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012154 else
12155 {
12156 l = list_alloc();
12157 if (l != NULL)
12158 {
12159 rettv->v_type = VAR_LIST;
12160 rettv->vval.v_list = l;
12161 ++l->lv_refcount;
12162
12163 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012164 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012165 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012166 }
12167 }
12168}
12169
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012170/*
12171 * "readfile()" function
12172 */
12173 static void
12174f_readfile(argvars, rettv)
12175 typval_T *argvars;
12176 typval_T *rettv;
12177{
12178 int binary = FALSE;
12179 char_u *fname;
12180 FILE *fd;
12181 list_T *l;
12182 listitem_T *li;
12183#define FREAD_SIZE 200 /* optimized for text lines */
12184 char_u buf[FREAD_SIZE];
12185 int readlen; /* size of last fread() */
12186 int buflen; /* nr of valid chars in buf[] */
12187 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12188 int tolist; /* first byte in buf[] still to be put in list */
12189 int chop; /* how many CR to chop off */
12190 char_u *prev = NULL; /* previously read bytes, if any */
12191 int prevlen = 0; /* length of "prev" if not NULL */
12192 char_u *s;
12193 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012194 long maxline = MAXLNUM;
12195 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012196
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012197 if (argvars[1].v_type != VAR_UNKNOWN)
12198 {
12199 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12200 binary = TRUE;
12201 if (argvars[2].v_type != VAR_UNKNOWN)
12202 maxline = get_tv_number(&argvars[2]);
12203 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012204
12205 l = list_alloc();
12206 if (l == NULL)
12207 return;
12208 rettv->v_type = VAR_LIST;
12209 rettv->vval.v_list = l;
12210 l->lv_refcount = 1;
12211
12212 /* Always open the file in binary mode, library functions have a mind of
12213 * their own about CR-LF conversion. */
12214 fname = get_tv_string(&argvars[0]);
12215 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12216 {
12217 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12218 return;
12219 }
12220
12221 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012222 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012223 {
12224 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12225 buflen = filtd + readlen;
12226 tolist = 0;
12227 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12228 {
12229 if (buf[filtd] == '\n' || readlen <= 0)
12230 {
12231 /* Only when in binary mode add an empty list item when the
12232 * last line ends in a '\n'. */
12233 if (!binary && readlen == 0 && filtd == 0)
12234 break;
12235
12236 /* Found end-of-line or end-of-file: add a text line to the
12237 * list. */
12238 chop = 0;
12239 if (!binary)
12240 while (filtd - chop - 1 >= tolist
12241 && buf[filtd - chop - 1] == '\r')
12242 ++chop;
12243 len = filtd - tolist - chop;
12244 if (prev == NULL)
12245 s = vim_strnsave(buf + tolist, len);
12246 else
12247 {
12248 s = alloc((unsigned)(prevlen + len + 1));
12249 if (s != NULL)
12250 {
12251 mch_memmove(s, prev, prevlen);
12252 vim_free(prev);
12253 prev = NULL;
12254 mch_memmove(s + prevlen, buf + tolist, len);
12255 s[prevlen + len] = NUL;
12256 }
12257 }
12258 tolist = filtd + 1;
12259
12260 li = listitem_alloc();
12261 if (li == NULL)
12262 {
12263 vim_free(s);
12264 break;
12265 }
12266 li->li_tv.v_type = VAR_STRING;
12267 li->li_tv.v_lock = 0;
12268 li->li_tv.vval.v_string = s;
12269 list_append(l, li);
12270
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012271 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012272 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012273 if (readlen <= 0)
12274 break;
12275 }
12276 else if (buf[filtd] == NUL)
12277 buf[filtd] = '\n';
12278 }
12279 if (readlen <= 0)
12280 break;
12281
12282 if (tolist == 0)
12283 {
12284 /* "buf" is full, need to move text to an allocated buffer */
12285 if (prev == NULL)
12286 {
12287 prev = vim_strnsave(buf, buflen);
12288 prevlen = buflen;
12289 }
12290 else
12291 {
12292 s = alloc((unsigned)(prevlen + buflen));
12293 if (s != NULL)
12294 {
12295 mch_memmove(s, prev, prevlen);
12296 mch_memmove(s + prevlen, buf, buflen);
12297 vim_free(prev);
12298 prev = s;
12299 prevlen += buflen;
12300 }
12301 }
12302 filtd = 0;
12303 }
12304 else
12305 {
12306 mch_memmove(buf, buf + tolist, buflen - tolist);
12307 filtd -= tolist;
12308 }
12309 }
12310
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012311 /*
12312 * For a negative line count use only the lines at the end of the file,
12313 * free the rest.
12314 */
12315 if (maxline < 0)
12316 while (cnt > -maxline)
12317 {
12318 listitem_remove(l, l->lv_first);
12319 --cnt;
12320 }
12321
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012322 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012323 fclose(fd);
12324}
12325
12326
Bram Moolenaar0d660222005-01-07 21:51:51 +000012327#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12328static void make_connection __ARGS((void));
12329static int check_connection __ARGS((void));
12330
12331 static void
12332make_connection()
12333{
12334 if (X_DISPLAY == NULL
12335# ifdef FEAT_GUI
12336 && !gui.in_use
12337# endif
12338 )
12339 {
12340 x_force_connect = TRUE;
12341 setup_term_clip();
12342 x_force_connect = FALSE;
12343 }
12344}
12345
12346 static int
12347check_connection()
12348{
12349 make_connection();
12350 if (X_DISPLAY == NULL)
12351 {
12352 EMSG(_("E240: No connection to Vim server"));
12353 return FAIL;
12354 }
12355 return OK;
12356}
12357#endif
12358
12359#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012360static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012361
12362 static void
12363remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012364 typval_T *argvars;
12365 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012366 int expr;
12367{
12368 char_u *server_name;
12369 char_u *keys;
12370 char_u *r = NULL;
12371 char_u buf[NUMBUFLEN];
12372# ifdef WIN32
12373 HWND w;
12374# else
12375 Window w;
12376# endif
12377
12378 if (check_restricted() || check_secure())
12379 return;
12380
12381# ifdef FEAT_X11
12382 if (check_connection() == FAIL)
12383 return;
12384# endif
12385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386 server_name = get_tv_string_chk(&argvars[0]);
12387 if (server_name == NULL)
12388 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389 keys = get_tv_string_buf(&argvars[1], buf);
12390# ifdef WIN32
12391 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12392# else
12393 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12394 < 0)
12395# endif
12396 {
12397 if (r != NULL)
12398 EMSG(r); /* sending worked but evaluation failed */
12399 else
12400 EMSG2(_("E241: Unable to send to %s"), server_name);
12401 return;
12402 }
12403
12404 rettv->vval.v_string = r;
12405
12406 if (argvars[2].v_type != VAR_UNKNOWN)
12407 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012409 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012411
12412 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012413 v.di_tv.v_type = VAR_STRING;
12414 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012415 idvar = get_tv_string_chk(&argvars[2]);
12416 if (idvar != NULL)
12417 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012418 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012419 }
12420}
12421#endif
12422
12423/*
12424 * "remote_expr()" function
12425 */
12426/*ARGSUSED*/
12427 static void
12428f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012429 typval_T *argvars;
12430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012431{
12432 rettv->v_type = VAR_STRING;
12433 rettv->vval.v_string = NULL;
12434#ifdef FEAT_CLIENTSERVER
12435 remote_common(argvars, rettv, TRUE);
12436#endif
12437}
12438
12439/*
12440 * "remote_foreground()" function
12441 */
12442/*ARGSUSED*/
12443 static void
12444f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012445 typval_T *argvars;
12446 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012447{
12448 rettv->vval.v_number = 0;
12449#ifdef FEAT_CLIENTSERVER
12450# ifdef WIN32
12451 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012452 {
12453 char_u *server_name = get_tv_string_chk(&argvars[0]);
12454
12455 if (server_name != NULL)
12456 serverForeground(server_name);
12457 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012458# else
12459 /* Send a foreground() expression to the server. */
12460 argvars[1].v_type = VAR_STRING;
12461 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12462 argvars[2].v_type = VAR_UNKNOWN;
12463 remote_common(argvars, rettv, TRUE);
12464 vim_free(argvars[1].vval.v_string);
12465# endif
12466#endif
12467}
12468
12469/*ARGSUSED*/
12470 static void
12471f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012472 typval_T *argvars;
12473 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012474{
12475#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012477 char_u *s = NULL;
12478# ifdef WIN32
12479 int n = 0;
12480# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012481 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482
12483 if (check_restricted() || check_secure())
12484 {
12485 rettv->vval.v_number = -1;
12486 return;
12487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012488 serverid = get_tv_string_chk(&argvars[0]);
12489 if (serverid == NULL)
12490 {
12491 rettv->vval.v_number = -1;
12492 return; /* type error; errmsg already given */
12493 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012494# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012495 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496 if (n == 0)
12497 rettv->vval.v_number = -1;
12498 else
12499 {
12500 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12501 rettv->vval.v_number = (s != NULL);
12502 }
12503# else
12504 rettv->vval.v_number = 0;
12505 if (check_connection() == FAIL)
12506 return;
12507
12508 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012510# endif
12511
12512 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 char_u *retvar;
12515
Bram Moolenaar33570922005-01-25 22:26:29 +000012516 v.di_tv.v_type = VAR_STRING;
12517 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012518 retvar = get_tv_string_chk(&argvars[1]);
12519 if (retvar != NULL)
12520 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012522 }
12523#else
12524 rettv->vval.v_number = -1;
12525#endif
12526}
12527
12528/*ARGSUSED*/
12529 static void
12530f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 typval_T *argvars;
12532 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533{
12534 char_u *r = NULL;
12535
12536#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537 char_u *serverid = get_tv_string_chk(&argvars[0]);
12538
12539 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 {
12541# ifdef WIN32
12542 /* The server's HWND is encoded in the 'id' parameter */
12543 int n = 0;
12544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012546 if (n != 0)
12547 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12548 if (r == NULL)
12549# else
12550 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552# endif
12553 EMSG(_("E277: Unable to read a server reply"));
12554 }
12555#endif
12556 rettv->v_type = VAR_STRING;
12557 rettv->vval.v_string = r;
12558}
12559
12560/*
12561 * "remote_send()" function
12562 */
12563/*ARGSUSED*/
12564 static void
12565f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012568{
12569 rettv->v_type = VAR_STRING;
12570 rettv->vval.v_string = NULL;
12571#ifdef FEAT_CLIENTSERVER
12572 remote_common(argvars, rettv, FALSE);
12573#endif
12574}
12575
12576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012577 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012578 */
12579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012581 typval_T *argvars;
12582 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012583{
Bram Moolenaar33570922005-01-25 22:26:29 +000012584 list_T *l;
12585 listitem_T *item, *item2;
12586 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012587 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012588 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012589 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012590 dict_T *d;
12591 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012592
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012593 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012594 if (argvars[0].v_type == VAR_DICT)
12595 {
12596 if (argvars[2].v_type != VAR_UNKNOWN)
12597 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012598 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012599 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 key = get_tv_string_chk(&argvars[1]);
12602 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 di = dict_find(d, key, -1);
12605 if (di == NULL)
12606 EMSG2(_(e_dictkey), key);
12607 else
12608 {
12609 *rettv = di->di_tv;
12610 init_tv(&di->di_tv);
12611 dictitem_remove(d, di);
12612 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012614 }
12615 }
12616 else if (argvars[0].v_type != VAR_LIST)
12617 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012618 else if ((l = argvars[0].vval.v_list) != NULL
12619 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012621 int error = FALSE;
12622
12623 idx = get_tv_number_chk(&argvars[1], &error);
12624 if (error)
12625 ; /* type error: do nothing, errmsg already given */
12626 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012627 EMSGN(_(e_listidx), idx);
12628 else
12629 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012630 if (argvars[2].v_type == VAR_UNKNOWN)
12631 {
12632 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012633 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012634 *rettv = item->li_tv;
12635 vim_free(item);
12636 }
12637 else
12638 {
12639 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 end = get_tv_number_chk(&argvars[2], &error);
12641 if (error)
12642 ; /* type error: do nothing */
12643 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012644 EMSGN(_(e_listidx), end);
12645 else
12646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012647 int cnt = 0;
12648
12649 for (li = item; li != NULL; li = li->li_next)
12650 {
12651 ++cnt;
12652 if (li == item2)
12653 break;
12654 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012655 if (li == NULL) /* didn't find "item2" after "item" */
12656 EMSG(_(e_invrange));
12657 else
12658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012659 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012660 l = list_alloc();
12661 if (l != NULL)
12662 {
12663 rettv->v_type = VAR_LIST;
12664 rettv->vval.v_list = l;
12665 l->lv_first = item;
12666 l->lv_last = item2;
12667 l->lv_refcount = 1;
12668 item->li_prev = NULL;
12669 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012670 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012671 }
12672 }
12673 }
12674 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012675 }
12676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677}
12678
12679/*
12680 * "rename({from}, {to})" function
12681 */
12682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012683f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012684 typval_T *argvars;
12685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686{
12687 char_u buf[NUMBUFLEN];
12688
12689 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012690 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12693 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694}
12695
12696/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012697 * "repeat()" function
12698 */
12699/*ARGSUSED*/
12700 static void
12701f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *argvars;
12703 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012704{
12705 char_u *p;
12706 int n;
12707 int slen;
12708 int len;
12709 char_u *r;
12710 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012711 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012712
12713 n = get_tv_number(&argvars[1]);
12714 if (argvars[0].v_type == VAR_LIST)
12715 {
12716 l = list_alloc();
12717 if (l != NULL && argvars[0].vval.v_list != NULL)
12718 {
12719 l->lv_refcount = 1;
12720 while (n-- > 0)
12721 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12722 break;
12723 }
12724 rettv->v_type = VAR_LIST;
12725 rettv->vval.v_list = l;
12726 }
12727 else
12728 {
12729 p = get_tv_string(&argvars[0]);
12730 rettv->v_type = VAR_STRING;
12731 rettv->vval.v_string = NULL;
12732
12733 slen = (int)STRLEN(p);
12734 len = slen * n;
12735 if (len <= 0)
12736 return;
12737
12738 r = alloc(len + 1);
12739 if (r != NULL)
12740 {
12741 for (i = 0; i < n; i++)
12742 mch_memmove(r + i * slen, p, (size_t)slen);
12743 r[len] = NUL;
12744 }
12745
12746 rettv->vval.v_string = r;
12747 }
12748}
12749
12750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 * "resolve()" function
12752 */
12753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012755 typval_T *argvars;
12756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757{
12758 char_u *p;
12759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761#ifdef FEAT_SHORTCUT
12762 {
12763 char_u *v = NULL;
12764
12765 v = mch_resolve_shortcut(p);
12766 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 }
12771#else
12772# ifdef HAVE_READLINK
12773 {
12774 char_u buf[MAXPATHL + 1];
12775 char_u *cpy;
12776 int len;
12777 char_u *remain = NULL;
12778 char_u *q;
12779 int is_relative_to_current = FALSE;
12780 int has_trailing_pathsep = FALSE;
12781 int limit = 100;
12782
12783 p = vim_strsave(p);
12784
12785 if (p[0] == '.' && (vim_ispathsep(p[1])
12786 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12787 is_relative_to_current = TRUE;
12788
12789 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012790 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 has_trailing_pathsep = TRUE;
12792
12793 q = getnextcomp(p);
12794 if (*q != NUL)
12795 {
12796 /* Separate the first path component in "p", and keep the
12797 * remainder (beginning with the path separator). */
12798 remain = vim_strsave(q - 1);
12799 q[-1] = NUL;
12800 }
12801
12802 for (;;)
12803 {
12804 for (;;)
12805 {
12806 len = readlink((char *)p, (char *)buf, MAXPATHL);
12807 if (len <= 0)
12808 break;
12809 buf[len] = NUL;
12810
12811 if (limit-- == 0)
12812 {
12813 vim_free(p);
12814 vim_free(remain);
12815 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 goto fail;
12818 }
12819
12820 /* Ensure that the result will have a trailing path separator
12821 * if the argument has one. */
12822 if (remain == NULL && has_trailing_pathsep)
12823 add_pathsep(buf);
12824
12825 /* Separate the first path component in the link value and
12826 * concatenate the remainders. */
12827 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12828 if (*q != NUL)
12829 {
12830 if (remain == NULL)
12831 remain = vim_strsave(q - 1);
12832 else
12833 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012834 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 if (cpy != NULL)
12836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 vim_free(remain);
12838 remain = cpy;
12839 }
12840 }
12841 q[-1] = NUL;
12842 }
12843
12844 q = gettail(p);
12845 if (q > p && *q == NUL)
12846 {
12847 /* Ignore trailing path separator. */
12848 q[-1] = NUL;
12849 q = gettail(p);
12850 }
12851 if (q > p && !mch_isFullName(buf))
12852 {
12853 /* symlink is relative to directory of argument */
12854 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12855 if (cpy != NULL)
12856 {
12857 STRCPY(cpy, p);
12858 STRCPY(gettail(cpy), buf);
12859 vim_free(p);
12860 p = cpy;
12861 }
12862 }
12863 else
12864 {
12865 vim_free(p);
12866 p = vim_strsave(buf);
12867 }
12868 }
12869
12870 if (remain == NULL)
12871 break;
12872
12873 /* Append the first path component of "remain" to "p". */
12874 q = getnextcomp(remain + 1);
12875 len = q - remain - (*q != NUL);
12876 cpy = vim_strnsave(p, STRLEN(p) + len);
12877 if (cpy != NULL)
12878 {
12879 STRNCAT(cpy, remain, len);
12880 vim_free(p);
12881 p = cpy;
12882 }
12883 /* Shorten "remain". */
12884 if (*q != NUL)
12885 STRCPY(remain, q - 1);
12886 else
12887 {
12888 vim_free(remain);
12889 remain = NULL;
12890 }
12891 }
12892
12893 /* If the result is a relative path name, make it explicitly relative to
12894 * the current directory if and only if the argument had this form. */
12895 if (!vim_ispathsep(*p))
12896 {
12897 if (is_relative_to_current
12898 && *p != NUL
12899 && !(p[0] == '.'
12900 && (p[1] == NUL
12901 || vim_ispathsep(p[1])
12902 || (p[1] == '.'
12903 && (p[2] == NUL
12904 || vim_ispathsep(p[2]))))))
12905 {
12906 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012907 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 if (cpy != NULL)
12909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 vim_free(p);
12911 p = cpy;
12912 }
12913 }
12914 else if (!is_relative_to_current)
12915 {
12916 /* Strip leading "./". */
12917 q = p;
12918 while (q[0] == '.' && vim_ispathsep(q[1]))
12919 q += 2;
12920 if (q > p)
12921 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12922 }
12923 }
12924
12925 /* Ensure that the result will have no trailing path separator
12926 * if the argument had none. But keep "/" or "//". */
12927 if (!has_trailing_pathsep)
12928 {
12929 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012930 if (after_pathsep(p, q))
12931 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 }
12933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 }
12936# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938# endif
12939#endif
12940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
12943#ifdef HAVE_READLINK
12944fail:
12945#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947}
12948
12949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012950 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 */
12952 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012953f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012954 typval_T *argvars;
12955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956{
Bram Moolenaar33570922005-01-25 22:26:29 +000012957 list_T *l;
12958 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960 rettv->vval.v_number = 0;
12961 if (argvars[0].v_type != VAR_LIST)
12962 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012963 else if ((l = argvars[0].vval.v_list) != NULL
12964 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012965 {
12966 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012967 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012968 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012969 while (li != NULL)
12970 {
12971 ni = li->li_prev;
12972 list_append(l, li);
12973 li = ni;
12974 }
12975 rettv->vval.v_list = l;
12976 rettv->v_type = VAR_LIST;
12977 ++l->lv_refcount;
12978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979}
12980
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012981#define SP_NOMOVE 1 /* don't move cursor */
12982#define SP_REPEAT 2 /* repeat to find outer pair */
12983#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012984#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012985
Bram Moolenaar33570922005-01-25 22:26:29 +000012986static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012987
12988/*
12989 * Get flags for a search function.
12990 * Possibly sets "p_ws".
12991 * Returns BACKWARD, FORWARD or zero (for an error).
12992 */
12993 static int
12994get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012995 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012996 int *flagsp;
12997{
12998 int dir = FORWARD;
12999 char_u *flags;
13000 char_u nbuf[NUMBUFLEN];
13001 int mask;
13002
13003 if (varp->v_type != VAR_UNKNOWN)
13004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013005 flags = get_tv_string_buf_chk(varp, nbuf);
13006 if (flags == NULL)
13007 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008 while (*flags != NUL)
13009 {
13010 switch (*flags)
13011 {
13012 case 'b': dir = BACKWARD; break;
13013 case 'w': p_ws = TRUE; break;
13014 case 'W': p_ws = FALSE; break;
13015 default: mask = 0;
13016 if (flagsp != NULL)
13017 switch (*flags)
13018 {
13019 case 'n': mask = SP_NOMOVE; break;
13020 case 'r': mask = SP_REPEAT; break;
13021 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013022 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023 }
13024 if (mask == 0)
13025 {
13026 EMSG2(_(e_invarg2), flags);
13027 dir = 0;
13028 }
13029 else
13030 *flagsp |= mask;
13031 }
13032 if (dir == 0)
13033 break;
13034 ++flags;
13035 }
13036 }
13037 return dir;
13038}
13039
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040/*
13041 * "search()" function
13042 */
13043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047{
13048 char_u *pat;
13049 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013050 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 int save_p_ws = p_ws;
13052 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013053 int flags = 0;
13054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013058 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13059 if (dir == 0)
13060 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013061 /*
13062 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13063 * Check to make sure only those flags are set.
13064 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13065 * flags cannot be set. Check for that condition also.
13066 */
13067 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13068 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013071 goto theend;
13072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013074 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13076 SEARCH_KEEP, RE_SEARCH) != FAIL)
13077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013079 if (flags & SP_SETPCMARK)
13080 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 curwin->w_cursor = pos;
13082 /* "/$" will put the cursor after the end of the line, may need to
13083 * correct that here */
13084 check_cursor();
13085 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013086
13087 /* If 'n' flag is used: restore cursor position. */
13088 if (flags & SP_NOMOVE)
13089 curwin->w_cursor = save_cursor;
13090theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091 p_ws = save_p_ws;
13092}
13093
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013095 * "searchdecl()" function
13096 */
13097 static void
13098f_searchdecl(argvars, rettv)
13099 typval_T *argvars;
13100 typval_T *rettv;
13101{
13102 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013103 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013104 int error = FALSE;
13105 char_u *name;
13106
13107 rettv->vval.v_number = 1; /* default: FAIL */
13108
13109 name = get_tv_string_chk(&argvars[0]);
13110 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013111 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013112 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013113 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13114 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13115 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013116 if (!error && name != NULL)
13117 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013118 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013119}
13120
13121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 * "searchpair()" function
13123 */
13124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013126 typval_T *argvars;
13127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128{
13129 char_u *spat, *mpat, *epat;
13130 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 int dir;
13133 int flags = 0;
13134 char_u nbuf1[NUMBUFLEN];
13135 char_u nbuf2[NUMBUFLEN];
13136 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013138 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013141 spat = get_tv_string_chk(&argvars[0]);
13142 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13143 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13144 if (spat == NULL || mpat == NULL || epat == NULL)
13145 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 /* Handle the optional fourth argument: flags */
13148 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013149 if (dir == 0)
13150 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013151 /*
13152 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13153 */
13154 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13155 {
13156 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13157 goto theend;
13158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159
13160 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013161 if (argvars[3].v_type == VAR_UNKNOWN
13162 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 skip = (char_u *)"";
13164 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13166 if (skip == NULL)
13167 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013169 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13170
13171theend:
13172 p_ws = save_p_ws;
13173}
13174
13175/*
13176 * Search for a start/middle/end thing.
13177 * Used by searchpair(), see its documentation for the details.
13178 * Returns 0 or -1 for no match,
13179 */
13180 long
13181do_searchpair(spat, mpat, epat, dir, skip, flags)
13182 char_u *spat; /* start pattern */
13183 char_u *mpat; /* middle pattern */
13184 char_u *epat; /* end pattern */
13185 int dir; /* BACKWARD or FORWARD */
13186 char_u *skip; /* skip expression */
13187 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13188{
13189 char_u *save_cpo;
13190 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13191 long retval = 0;
13192 pos_T pos;
13193 pos_T firstpos;
13194 pos_T foundpos;
13195 pos_T save_cursor;
13196 pos_T save_pos;
13197 int n;
13198 int r;
13199 int nest = 1;
13200 int err;
13201
13202 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13203 save_cpo = p_cpo;
13204 p_cpo = (char_u *)"";
13205
13206 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13207 * start/middle/end (pat3, for the top pair). */
13208 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13209 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13210 if (pat2 == NULL || pat3 == NULL)
13211 goto theend;
13212 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13213 if (*mpat == NUL)
13214 STRCPY(pat3, pat2);
13215 else
13216 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13217 spat, epat, mpat);
13218
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 save_cursor = curwin->w_cursor;
13220 pos = curwin->w_cursor;
13221 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013222 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 pat = pat3;
13224 for (;;)
13225 {
13226 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13227 SEARCH_KEEP, RE_SEARCH);
13228 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13229 /* didn't find it or found the first match again: FAIL */
13230 break;
13231
13232 if (firstpos.lnum == 0)
13233 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013234 if (equalpos(pos, foundpos))
13235 {
13236 /* Found the same position again. Can happen with a pattern that
13237 * has "\zs" at the end and searching backwards. Advance one
13238 * character and try again. */
13239 if (dir == BACKWARD)
13240 decl(&pos);
13241 else
13242 incl(&pos);
13243 }
13244 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245
13246 /* If the skip pattern matches, ignore this match. */
13247 if (*skip != NUL)
13248 {
13249 save_pos = curwin->w_cursor;
13250 curwin->w_cursor = pos;
13251 r = eval_to_bool(skip, &err, NULL, FALSE);
13252 curwin->w_cursor = save_pos;
13253 if (err)
13254 {
13255 /* Evaluating {skip} caused an error, break here. */
13256 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013257 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 break;
13259 }
13260 if (r)
13261 continue;
13262 }
13263
13264 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13265 {
13266 /* Found end when searching backwards or start when searching
13267 * forward: nested pair. */
13268 ++nest;
13269 pat = pat2; /* nested, don't search for middle */
13270 }
13271 else
13272 {
13273 /* Found end when searching forward or start when searching
13274 * backward: end of (nested) pair; or found middle in outer pair. */
13275 if (--nest == 1)
13276 pat = pat3; /* outer level, search for middle */
13277 }
13278
13279 if (nest == 0)
13280 {
13281 /* Found the match: return matchcount or line number. */
13282 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013283 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013285 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013286 if (flags & SP_SETPCMARK)
13287 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288 curwin->w_cursor = pos;
13289 if (!(flags & SP_REPEAT))
13290 break;
13291 nest = 1; /* search for next unmatched */
13292 }
13293 }
13294
13295 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013296 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 curwin->w_cursor = save_cursor;
13298
13299theend:
13300 vim_free(pat2);
13301 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013303
13304 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305}
13306
Bram Moolenaar0d660222005-01-07 21:51:51 +000013307/*ARGSUSED*/
13308 static void
13309f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013310 typval_T *argvars;
13311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013313#ifdef FEAT_CLIENTSERVER
13314 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315 char_u *server = get_tv_string_chk(&argvars[0]);
13316 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317
Bram Moolenaar0d660222005-01-07 21:51:51 +000013318 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 if (server == NULL || reply == NULL)
13320 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013321 if (check_restricted() || check_secure())
13322 return;
13323# ifdef FEAT_X11
13324 if (check_connection() == FAIL)
13325 return;
13326# endif
13327
13328 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330 EMSG(_("E258: Unable to send to client"));
13331 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013333 rettv->vval.v_number = 0;
13334#else
13335 rettv->vval.v_number = -1;
13336#endif
13337}
13338
13339/*ARGSUSED*/
13340 static void
13341f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013342 typval_T *argvars;
13343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013344{
13345 char_u *r = NULL;
13346
13347#ifdef FEAT_CLIENTSERVER
13348# ifdef WIN32
13349 r = serverGetVimNames();
13350# else
13351 make_connection();
13352 if (X_DISPLAY != NULL)
13353 r = serverGetVimNames(X_DISPLAY);
13354# endif
13355#endif
13356 rettv->v_type = VAR_STRING;
13357 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358}
13359
13360/*
13361 * "setbufvar()" function
13362 */
13363/*ARGSUSED*/
13364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013365f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 typval_T *argvars;
13367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368{
13369 buf_T *buf;
13370#ifdef FEAT_AUTOCMD
13371 aco_save_T aco;
13372#else
13373 buf_T *save_curbuf;
13374#endif
13375 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 char_u nbuf[NUMBUFLEN];
13378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 rettv->vval.v_number = 0;
13380
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381 if (check_restricted() || check_secure())
13382 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13384 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 varp = &argvars[2];
13387
13388 if (buf != NULL && varname != NULL && varp != NULL)
13389 {
13390 /* set curbuf to be our buf, temporarily */
13391#ifdef FEAT_AUTOCMD
13392 aucmd_prepbuf(&aco, buf);
13393#else
13394 save_curbuf = curbuf;
13395 curbuf = buf;
13396#endif
13397
13398 if (*varname == '&')
13399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013400 long numval;
13401 char_u *strval;
13402 int error = FALSE;
13403
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 numval = get_tv_number_chk(varp, &error);
13406 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 if (!error && strval != NULL)
13408 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 }
13410 else
13411 {
13412 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13413 if (bufvarname != NULL)
13414 {
13415 STRCPY(bufvarname, "b:");
13416 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013417 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 vim_free(bufvarname);
13419 }
13420 }
13421
13422 /* reset notion of buffer */
13423#ifdef FEAT_AUTOCMD
13424 aucmd_restbuf(&aco);
13425#else
13426 curbuf = save_curbuf;
13427#endif
13428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429}
13430
13431/*
13432 * "setcmdpos()" function
13433 */
13434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 typval_T *argvars;
13437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013439 int pos = (int)get_tv_number(&argvars[0]) - 1;
13440
13441 if (pos >= 0)
13442 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443}
13444
13445/*
13446 * "setline()" function
13447 */
13448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
13453 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013454 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013455 list_T *l = NULL;
13456 listitem_T *li = NULL;
13457 long added = 0;
13458 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013460 lnum = get_tv_lnum(&argvars[0]);
13461 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013463 l = argvars[1].vval.v_list;
13464 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013466 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013467 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013468
13469 rettv->vval.v_number = 0; /* OK */
13470 for (;;)
13471 {
13472 if (l != NULL)
13473 {
13474 /* list argument, get next string */
13475 if (li == NULL)
13476 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013478 li = li->li_next;
13479 }
13480
13481 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013483 break;
13484 if (lnum <= curbuf->b_ml.ml_line_count)
13485 {
13486 /* existing line, replace it */
13487 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13488 {
13489 changed_bytes(lnum, 0);
13490 check_cursor_col();
13491 rettv->vval.v_number = 0; /* OK */
13492 }
13493 }
13494 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13495 {
13496 /* lnum is one past the last line, append the line */
13497 ++added;
13498 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13499 rettv->vval.v_number = 0; /* OK */
13500 }
13501
13502 if (l == NULL) /* only one string argument */
13503 break;
13504 ++lnum;
13505 }
13506
13507 if (added > 0)
13508 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509}
13510
13511/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013512 * "setqflist()" function
13513 */
13514/*ARGSUSED*/
13515 static void
13516f_setqflist(argvars, rettv)
13517 typval_T *argvars;
13518 typval_T *rettv;
13519{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013520 char_u *act;
13521 int action = ' ';
13522
Bram Moolenaar2641f772005-03-25 21:58:17 +000013523 rettv->vval.v_number = -1;
13524
13525#ifdef FEAT_QUICKFIX
13526 if (argvars[0].v_type != VAR_LIST)
13527 EMSG(_(e_listreq));
13528 else
13529 {
13530 list_T *l = argvars[0].vval.v_list;
13531
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013532 if (argvars[1].v_type == VAR_STRING)
13533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013534 act = get_tv_string_chk(&argvars[1]);
13535 if (act == NULL)
13536 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013537 if (*act == 'a' || *act == 'r')
13538 action = *act;
13539 }
13540
13541 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013542 rettv->vval.v_number = 0;
13543 }
13544#endif
13545}
13546
13547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 * "setreg()" function
13549 */
13550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 typval_T *argvars;
13553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554{
13555 int regname;
13556 char_u *strregname;
13557 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013558 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 int append;
13560 char_u yank_type;
13561 long block_len;
13562
13563 block_len = -1;
13564 yank_type = MAUTO;
13565 append = FALSE;
13566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013567 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 if (strregname == NULL)
13571 return; /* type error; errmsg already given */
13572 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 if (regname == 0 || regname == '@')
13574 regname = '"';
13575 else if (regname == '=')
13576 return;
13577
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013578 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 stropt = get_tv_string_chk(&argvars[2]);
13581 if (stropt == NULL)
13582 return; /* type error */
13583 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 switch (*stropt)
13585 {
13586 case 'a': case 'A': /* append */
13587 append = TRUE;
13588 break;
13589 case 'v': case 'c': /* character-wise selection */
13590 yank_type = MCHAR;
13591 break;
13592 case 'V': case 'l': /* line-wise selection */
13593 yank_type = MLINE;
13594 break;
13595#ifdef FEAT_VISUAL
13596 case 'b': case Ctrl_V: /* block-wise selection */
13597 yank_type = MBLOCK;
13598 if (VIM_ISDIGIT(stropt[1]))
13599 {
13600 ++stropt;
13601 block_len = getdigits(&stropt) - 1;
13602 --stropt;
13603 }
13604 break;
13605#endif
13606 }
13607 }
13608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 strval = get_tv_string_chk(&argvars[1]);
13610 if (strval != NULL)
13611 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614}
13615
13616
13617/*
13618 * "setwinvar(expr)" function
13619 */
13620/*ARGSUSED*/
13621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 typval_T *argvars;
13624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625{
13626 win_T *win;
13627#ifdef FEAT_WINDOWS
13628 win_T *save_curwin;
13629#endif
13630 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013631 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 char_u nbuf[NUMBUFLEN];
13633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 rettv->vval.v_number = 0;
13635
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 if (check_restricted() || check_secure())
13637 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013639 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 varp = &argvars[2];
13641
13642 if (win != NULL && varname != NULL && varp != NULL)
13643 {
13644#ifdef FEAT_WINDOWS
13645 /* set curwin to be our win, temporarily */
13646 save_curwin = curwin;
13647 curwin = win;
13648 curbuf = curwin->w_buffer;
13649#endif
13650
13651 if (*varname == '&')
13652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013653 long numval;
13654 char_u *strval;
13655 int error = FALSE;
13656
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 numval = get_tv_number_chk(varp, &error);
13659 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013660 if (!error && strval != NULL)
13661 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 }
13663 else
13664 {
13665 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13666 if (winvarname != NULL)
13667 {
13668 STRCPY(winvarname, "w:");
13669 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013670 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 vim_free(winvarname);
13672 }
13673 }
13674
13675#ifdef FEAT_WINDOWS
13676 /* Restore current window, if it's still valid (autocomands can make
13677 * it invalid). */
13678 if (win_valid(save_curwin))
13679 {
13680 curwin = save_curwin;
13681 curbuf = curwin->w_buffer;
13682 }
13683#endif
13684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685}
13686
13687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013688 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 */
13690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013691f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013692 typval_T *argvars;
13693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
Bram Moolenaar0d660222005-01-07 21:51:51 +000013697 p = get_tv_string(&argvars[0]);
13698 rettv->vval.v_string = vim_strsave(p);
13699 simplify_filename(rettv->vval.v_string); /* simplify in place */
13700 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701}
13702
Bram Moolenaar0d660222005-01-07 21:51:51 +000013703static int
13704#ifdef __BORLANDC__
13705 _RTLENTRYF
13706#endif
13707 item_compare __ARGS((const void *s1, const void *s2));
13708static int
13709#ifdef __BORLANDC__
13710 _RTLENTRYF
13711#endif
13712 item_compare2 __ARGS((const void *s1, const void *s2));
13713
13714static int item_compare_ic;
13715static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013717#define ITEM_COMPARE_FAIL 999
13718
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013720 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013722 static int
13723#ifdef __BORLANDC__
13724_RTLENTRYF
13725#endif
13726item_compare(s1, s2)
13727 const void *s1;
13728 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013730 char_u *p1, *p2;
13731 char_u *tofree1, *tofree2;
13732 int res;
13733 char_u numbuf1[NUMBUFLEN];
13734 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13737 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013738 if (item_compare_ic)
13739 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013741 res = STRCMP(p1, p2);
13742 vim_free(tofree1);
13743 vim_free(tofree2);
13744 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745}
13746
13747 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748#ifdef __BORLANDC__
13749_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751item_compare2(s1, s2)
13752 const void *s1;
13753 const void *s2;
13754{
13755 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013756 typval_T rettv;
13757 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013758 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 /* shortcut after failure in previous call; compare all items equal */
13761 if (item_compare_func_err)
13762 return 0;
13763
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013764 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13765 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13767 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013768
13769 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13770 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013771 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772 clear_tv(&argv[0]);
13773 clear_tv(&argv[1]);
13774
13775 if (res == FAIL)
13776 res = ITEM_COMPARE_FAIL;
13777 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 /* return value has wrong type */
13779 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13780 if (item_compare_func_err)
13781 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013782 clear_tv(&rettv);
13783 return res;
13784}
13785
13786/*
13787 * "sort({list})" function
13788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013790f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013791 typval_T *argvars;
13792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793{
Bram Moolenaar33570922005-01-25 22:26:29 +000013794 list_T *l;
13795 listitem_T *li;
13796 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013797 long len;
13798 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800 rettv->vval.v_number = 0;
13801 if (argvars[0].v_type != VAR_LIST)
13802 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 else
13804 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013805 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013806 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013807 return;
13808 rettv->vval.v_list = l;
13809 rettv->v_type = VAR_LIST;
13810 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811
Bram Moolenaar0d660222005-01-07 21:51:51 +000013812 len = list_len(l);
13813 if (len <= 1)
13814 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815
Bram Moolenaar0d660222005-01-07 21:51:51 +000013816 item_compare_ic = FALSE;
13817 item_compare_func = NULL;
13818 if (argvars[1].v_type != VAR_UNKNOWN)
13819 {
13820 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013822 else
13823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013824 int error = FALSE;
13825
13826 i = get_tv_number_chk(&argvars[1], &error);
13827 if (error)
13828 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013829 if (i == 1)
13830 item_compare_ic = TRUE;
13831 else
13832 item_compare_func = get_tv_string(&argvars[1]);
13833 }
13834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835
Bram Moolenaar0d660222005-01-07 21:51:51 +000013836 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013837 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013838 if (ptrs == NULL)
13839 return;
13840 i = 0;
13841 for (li = l->lv_first; li != NULL; li = li->li_next)
13842 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013845 /* test the compare function */
13846 if (item_compare_func != NULL
13847 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13848 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013849 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013851 {
13852 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013853 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013854 item_compare_func == NULL ? item_compare : item_compare2);
13855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856 if (!item_compare_func_err)
13857 {
13858 /* Clear the List and append the items in the sorted order. */
13859 l->lv_first = l->lv_last = NULL;
13860 l->lv_len = 0;
13861 for (i = 0; i < len; ++i)
13862 list_append(l, ptrs[i]);
13863 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013864 }
13865
13866 vim_free(ptrs);
13867 }
13868}
13869
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013870/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013871 * "soundfold({word})" function
13872 */
13873 static void
13874f_soundfold(argvars, rettv)
13875 typval_T *argvars;
13876 typval_T *rettv;
13877{
13878 char_u *s;
13879
13880 rettv->v_type = VAR_STRING;
13881 s = get_tv_string(&argvars[0]);
13882#ifdef FEAT_SYN_HL
13883 rettv->vval.v_string = eval_soundfold(s);
13884#else
13885 rettv->vval.v_string = vim_strsave(s);
13886#endif
13887}
13888
13889/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013890 * "spellbadword()" function
13891 */
13892/* ARGSUSED */
13893 static void
13894f_spellbadword(argvars, rettv)
13895 typval_T *argvars;
13896 typval_T *rettv;
13897{
Bram Moolenaar4463f292005-09-25 22:20:24 +000013898 char_u *word = (char_u *)"";
13899#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013900 int len = 0;
13901 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013902 list_T *l;
13903#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013904
Bram Moolenaar4463f292005-09-25 22:20:24 +000013905 l = list_alloc();
13906 if (l == NULL)
13907 return;
13908 rettv->v_type = VAR_LIST;
13909 rettv->vval.v_list = l;
13910 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013911
13912#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000013913 if (argvars[0].v_type == VAR_UNKNOWN)
13914 {
13915 /* Find the start and length of the badly spelled word. */
13916 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
13917 if (len != 0)
13918 word = ml_get_cursor();
13919 }
13920 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13921 {
13922 char_u *str = get_tv_string_chk(&argvars[0]);
13923 int capcol = -1;
13924
13925 if (str != NULL)
13926 {
13927 /* Check the argument for spelling. */
13928 while (*str != NUL)
13929 {
13930 len = spell_check(curwin, str, &attr, &capcol);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013931 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013932 {
13933 word = str;
13934 break;
13935 }
13936 str += len;
13937 }
13938 }
13939 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013940#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000013941
13942 list_append_string(l, word, len);
13943 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013944 attr == HLF_SPB ? "bad" :
13945 attr == HLF_SPR ? "rare" :
13946 attr == HLF_SPL ? "local" :
13947 attr == HLF_SPC ? "caps" :
13948 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013949}
13950
13951/*
13952 * "spellsuggest()" function
13953 */
13954 static void
13955f_spellsuggest(argvars, rettv)
13956 typval_T *argvars;
13957 typval_T *rettv;
13958{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013959 list_T *l;
13960#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013961 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013962 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013963 int maxcount;
13964 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013965 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013966 listitem_T *li;
13967 int need_capital = FALSE;
13968#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013969
13970 l = list_alloc();
13971 if (l == NULL)
13972 return;
13973 rettv->v_type = VAR_LIST;
13974 rettv->vval.v_list = l;
13975 ++l->lv_refcount;
13976
13977#ifdef FEAT_SYN_HL
13978 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13979 {
13980 str = get_tv_string(&argvars[0]);
13981 if (argvars[1].v_type != VAR_UNKNOWN)
13982 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013983 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013984 if (maxcount <= 0)
13985 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013986 if (argvars[2].v_type != VAR_UNKNOWN)
13987 {
13988 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
13989 if (typeerr)
13990 return;
13991 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013992 }
13993 else
13994 maxcount = 25;
13995
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013996 spell_suggest_list(&ga, str, maxcount, need_capital);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013997
13998 for (i = 0; i < ga.ga_len; ++i)
13999 {
14000 str = ((char_u **)ga.ga_data)[i];
14001
14002 li = listitem_alloc();
14003 if (li == NULL)
14004 vim_free(str);
14005 else
14006 {
14007 li->li_tv.v_type = VAR_STRING;
14008 li->li_tv.v_lock = 0;
14009 li->li_tv.vval.v_string = str;
14010 list_append(l, li);
14011 }
14012 }
14013 ga_clear(&ga);
14014 }
14015#endif
14016}
14017
Bram Moolenaar0d660222005-01-07 21:51:51 +000014018 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014019f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022{
14023 char_u *str;
14024 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014025 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014026 regmatch_T regmatch;
14027 char_u patbuf[NUMBUFLEN];
14028 char_u *save_cpo;
14029 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014030 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014031 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014032 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034
14035 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14036 save_cpo = p_cpo;
14037 p_cpo = (char_u *)"";
14038
14039 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014040 if (argvars[1].v_type != VAR_UNKNOWN)
14041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14043 if (pat == NULL)
14044 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014047 }
14048 if (pat == NULL || *pat == NUL)
14049 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014050
14051 l = list_alloc();
14052 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054 rettv->v_type = VAR_LIST;
14055 rettv->vval.v_list = l;
14056 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057 if (typeerr)
14058 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14061 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014063 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014064 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014065 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014066 if (*str == NUL)
14067 match = FALSE; /* empty item at the end */
14068 else
14069 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014070 if (match)
14071 end = regmatch.startp[0];
14072 else
14073 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014074 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14075 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014077 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014079 }
14080 if (!match)
14081 break;
14082 /* Advance to just after the match. */
14083 if (regmatch.endp[0] > str)
14084 col = 0;
14085 else
14086 {
14087 /* Don't get stuck at the same match. */
14088#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014089 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090#else
14091 col = 1;
14092#endif
14093 }
14094 str = regmatch.endp[0];
14095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101}
14102
14103#ifdef HAVE_STRFTIME
14104/*
14105 * "strftime({format}[, {time}])" function
14106 */
14107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014108f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014109 typval_T *argvars;
14110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111{
14112 char_u result_buf[256];
14113 struct tm *curtime;
14114 time_t seconds;
14115 char_u *p;
14116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014120 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 seconds = time(NULL);
14122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 curtime = localtime(&seconds);
14125 /* MSVC returns NULL for an invalid value of seconds. */
14126 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 else
14129 {
14130# ifdef FEAT_MBYTE
14131 vimconv_T conv;
14132 char_u *enc;
14133
14134 conv.vc_type = CONV_NONE;
14135 enc = enc_locale();
14136 convert_setup(&conv, p_enc, enc);
14137 if (conv.vc_type != CONV_NONE)
14138 p = string_convert(&conv, p, NULL);
14139# endif
14140 if (p != NULL)
14141 (void)strftime((char *)result_buf, sizeof(result_buf),
14142 (char *)p, curtime);
14143 else
14144 result_buf[0] = NUL;
14145
14146# ifdef FEAT_MBYTE
14147 if (conv.vc_type != CONV_NONE)
14148 vim_free(p);
14149 convert_setup(&conv, enc, p_enc);
14150 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 else
14153# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155
14156# ifdef FEAT_MBYTE
14157 /* Release conversion descriptors */
14158 convert_setup(&conv, NULL, NULL);
14159 vim_free(enc);
14160# endif
14161 }
14162}
14163#endif
14164
14165/*
14166 * "stridx()" function
14167 */
14168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014169f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 typval_T *argvars;
14171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172{
14173 char_u buf[NUMBUFLEN];
14174 char_u *needle;
14175 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014178 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 needle = get_tv_string_chk(&argvars[1]);
14181 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014182 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 if (needle == NULL || haystack == NULL)
14184 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185
Bram Moolenaar33570922005-01-25 22:26:29 +000014186 if (argvars[2].v_type != VAR_UNKNOWN)
14187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 int error = FALSE;
14189
14190 start_idx = get_tv_number_chk(&argvars[2], &error);
14191 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014193 if (start_idx >= 0)
14194 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014195 }
14196
14197 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14198 if (pos != NULL)
14199 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200}
14201
14202/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014203 * "string()" function
14204 */
14205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014207 typval_T *argvars;
14208 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014209{
14210 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014211 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014213 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014214 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014215 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217}
14218
14219/*
14220 * "strlen()" function
14221 */
14222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014223f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014224 typval_T *argvars;
14225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014227 rettv->vval.v_number = (varnumber_T)(STRLEN(
14228 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229}
14230
14231/*
14232 * "strpart()" function
14233 */
14234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014236 typval_T *argvars;
14237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238{
14239 char_u *p;
14240 int n;
14241 int len;
14242 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014245 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 slen = (int)STRLEN(p);
14247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 n = get_tv_number_chk(&argvars[1], &error);
14249 if (error)
14250 len = 0;
14251 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 else
14254 len = slen - n; /* default len: all bytes that are available. */
14255
14256 /*
14257 * Only return the overlap between the specified part and the actual
14258 * string.
14259 */
14260 if (n < 0)
14261 {
14262 len += n;
14263 n = 0;
14264 }
14265 else if (n > slen)
14266 n = slen;
14267 if (len < 0)
14268 len = 0;
14269 else if (n + len > slen)
14270 len = slen - n;
14271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->v_type = VAR_STRING;
14273 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274}
14275
14276/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014277 * "strridx()" function
14278 */
14279 static void
14280f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014281 typval_T *argvars;
14282 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014283{
14284 char_u buf[NUMBUFLEN];
14285 char_u *needle;
14286 char_u *haystack;
14287 char_u *rest;
14288 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014289 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 needle = get_tv_string_chk(&argvars[1]);
14292 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014293 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294
14295 rettv->vval.v_number = -1;
14296 if (needle == NULL || haystack == NULL)
14297 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014298 if (argvars[2].v_type != VAR_UNKNOWN)
14299 {
14300 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014301 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014302 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014304 }
14305 else
14306 end_idx = haystack_len;
14307
Bram Moolenaar0d660222005-01-07 21:51:51 +000014308 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014309 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014311 lastmatch = haystack + end_idx;
14312 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014313 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014314 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014315 for (rest = haystack; *rest != '\0'; ++rest)
14316 {
14317 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014318 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014319 break;
14320 lastmatch = rest;
14321 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014322 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323
14324 if (lastmatch == NULL)
14325 rettv->vval.v_number = -1;
14326 else
14327 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14328}
14329
14330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 * "strtrans()" function
14332 */
14333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014335 typval_T *argvars;
14336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 rettv->v_type = VAR_STRING;
14339 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340}
14341
14342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014343 * "submatch()" function
14344 */
14345 static void
14346f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014347 typval_T *argvars;
14348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349{
14350 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 rettv->vval.v_string =
14352 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014353}
14354
14355/*
14356 * "substitute()" function
14357 */
14358 static void
14359f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 typval_T *argvars;
14361 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014362{
14363 char_u patbuf[NUMBUFLEN];
14364 char_u subbuf[NUMBUFLEN];
14365 char_u flagsbuf[NUMBUFLEN];
14366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 char_u *str = get_tv_string_chk(&argvars[0]);
14368 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14369 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14370 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14371
Bram Moolenaar0d660222005-01-07 21:51:51 +000014372 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14374 rettv->vval.v_string = NULL;
14375 else
14376 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377}
14378
14379/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014380 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 */
14382/*ARGSUSED*/
14383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 typval_T *argvars;
14386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387{
14388 int id = 0;
14389#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014390 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 long col;
14392 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014393 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 lnum = get_tv_lnum(argvars); /* -1 on type error */
14396 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14397 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014399 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014400 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014401 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402#endif
14403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405}
14406
14407/*
14408 * "synIDattr(id, what [, mode])" function
14409 */
14410/*ARGSUSED*/
14411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 typval_T *argvars;
14414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415{
14416 char_u *p = NULL;
14417#ifdef FEAT_SYN_HL
14418 int id;
14419 char_u *what;
14420 char_u *mode;
14421 char_u modebuf[NUMBUFLEN];
14422 int modec;
14423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014424 id = get_tv_number(&argvars[0]);
14425 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 modec = TOLOWER_ASC(mode[0]);
14430 if (modec != 't' && modec != 'c'
14431#ifdef FEAT_GUI
14432 && modec != 'g'
14433#endif
14434 )
14435 modec = 0; /* replace invalid with current */
14436 }
14437 else
14438 {
14439#ifdef FEAT_GUI
14440 if (gui.in_use)
14441 modec = 'g';
14442 else
14443#endif
14444 if (t_colors > 1)
14445 modec = 'c';
14446 else
14447 modec = 't';
14448 }
14449
14450
14451 switch (TOLOWER_ASC(what[0]))
14452 {
14453 case 'b':
14454 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14455 p = highlight_color(id, what, modec);
14456 else /* bold */
14457 p = highlight_has_attr(id, HL_BOLD, modec);
14458 break;
14459
14460 case 'f': /* fg[#] */
14461 p = highlight_color(id, what, modec);
14462 break;
14463
14464 case 'i':
14465 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14466 p = highlight_has_attr(id, HL_INVERSE, modec);
14467 else /* italic */
14468 p = highlight_has_attr(id, HL_ITALIC, modec);
14469 break;
14470
14471 case 'n': /* name */
14472 p = get_highlight_name(NULL, id - 1);
14473 break;
14474
14475 case 'r': /* reverse */
14476 p = highlight_has_attr(id, HL_INVERSE, modec);
14477 break;
14478
14479 case 's': /* standout */
14480 p = highlight_has_attr(id, HL_STANDOUT, modec);
14481 break;
14482
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014483 case 'u':
14484 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14485 /* underline */
14486 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14487 else
14488 /* undercurl */
14489 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 break;
14491 }
14492
14493 if (p != NULL)
14494 p = vim_strsave(p);
14495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->v_type = VAR_STRING;
14497 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498}
14499
14500/*
14501 * "synIDtrans(id)" function
14502 */
14503/*ARGSUSED*/
14504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014506 typval_T *argvars;
14507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508{
14509 int id;
14510
14511#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014512 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513
14514 if (id > 0)
14515 id = syn_get_final_id(id);
14516 else
14517#endif
14518 id = 0;
14519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521}
14522
14523/*
14524 * "system()" function
14525 */
14526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014527f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014528 typval_T *argvars;
14529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014531 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014533 char_u *infile = NULL;
14534 char_u buf[NUMBUFLEN];
14535 int err = FALSE;
14536 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014538 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014539 {
14540 /*
14541 * Write the string to a temp file, to be used for input of the shell
14542 * command.
14543 */
14544 if ((infile = vim_tempname('i')) == NULL)
14545 {
14546 EMSG(_(e_notmp));
14547 return;
14548 }
14549
14550 fd = mch_fopen((char *)infile, WRITEBIN);
14551 if (fd == NULL)
14552 {
14553 EMSG2(_(e_notopen), infile);
14554 goto done;
14555 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 p = get_tv_string_buf_chk(&argvars[1], buf);
14557 if (p == NULL)
14558 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014559 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14560 err = TRUE;
14561 if (fclose(fd) != 0)
14562 err = TRUE;
14563 if (err)
14564 {
14565 EMSG(_("E677: Error writing temp file"));
14566 goto done;
14567 }
14568 }
14569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014570 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014571
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572#ifdef USE_CR
14573 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014574 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 {
14576 char_u *s;
14577
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014578 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 {
14580 if (*s == CAR)
14581 *s = NL;
14582 }
14583 }
14584#else
14585# ifdef USE_CRNL
14586 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014587 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 {
14589 char_u *s, *d;
14590
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014591 d = res;
14592 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 {
14594 if (s[0] == CAR && s[1] == NL)
14595 ++s;
14596 *d++ = *s;
14597 }
14598 *d = NUL;
14599 }
14600# endif
14601#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014602
14603done:
14604 if (infile != NULL)
14605 {
14606 mch_remove(infile);
14607 vim_free(infile);
14608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 rettv->v_type = VAR_STRING;
14610 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611}
14612
14613/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014614 * "tagfiles()" function
14615 */
14616/*ARGSUSED*/
14617 static void
14618f_tagfiles(argvars, rettv)
14619 typval_T *argvars;
14620 typval_T *rettv;
14621{
14622 char_u fname[MAXPATHL + 1];
14623 list_T *l;
14624
14625 l = list_alloc();
14626 if (l == NULL)
14627 {
14628 rettv->vval.v_number = 0;
14629 return;
14630 }
14631 rettv->vval.v_list = l;
14632 rettv->v_type = VAR_LIST;
14633 ++l->lv_refcount;
14634
14635 get_tagfname(TRUE, NULL);
14636 for (;;)
14637 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014638 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014639 break;
14640}
14641
14642/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014643 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014644 */
14645 static void
14646f_taglist(argvars, rettv)
14647 typval_T *argvars;
14648 typval_T *rettv;
14649{
14650 char_u *tag_pattern;
14651 list_T *l;
14652
14653 tag_pattern = get_tv_string(&argvars[0]);
14654
14655 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 if (*tag_pattern == NUL)
14657 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014658
14659 l = list_alloc();
14660 if (l != NULL)
14661 {
14662 if (get_tags(l, tag_pattern) != FAIL)
14663 {
14664 rettv->vval.v_list = l;
14665 rettv->v_type = VAR_LIST;
14666 ++l->lv_refcount;
14667 }
14668 else
14669 list_free(l);
14670 }
14671}
14672
14673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 * "tempname()" function
14675 */
14676/*ARGSUSED*/
14677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014679 typval_T *argvars;
14680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681{
14682 static int x = 'A';
14683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684 rettv->v_type = VAR_STRING;
14685 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686
14687 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14688 * names. Skip 'I' and 'O', they are used for shell redirection. */
14689 do
14690 {
14691 if (x == 'Z')
14692 x = '0';
14693 else if (x == '9')
14694 x = 'A';
14695 else
14696 {
14697#ifdef EBCDIC
14698 if (x == 'I')
14699 x = 'J';
14700 else if (x == 'R')
14701 x = 'S';
14702 else
14703#endif
14704 ++x;
14705 }
14706 } while (x == 'I' || x == 'O');
14707}
14708
14709/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014710 * "test(list)" function: Just checking the walls...
14711 */
14712/*ARGSUSED*/
14713 static void
14714f_test(argvars, rettv)
14715 typval_T *argvars;
14716 typval_T *rettv;
14717{
14718 /* Used for unit testing. Change the code below to your liking. */
14719#if 0
14720 listitem_T *li;
14721 list_T *l;
14722 char_u *bad, *good;
14723
14724 if (argvars[0].v_type != VAR_LIST)
14725 return;
14726 l = argvars[0].vval.v_list;
14727 if (l == NULL)
14728 return;
14729 li = l->lv_first;
14730 if (li == NULL)
14731 return;
14732 bad = get_tv_string(&li->li_tv);
14733 li = li->li_next;
14734 if (li == NULL)
14735 return;
14736 good = get_tv_string(&li->li_tv);
14737 rettv->vval.v_number = test_edit_score(bad, good);
14738#endif
14739}
14740
14741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 * "tolower(string)" function
14743 */
14744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014745f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 typval_T *argvars;
14747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748{
14749 char_u *p;
14750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 p = vim_strsave(get_tv_string(&argvars[0]));
14752 rettv->v_type = VAR_STRING;
14753 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754
14755 if (p != NULL)
14756 while (*p != NUL)
14757 {
14758#ifdef FEAT_MBYTE
14759 int l;
14760
14761 if (enc_utf8)
14762 {
14763 int c, lc;
14764
14765 c = utf_ptr2char(p);
14766 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014767 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 /* TODO: reallocate string when byte count changes. */
14769 if (utf_char2len(lc) == l)
14770 utf_char2bytes(lc, p);
14771 p += l;
14772 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014773 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 p += l; /* skip multi-byte character */
14775 else
14776#endif
14777 {
14778 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14779 ++p;
14780 }
14781 }
14782}
14783
14784/*
14785 * "toupper(string)" function
14786 */
14787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014789 typval_T *argvars;
14790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794}
14795
14796/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014797 * "tr(string, fromstr, tostr)" function
14798 */
14799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014800f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014801 typval_T *argvars;
14802 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014803{
14804 char_u *instr;
14805 char_u *fromstr;
14806 char_u *tostr;
14807 char_u *p;
14808#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014809 int inlen;
14810 int fromlen;
14811 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014812 int idx;
14813 char_u *cpstr;
14814 int cplen;
14815 int first = TRUE;
14816#endif
14817 char_u buf[NUMBUFLEN];
14818 char_u buf2[NUMBUFLEN];
14819 garray_T ga;
14820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14823 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014824
14825 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014826 rettv->v_type = VAR_STRING;
14827 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014828 if (fromstr == NULL || tostr == NULL)
14829 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014830 ga_init2(&ga, (int)sizeof(char), 80);
14831
14832#ifdef FEAT_MBYTE
14833 if (!has_mbyte)
14834#endif
14835 /* not multi-byte: fromstr and tostr must be the same length */
14836 if (STRLEN(fromstr) != STRLEN(tostr))
14837 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014838#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014839error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014840#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014841 EMSG2(_(e_invarg2), fromstr);
14842 ga_clear(&ga);
14843 return;
14844 }
14845
14846 /* fromstr and tostr have to contain the same number of chars */
14847 while (*instr != NUL)
14848 {
14849#ifdef FEAT_MBYTE
14850 if (has_mbyte)
14851 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014852 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014853 cpstr = instr;
14854 cplen = inlen;
14855 idx = 0;
14856 for (p = fromstr; *p != NUL; p += fromlen)
14857 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014858 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014859 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14860 {
14861 for (p = tostr; *p != NUL; p += tolen)
14862 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014863 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014864 if (idx-- == 0)
14865 {
14866 cplen = tolen;
14867 cpstr = p;
14868 break;
14869 }
14870 }
14871 if (*p == NUL) /* tostr is shorter than fromstr */
14872 goto error;
14873 break;
14874 }
14875 ++idx;
14876 }
14877
14878 if (first && cpstr == instr)
14879 {
14880 /* Check that fromstr and tostr have the same number of
14881 * (multi-byte) characters. Done only once when a character
14882 * of instr doesn't appear in fromstr. */
14883 first = FALSE;
14884 for (p = tostr; *p != NUL; p += tolen)
14885 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014886 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014887 --idx;
14888 }
14889 if (idx != 0)
14890 goto error;
14891 }
14892
14893 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014894 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014895 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014896
14897 instr += inlen;
14898 }
14899 else
14900#endif
14901 {
14902 /* When not using multi-byte chars we can do it faster. */
14903 p = vim_strchr(fromstr, *instr);
14904 if (p != NULL)
14905 ga_append(&ga, tostr[p - fromstr]);
14906 else
14907 ga_append(&ga, *instr);
14908 ++instr;
14909 }
14910 }
14911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014912 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014913}
14914
14915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916 * "type(expr)" function
14917 */
14918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014919f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014920 typval_T *argvars;
14921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014923 int n;
14924
14925 switch (argvars[0].v_type)
14926 {
14927 case VAR_NUMBER: n = 0; break;
14928 case VAR_STRING: n = 1; break;
14929 case VAR_FUNC: n = 2; break;
14930 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014931 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014932 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14933 }
14934 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935}
14936
14937/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014938 * "values(dict)" function
14939 */
14940 static void
14941f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 typval_T *argvars;
14943 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944{
14945 dict_list(argvars, rettv, 1);
14946}
14947
14948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 * "virtcol(string)" function
14950 */
14951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014952f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *argvars;
14954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955{
14956 colnr_T vcol = 0;
14957 pos_T *fp;
14958
14959 fp = var2fpos(&argvars[0], FALSE);
14960 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14961 {
14962 getvvcol(curwin, fp, NULL, NULL, &vcol);
14963 ++vcol;
14964 }
14965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967}
14968
14969/*
14970 * "visualmode()" function
14971 */
14972/*ARGSUSED*/
14973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014974f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014975 typval_T *argvars;
14976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977{
14978#ifdef FEAT_VISUAL
14979 char_u str[2];
14980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014981 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 str[0] = curbuf->b_visual_mode_eval;
14983 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985
14986 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014987 if ((argvars[0].v_type == VAR_NUMBER
14988 && argvars[0].vval.v_number != 0)
14989 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 curbuf->b_visual_mode_eval = NUL;
14992#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994#endif
14995}
14996
14997/*
14998 * "winbufnr(nr)" function
14999 */
15000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015001f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015002 typval_T *argvars;
15003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004{
15005 win_T *wp;
15006
15007 wp = find_win_by_nr(&argvars[0]);
15008 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015011 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012}
15013
15014/*
15015 * "wincol()" function
15016 */
15017/*ARGSUSED*/
15018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015020 typval_T *argvars;
15021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022{
15023 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015024 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025}
15026
15027/*
15028 * "winheight(nr)" function
15029 */
15030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 typval_T *argvars;
15033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034{
15035 win_T *wp;
15036
15037 wp = find_win_by_nr(&argvars[0]);
15038 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015041 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042}
15043
15044/*
15045 * "winline()" function
15046 */
15047/*ARGSUSED*/
15048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015050 typval_T *argvars;
15051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052{
15053 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015054 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055}
15056
15057/*
15058 * "winnr()" function
15059 */
15060/* ARGSUSED */
15061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015062f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015063 typval_T *argvars;
15064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065{
15066 int nr = 1;
15067#ifdef FEAT_WINDOWS
15068 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015069 win_T *twin = curwin;
15070 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015072 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015074 arg = get_tv_string_chk(&argvars[0]);
15075 if (arg == NULL)
15076 nr = 0; /* type error; errmsg already given */
15077 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015078 twin = lastwin;
15079 else if (STRCMP(arg, "#") == 0)
15080 {
15081 twin = prevwin;
15082 if (prevwin == NULL)
15083 nr = 0;
15084 }
15085 else
15086 {
15087 EMSG2(_(e_invexpr2), arg);
15088 nr = 0;
15089 }
15090 }
15091
15092 if (nr > 0)
15093 for (wp = firstwin; wp != twin; wp = wp->w_next)
15094 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015096 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097}
15098
15099/*
15100 * "winrestcmd()" function
15101 */
15102/* ARGSUSED */
15103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015104f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 typval_T *argvars;
15106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107{
15108#ifdef FEAT_WINDOWS
15109 win_T *wp;
15110 int winnr = 1;
15111 garray_T ga;
15112 char_u buf[50];
15113
15114 ga_init2(&ga, (int)sizeof(char), 70);
15115 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15116 {
15117 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15118 ga_concat(&ga, buf);
15119# ifdef FEAT_VERTSPLIT
15120 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15121 ga_concat(&ga, buf);
15122# endif
15123 ++winnr;
15124 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015125 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132}
15133
15134/*
15135 * "winwidth(nr)" function
15136 */
15137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015138f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 typval_T *argvars;
15140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141{
15142 win_T *wp;
15143
15144 wp = find_win_by_nr(&argvars[0]);
15145 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 else
15148#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015151 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152#endif
15153}
15154
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015156 * "writefile()" function
15157 */
15158 static void
15159f_writefile(argvars, rettv)
15160 typval_T *argvars;
15161 typval_T *rettv;
15162{
15163 int binary = FALSE;
15164 char_u *fname;
15165 FILE *fd;
15166 listitem_T *li;
15167 char_u *s;
15168 int ret = 0;
15169 int c;
15170
15171 if (argvars[0].v_type != VAR_LIST)
15172 {
15173 EMSG2(_(e_listarg), "writefile()");
15174 return;
15175 }
15176 if (argvars[0].vval.v_list == NULL)
15177 return;
15178
15179 if (argvars[2].v_type != VAR_UNKNOWN
15180 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15181 binary = TRUE;
15182
15183 /* Always open the file in binary mode, library functions have a mind of
15184 * their own about CR-LF conversion. */
15185 fname = get_tv_string(&argvars[1]);
15186 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15187 {
15188 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15189 ret = -1;
15190 }
15191 else
15192 {
15193 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15194 li = li->li_next)
15195 {
15196 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15197 {
15198 if (*s == '\n')
15199 c = putc(NUL, fd);
15200 else
15201 c = putc(*s, fd);
15202 if (c == EOF)
15203 {
15204 ret = -1;
15205 break;
15206 }
15207 }
15208 if (!binary || li->li_next != NULL)
15209 if (putc('\n', fd) == EOF)
15210 {
15211 ret = -1;
15212 break;
15213 }
15214 if (ret < 0)
15215 {
15216 EMSG(_(e_write));
15217 break;
15218 }
15219 }
15220 fclose(fd);
15221 }
15222
15223 rettv->vval.v_number = ret;
15224}
15225
15226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227 * Translate a String variable into a position.
15228 */
15229 static pos_T *
15230var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015231 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 int lnum; /* TRUE when $ is last line */
15233{
15234 char_u *name;
15235 static pos_T pos;
15236 pos_T *pp;
15237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 name = get_tv_string_chk(varp);
15239 if (name == NULL)
15240 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 if (name[0] == '.') /* cursor */
15242 return &curwin->w_cursor;
15243 if (name[0] == '\'') /* mark */
15244 {
15245 pp = getmark(name[1], FALSE);
15246 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15247 return NULL;
15248 return pp;
15249 }
15250 if (name[0] == '$') /* last column or line */
15251 {
15252 if (lnum)
15253 {
15254 pos.lnum = curbuf->b_ml.ml_line_count;
15255 pos.col = 0;
15256 }
15257 else
15258 {
15259 pos.lnum = curwin->w_cursor.lnum;
15260 pos.col = (colnr_T)STRLEN(ml_get_curline());
15261 }
15262 return &pos;
15263 }
15264 return NULL;
15265}
15266
15267/*
15268 * Get the length of an environment variable name.
15269 * Advance "arg" to the first character after the name.
15270 * Return 0 for error.
15271 */
15272 static int
15273get_env_len(arg)
15274 char_u **arg;
15275{
15276 char_u *p;
15277 int len;
15278
15279 for (p = *arg; vim_isIDc(*p); ++p)
15280 ;
15281 if (p == *arg) /* no name found */
15282 return 0;
15283
15284 len = (int)(p - *arg);
15285 *arg = p;
15286 return len;
15287}
15288
15289/*
15290 * Get the length of the name of a function or internal variable.
15291 * "arg" is advanced to the first non-white character after the name.
15292 * Return 0 if something is wrong.
15293 */
15294 static int
15295get_id_len(arg)
15296 char_u **arg;
15297{
15298 char_u *p;
15299 int len;
15300
15301 /* Find the end of the name. */
15302 for (p = *arg; eval_isnamec(*p); ++p)
15303 ;
15304 if (p == *arg) /* no name found */
15305 return 0;
15306
15307 len = (int)(p - *arg);
15308 *arg = skipwhite(p);
15309
15310 return len;
15311}
15312
15313/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015314 * Get the length of the name of a variable or function.
15315 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015317 * Return -1 if curly braces expansion failed.
15318 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 * If the name contains 'magic' {}'s, expand them and return the
15320 * expanded name in an allocated string via 'alias' - caller must free.
15321 */
15322 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015323get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 char_u **arg;
15325 char_u **alias;
15326 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015327 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328{
15329 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 char_u *p;
15331 char_u *expr_start;
15332 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333
15334 *alias = NULL; /* default to no alias */
15335
15336 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15337 && (*arg)[2] == (int)KE_SNR)
15338 {
15339 /* hard coded <SNR>, already translated */
15340 *arg += 3;
15341 return get_id_len(arg) + 3;
15342 }
15343 len = eval_fname_script(*arg);
15344 if (len > 0)
15345 {
15346 /* literal "<SID>", "s:" or "<SNR>" */
15347 *arg += len;
15348 }
15349
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015353 p = find_name_end(*arg, &expr_start, &expr_end,
15354 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 if (expr_start != NULL)
15356 {
15357 char_u *temp_string;
15358
15359 if (!evaluate)
15360 {
15361 len += (int)(p - *arg);
15362 *arg = skipwhite(p);
15363 return len;
15364 }
15365
15366 /*
15367 * Include any <SID> etc in the expanded string:
15368 * Thus the -len here.
15369 */
15370 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15371 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 *alias = temp_string;
15374 *arg = skipwhite(p);
15375 return (int)STRLEN(temp_string);
15376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377
15378 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015379 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 EMSG2(_(e_invexpr2), *arg);
15381
15382 return len;
15383}
15384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385/*
15386 * Find the end of a variable or function name, taking care of magic braces.
15387 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15388 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015389 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 * Return a pointer to just after the name. Equal to "arg" if there is no
15391 * valid name.
15392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015394find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 char_u *arg;
15396 char_u **expr_start;
15397 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015398 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 int mb_nest = 0;
15401 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 char_u *p;
15403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 *expr_start = NULL;
15407 *expr_end = NULL;
15408 }
15409
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015410 /* Quick check for valid starting character. */
15411 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15412 return arg;
15413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 for (p = arg; *p != NUL
15415 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015416 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015417 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015418 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015419 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015421 if (*p == '\'')
15422 {
15423 /* skip over 'string' to avoid counting [ and ] inside it. */
15424 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15425 ;
15426 if (*p == NUL)
15427 break;
15428 }
15429 else if (*p == '"')
15430 {
15431 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15432 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15433 if (*p == '\\' && p[1] != NUL)
15434 ++p;
15435 if (*p == NUL)
15436 break;
15437 }
15438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 if (*p == '[')
15442 ++br_nest;
15443 else if (*p == ']')
15444 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 if (*p == '{')
15450 {
15451 mb_nest++;
15452 if (expr_start != NULL && *expr_start == NULL)
15453 *expr_start = p;
15454 }
15455 else if (*p == '}')
15456 {
15457 mb_nest--;
15458 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15459 *expr_end = p;
15460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 }
15463
15464 return p;
15465}
15466
15467/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015468 * Expands out the 'magic' {}'s in a variable/function name.
15469 * Note that this can call itself recursively, to deal with
15470 * constructs like foo{bar}{baz}{bam}
15471 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15472 * "in_start" ^
15473 * "expr_start" ^
15474 * "expr_end" ^
15475 * "in_end" ^
15476 *
15477 * Returns a new allocated string, which the caller must free.
15478 * Returns NULL for failure.
15479 */
15480 static char_u *
15481make_expanded_name(in_start, expr_start, expr_end, in_end)
15482 char_u *in_start;
15483 char_u *expr_start;
15484 char_u *expr_end;
15485 char_u *in_end;
15486{
15487 char_u c1;
15488 char_u *retval = NULL;
15489 char_u *temp_result;
15490 char_u *nextcmd = NULL;
15491
15492 if (expr_end == NULL || in_end == NULL)
15493 return NULL;
15494 *expr_start = NUL;
15495 *expr_end = NUL;
15496 c1 = *in_end;
15497 *in_end = NUL;
15498
15499 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15500 if (temp_result != NULL && nextcmd == NULL)
15501 {
15502 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15503 + (in_end - expr_end) + 1));
15504 if (retval != NULL)
15505 {
15506 STRCPY(retval, in_start);
15507 STRCAT(retval, temp_result);
15508 STRCAT(retval, expr_end + 1);
15509 }
15510 }
15511 vim_free(temp_result);
15512
15513 *in_end = c1; /* put char back for error messages */
15514 *expr_start = '{';
15515 *expr_end = '}';
15516
15517 if (retval != NULL)
15518 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015519 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015520 if (expr_start != NULL)
15521 {
15522 /* Further expansion! */
15523 temp_result = make_expanded_name(retval, expr_start,
15524 expr_end, temp_result);
15525 vim_free(retval);
15526 retval = temp_result;
15527 }
15528 }
15529
15530 return retval;
15531}
15532
15533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015535 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 */
15537 static int
15538eval_isnamec(c)
15539 int c;
15540{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015541 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15542}
15543
15544/*
15545 * Return TRUE if character "c" can be used as the first character in a
15546 * variable or function name (excluding '{' and '}').
15547 */
15548 static int
15549eval_isnamec1(c)
15550 int c;
15551{
15552 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553}
15554
15555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 * Set number v: variable to "val".
15557 */
15558 void
15559set_vim_var_nr(idx, val)
15560 int idx;
15561 long val;
15562{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015563 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564}
15565
15566/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015567 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 */
15569 long
15570get_vim_var_nr(idx)
15571 int idx;
15572{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015573 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574}
15575
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015576#if defined(FEAT_AUTOCMD) || defined(PROTO)
15577/*
15578 * Get string v: variable value. Uses a static buffer, can only be used once.
15579 */
15580 char_u *
15581get_vim_var_str(idx)
15582 int idx;
15583{
15584 return get_tv_string(&vimvars[idx].vv_tv);
15585}
15586#endif
15587
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588/*
15589 * Set v:count, v:count1 and v:prevcount.
15590 */
15591 void
15592set_vcount(count, count1)
15593 long count;
15594 long count1;
15595{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015596 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15597 vimvars[VV_COUNT].vv_nr = count;
15598 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599}
15600
15601/*
15602 * Set string v: variable to a copy of "val".
15603 */
15604 void
15605set_vim_var_string(idx, val, len)
15606 int idx;
15607 char_u *val;
15608 int len; /* length of "val" to use or -1 (whole string) */
15609{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015610 /* Need to do this (at least) once, since we can't initialize a union.
15611 * Will always be invoked when "v:progname" is set. */
15612 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15613
Bram Moolenaare9a41262005-01-15 22:18:47 +000015614 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015616 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015618 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015620 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621}
15622
15623/*
15624 * Set v:register if needed.
15625 */
15626 void
15627set_reg_var(c)
15628 int c;
15629{
15630 char_u regname;
15631
15632 if (c == 0 || c == ' ')
15633 regname = '"';
15634 else
15635 regname = c;
15636 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015637 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 set_vim_var_string(VV_REG, &regname, 1);
15639}
15640
15641/*
15642 * Get or set v:exception. If "oldval" == NULL, return the current value.
15643 * Otherwise, restore the value to "oldval" and return NULL.
15644 * Must always be called in pairs to save and restore v:exception! Does not
15645 * take care of memory allocations.
15646 */
15647 char_u *
15648v_exception(oldval)
15649 char_u *oldval;
15650{
15651 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015652 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653
Bram Moolenaare9a41262005-01-15 22:18:47 +000015654 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 return NULL;
15656}
15657
15658/*
15659 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15660 * Otherwise, restore the value to "oldval" and return NULL.
15661 * Must always be called in pairs to save and restore v:throwpoint! Does not
15662 * take care of memory allocations.
15663 */
15664 char_u *
15665v_throwpoint(oldval)
15666 char_u *oldval;
15667{
15668 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015669 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670
Bram Moolenaare9a41262005-01-15 22:18:47 +000015671 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 return NULL;
15673}
15674
15675#if defined(FEAT_AUTOCMD) || defined(PROTO)
15676/*
15677 * Set v:cmdarg.
15678 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15679 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15680 * Must always be called in pairs!
15681 */
15682 char_u *
15683set_cmdarg(eap, oldarg)
15684 exarg_T *eap;
15685 char_u *oldarg;
15686{
15687 char_u *oldval;
15688 char_u *newval;
15689 unsigned len;
15690
Bram Moolenaare9a41262005-01-15 22:18:47 +000015691 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015692 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015694 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015695 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015696 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697 }
15698
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015699 if (eap->force_bin == FORCE_BIN)
15700 len = 6;
15701 else if (eap->force_bin == FORCE_NOBIN)
15702 len = 8;
15703 else
15704 len = 0;
15705 if (eap->force_ff != 0)
15706 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15707# ifdef FEAT_MBYTE
15708 if (eap->force_enc != 0)
15709 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015710 if (eap->bad_char != 0)
15711 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015712# endif
15713
15714 newval = alloc(len + 1);
15715 if (newval == NULL)
15716 return NULL;
15717
15718 if (eap->force_bin == FORCE_BIN)
15719 sprintf((char *)newval, " ++bin");
15720 else if (eap->force_bin == FORCE_NOBIN)
15721 sprintf((char *)newval, " ++nobin");
15722 else
15723 *newval = NUL;
15724 if (eap->force_ff != 0)
15725 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15726 eap->cmd + eap->force_ff);
15727# ifdef FEAT_MBYTE
15728 if (eap->force_enc != 0)
15729 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15730 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000015731 if (eap->bad_char != 0)
15732 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
15733 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015734# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015735 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015736 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737}
15738#endif
15739
15740/*
15741 * Get the value of internal variable "name".
15742 * Return OK or FAIL.
15743 */
15744 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015745get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 char_u *name;
15747 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015749 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750{
15751 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015752 typval_T *tv = NULL;
15753 typval_T atv;
15754 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756
15757 /* truncate the name, so that we can use strcmp() */
15758 cc = name[len];
15759 name[len] = NUL;
15760
15761 /*
15762 * Check for "b:changedtick".
15763 */
15764 if (STRCMP(name, "b:changedtick") == 0)
15765 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015766 atv.v_type = VAR_NUMBER;
15767 atv.vval.v_number = curbuf->b_changedtick;
15768 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 }
15770
15771 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 * Check for user-defined variables.
15773 */
15774 else
15775 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015776 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015778 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 }
15780
Bram Moolenaare9a41262005-01-15 22:18:47 +000015781 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015783 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 ret = FAIL;
15786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015788 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789
15790 name[len] = cc;
15791
15792 return ret;
15793}
15794
15795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015796 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15797 * Also handle function call with Funcref variable: func(expr)
15798 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15799 */
15800 static int
15801handle_subscript(arg, rettv, evaluate, verbose)
15802 char_u **arg;
15803 typval_T *rettv;
15804 int evaluate; /* do more than finding the end */
15805 int verbose; /* give error messages */
15806{
15807 int ret = OK;
15808 dict_T *selfdict = NULL;
15809 char_u *s;
15810 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015811 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015812
15813 while (ret == OK
15814 && (**arg == '['
15815 || (**arg == '.' && rettv->v_type == VAR_DICT)
15816 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15817 && !vim_iswhite(*(*arg - 1)))
15818 {
15819 if (**arg == '(')
15820 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015821 /* need to copy the funcref so that we can clear rettv */
15822 functv = *rettv;
15823 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015824
15825 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015826 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015827 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015828 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15829 &len, evaluate, selfdict);
15830
15831 /* Clear the funcref afterwards, so that deleting it while
15832 * evaluating the arguments is possible (see test55). */
15833 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015834
15835 /* Stop the expression evaluation when immediately aborting on
15836 * error, or when an interrupt occurred or an exception was thrown
15837 * but not caught. */
15838 if (aborting())
15839 {
15840 if (ret == OK)
15841 clear_tv(rettv);
15842 ret = FAIL;
15843 }
15844 dict_unref(selfdict);
15845 selfdict = NULL;
15846 }
15847 else /* **arg == '[' || **arg == '.' */
15848 {
15849 dict_unref(selfdict);
15850 if (rettv->v_type == VAR_DICT)
15851 {
15852 selfdict = rettv->vval.v_dict;
15853 if (selfdict != NULL)
15854 ++selfdict->dv_refcount;
15855 }
15856 else
15857 selfdict = NULL;
15858 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15859 {
15860 clear_tv(rettv);
15861 ret = FAIL;
15862 }
15863 }
15864 }
15865 dict_unref(selfdict);
15866 return ret;
15867}
15868
15869/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015870 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15871 * value).
15872 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015874alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015875{
Bram Moolenaar33570922005-01-25 22:26:29 +000015876 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015877}
15878
15879/*
15880 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 * The string "s" must have been allocated, it is consumed.
15882 * Return NULL for out of memory, the variable otherwise.
15883 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015884 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 char_u *s;
15887{
Bram Moolenaar33570922005-01-25 22:26:29 +000015888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015890 rettv = alloc_tv();
15891 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015893 rettv->v_type = VAR_STRING;
15894 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 }
15896 else
15897 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015898 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899}
15900
15901/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015902 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 */
15904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015905free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015906 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907{
15908 if (varp != NULL)
15909 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015910 switch (varp->v_type)
15911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015912 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015913 func_unref(varp->vval.v_string);
15914 /*FALLTHROUGH*/
15915 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015916 vim_free(varp->vval.v_string);
15917 break;
15918 case VAR_LIST:
15919 list_unref(varp->vval.v_list);
15920 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015921 case VAR_DICT:
15922 dict_unref(varp->vval.v_dict);
15923 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015924 case VAR_NUMBER:
15925 case VAR_UNKNOWN:
15926 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015927 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015928 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015929 break;
15930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 vim_free(varp);
15932 }
15933}
15934
15935/*
15936 * Free the memory for a variable value and set the value to NULL or 0.
15937 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015938 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015940 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941{
15942 if (varp != NULL)
15943 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015944 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015946 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015947 func_unref(varp->vval.v_string);
15948 /*FALLTHROUGH*/
15949 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015950 vim_free(varp->vval.v_string);
15951 varp->vval.v_string = NULL;
15952 break;
15953 case VAR_LIST:
15954 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015955 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015956 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015957 case VAR_DICT:
15958 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015959 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015960 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015961 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015962 varp->vval.v_number = 0;
15963 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015964 case VAR_UNKNOWN:
15965 break;
15966 default:
15967 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015969 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 }
15971}
15972
15973/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015974 * Set the value of a variable to NULL without freeing items.
15975 */
15976 static void
15977init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015979{
15980 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015981 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015982}
15983
15984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985 * Get the number value of a variable.
15986 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015987 * For incompatible types, return 0.
15988 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15989 * caller of incompatible types: it sets *denote to TRUE if "denote"
15990 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 */
15992 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015993get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015994 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015996 int error = FALSE;
15997
15998 return get_tv_number_chk(varp, &error); /* return 0L on error */
15999}
16000
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016001 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016002get_tv_number_chk(varp, denote)
16003 typval_T *varp;
16004 int *denote;
16005{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016006 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016008 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016010 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016011 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016012 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016013 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016014 break;
16015 case VAR_STRING:
16016 if (varp->vval.v_string != NULL)
16017 vim_str2nr(varp->vval.v_string, NULL, NULL,
16018 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016020 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016021 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016022 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016023 case VAR_DICT:
16024 EMSG(_("E728: Using a Dictionary as a number"));
16025 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016026 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016027 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016028 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 if (denote == NULL) /* useful for values that must be unsigned */
16031 n = -1;
16032 else
16033 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016034 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035}
16036
16037/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016038 * Get the lnum from the first argument.
16039 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016040 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 */
16042 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016043get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045{
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 linenr_T lnum;
16048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016049 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 if (lnum == 0) /* no valid number, try using line() */
16051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016052 rettv.v_type = VAR_NUMBER;
16053 f_line(argvars, &rettv);
16054 lnum = rettv.vval.v_number;
16055 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 }
16057 return lnum;
16058}
16059
16060/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016061 * Get the lnum from the first argument.
16062 * Also accepts "$", then "buf" is used.
16063 * Returns 0 on error.
16064 */
16065 static linenr_T
16066get_tv_lnum_buf(argvars, buf)
16067 typval_T *argvars;
16068 buf_T *buf;
16069{
16070 if (argvars[0].v_type == VAR_STRING
16071 && argvars[0].vval.v_string != NULL
16072 && argvars[0].vval.v_string[0] == '$'
16073 && buf != NULL)
16074 return buf->b_ml.ml_line_count;
16075 return get_tv_number_chk(&argvars[0], NULL);
16076}
16077
16078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 * Get the string value of a variable.
16080 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016081 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16082 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 * If the String variable has never been set, return an empty string.
16084 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016085 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16086 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 */
16088 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016089get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091{
16092 static char_u mybuf[NUMBUFLEN];
16093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016094 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095}
16096
16097 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016098get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016100 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016102 char_u *res = get_tv_string_buf_chk(varp, buf);
16103
16104 return res != NULL ? res : (char_u *)"";
16105}
16106
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016107 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016108get_tv_string_chk(varp)
16109 typval_T *varp;
16110{
16111 static char_u mybuf[NUMBUFLEN];
16112
16113 return get_tv_string_buf_chk(varp, mybuf);
16114}
16115
16116 static char_u *
16117get_tv_string_buf_chk(varp, buf)
16118 typval_T *varp;
16119 char_u *buf;
16120{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016121 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016123 case VAR_NUMBER:
16124 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16125 return buf;
16126 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016127 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016128 break;
16129 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016130 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016131 break;
16132 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016133 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016134 break;
16135 case VAR_STRING:
16136 if (varp->vval.v_string != NULL)
16137 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016138 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016139 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016140 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016143 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144}
16145
16146/*
16147 * Find variable "name" in the list of variables.
16148 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016149 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016150 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016151 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016154find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016156 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160
Bram Moolenaara7043832005-01-21 11:56:39 +000016161 ht = find_var_ht(name, &varname);
16162 if (htp != NULL)
16163 *htp = ht;
16164 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016166 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167}
16168
16169/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016170 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016171 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016174find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016175 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016176 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016177 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016178{
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 hashitem_T *hi;
16180
16181 if (*varname == NUL)
16182 {
16183 /* Must be something like "s:", otherwise "ht" would be NULL. */
16184 switch (varname[-2])
16185 {
16186 case 's': return &SCRIPT_SV(current_SID).sv_var;
16187 case 'g': return &globvars_var;
16188 case 'v': return &vimvars_var;
16189 case 'b': return &curbuf->b_bufvar;
16190 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016191 case 'l': return current_funccal == NULL
16192 ? NULL : &current_funccal->l_vars_var;
16193 case 'a': return current_funccal == NULL
16194 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016195 }
16196 return NULL;
16197 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016198
16199 hi = hash_find(ht, varname);
16200 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016201 {
16202 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016203 * worked find the variable again. Don't auto-load a script if it was
16204 * loaded already, otherwise it would be loaded every time when
16205 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016206 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016207 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016208 hi = hash_find(ht, varname);
16209 if (HASHITEM_EMPTY(hi))
16210 return NULL;
16211 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016213}
16214
16215/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016216 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016217 * Set "varname" to the start of name without ':'.
16218 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016219 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016220find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 char_u *name;
16222 char_u **varname;
16223{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016224 hashitem_T *hi;
16225
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 if (name[1] != ':')
16227 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016228 /* The name must not start with a colon or #. */
16229 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 return NULL;
16231 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016232
16233 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016234 hi = hash_find(&compat_hashtab, name);
16235 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016236 return &compat_hashtab;
16237
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 return &globvarht; /* global variable */
16240 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 }
16242 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016243 if (*name == 'g') /* global variable */
16244 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016245 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16246 */
16247 if (vim_strchr(name + 2, ':') != NULL
16248 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016249 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016251 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016253 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 if (*name == 'v') /* v: variable */
16255 return &vimvarht;
16256 if (*name == 'a' && current_funccal != NULL) /* function argument */
16257 return &current_funccal->l_avars.dv_hashtab;
16258 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16259 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260 if (*name == 's' /* script variable */
16261 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16262 return &SCRIPT_VARS(current_SID);
16263 return NULL;
16264}
16265
16266/*
16267 * Get the string value of a (global/local) variable.
16268 * Returns NULL when it doesn't exist.
16269 */
16270 char_u *
16271get_var_value(name)
16272 char_u *name;
16273{
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275
Bram Moolenaara7043832005-01-21 11:56:39 +000016276 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 if (v == NULL)
16278 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016279 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280}
16281
16282/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 * sourcing this script and when executing functions defined in the script.
16285 */
16286 void
16287new_script_vars(id)
16288 scid_T id;
16289{
Bram Moolenaara7043832005-01-21 11:56:39 +000016290 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 hashtab_T *ht;
16292 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016293
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16295 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016296 /* Re-allocating ga_data means that an ht_array pointing to
16297 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016298 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016299 for (i = 1; i <= ga_scripts.ga_len; ++i)
16300 {
16301 ht = &SCRIPT_VARS(i);
16302 if (ht->ht_mask == HT_INIT_SIZE - 1)
16303 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 sv = &SCRIPT_SV(i);
16305 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016306 }
16307
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 while (ga_scripts.ga_len < id)
16309 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016310 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16311 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 }
16314 }
16315}
16316
16317/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016318 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16319 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 */
16321 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016322init_var_dict(dict, dict_var)
16323 dict_T *dict;
16324 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325{
Bram Moolenaar33570922005-01-25 22:26:29 +000016326 hash_init(&dict->dv_hashtab);
16327 dict->dv_refcount = 99999;
16328 dict_var->di_tv.vval.v_dict = dict;
16329 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016330 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016331 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16332 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333}
16334
16335/*
16336 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016337 * Frees all allocated variables and the value they contain.
16338 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 */
16340 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016341vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016342 hashtab_T *ht;
16343{
16344 vars_clear_ext(ht, TRUE);
16345}
16346
16347/*
16348 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16349 */
16350 static void
16351vars_clear_ext(ht, free_val)
16352 hashtab_T *ht;
16353 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354{
Bram Moolenaara7043832005-01-21 11:56:39 +000016355 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 hashitem_T *hi;
16357 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358
Bram Moolenaar33570922005-01-25 22:26:29 +000016359 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016360 todo = ht->ht_used;
16361 for (hi = ht->ht_array; todo > 0; ++hi)
16362 {
16363 if (!HASHITEM_EMPTY(hi))
16364 {
16365 --todo;
16366
Bram Moolenaar33570922005-01-25 22:26:29 +000016367 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016368 * ht_array might change then. hash_clear() takes care of it
16369 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016370 v = HI2DI(hi);
16371 if (free_val)
16372 clear_tv(&v->di_tv);
16373 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16374 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016375 }
16376 }
16377 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016378 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379}
16380
Bram Moolenaara7043832005-01-21 11:56:39 +000016381/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016382 * Delete a variable from hashtab "ht" at item "hi".
16383 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016386delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016387 hashtab_T *ht;
16388 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389{
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016391
16392 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016393 clear_tv(&di->di_tv);
16394 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395}
16396
16397/*
16398 * List the value of one internal variable.
16399 */
16400 static void
16401list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 char_u *prefix;
16404{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016405 char_u *tofree;
16406 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016407 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016408
Bram Moolenaar33570922005-01-25 22:26:29 +000016409 s = echo_string(&v->di_tv, &tofree, numbuf);
16410 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016411 s == NULL ? (char_u *)"" : s);
16412 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413}
16414
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 static void
16416list_one_var_a(prefix, name, type, string)
16417 char_u *prefix;
16418 char_u *name;
16419 int type;
16420 char_u *string;
16421{
16422 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16423 if (name != NULL) /* "a:" vars don't have a name stored */
16424 msg_puts(name);
16425 msg_putchar(' ');
16426 msg_advance(22);
16427 if (type == VAR_NUMBER)
16428 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016429 else if (type == VAR_FUNC)
16430 msg_putchar('*');
16431 else if (type == VAR_LIST)
16432 {
16433 msg_putchar('[');
16434 if (*string == '[')
16435 ++string;
16436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016437 else if (type == VAR_DICT)
16438 {
16439 msg_putchar('{');
16440 if (*string == '{')
16441 ++string;
16442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 else
16444 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016445
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016447
16448 if (type == VAR_FUNC)
16449 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450}
16451
16452/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016453 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 * If the variable already exists, the value is updated.
16455 * Otherwise the variable is created.
16456 */
16457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016458set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016460 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016461 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462{
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016465 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016466 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016468 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016469 {
16470 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16471 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16472 ? name[2] : name[0]))
16473 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016474 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016475 return;
16476 }
16477 if (function_exists(name))
16478 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016479 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016480 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016481 return;
16482 }
16483 }
16484
Bram Moolenaara7043832005-01-21 11:56:39 +000016485 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016487 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016488 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016489 return;
16490 }
16491
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016492 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016493 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016495 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016496 if (var_check_ro(v->di_flags, name)
16497 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016498 return;
16499 if (v->di_tv.v_type != tv->v_type
16500 && !((v->di_tv.v_type == VAR_STRING
16501 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 && (tv->v_type == VAR_STRING
16503 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016504 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016505 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016506 return;
16507 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016508
16509 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016510 * Handle setting internal v: variables separately: we don't change
16511 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016512 */
16513 if (ht == &vimvarht)
16514 {
16515 if (v->di_tv.v_type == VAR_STRING)
16516 {
16517 vim_free(v->di_tv.vval.v_string);
16518 if (copy || tv->v_type != VAR_STRING)
16519 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16520 else
16521 {
16522 /* Take over the string to avoid an extra alloc/free. */
16523 v->di_tv.vval.v_string = tv->vval.v_string;
16524 tv->vval.v_string = NULL;
16525 }
16526 }
16527 else if (v->di_tv.v_type != VAR_NUMBER)
16528 EMSG2(_(e_intern2), "set_var()");
16529 else
16530 v->di_tv.vval.v_number = get_tv_number(tv);
16531 return;
16532 }
16533
16534 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 }
16536 else /* add a new variable */
16537 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016538 /* Make sure the variable name is valid. */
16539 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016540 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16541 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016542 {
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) */