blob: 506cfa5bb5bf3a06c5a9977cf3ee5f0307c660ac [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));
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static listitem_T *listitem_alloc __ARGS((void));
404static void listitem_free __ARGS((listitem_T *item));
405static void listitem_remove __ARGS((list_T *l, listitem_T *item));
406static long list_len __ARGS((list_T *l));
407static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
408static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
409static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static listitem_T *list_find __ARGS((list_T *l, long n));
411static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static void list_append __ARGS((list_T *l, listitem_T *item));
413static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000414static int list_append_string __ARGS((list_T *l, char_u *str, int len));
415static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
417static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
418static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000421static char_u *list2string __ARGS((typval_T *tv, int copyID));
422static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000423static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
424static void set_ref_in_list __ARGS((list_T *l, int copyID));
425static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static void dict_unref __ARGS((dict_T *d));
427static void dict_free __ARGS((dict_T *d));
428static dictitem_T *dictitem_alloc __ARGS((char_u *key));
429static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
430static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
431static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static int dict_add __ARGS((dict_T *d, dictitem_T *item));
434static long dict_len __ARGS((dict_T *d));
435static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
439static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
441static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
442static int find_internal_func __ARGS((char_u *name));
443static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
444static 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));
445static 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 +0000446static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447
448static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000467#if defined(FEAT_INS_EXPAND)
468static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
476static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000502static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000504static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000510static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000518static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000541static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000547static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000564static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000568#ifdef vim_mkdir
569static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
570#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000575static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000576static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000578static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000590static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000592static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000599static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000600static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000605static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
609#ifdef HAVE_STRFTIME
610static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
612static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000624static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000625static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000626static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000627static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000628static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000630static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
648static int get_env_len __ARGS((char_u **arg));
649static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000651static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
652#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
653#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
654 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000655static 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 +0000656static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000657static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
659static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static typval_T *alloc_tv __ARGS((void));
661static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void init_tv __ARGS((typval_T *varp));
663static long get_tv_number __ARGS((typval_T *varp));
664static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000665static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static char_u *get_tv_string __ARGS((typval_T *varp));
667static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000668static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000670static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
672static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
673static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
674static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
675static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
676static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
677static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000678static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000680static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
682static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
683static int eval_fname_script __ARGS((char_u *p));
684static int eval_fname_sid __ARGS((char_u *p));
685static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static ufunc_T *find_func __ARGS((char_u *name));
687static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000688static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000689#ifdef FEAT_PROFILE
690static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000691static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
692static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
693static int
694# ifdef __BORLANDC__
695 _RTLENTRYF
696# endif
697 prof_total_cmp __ARGS((const void *s1, const void *s2));
698static int
699# ifdef __BORLANDC__
700 _RTLENTRYF
701# endif
702 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000704static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000705static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000706static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static void func_free __ARGS((ufunc_T *fp));
708static void func_unref __ARGS((char_u *name));
709static void func_ref __ARGS((char_u *name));
710static 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));
711static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000712static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000713static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
714static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000716/* Character used as separated in autoload function/variable names. */
717#define AUTOLOAD_CHAR '#'
718
Bram Moolenaar33570922005-01-25 22:26:29 +0000719/*
720 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000721 */
722 void
723eval_init()
724{
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 int i;
726 struct vimvar *p;
727
728 init_var_dict(&globvardict, &globvars_var);
729 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000730 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000731 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000732
733 for (i = 0; i < VV_LEN; ++i)
734 {
735 p = &vimvars[i];
736 STRCPY(p->vv_di.di_key, p->vv_name);
737 if (p->vv_flags & VV_RO)
738 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
739 else if (p->vv_flags & VV_RO_SBX)
740 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
741 else
742 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000743
744 /* add to v: scope dict, unless the value is not always available */
745 if (p->vv_type != VAR_UNKNOWN)
746 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000747 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000748 /* add to compat scope dict */
749 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000751}
752
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000753#if defined(EXITFREE) || defined(PROTO)
754 void
755eval_clear()
756{
757 int i;
758 struct vimvar *p;
759
760 for (i = 0; i < VV_LEN; ++i)
761 {
762 p = &vimvars[i];
763 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000764 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000766 p->vv_di.di_tv.vval.v_string = NULL;
767 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000768 }
769 hash_clear(&vimvarht);
770 hash_clear(&compat_hashtab);
771
772 /* script-local variables */
773 for (i = 1; i <= ga_scripts.ga_len; ++i)
774 vars_clear(&SCRIPT_VARS(i));
775 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000776 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000777
778 /* global variables */
779 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000780
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000781 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000782 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000783 hash_clear(&func_hashtab);
784
785 /* unreferenced lists and dicts */
786 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000787}
788#endif
789
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 * Return the name of the executed function.
792 */
793 char_u *
794func_name(cookie)
795 void *cookie;
796{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000797 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
800/*
801 * Return the address holding the next breakpoint line for a funccall cookie.
802 */
803 linenr_T *
804func_breakpoint(cookie)
805 void *cookie;
806{
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808}
809
810/*
811 * Return the address holding the debug tick for a funccall cookie.
812 */
813 int *
814func_dbg_tick(cookie)
815 void *cookie;
816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820/*
821 * Return the nesting level for a funccall cookie.
822 */
823 int
824func_level(cookie)
825 void *cookie;
826{
Bram Moolenaar33570922005-01-25 22:26:29 +0000827 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828}
829
830/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000831funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833/*
834 * Return TRUE when a function was ended by a ":return" command.
835 */
836 int
837current_func_returned()
838{
839 return current_funccal->returned;
840}
841
842
843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 * Set an internal variable to a string value. Creates the variable if it does
845 * not already exist.
846 */
847 void
848set_internal_string_var(name, value)
849 char_u *name;
850 char_u *value;
851{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000852 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
855 val = vim_strsave(value);
856 if (val != NULL)
857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000858 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000859 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000861 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000862 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 }
865}
866
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000867static lval_T *redir_lval = NULL;
868static char_u *redir_endp = NULL;
869static char_u *redir_varname = NULL;
870
871/*
872 * Start recording command output to a variable
873 * Returns OK if successfully completed the setup. FAIL otherwise.
874 */
875 int
876var_redir_start(name, append)
877 char_u *name;
878 int append; /* append to an existing variable */
879{
880 int save_emsg;
881 int err;
882 typval_T tv;
883
884 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000885 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000886 {
887 EMSG(_(e_invarg));
888 return FAIL;
889 }
890
891 redir_varname = vim_strsave(name);
892 if (redir_varname == NULL)
893 return FAIL;
894
895 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
896 if (redir_lval == NULL)
897 {
898 var_redir_stop();
899 return FAIL;
900 }
901
902 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000903 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
904 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000905 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
906 {
907 if (redir_endp != NULL && *redir_endp != NUL)
908 /* Trailing characters are present after the variable name */
909 EMSG(_(e_trailing));
910 else
911 EMSG(_(e_invarg));
912 var_redir_stop();
913 return FAIL;
914 }
915
916 /* check if we can write to the variable: set it to or append an empty
917 * string */
918 save_emsg = did_emsg;
919 did_emsg = FALSE;
920 tv.v_type = VAR_STRING;
921 tv.vval.v_string = (char_u *)"";
922 if (append)
923 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
924 else
925 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
926 err = did_emsg;
927 did_emsg += save_emsg;
928 if (err)
929 {
930 var_redir_stop();
931 return FAIL;
932 }
933 if (redir_lval->ll_newkey != NULL)
934 {
935 /* Dictionary item was created, don't do it again. */
936 vim_free(redir_lval->ll_newkey);
937 redir_lval->ll_newkey = NULL;
938 }
939
940 return OK;
941}
942
943/*
944 * Append "value[len]" to the variable set by var_redir_start().
945 */
946 void
947var_redir_str(value, len)
948 char_u *value;
949 int len;
950{
951 char_u *val;
952 typval_T tv;
953 int save_emsg;
954 int err;
955
956 if (redir_lval == NULL)
957 return;
958
959 if (len == -1)
960 /* Append the entire string */
961 val = vim_strsave(value);
962 else
963 /* Append only the specified number of characters */
964 val = vim_strnsave(value, len);
965 if (val == NULL)
966 return;
967
968 tv.v_type = VAR_STRING;
969 tv.vval.v_string = val;
970
971 save_emsg = did_emsg;
972 did_emsg = FALSE;
973 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
974 err = did_emsg;
975 did_emsg += save_emsg;
976 if (err)
977 var_redir_stop();
978
979 vim_free(tv.vval.v_string);
980}
981
982/*
983 * Stop redirecting command output to a variable.
984 */
985 void
986var_redir_stop()
987{
988 if (redir_lval != NULL)
989 {
990 clear_lval(redir_lval);
991 vim_free(redir_lval);
992 redir_lval = NULL;
993 }
994 vim_free(redir_varname);
995 redir_varname = NULL;
996}
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998# if defined(FEAT_MBYTE) || defined(PROTO)
999 int
1000eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1001 char_u *enc_from;
1002 char_u *enc_to;
1003 char_u *fname_from;
1004 char_u *fname_to;
1005{
1006 int err = FALSE;
1007
1008 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1009 set_vim_var_string(VV_CC_TO, enc_to, -1);
1010 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1011 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1012 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1013 err = TRUE;
1014 set_vim_var_string(VV_CC_FROM, NULL, -1);
1015 set_vim_var_string(VV_CC_TO, NULL, -1);
1016 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1017 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1018
1019 if (err)
1020 return FAIL;
1021 return OK;
1022}
1023# endif
1024
1025# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1026 int
1027eval_printexpr(fname, args)
1028 char_u *fname;
1029 char_u *args;
1030{
1031 int err = FALSE;
1032
1033 set_vim_var_string(VV_FNAME_IN, fname, -1);
1034 set_vim_var_string(VV_CMDARG, args, -1);
1035 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1036 err = TRUE;
1037 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1038 set_vim_var_string(VV_CMDARG, NULL, -1);
1039
1040 if (err)
1041 {
1042 mch_remove(fname);
1043 return FAIL;
1044 }
1045 return OK;
1046}
1047# endif
1048
1049# if defined(FEAT_DIFF) || defined(PROTO)
1050 void
1051eval_diff(origfile, newfile, outfile)
1052 char_u *origfile;
1053 char_u *newfile;
1054 char_u *outfile;
1055{
1056 int err = FALSE;
1057
1058 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1059 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1060 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1061 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1062 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1063 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1064 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1065}
1066
1067 void
1068eval_patch(origfile, difffile, outfile)
1069 char_u *origfile;
1070 char_u *difffile;
1071 char_u *outfile;
1072{
1073 int err;
1074
1075 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1076 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1077 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1078 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1079 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1080 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1081 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1082}
1083# endif
1084
1085/*
1086 * Top level evaluation function, returning a boolean.
1087 * Sets "error" to TRUE if there was an error.
1088 * Return TRUE or FALSE.
1089 */
1090 int
1091eval_to_bool(arg, error, nextcmd, skip)
1092 char_u *arg;
1093 int *error;
1094 char_u **nextcmd;
1095 int skip; /* only parse, don't execute */
1096{
Bram Moolenaar33570922005-01-25 22:26:29 +00001097 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 int retval = FALSE;
1099
1100 if (skip)
1101 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001102 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 else
1105 {
1106 *error = FALSE;
1107 if (!skip)
1108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001109 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 }
1113 if (skip)
1114 --emsg_skip;
1115
1116 return retval;
1117}
1118
1119/*
1120 * Top level evaluation function, returning a string. If "skip" is TRUE,
1121 * only parsing to "nextcmd" is done, without reporting errors. Return
1122 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1123 */
1124 char_u *
1125eval_to_string_skip(arg, nextcmd, skip)
1126 char_u *arg;
1127 char_u **nextcmd;
1128 int skip; /* only parse, don't execute */
1129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 char_u *retval;
1132
1133 if (skip)
1134 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 retval = NULL;
1137 else
1138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 retval = vim_strsave(get_tv_string(&tv));
1140 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 }
1142 if (skip)
1143 --emsg_skip;
1144
1145 return retval;
1146}
1147
1148/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001149 * Skip over an expression at "*pp".
1150 * Return FAIL for an error, OK otherwise.
1151 */
1152 int
1153skip_expr(pp)
1154 char_u **pp;
1155{
Bram Moolenaar33570922005-01-25 22:26:29 +00001156 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001157
1158 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001160}
1161
1162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 * Top level evaluation function, returning a string.
1164 * Return pointer to allocated memory, or NULL for failure.
1165 */
1166 char_u *
1167eval_to_string(arg, nextcmd)
1168 char_u *arg;
1169 char_u **nextcmd;
1170{
Bram Moolenaar33570922005-01-25 22:26:29 +00001171 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 char_u *retval;
1173
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 retval = NULL;
1176 else
1177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 retval = vim_strsave(get_tv_string(&tv));
1179 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
1181
1182 return retval;
1183}
1184
1185/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001186 * Call eval_to_string() without using current local variables and using
1187 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 */
1189 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001190eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 char_u *arg;
1192 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001193 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
1195 char_u *retval;
1196 void *save_funccalp;
1197
1198 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001199 if (use_sandbox)
1200 ++sandbox;
1201 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 retval = eval_to_string(arg, nextcmd);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001203 if (use_sandbox)
1204 --sandbox;
1205 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 restore_funccal(save_funccalp);
1207 return retval;
1208}
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210/*
1211 * Top level evaluation function, returning a number.
1212 * Evaluates "expr" silently.
1213 * Returns -1 for an error.
1214 */
1215 int
1216eval_to_number(expr)
1217 char_u *expr;
1218{
Bram Moolenaar33570922005-01-25 22:26:29 +00001219 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001221 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
1223 ++emsg_off;
1224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 retval = -1;
1227 else
1228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001229 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
1232 --emsg_off;
1233
1234 return retval;
1235}
1236
Bram Moolenaara40058a2005-07-11 22:42:07 +00001237/*
1238 * Prepare v: variable "idx" to be used.
1239 * Save the current typeval in "save_tv".
1240 * When not used yet add the variable to the v: hashtable.
1241 */
1242 static void
1243prepare_vimvar(idx, save_tv)
1244 int idx;
1245 typval_T *save_tv;
1246{
1247 *save_tv = vimvars[idx].vv_tv;
1248 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1249 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1250}
1251
1252/*
1253 * Restore v: variable "idx" to typeval "save_tv".
1254 * When no longer defined, remove the variable from the v: hashtable.
1255 */
1256 static void
1257restore_vimvar(idx, save_tv)
1258 int idx;
1259 typval_T *save_tv;
1260{
1261 hashitem_T *hi;
1262
1263 clear_tv(&vimvars[idx].vv_tv);
1264 vimvars[idx].vv_tv = *save_tv;
1265 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1266 {
1267 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1268 if (HASHITEM_EMPTY(hi))
1269 EMSG2(_(e_intern2), "restore_vimvar()");
1270 else
1271 hash_remove(&vimvarht, hi);
1272 }
1273}
1274
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001275#if defined(FEAT_SYN_HL) || defined(PROTO)
1276/*
1277 * Evaluate an expression to a list with suggestions.
1278 * For the "expr:" part of 'spellsuggest'.
1279 */
1280 list_T *
1281eval_spell_expr(badword, expr)
1282 char_u *badword;
1283 char_u *expr;
1284{
1285 typval_T save_val;
1286 typval_T rettv;
1287 list_T *list = NULL;
1288 char_u *p = skipwhite(expr);
1289
1290 /* Set "v:val" to the bad word. */
1291 prepare_vimvar(VV_VAL, &save_val);
1292 vimvars[VV_VAL].vv_type = VAR_STRING;
1293 vimvars[VV_VAL].vv_str = badword;
1294 if (p_verbose == 0)
1295 ++emsg_off;
1296
1297 if (eval1(&p, &rettv, TRUE) == OK)
1298 {
1299 if (rettv.v_type != VAR_LIST)
1300 clear_tv(&rettv);
1301 else
1302 list = rettv.vval.v_list;
1303 }
1304
1305 if (p_verbose == 0)
1306 --emsg_off;
1307 vimvars[VV_VAL].vv_str = NULL;
1308 restore_vimvar(VV_VAL, &save_val);
1309
1310 return list;
1311}
1312
1313/*
1314 * "list" is supposed to contain two items: a word and a number. Return the
1315 * word in "pp" and the number as the return value.
1316 * Return -1 if anything isn't right.
1317 * Used to get the good word and score from the eval_spell_expr() result.
1318 */
1319 int
1320get_spellword(list, pp)
1321 list_T *list;
1322 char_u **pp;
1323{
1324 listitem_T *li;
1325
1326 li = list->lv_first;
1327 if (li == NULL)
1328 return -1;
1329 *pp = get_tv_string(&li->li_tv);
1330
1331 li = li->li_next;
1332 if (li == NULL)
1333 return -1;
1334 return get_tv_number(&li->li_tv);
1335}
1336#endif
1337
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001338/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001339 * Top level evaluation function.
1340 * Returns an allocated typval_T with the result.
1341 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001342 */
1343 typval_T *
1344eval_expr(arg, nextcmd)
1345 char_u *arg;
1346 char_u **nextcmd;
1347{
1348 typval_T *tv;
1349
1350 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001351 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001352 {
1353 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001354 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001355 }
1356
1357 return tv;
1358}
1359
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1362/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001363 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001365 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001367 static int
1368call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *func;
1370 int argc;
1371 char_u **argv;
1372 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 long n;
1377 int len;
1378 int i;
1379 int doesrange;
1380 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001381 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaar33570922005-01-25 22:26:29 +00001383 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001385 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 for (i = 0; i < argc; i++)
1388 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001389 /* Pass a NULL or empty argument as an empty string */
1390 if (argv[i] == NULL || *argv[i] == NUL)
1391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001392 argvars[i].v_type = VAR_STRING;
1393 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001394 continue;
1395 }
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 /* Recognize a number argument, the others must be strings. */
1398 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1399 if (len != 0 && len == (int)STRLEN(argv[i]))
1400 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001401 argvars[i].v_type = VAR_NUMBER;
1402 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 else
1405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001406 argvars[i].v_type = VAR_STRING;
1407 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 }
1410
1411 if (safe)
1412 {
1413 save_funccalp = save_funccal();
1414 ++sandbox;
1415 }
1416
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001417 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1418 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (safe)
1422 {
1423 --sandbox;
1424 restore_funccal(save_funccalp);
1425 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 vim_free(argvars);
1427
1428 if (ret == FAIL)
1429 clear_tv(rettv);
1430
1431 return ret;
1432}
1433
1434/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001435 * Call vimL function "func" and return the result as a string.
1436 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001437 * Uses argv[argc] for the function arguments.
1438 */
1439 void *
1440call_func_retstr(func, argc, argv, safe)
1441 char_u *func;
1442 int argc;
1443 char_u **argv;
1444 int safe; /* use the sandbox */
1445{
1446 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001447 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001448
1449 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1450 return NULL;
1451
1452 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 return retval;
1455}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001456
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001457#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001458/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001459 * Call vimL function "func" and return the result as a number.
1460 * Returns -1 when calling the function fails.
1461 * Uses argv[argc] for the function arguments.
1462 */
1463 long
1464call_func_retnr(func, argc, argv, safe)
1465 char_u *func;
1466 int argc;
1467 char_u **argv;
1468 int safe; /* use the sandbox */
1469{
1470 typval_T rettv;
1471 long retval;
1472
1473 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1474 return -1;
1475
1476 retval = get_tv_number_chk(&rettv, NULL);
1477 clear_tv(&rettv);
1478 return retval;
1479}
1480#endif
1481
1482/*
1483 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001484 * Uses argv[argc] for the function arguments.
1485 */
1486 void *
1487call_func_retlist(func, argc, argv, safe)
1488 char_u *func;
1489 int argc;
1490 char_u **argv;
1491 int safe; /* use the sandbox */
1492{
1493 typval_T rettv;
1494
1495 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1496 return NULL;
1497
1498 if (rettv.v_type != VAR_LIST)
1499 {
1500 clear_tv(&rettv);
1501 return NULL;
1502 }
1503
1504 return rettv.vval.v_list;
1505}
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#endif
1508
1509/*
1510 * Save the current function call pointer, and set it to NULL.
1511 * Used when executing autocommands and for ":source".
1512 */
1513 void *
1514save_funccal()
1515{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001516 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 current_funccal = NULL;
1519 return (void *)fc;
1520}
1521
1522 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001523restore_funccal(vfc)
1524 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001526 funccall_T *fc = (funccall_T *)vfc;
1527
1528 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529}
1530
Bram Moolenaar05159a02005-02-26 23:04:13 +00001531#if defined(FEAT_PROFILE) || defined(PROTO)
1532/*
1533 * Prepare profiling for entering a child or something else that is not
1534 * counted for the script/function itself.
1535 * Should always be called in pair with prof_child_exit().
1536 */
1537 void
1538prof_child_enter(tm)
1539 proftime_T *tm; /* place to store waittime */
1540{
1541 funccall_T *fc = current_funccal;
1542
1543 if (fc != NULL && fc->func->uf_profiling)
1544 profile_start(&fc->prof_child);
1545 script_prof_save(tm);
1546}
1547
1548/*
1549 * Take care of time spent in a child.
1550 * Should always be called after prof_child_enter().
1551 */
1552 void
1553prof_child_exit(tm)
1554 proftime_T *tm; /* where waittime was stored */
1555{
1556 funccall_T *fc = current_funccal;
1557
1558 if (fc != NULL && fc->func->uf_profiling)
1559 {
1560 profile_end(&fc->prof_child);
1561 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1562 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1563 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1564 }
1565 script_prof_restore(tm);
1566}
1567#endif
1568
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#ifdef FEAT_FOLDING
1571/*
1572 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1573 * it in "*cp". Doesn't give error messages.
1574 */
1575 int
1576eval_foldexpr(arg, cp)
1577 char_u *arg;
1578 int *cp;
1579{
Bram Moolenaar33570922005-01-25 22:26:29 +00001580 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 int retval;
1582 char_u *s;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001583 int use_sandbox = was_set_insecurely((char_u *)"foldexpr");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001586 if (use_sandbox)
1587 ++sandbox;
1588 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 retval = 0;
1592 else
1593 {
1594 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595 if (tv.v_type == VAR_NUMBER)
1596 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001597 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 retval = 0;
1599 else
1600 {
1601 /* If the result is a string, check if there is a non-digit before
1602 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (!VIM_ISDIGIT(*s) && *s != '-')
1605 *cp = *s++;
1606 retval = atol((char *)s);
1607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001611 if (use_sandbox)
1612 --sandbox;
1613 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 return retval;
1616}
1617#endif
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 * ":let" list all variable values
1621 * ":let var1 var2" list variable values
1622 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001623 * ":let var += expr" assignment command.
1624 * ":let var -= expr" assignment command.
1625 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 */
1628 void
1629ex_let(eap)
1630 exarg_T *eap;
1631{
1632 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001634 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 int var_count = 0;
1637 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640 expr = skip_var_list(arg, &var_count, &semicolon);
1641 if (expr == NULL)
1642 return;
1643 expr = vim_strchr(expr, '=');
1644 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001646 /*
1647 * ":let" without "=": list variables
1648 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649 if (*arg == '[')
1650 EMSG(_(e_invarg));
1651 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 /* ":let var1 var2" */
1653 arg = list_arg_vars(eap, arg);
1654 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001657 list_glob_vars();
1658 list_buf_vars();
1659 list_win_vars();
1660 list_vim_vars();
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 eap->nextcmd = check_nextcmd(arg);
1663 }
1664 else
1665 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001666 op[0] = '=';
1667 op[1] = NUL;
1668 if (expr > arg)
1669 {
1670 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1671 op[0] = expr[-1]; /* +=, -= or .= */
1672 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (eap->skip)
1676 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (eap->skip)
1679 {
1680 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 --emsg_skip;
1683 }
1684 else if (i != FAIL)
1685 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001687 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001688 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 }
1691}
1692
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693/*
1694 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1695 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001696 * When "nextchars" is not NULL it points to a string with characters that
1697 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1698 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699 * Returns OK or FAIL;
1700 */
1701 static int
1702ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1703 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001704 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 int copy; /* copy values from "tv", don't move */
1706 int semicolon; /* from skip_var_list() */
1707 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709{
1710 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001711 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001713 listitem_T *item;
1714 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715
1716 if (*arg != '[')
1717 {
1718 /*
1719 * ":let var = expr" or ":for var in list"
1720 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 return FAIL;
1723 return OK;
1724 }
1725
1726 /*
1727 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1728 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001729 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 {
1731 EMSG(_(e_listreq));
1732 return FAIL;
1733 }
1734
1735 i = list_len(l);
1736 if (semicolon == 0 && var_count < i)
1737 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001738 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 return FAIL;
1740 }
1741 if (var_count - semicolon > i)
1742 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001743 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 return FAIL;
1745 }
1746
1747 item = l->lv_first;
1748 while (*arg != ']')
1749 {
1750 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 item = item->li_next;
1753 if (arg == NULL)
1754 return FAIL;
1755
1756 arg = skipwhite(arg);
1757 if (*arg == ';')
1758 {
1759 /* Put the rest of the list (may be empty) in the var after ';'.
1760 * Create a new list for this. */
1761 l = list_alloc();
1762 if (l == NULL)
1763 return FAIL;
1764 while (item != NULL)
1765 {
1766 list_append_tv(l, &item->li_tv);
1767 item = item->li_next;
1768 }
1769
1770 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001771 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 ltv.vval.v_list = l;
1773 l->lv_refcount = 1;
1774
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1776 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 clear_tv(&ltv);
1778 if (arg == NULL)
1779 return FAIL;
1780 break;
1781 }
1782 else if (*arg != ',' && *arg != ']')
1783 {
1784 EMSG2(_(e_intern2), "ex_let_vars()");
1785 return FAIL;
1786 }
1787 }
1788
1789 return OK;
1790}
1791
1792/*
1793 * Skip over assignable variable "var" or list of variables "[var, var]".
1794 * Used for ":let varvar = expr" and ":for varvar in expr".
1795 * For "[var, var]" increment "*var_count" for each variable.
1796 * for "[var, var; var]" set "semicolon".
1797 * Return NULL for an error.
1798 */
1799 static char_u *
1800skip_var_list(arg, var_count, semicolon)
1801 char_u *arg;
1802 int *var_count;
1803 int *semicolon;
1804{
1805 char_u *p, *s;
1806
1807 if (*arg == '[')
1808 {
1809 /* "[var, var]": find the matching ']'. */
1810 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001811 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 {
1813 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1814 s = skip_var_one(p);
1815 if (s == p)
1816 {
1817 EMSG2(_(e_invarg2), p);
1818 return NULL;
1819 }
1820 ++*var_count;
1821
1822 p = skipwhite(s);
1823 if (*p == ']')
1824 break;
1825 else if (*p == ';')
1826 {
1827 if (*semicolon == 1)
1828 {
1829 EMSG(_("Double ; in list of variables"));
1830 return NULL;
1831 }
1832 *semicolon = 1;
1833 }
1834 else if (*p != ',')
1835 {
1836 EMSG2(_(e_invarg2), p);
1837 return NULL;
1838 }
1839 }
1840 return p + 1;
1841 }
1842 else
1843 return skip_var_one(arg);
1844}
1845
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001847 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1848 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001849 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 static char_u *
1851skip_var_one(arg)
1852 char_u *arg;
1853{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001854 if (*arg == '@' && arg[1] != NUL)
1855 return arg + 2;
1856 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1857 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858}
1859
Bram Moolenaara7043832005-01-21 11:56:39 +00001860/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 * List variables for hashtab "ht" with prefix "prefix".
1862 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001865list_hashtable_vars(ht, prefix, empty)
1866 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001869{
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 hashitem_T *hi;
1871 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001872 int todo;
1873
1874 todo = ht->ht_used;
1875 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1876 {
1877 if (!HASHITEM_EMPTY(hi))
1878 {
1879 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001880 di = HI2DI(hi);
1881 if (empty || di->di_tv.v_type != VAR_STRING
1882 || di->di_tv.vval.v_string != NULL)
1883 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 }
1885 }
1886}
1887
1888/*
1889 * List global variables.
1890 */
1891 static void
1892list_glob_vars()
1893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895}
1896
1897/*
1898 * List buffer variables.
1899 */
1900 static void
1901list_buf_vars()
1902{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903 char_u numbuf[NUMBUFLEN];
1904
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001906
1907 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1908 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001909}
1910
1911/*
1912 * List window variables.
1913 */
1914 static void
1915list_win_vars()
1916{
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001918}
1919
1920/*
1921 * List Vim variables.
1922 */
1923 static void
1924list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925{
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927}
1928
1929/*
1930 * List variables in "arg".
1931 */
1932 static char_u *
1933list_arg_vars(eap, arg)
1934 exarg_T *eap;
1935 char_u *arg;
1936{
1937 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001938 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 char_u *name_start;
1941 char_u *arg_subsc;
1942 char_u *tofree;
1943 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944
1945 while (!ends_excmd(*arg) && !got_int)
1946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001947 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001949 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001950 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1951 {
1952 emsg_severe = TRUE;
1953 EMSG(_(e_trailing));
1954 break;
1955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001959 /* get_name_len() takes care of expanding curly braces */
1960 name_start = name = arg;
1961 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1962 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001964 /* This is mainly to keep test 49 working: when expanding
1965 * curly braces fails overrule the exception error message. */
1966 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968 emsg_severe = TRUE;
1969 EMSG2(_(e_invarg2), arg);
1970 break;
1971 }
1972 error = TRUE;
1973 }
1974 else
1975 {
1976 if (tofree != NULL)
1977 name = tofree;
1978 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001979 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980 else
1981 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 /* handle d.key, l[idx], f(expr) */
1983 arg_subsc = arg;
1984 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001985 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001987 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001988 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001989 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001990 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001991 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001992 case 'g': list_glob_vars(); break;
1993 case 'b': list_buf_vars(); break;
1994 case 'w': list_win_vars(); break;
1995 case 'v': list_vim_vars(); break;
1996 default:
1997 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001998 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001999 }
2000 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001 {
2002 char_u numbuf[NUMBUFLEN];
2003 char_u *tf;
2004 int c;
2005 char_u *s;
2006
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002007 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002008 c = *arg;
2009 *arg = NUL;
2010 list_one_var_a((char_u *)"",
2011 arg == arg_subsc ? name : name_start,
2012 tv.v_type, s == NULL ? (char_u *)"" : s);
2013 *arg = c;
2014 vim_free(tf);
2015 }
2016 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 }
2019 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002020
2021 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002023
2024 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 }
2026
2027 return arg;
2028}
2029
2030/*
2031 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2032 * Returns a pointer to the char just after the var name.
2033 * Returns NULL if there is an error.
2034 */
2035 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002036ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002038 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002041 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042{
2043 int c1;
2044 char_u *name;
2045 char_u *p;
2046 char_u *arg_end = NULL;
2047 int len;
2048 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050
2051 /*
2052 * ":let $VAR = expr": Set environment variable.
2053 */
2054 if (*arg == '$')
2055 {
2056 /* Find the end of the name. */
2057 ++arg;
2058 name = arg;
2059 len = get_env_len(&arg);
2060 if (len == 0)
2061 EMSG2(_(e_invarg2), name - 1);
2062 else
2063 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 if (op != NULL && (*op == '+' || *op == '-'))
2065 EMSG2(_(e_letwrong), op);
2066 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002067 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 EMSG(_(e_letunexp));
2069 else
2070 {
2071 c1 = name[len];
2072 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002073 p = get_tv_string_chk(tv);
2074 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 {
2076 int mustfree = FALSE;
2077 char_u *s = vim_getenv(name, &mustfree);
2078
2079 if (s != NULL)
2080 {
2081 p = tofree = concat_str(s, p);
2082 if (mustfree)
2083 vim_free(s);
2084 }
2085 }
2086 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002087 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002088 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002089 if (STRICMP(name, "HOME") == 0)
2090 init_homedir();
2091 else if (didset_vim && STRICMP(name, "VIM") == 0)
2092 didset_vim = FALSE;
2093 else if (didset_vimruntime
2094 && STRICMP(name, "VIMRUNTIME") == 0)
2095 didset_vimruntime = FALSE;
2096 arg_end = arg;
2097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002099 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 }
2101 }
2102 }
2103
2104 /*
2105 * ":let &option = expr": Set option value.
2106 * ":let &l:option = expr": Set local option value.
2107 * ":let &g:option = expr": Set global option value.
2108 */
2109 else if (*arg == '&')
2110 {
2111 /* Find the end of the name. */
2112 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113 if (p == NULL || (endchars != NULL
2114 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 EMSG(_(e_letunexp));
2116 else
2117 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002118 long n;
2119 int opt_type;
2120 long numval;
2121 char_u *stringval = NULL;
2122 char_u *s;
2123
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 c1 = *p;
2125 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126
2127 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002128 s = get_tv_string_chk(tv); /* != NULL if number or string */
2129 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 {
2131 opt_type = get_option_value(arg, &numval,
2132 &stringval, opt_flags);
2133 if ((opt_type == 1 && *op == '.')
2134 || (opt_type == 0 && *op != '.'))
2135 EMSG2(_(e_letwrong), op);
2136 else
2137 {
2138 if (opt_type == 1) /* number */
2139 {
2140 if (*op == '+')
2141 n = numval + n;
2142 else
2143 n = numval - n;
2144 }
2145 else if (opt_type == 0 && stringval != NULL) /* string */
2146 {
2147 s = concat_str(stringval, s);
2148 vim_free(stringval);
2149 stringval = s;
2150 }
2151 }
2152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002153 if (s != NULL)
2154 {
2155 set_option_value(arg, n, s, opt_flags);
2156 arg_end = p;
2157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 }
2161 }
2162
2163 /*
2164 * ":let @r = expr": Set register contents.
2165 */
2166 else if (*arg == '@')
2167 {
2168 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002169 if (op != NULL && (*op == '+' || *op == '-'))
2170 EMSG2(_(e_letwrong), op);
2171 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 EMSG(_(e_letunexp));
2174 else
2175 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002176 char_u *tofree = NULL;
2177 char_u *s;
2178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002179 p = get_tv_string_chk(tv);
2180 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002181 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002182 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 if (s != NULL)
2184 {
2185 p = tofree = concat_str(s, p);
2186 vim_free(s);
2187 }
2188 }
2189 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002190 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002191 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002192 arg_end = arg + 1;
2193 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002194 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 }
2196 }
2197
2198 /*
2199 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002202 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002204 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002206 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002207 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2210 EMSG(_(e_letunexp));
2211 else
2212 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002214 arg_end = p;
2215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002217 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 }
2219
2220 else
2221 EMSG2(_(e_invarg2), arg);
2222
2223 return arg_end;
2224}
2225
2226/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002227 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2228 */
2229 static int
2230check_changedtick(arg)
2231 char_u *arg;
2232{
2233 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2234 {
2235 EMSG2(_(e_readonlyvar), arg);
2236 return TRUE;
2237 }
2238 return FALSE;
2239}
2240
2241/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 * Get an lval: variable, Dict item or List item that can be assigned a value
2243 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2244 * "name.key", "name.key[expr]" etc.
2245 * Indexing only works if "name" is an existing List or Dictionary.
2246 * "name" points to the start of the name.
2247 * If "rettv" is not NULL it points to the value to be assigned.
2248 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2249 * wrong; must end in space or cmd separator.
2250 *
2251 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002252 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 */
2255 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002256get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002258 typval_T *rettv;
2259 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 int unlet;
2261 int skip;
2262 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002263 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 char_u *expr_start, *expr_end;
2267 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002268 dictitem_T *v;
2269 typval_T var1;
2270 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002272 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002273 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002274 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002275 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002277 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002278 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279
2280 if (skip)
2281 {
2282 /* When skipping just find the end of the name. */
2283 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002284 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 }
2286
2287 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002288 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 if (expr_start != NULL)
2290 {
2291 /* Don't expand the name when we already know there is an error. */
2292 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2293 && *p != '[' && *p != '.')
2294 {
2295 EMSG(_(e_trailing));
2296 return NULL;
2297 }
2298
2299 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2300 if (lp->ll_exp_name == NULL)
2301 {
2302 /* Report an invalid expression in braces, unless the
2303 * expression evaluation has been cancelled due to an
2304 * aborting error, an interrupt, or an exception. */
2305 if (!aborting() && !quiet)
2306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002307 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 EMSG2(_(e_invarg2), name);
2309 return NULL;
2310 }
2311 }
2312 lp->ll_name = lp->ll_exp_name;
2313 }
2314 else
2315 lp->ll_name = name;
2316
2317 /* Without [idx] or .key we are done. */
2318 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2319 return p;
2320
2321 cc = *p;
2322 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002323 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 if (v == NULL && !quiet)
2325 EMSG2(_(e_undefvar), lp->ll_name);
2326 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 if (v == NULL)
2328 return NULL;
2329
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 /*
2331 * Loop until no more [idx] or .key is following.
2332 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002333 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2337 && !(lp->ll_tv->v_type == VAR_DICT
2338 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 if (!quiet)
2341 EMSG(_("E689: Can only index a List or Dictionary"));
2342 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 if (!quiet)
2347 EMSG(_("E708: [:] must come last"));
2348 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002350
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 len = -1;
2352 if (*p == '.')
2353 {
2354 key = p + 1;
2355 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2356 ;
2357 if (len == 0)
2358 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 if (!quiet)
2360 EMSG(_(e_emptykey));
2361 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 }
2363 p = key + len;
2364 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002365 else
2366 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002367 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 if (*p == ':')
2370 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 else
2372 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 empty1 = FALSE;
2374 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 if (get_tv_string_chk(&var1) == NULL)
2377 {
2378 /* not a number or string */
2379 clear_tv(&var1);
2380 return NULL;
2381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002382 }
2383
2384 /* Optionally get the second index [ :expr]. */
2385 if (*p == ':')
2386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002391 if (!empty1)
2392 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002394 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 if (rettv != NULL && (rettv->v_type != VAR_LIST
2396 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 if (!quiet)
2399 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 if (!empty1)
2401 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002403 }
2404 p = skipwhite(p + 1);
2405 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 else
2408 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 if (!empty1)
2413 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 if (get_tv_string_chk(&var2) == NULL)
2417 {
2418 /* not a number or string */
2419 if (!empty1)
2420 clear_tv(&var1);
2421 clear_tv(&var2);
2422 return NULL;
2423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002426 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002429
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002431 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 if (!quiet)
2433 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 if (!empty1)
2435 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 }
2440
2441 /* Skip to past ']'. */
2442 ++p;
2443 }
2444
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 {
2447 if (len == -1)
2448 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 if (*key == NUL)
2452 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (!quiet)
2454 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 }
2458 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 lp->ll_list = NULL;
2460 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002461 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002464 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 if (len == -1)
2470 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 }
2473 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 if (len == -1)
2478 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 p = NULL;
2481 break;
2482 }
2483 if (len == -1)
2484 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 }
2487 else
2488 {
2489 /*
2490 * Get the number and item for the only or first index of the List.
2491 */
2492 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 else
2495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 clear_tv(&var1);
2498 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 lp->ll_list = lp->ll_tv->vval.v_list;
2501 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2502 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (!quiet)
2505 EMSGN(_(e_listidx), lp->ll_n1);
2506 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 }
2510
2511 /*
2512 * May need to find the item or absolute index for the second
2513 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * When no index given: "lp->ll_empty2" is TRUE.
2515 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002519 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 if (ni == NULL)
2525 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (!quiet)
2527 EMSGN(_(e_listidx), lp->ll_n2);
2528 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 }
2532
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2534 if (lp->ll_n1 < 0)
2535 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2536 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 if (!quiet)
2539 EMSGN(_(e_listidx), lp->ll_n2);
2540 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002541 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002542 }
2543
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
2547
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 return p;
2549}
2550
2551/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 */
2554 static void
2555clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557{
2558 vim_free(lp->ll_exp_name);
2559 vim_free(lp->ll_newkey);
2560}
2561
2562/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002563 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 */
2567 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002569 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002573 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574{
2575 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 listitem_T *ri;
2577 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578
2579 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 cc = *endp;
2584 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002585 if (op != NULL && *op != '=')
2586 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002589 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002590 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2591 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 {
2593 if (tv_op(&tv, rettv, op) == OK)
2594 set_var(lp->ll_name, &tv, FALSE);
2595 clear_tv(&tv);
2596 }
2597 }
2598 else
2599 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002603 else if (tv_check_lock(lp->ll_newkey == NULL
2604 ? lp->ll_tv->v_lock
2605 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2606 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 else if (lp->ll_range)
2608 {
2609 /*
2610 * Assign the List values to the list items.
2611 */
2612 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002614 if (op != NULL && *op != '=')
2615 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2616 else
2617 {
2618 clear_tv(&lp->ll_li->li_tv);
2619 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2620 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 ri = ri->li_next;
2622 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2623 break;
2624 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002627 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 ri = NULL;
2630 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 lp->ll_li = lp->ll_li->li_next;
2634 ++lp->ll_n1;
2635 }
2636 if (ri != NULL)
2637 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002638 else if (lp->ll_empty2
2639 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 : lp->ll_n1 != lp->ll_n2)
2641 EMSG(_("E711: List value has not enough items"));
2642 }
2643 else
2644 {
2645 /*
2646 * Assign to a List or Dictionary item.
2647 */
2648 if (lp->ll_newkey != NULL)
2649 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 if (op != NULL && *op != '=')
2651 {
2652 EMSG2(_(e_letwrong), op);
2653 return;
2654 }
2655
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002657 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (di == NULL)
2659 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002660 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2661 {
2662 vim_free(di);
2663 return;
2664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667 else if (op != NULL && *op != '=')
2668 {
2669 tv_op(lp->ll_tv, rettv, op);
2670 return;
2671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 /*
2676 * Assign the value to the variable or list item.
2677 */
2678 if (copy)
2679 copy_tv(rettv, lp->ll_tv);
2680 else
2681 {
2682 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002683 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 }
2686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687}
2688
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002689/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2691 * Returns OK or FAIL.
2692 */
2693 static int
2694tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002695 typval_T *tv1;
2696 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 char_u *op;
2698{
2699 long n;
2700 char_u numbuf[NUMBUFLEN];
2701 char_u *s;
2702
2703 /* Can't do anything with a Funcref or a Dict on the right. */
2704 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2705 {
2706 switch (tv1->v_type)
2707 {
2708 case VAR_DICT:
2709 case VAR_FUNC:
2710 break;
2711
2712 case VAR_LIST:
2713 if (*op != '+' || tv2->v_type != VAR_LIST)
2714 break;
2715 /* List += List */
2716 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2717 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2718 return OK;
2719
2720 case VAR_NUMBER:
2721 case VAR_STRING:
2722 if (tv2->v_type == VAR_LIST)
2723 break;
2724 if (*op == '+' || *op == '-')
2725 {
2726 /* nr += nr or nr -= nr*/
2727 n = get_tv_number(tv1);
2728 if (*op == '+')
2729 n += get_tv_number(tv2);
2730 else
2731 n -= get_tv_number(tv2);
2732 clear_tv(tv1);
2733 tv1->v_type = VAR_NUMBER;
2734 tv1->vval.v_number = n;
2735 }
2736 else
2737 {
2738 /* str .= str */
2739 s = get_tv_string(tv1);
2740 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2741 clear_tv(tv1);
2742 tv1->v_type = VAR_STRING;
2743 tv1->vval.v_string = s;
2744 }
2745 return OK;
2746 }
2747 }
2748
2749 EMSG2(_(e_letwrong), op);
2750 return FAIL;
2751}
2752
2753/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002754 * Add a watcher to a list.
2755 */
2756 static void
2757list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002758 list_T *l;
2759 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002760{
2761 lw->lw_next = l->lv_watch;
2762 l->lv_watch = lw;
2763}
2764
2765/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002766 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002767 * No warning when it isn't found...
2768 */
2769 static void
2770list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002771 list_T *l;
2772 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002773{
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775
2776 lwp = &l->lv_watch;
2777 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2778 {
2779 if (lw == lwrem)
2780 {
2781 *lwp = lw->lw_next;
2782 break;
2783 }
2784 lwp = &lw->lw_next;
2785 }
2786}
2787
2788/*
2789 * Just before removing an item from a list: advance watchers to the next
2790 * item.
2791 */
2792 static void
2793list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002794 list_T *l;
2795 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002796{
Bram Moolenaar33570922005-01-25 22:26:29 +00002797 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002798
2799 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2800 if (lw->lw_item == item)
2801 lw->lw_item = item->li_next;
2802}
2803
2804/*
2805 * Evaluate the expression used in a ":for var in expr" command.
2806 * "arg" points to "var".
2807 * Set "*errp" to TRUE for an error, FALSE otherwise;
2808 * Return a pointer that holds the info. Null when there is an error.
2809 */
2810 void *
2811eval_for_line(arg, errp, nextcmdp, skip)
2812 char_u *arg;
2813 int *errp;
2814 char_u **nextcmdp;
2815 int skip;
2816{
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002818 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 typval_T tv;
2820 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002821
2822 *errp = TRUE; /* default: there is an error */
2823
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002825 if (fi == NULL)
2826 return NULL;
2827
2828 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2829 if (expr == NULL)
2830 return fi;
2831
2832 expr = skipwhite(expr);
2833 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2834 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002835 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836 return fi;
2837 }
2838
2839 if (skip)
2840 ++emsg_skip;
2841 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2842 {
2843 *errp = FALSE;
2844 if (!skip)
2845 {
2846 l = tv.vval.v_list;
2847 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002848 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002849 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002850 clear_tv(&tv);
2851 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 else
2853 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002854 /* No need to increment the refcount, it's already set for the
2855 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856 fi->fi_list = l;
2857 list_add_watch(l, &fi->fi_lw);
2858 fi->fi_lw.lw_item = l->lv_first;
2859 }
2860 }
2861 }
2862 if (skip)
2863 --emsg_skip;
2864
2865 return fi;
2866}
2867
2868/*
2869 * Use the first item in a ":for" list. Advance to the next.
2870 * Assign the values to the variable (list). "arg" points to the first one.
2871 * Return TRUE when a valid item was found, FALSE when at end of list or
2872 * something wrong.
2873 */
2874 int
2875next_for_item(fi_void, arg)
2876 void *fi_void;
2877 char_u *arg;
2878{
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002880 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882
2883 item = fi->fi_lw.lw_item;
2884 if (item == NULL)
2885 result = FALSE;
2886 else
2887 {
2888 fi->fi_lw.lw_item = item->li_next;
2889 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2890 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2891 }
2892 return result;
2893}
2894
2895/*
2896 * Free the structure used to store info used by ":for".
2897 */
2898 void
2899free_for_info(fi_void)
2900 void *fi_void;
2901{
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002904 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002905 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002907 list_unref(fi->fi_list);
2908 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909 vim_free(fi);
2910}
2911
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2913
2914 void
2915set_context_for_expression(xp, arg, cmdidx)
2916 expand_T *xp;
2917 char_u *arg;
2918 cmdidx_T cmdidx;
2919{
2920 int got_eq = FALSE;
2921 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 if (cmdidx == CMD_let)
2925 {
2926 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002927 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 {
2929 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002930 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 {
2932 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002933 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934 if (vim_iswhite(*p))
2935 break;
2936 }
2937 return;
2938 }
2939 }
2940 else
2941 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2942 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 while ((xp->xp_pattern = vim_strpbrk(arg,
2944 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2945 {
2946 c = *xp->xp_pattern;
2947 if (c == '&')
2948 {
2949 c = xp->xp_pattern[1];
2950 if (c == '&')
2951 {
2952 ++xp->xp_pattern;
2953 xp->xp_context = cmdidx != CMD_let || got_eq
2954 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2955 }
2956 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002959 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2960 xp->xp_pattern += 2;
2961
2962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964 else if (c == '$')
2965 {
2966 /* environment variable */
2967 xp->xp_context = EXPAND_ENV_VARS;
2968 }
2969 else if (c == '=')
2970 {
2971 got_eq = TRUE;
2972 xp->xp_context = EXPAND_EXPRESSION;
2973 }
2974 else if (c == '<'
2975 && xp->xp_context == EXPAND_FUNCTIONS
2976 && vim_strchr(xp->xp_pattern, '(') == NULL)
2977 {
2978 /* Function name can start with "<SNR>" */
2979 break;
2980 }
2981 else if (cmdidx != CMD_let || got_eq)
2982 {
2983 if (c == '"') /* string */
2984 {
2985 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2986 if (c == '\\' && xp->xp_pattern[1] != NUL)
2987 ++xp->xp_pattern;
2988 xp->xp_context = EXPAND_NOTHING;
2989 }
2990 else if (c == '\'') /* literal string */
2991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002992 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2994 /* skip */ ;
2995 xp->xp_context = EXPAND_NOTHING;
2996 }
2997 else if (c == '|')
2998 {
2999 if (xp->xp_pattern[1] == '|')
3000 {
3001 ++xp->xp_pattern;
3002 xp->xp_context = EXPAND_EXPRESSION;
3003 }
3004 else
3005 xp->xp_context = EXPAND_COMMANDS;
3006 }
3007 else
3008 xp->xp_context = EXPAND_EXPRESSION;
3009 }
3010 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 /* Doesn't look like something valid, expand as an expression
3012 * anyway. */
3013 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 arg = xp->xp_pattern;
3015 if (*arg != NUL)
3016 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3017 /* skip */ ;
3018 }
3019 xp->xp_pattern = arg;
3020}
3021
3022#endif /* FEAT_CMDL_COMPL */
3023
3024/*
3025 * ":1,25call func(arg1, arg2)" function call.
3026 */
3027 void
3028ex_call(eap)
3029 exarg_T *eap;
3030{
3031 char_u *arg = eap->arg;
3032 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003036 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 linenr_T lnum;
3038 int doesrange;
3039 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003040 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003042 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3043 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 if (tofree == NULL)
3045 return;
3046
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003047 /* Increase refcount on dictionary, it could get deleted when evaluating
3048 * the arguments. */
3049 if (fudi.fd_dict != NULL)
3050 ++fudi.fd_dict->dv_refcount;
3051
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3053 len = STRLEN(tofree);
3054 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
Bram Moolenaar532c7802005-01-27 14:44:31 +00003056 /* Skip white space to allow ":call func ()". Not good, but required for
3057 * backward compatibility. */
3058 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
3061 if (*startarg != '(')
3062 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003063 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 goto end;
3065 }
3066
3067 /*
3068 * When skipping, evaluate the function once, to find the end of the
3069 * arguments.
3070 * When the function takes a range, this is discovered after the first
3071 * call, and the loop is broken.
3072 */
3073 if (eap->skip)
3074 {
3075 ++emsg_skip;
3076 lnum = eap->line2; /* do it once, also with an invalid range */
3077 }
3078 else
3079 lnum = eap->line1;
3080 for ( ; lnum <= eap->line2; ++lnum)
3081 {
3082 if (!eap->skip && eap->addr_count > 0)
3083 {
3084 curwin->w_cursor.lnum = lnum;
3085 curwin->w_cursor.col = 0;
3086 }
3087 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003089 eap->line1, eap->line2, &doesrange,
3090 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 {
3092 failed = TRUE;
3093 break;
3094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (doesrange || eap->skip)
3097 break;
3098 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003099 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003100 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003101 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (aborting())
3103 break;
3104 }
3105 if (eap->skip)
3106 --emsg_skip;
3107
3108 if (!failed)
3109 {
3110 /* Check for trailing illegal characters and a following command. */
3111 if (!ends_excmd(*arg))
3112 {
3113 emsg_severe = TRUE;
3114 EMSG(_(e_trailing));
3115 }
3116 else
3117 eap->nextcmd = check_nextcmd(arg);
3118 }
3119
3120end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003121 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
3125/*
3126 * ":unlet[!] var1 ... " command.
3127 */
3128 void
3129ex_unlet(eap)
3130 exarg_T *eap;
3131{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003132 ex_unletlock(eap, eap->arg, 0);
3133}
3134
3135/*
3136 * ":lockvar" and ":unlockvar" commands
3137 */
3138 void
3139ex_lockvar(eap)
3140 exarg_T *eap;
3141{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003143 int deep = 2;
3144
3145 if (eap->forceit)
3146 deep = -1;
3147 else if (vim_isdigit(*arg))
3148 {
3149 deep = getdigits(&arg);
3150 arg = skipwhite(arg);
3151 }
3152
3153 ex_unletlock(eap, arg, deep);
3154}
3155
3156/*
3157 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3158 */
3159 static void
3160ex_unletlock(eap, argstart, deep)
3161 exarg_T *eap;
3162 char_u *argstart;
3163 int deep;
3164{
3165 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003168 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 do
3171 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003172 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003173 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3174 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003175 if (lv.ll_name == NULL)
3176 error = TRUE; /* error but continue parsing */
3177 if (name_end == NULL || (!vim_iswhite(*name_end)
3178 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 if (name_end != NULL)
3181 {
3182 emsg_severe = TRUE;
3183 EMSG(_(e_trailing));
3184 }
3185 if (!(eap->skip || error))
3186 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 break;
3188 }
3189
3190 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003191 {
3192 if (eap->cmdidx == CMD_unlet)
3193 {
3194 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3195 error = TRUE;
3196 }
3197 else
3198 {
3199 if (do_lock_var(&lv, name_end, deep,
3200 eap->cmdidx == CMD_lockvar) == FAIL)
3201 error = TRUE;
3202 }
3203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003205 if (!eap->skip)
3206 clear_lval(&lv);
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 arg = skipwhite(name_end);
3209 } while (!ends_excmd(*arg));
3210
3211 eap->nextcmd = check_nextcmd(arg);
3212}
3213
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218 int forceit;
3219{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 int ret = OK;
3221 int cc;
3222
3223 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003224 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 cc = *name_end;
3226 *name_end = NUL;
3227
3228 /* Normal name or expanded name. */
3229 if (check_changedtick(lp->ll_name))
3230 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003231 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003234 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003235 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3236 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237 else if (lp->ll_range)
3238 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003240
3241 /* Delete a range of List items. */
3242 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3243 {
3244 li = lp->ll_li->li_next;
3245 listitem_remove(lp->ll_list, lp->ll_li);
3246 lp->ll_li = li;
3247 ++lp->ll_n1;
3248 }
3249 }
3250 else
3251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 /* unlet a List item. */
3254 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003256 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003257 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 }
3259
3260 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003261}
3262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263/*
3264 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
3267 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 hashtab_T *ht;
3273 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003274 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 ht = find_var_ht(name, &varname);
3277 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003279 hi = hash_find(ht, varname);
3280 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003281 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003282 if (var_check_ro(HI2DI(hi)->di_flags, name))
3283 return FAIL;
3284 delete_var(ht, hi);
3285 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003288 if (forceit)
3289 return OK;
3290 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292}
3293
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003294/*
3295 * Lock or unlock variable indicated by "lp".
3296 * "deep" is the levels to go (-1 for unlimited);
3297 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3298 */
3299 static int
3300do_lock_var(lp, name_end, deep, lock)
3301 lval_T *lp;
3302 char_u *name_end;
3303 int deep;
3304 int lock;
3305{
3306 int ret = OK;
3307 int cc;
3308 dictitem_T *di;
3309
3310 if (deep == 0) /* nothing to do */
3311 return OK;
3312
3313 if (lp->ll_tv == NULL)
3314 {
3315 cc = *name_end;
3316 *name_end = NUL;
3317
3318 /* Normal name or expanded name. */
3319 if (check_changedtick(lp->ll_name))
3320 ret = FAIL;
3321 else
3322 {
3323 di = find_var(lp->ll_name, NULL);
3324 if (di == NULL)
3325 ret = FAIL;
3326 else
3327 {
3328 if (lock)
3329 di->di_flags |= DI_FLAGS_LOCK;
3330 else
3331 di->di_flags &= ~DI_FLAGS_LOCK;
3332 item_lock(&di->di_tv, deep, lock);
3333 }
3334 }
3335 *name_end = cc;
3336 }
3337 else if (lp->ll_range)
3338 {
3339 listitem_T *li = lp->ll_li;
3340
3341 /* (un)lock a range of List items. */
3342 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3343 {
3344 item_lock(&li->li_tv, deep, lock);
3345 li = li->li_next;
3346 ++lp->ll_n1;
3347 }
3348 }
3349 else if (lp->ll_list != NULL)
3350 /* (un)lock a List item. */
3351 item_lock(&lp->ll_li->li_tv, deep, lock);
3352 else
3353 /* un(lock) a Dictionary item. */
3354 item_lock(&lp->ll_di->di_tv, deep, lock);
3355
3356 return ret;
3357}
3358
3359/*
3360 * Lock or unlock an item. "deep" is nr of levels to go.
3361 */
3362 static void
3363item_lock(tv, deep, lock)
3364 typval_T *tv;
3365 int deep;
3366 int lock;
3367{
3368 static int recurse = 0;
3369 list_T *l;
3370 listitem_T *li;
3371 dict_T *d;
3372 hashitem_T *hi;
3373 int todo;
3374
3375 if (recurse >= DICT_MAXNEST)
3376 {
3377 EMSG(_("E743: variable nested too deep for (un)lock"));
3378 return;
3379 }
3380 if (deep == 0)
3381 return;
3382 ++recurse;
3383
3384 /* lock/unlock the item itself */
3385 if (lock)
3386 tv->v_lock |= VAR_LOCKED;
3387 else
3388 tv->v_lock &= ~VAR_LOCKED;
3389
3390 switch (tv->v_type)
3391 {
3392 case VAR_LIST:
3393 if ((l = tv->vval.v_list) != NULL)
3394 {
3395 if (lock)
3396 l->lv_lock |= VAR_LOCKED;
3397 else
3398 l->lv_lock &= ~VAR_LOCKED;
3399 if (deep < 0 || deep > 1)
3400 /* recursive: lock/unlock the items the List contains */
3401 for (li = l->lv_first; li != NULL; li = li->li_next)
3402 item_lock(&li->li_tv, deep - 1, lock);
3403 }
3404 break;
3405 case VAR_DICT:
3406 if ((d = tv->vval.v_dict) != NULL)
3407 {
3408 if (lock)
3409 d->dv_lock |= VAR_LOCKED;
3410 else
3411 d->dv_lock &= ~VAR_LOCKED;
3412 if (deep < 0 || deep > 1)
3413 {
3414 /* recursive: lock/unlock the items the List contains */
3415 todo = d->dv_hashtab.ht_used;
3416 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3417 {
3418 if (!HASHITEM_EMPTY(hi))
3419 {
3420 --todo;
3421 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3422 }
3423 }
3424 }
3425 }
3426 }
3427 --recurse;
3428}
3429
Bram Moolenaara40058a2005-07-11 22:42:07 +00003430/*
3431 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3432 * it refers to a List or Dictionary that is locked.
3433 */
3434 static int
3435tv_islocked(tv)
3436 typval_T *tv;
3437{
3438 return (tv->v_lock & VAR_LOCKED)
3439 || (tv->v_type == VAR_LIST
3440 && tv->vval.v_list != NULL
3441 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3442 || (tv->v_type == VAR_DICT
3443 && tv->vval.v_dict != NULL
3444 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3445}
3446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3448/*
3449 * Delete all "menutrans_" variables.
3450 */
3451 void
3452del_menutrans_vars()
3453{
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003455 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar33570922005-01-25 22:26:29 +00003457 hash_lock(&globvarht);
3458 todo = globvarht.ht_used;
3459 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003460 {
3461 if (!HASHITEM_EMPTY(hi))
3462 {
3463 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3465 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003466 }
3467 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003468 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
3470#endif
3471
3472#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3473
3474/*
3475 * Local string buffer for the next two functions to store a variable name
3476 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3477 * get_user_var_name().
3478 */
3479
3480static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3481
3482static char_u *varnamebuf = NULL;
3483static int varnamebuflen = 0;
3484
3485/*
3486 * Function to concatenate a prefix and a variable name.
3487 */
3488 static char_u *
3489cat_prefix_varname(prefix, name)
3490 int prefix;
3491 char_u *name;
3492{
3493 int len;
3494
3495 len = (int)STRLEN(name) + 3;
3496 if (len > varnamebuflen)
3497 {
3498 vim_free(varnamebuf);
3499 len += 10; /* some additional space */
3500 varnamebuf = alloc(len);
3501 if (varnamebuf == NULL)
3502 {
3503 varnamebuflen = 0;
3504 return NULL;
3505 }
3506 varnamebuflen = len;
3507 }
3508 *varnamebuf = prefix;
3509 varnamebuf[1] = ':';
3510 STRCPY(varnamebuf + 2, name);
3511 return varnamebuf;
3512}
3513
3514/*
3515 * Function given to ExpandGeneric() to obtain the list of user defined
3516 * (global/buffer/window/built-in) variable names.
3517 */
3518/*ARGSUSED*/
3519 char_u *
3520get_user_var_name(xp, idx)
3521 expand_T *xp;
3522 int idx;
3523{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003524 static long_u gdone;
3525 static long_u bdone;
3526 static long_u wdone;
3527 static int vidx;
3528 static hashitem_T *hi;
3529 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003532 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003533
3534 /* Global variables */
3535 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003537 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003539 else
3540 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003541 while (HASHITEM_EMPTY(hi))
3542 ++hi;
3543 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3544 return cat_prefix_varname('g', hi->hi_key);
3545 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003547
3548 /* b: variables */
3549 ht = &curbuf->b_vars.dv_hashtab;
3550 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003552 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003553 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003554 else
3555 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003556 while (HASHITEM_EMPTY(hi))
3557 ++hi;
3558 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003560 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003562 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 return (char_u *)"b:changedtick";
3564 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003565
3566 /* w: variables */
3567 ht = &curwin->w_vars.dv_hashtab;
3568 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003570 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003572 else
3573 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003574 while (HASHITEM_EMPTY(hi))
3575 ++hi;
3576 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003578
3579 /* v: variables */
3580 if (vidx < VV_LEN)
3581 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 vim_free(varnamebuf);
3584 varnamebuf = NULL;
3585 varnamebuflen = 0;
3586 return NULL;
3587}
3588
3589#endif /* FEAT_CMDL_COMPL */
3590
3591/*
3592 * types for expressions.
3593 */
3594typedef enum
3595{
3596 TYPE_UNKNOWN = 0
3597 , TYPE_EQUAL /* == */
3598 , TYPE_NEQUAL /* != */
3599 , TYPE_GREATER /* > */
3600 , TYPE_GEQUAL /* >= */
3601 , TYPE_SMALLER /* < */
3602 , TYPE_SEQUAL /* <= */
3603 , TYPE_MATCH /* =~ */
3604 , TYPE_NOMATCH /* !~ */
3605} exptype_T;
3606
3607/*
3608 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003609 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3611 */
3612
3613/*
3614 * Handle zero level expression.
3615 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003617 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 * Return OK or FAIL.
3619 */
3620 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003621eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 char_u **nextcmd;
3625 int evaluate;
3626{
3627 int ret;
3628 char_u *p;
3629
3630 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003631 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 if (ret == FAIL || !ends_excmd(*p))
3633 {
3634 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003635 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 /*
3637 * Report the invalid expression unless the expression evaluation has
3638 * been cancelled due to an aborting error, an interrupt, or an
3639 * exception.
3640 */
3641 if (!aborting())
3642 EMSG2(_(e_invexpr2), arg);
3643 ret = FAIL;
3644 }
3645 if (nextcmd != NULL)
3646 *nextcmd = check_nextcmd(p);
3647
3648 return ret;
3649}
3650
3651/*
3652 * Handle top level expression:
3653 * expr1 ? expr0 : expr0
3654 *
3655 * "arg" must point to the first non-white of the expression.
3656 * "arg" is advanced to the next non-white after the recognized expression.
3657 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003658 * Note: "rettv.v_lock" is not set.
3659 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 * Return OK or FAIL.
3661 */
3662 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003663eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 int evaluate;
3667{
3668 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003669 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671 /*
3672 * Get the first variable.
3673 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676
3677 if ((*arg)[0] == '?')
3678 {
3679 result = FALSE;
3680 if (evaluate)
3681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 int error = FALSE;
3683
3684 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003686 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003687 if (error)
3688 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690
3691 /*
3692 * Get the second variable.
3693 */
3694 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697
3698 /*
3699 * Check for the ":".
3700 */
3701 if ((*arg)[0] != ':')
3702 {
3703 EMSG(_("E109: Missing ':' after '?'"));
3704 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003705 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 return FAIL;
3707 }
3708
3709 /*
3710 * Get the third variable.
3711 */
3712 *arg = skipwhite(*arg + 1);
3713 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3714 {
3715 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003716 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 return FAIL;
3718 }
3719 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722
3723 return OK;
3724}
3725
3726/*
3727 * Handle first level expression:
3728 * expr2 || expr2 || expr2 logical OR
3729 *
3730 * "arg" must point to the first non-white of the expression.
3731 * "arg" is advanced to the next non-white after the recognized expression.
3732 *
3733 * Return OK or FAIL.
3734 */
3735 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003736eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 int evaluate;
3740{
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 long result;
3743 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 /*
3747 * Get the first variable.
3748 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003749 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 return FAIL;
3751
3752 /*
3753 * Repeat until there is no following "||".
3754 */
3755 first = TRUE;
3756 result = FALSE;
3757 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3758 {
3759 if (evaluate && first)
3760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 if (error)
3765 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 first = FALSE;
3767 }
3768
3769 /*
3770 * Get the second variable.
3771 */
3772 *arg = skipwhite(*arg + 2);
3773 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3774 return FAIL;
3775
3776 /*
3777 * Compute the result.
3778 */
3779 if (evaluate && !result)
3780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003781 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003783 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784 if (error)
3785 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 if (evaluate)
3788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789 rettv->v_type = VAR_NUMBER;
3790 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 }
3793
3794 return OK;
3795}
3796
3797/*
3798 * Handle second level expression:
3799 * expr3 && expr3 && expr3 logical AND
3800 *
3801 * "arg" must point to the first non-white of the expression.
3802 * "arg" is advanced to the next non-white after the recognized expression.
3803 *
3804 * Return OK or FAIL.
3805 */
3806 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 int evaluate;
3811{
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 long result;
3814 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003815 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
3817 /*
3818 * Get the first variable.
3819 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822
3823 /*
3824 * Repeat until there is no following "&&".
3825 */
3826 first = TRUE;
3827 result = TRUE;
3828 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3829 {
3830 if (evaluate && first)
3831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 if (error)
3836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 first = FALSE;
3838 }
3839
3840 /*
3841 * Get the second variable.
3842 */
3843 *arg = skipwhite(*arg + 2);
3844 if (eval4(arg, &var2, evaluate && result) == FAIL)
3845 return FAIL;
3846
3847 /*
3848 * Compute the result.
3849 */
3850 if (evaluate && result)
3851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003852 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 if (error)
3856 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 if (evaluate)
3859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860 rettv->v_type = VAR_NUMBER;
3861 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 }
3864
3865 return OK;
3866}
3867
3868/*
3869 * Handle third level expression:
3870 * var1 == var2
3871 * var1 =~ var2
3872 * var1 != var2
3873 * var1 !~ var2
3874 * var1 > var2
3875 * var1 >= var2
3876 * var1 < var2
3877 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003878 * var1 is var2
3879 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 *
3881 * "arg" must point to the first non-white of the expression.
3882 * "arg" is advanced to the next non-white after the recognized expression.
3883 *
3884 * Return OK or FAIL.
3885 */
3886 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 int evaluate;
3891{
Bram Moolenaar33570922005-01-25 22:26:29 +00003892 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 char_u *p;
3894 int i;
3895 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003896 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 int len = 2;
3898 long n1, n2;
3899 char_u *s1, *s2;
3900 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3901 regmatch_T regmatch;
3902 int ic;
3903 char_u *save_cpo;
3904
3905 /*
3906 * Get the first variable.
3907 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return FAIL;
3910
3911 p = *arg;
3912 switch (p[0])
3913 {
3914 case '=': if (p[1] == '=')
3915 type = TYPE_EQUAL;
3916 else if (p[1] == '~')
3917 type = TYPE_MATCH;
3918 break;
3919 case '!': if (p[1] == '=')
3920 type = TYPE_NEQUAL;
3921 else if (p[1] == '~')
3922 type = TYPE_NOMATCH;
3923 break;
3924 case '>': if (p[1] != '=')
3925 {
3926 type = TYPE_GREATER;
3927 len = 1;
3928 }
3929 else
3930 type = TYPE_GEQUAL;
3931 break;
3932 case '<': if (p[1] != '=')
3933 {
3934 type = TYPE_SMALLER;
3935 len = 1;
3936 }
3937 else
3938 type = TYPE_SEQUAL;
3939 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003940 case 'i': if (p[1] == 's')
3941 {
3942 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3943 len = 5;
3944 if (!vim_isIDc(p[len]))
3945 {
3946 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3947 type_is = TRUE;
3948 }
3949 }
3950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952
3953 /*
3954 * If there is a comparitive operator, use it.
3955 */
3956 if (type != TYPE_UNKNOWN)
3957 {
3958 /* extra question mark appended: ignore case */
3959 if (p[len] == '?')
3960 {
3961 ic = TRUE;
3962 ++len;
3963 }
3964 /* extra '#' appended: match case */
3965 else if (p[len] == '#')
3966 {
3967 ic = FALSE;
3968 ++len;
3969 }
3970 /* nothing appened: use 'ignorecase' */
3971 else
3972 ic = p_ic;
3973
3974 /*
3975 * Get the second variable.
3976 */
3977 *arg = skipwhite(p + len);
3978 if (eval5(arg, &var2, evaluate) == FAIL)
3979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003980 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 return FAIL;
3982 }
3983
3984 if (evaluate)
3985 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003986 if (type_is && rettv->v_type != var2.v_type)
3987 {
3988 /* For "is" a different type always means FALSE, for "notis"
3989 * it means TRUE. */
3990 n1 = (type == TYPE_NEQUAL);
3991 }
3992 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3993 {
3994 if (type_is)
3995 {
3996 n1 = (rettv->v_type == var2.v_type
3997 && rettv->vval.v_list == var2.vval.v_list);
3998 if (type == TYPE_NEQUAL)
3999 n1 = !n1;
4000 }
4001 else if (rettv->v_type != var2.v_type
4002 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4003 {
4004 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004005 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004006 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004007 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004008 clear_tv(rettv);
4009 clear_tv(&var2);
4010 return FAIL;
4011 }
4012 else
4013 {
4014 /* Compare two Lists for being equal or unequal. */
4015 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4016 if (type == TYPE_NEQUAL)
4017 n1 = !n1;
4018 }
4019 }
4020
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004021 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4022 {
4023 if (type_is)
4024 {
4025 n1 = (rettv->v_type == var2.v_type
4026 && rettv->vval.v_dict == var2.vval.v_dict);
4027 if (type == TYPE_NEQUAL)
4028 n1 = !n1;
4029 }
4030 else if (rettv->v_type != var2.v_type
4031 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4032 {
4033 if (rettv->v_type != var2.v_type)
4034 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4035 else
4036 EMSG(_("E736: Invalid operation for Dictionary"));
4037 clear_tv(rettv);
4038 clear_tv(&var2);
4039 return FAIL;
4040 }
4041 else
4042 {
4043 /* Compare two Dictionaries for being equal or unequal. */
4044 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4045 if (type == TYPE_NEQUAL)
4046 n1 = !n1;
4047 }
4048 }
4049
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004050 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4051 {
4052 if (rettv->v_type != var2.v_type
4053 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4054 {
4055 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004056 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004057 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004058 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 clear_tv(rettv);
4060 clear_tv(&var2);
4061 return FAIL;
4062 }
4063 else
4064 {
4065 /* Compare two Funcrefs for being equal or unequal. */
4066 if (rettv->vval.v_string == NULL
4067 || var2.vval.v_string == NULL)
4068 n1 = FALSE;
4069 else
4070 n1 = STRCMP(rettv->vval.v_string,
4071 var2.vval.v_string) == 0;
4072 if (type == TYPE_NEQUAL)
4073 n1 = !n1;
4074 }
4075 }
4076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 /*
4078 * If one of the two variables is a number, compare as a number.
4079 * When using "=~" or "!~", always compare as string.
4080 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004081 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004084 n1 = get_tv_number(rettv);
4085 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 switch (type)
4087 {
4088 case TYPE_EQUAL: n1 = (n1 == n2); break;
4089 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4090 case TYPE_GREATER: n1 = (n1 > n2); break;
4091 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4092 case TYPE_SMALLER: n1 = (n1 < n2); break;
4093 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4094 case TYPE_UNKNOWN:
4095 case TYPE_MATCH:
4096 case TYPE_NOMATCH: break; /* avoid gcc warning */
4097 }
4098 }
4099 else
4100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 s1 = get_tv_string_buf(rettv, buf1);
4102 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4104 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4105 else
4106 i = 0;
4107 n1 = FALSE;
4108 switch (type)
4109 {
4110 case TYPE_EQUAL: n1 = (i == 0); break;
4111 case TYPE_NEQUAL: n1 = (i != 0); break;
4112 case TYPE_GREATER: n1 = (i > 0); break;
4113 case TYPE_GEQUAL: n1 = (i >= 0); break;
4114 case TYPE_SMALLER: n1 = (i < 0); break;
4115 case TYPE_SEQUAL: n1 = (i <= 0); break;
4116
4117 case TYPE_MATCH:
4118 case TYPE_NOMATCH:
4119 /* avoid 'l' flag in 'cpoptions' */
4120 save_cpo = p_cpo;
4121 p_cpo = (char_u *)"";
4122 regmatch.regprog = vim_regcomp(s2,
4123 RE_MAGIC + RE_STRING);
4124 regmatch.rm_ic = ic;
4125 if (regmatch.regprog != NULL)
4126 {
4127 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4128 vim_free(regmatch.regprog);
4129 if (type == TYPE_NOMATCH)
4130 n1 = !n1;
4131 }
4132 p_cpo = save_cpo;
4133 break;
4134
4135 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4136 }
4137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 clear_tv(rettv);
4139 clear_tv(&var2);
4140 rettv->v_type = VAR_NUMBER;
4141 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 }
4144
4145 return OK;
4146}
4147
4148/*
4149 * Handle fourth level expression:
4150 * + number addition
4151 * - number subtraction
4152 * . string concatenation
4153 *
4154 * "arg" must point to the first non-white of the expression.
4155 * "arg" is advanced to the next non-white after the recognized expression.
4156 *
4157 * Return OK or FAIL.
4158 */
4159 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 int evaluate;
4164{
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T var2;
4166 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 int op;
4168 long n1, n2;
4169 char_u *s1, *s2;
4170 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4171 char_u *p;
4172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 /*
4180 * Repeat computing, until no '+', '-' or '.' is following.
4181 */
4182 for (;;)
4183 {
4184 op = **arg;
4185 if (op != '+' && op != '-' && op != '.')
4186 break;
4187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (op != '+' || rettv->v_type != VAR_LIST)
4189 {
4190 /* For "list + ...", an illegal use of the first operand as
4191 * a number cannot be determined before evaluating the 2nd
4192 * operand: if this is also a list, all is ok.
4193 * For "something . ...", "something - ..." or "non-list + ...",
4194 * we know that the first operand needs to be a string or number
4195 * without evaluating the 2nd operand. So check before to avoid
4196 * side effects after an error. */
4197 if (evaluate && get_tv_string_chk(rettv) == NULL)
4198 {
4199 clear_tv(rettv);
4200 return FAIL;
4201 }
4202 }
4203
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 /*
4205 * Get the second variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
4208 if (eval6(arg, &var2, evaluate) == FAIL)
4209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 return FAIL;
4212 }
4213
4214 if (evaluate)
4215 {
4216 /*
4217 * Compute the result.
4218 */
4219 if (op == '.')
4220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4222 s2 = get_tv_string_buf_chk(&var2, buf2);
4223 if (s2 == NULL) /* type error ? */
4224 {
4225 clear_tv(rettv);
4226 clear_tv(&var2);
4227 return FAIL;
4228 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004229 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
4231 rettv->v_type = VAR_STRING;
4232 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004234 else if (op == '+' && rettv->v_type == VAR_LIST
4235 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004236 {
4237 /* concatenate Lists */
4238 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4239 &var3) == FAIL)
4240 {
4241 clear_tv(rettv);
4242 clear_tv(&var2);
4243 return FAIL;
4244 }
4245 clear_tv(rettv);
4246 *rettv = var3;
4247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 else
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
4251
4252 n1 = get_tv_number_chk(rettv, &error);
4253 if (error)
4254 {
4255 /* This can only happen for "list + non-list".
4256 * For "non-list + ..." or "something - ...", we returned
4257 * before evaluating the 2nd operand. */
4258 clear_tv(rettv);
4259 return FAIL;
4260 }
4261 n2 = get_tv_number_chk(&var2, &error);
4262 if (error)
4263 {
4264 clear_tv(rettv);
4265 clear_tv(&var2);
4266 return FAIL;
4267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (op == '+')
4270 n1 = n1 + n2;
4271 else
4272 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 rettv->v_type = VAR_NUMBER;
4274 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 }
4279 return OK;
4280}
4281
4282/*
4283 * Handle fifth level expression:
4284 * * number multiplication
4285 * / number division
4286 * % number modulo
4287 *
4288 * "arg" must point to the first non-white of the expression.
4289 * "arg" is advanced to the next non-white after the recognized expression.
4290 *
4291 * Return OK or FAIL.
4292 */
4293 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 int evaluate;
4298{
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int op;
4301 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /*
4305 * Get the first variable.
4306 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 return FAIL;
4309
4310 /*
4311 * Repeat computing, until no '*', '/' or '%' is following.
4312 */
4313 for (;;)
4314 {
4315 op = **arg;
4316 if (op != '*' && op != '/' && op != '%')
4317 break;
4318
4319 if (evaluate)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (error)
4324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 else
4327 n1 = 0;
4328
4329 /*
4330 * Get the second variable.
4331 */
4332 *arg = skipwhite(*arg + 1);
4333 if (eval7(arg, &var2, evaluate) == FAIL)
4334 return FAIL;
4335
4336 if (evaluate)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (error)
4341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
4343 /*
4344 * Compute the result.
4345 */
4346 if (op == '*')
4347 n1 = n1 * n2;
4348 else if (op == '/')
4349 {
4350 if (n2 == 0) /* give an error message? */
4351 n1 = 0x7fffffffL;
4352 else
4353 n1 = n1 / n2;
4354 }
4355 else
4356 {
4357 if (n2 == 0) /* give an error message? */
4358 n1 = 0;
4359 else
4360 n1 = n1 % n2;
4361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 rettv->v_type = VAR_NUMBER;
4363 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 }
4366
4367 return OK;
4368}
4369
4370/*
4371 * Handle sixth level expression:
4372 * number number constant
4373 * "string" string contstant
4374 * 'string' literal string contstant
4375 * &option-name option value
4376 * @r register contents
4377 * identifier variable value
4378 * function() function call
4379 * $VAR environment variable
4380 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004381 * [expr, expr] List
4382 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 *
4384 * Also handle:
4385 * ! in front logical NOT
4386 * - in front unary minus
4387 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388 * trailing [] subscript in String or List
4389 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 *
4391 * "arg" must point to the first non-white of the expression.
4392 * "arg" is advanced to the next non-white after the recognized expression.
4393 *
4394 * Return OK or FAIL.
4395 */
4396 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 int evaluate;
4401{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 long n;
4403 int len;
4404 char_u *s;
4405 int val;
4406 char_u *start_leader, *end_leader;
4407 int ret = OK;
4408 char_u *alias;
4409
4410 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004412 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 /*
4417 * Skip '!' and '-' characters. They are handled later.
4418 */
4419 start_leader = *arg;
4420 while (**arg == '!' || **arg == '-' || **arg == '+')
4421 *arg = skipwhite(*arg + 1);
4422 end_leader = *arg;
4423
4424 switch (**arg)
4425 {
4426 /*
4427 * Number constant.
4428 */
4429 case '0':
4430 case '1':
4431 case '2':
4432 case '3':
4433 case '4':
4434 case '5':
4435 case '6':
4436 case '7':
4437 case '8':
4438 case '9':
4439 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4440 *arg += len;
4441 if (evaluate)
4442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 rettv->v_type = VAR_NUMBER;
4444 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 break;
4447
4448 /*
4449 * String constant: "string".
4450 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 break;
4453
4454 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458 break;
4459
4460 /*
4461 * List: [expr, expr]
4462 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 break;
4465
4466 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 * Dictionary: {key: val, key: val}
4468 */
4469 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4470 break;
4471
4472 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004473 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004475 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 break;
4477
4478 /*
4479 * Environment variable: $VAR.
4480 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 break;
4483
4484 /*
4485 * Register contents: @r.
4486 */
4487 case '@': ++*arg;
4488 if (evaluate)
4489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004491 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493 if (**arg != NUL)
4494 ++*arg;
4495 break;
4496
4497 /*
4498 * nested expression: (expression).
4499 */
4500 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004501 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (**arg == ')')
4503 ++*arg;
4504 else if (ret == OK)
4505 {
4506 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004507 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 ret = FAIL;
4509 }
4510 break;
4511
Bram Moolenaar8c711452005-01-14 21:53:12 +00004512 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 break;
4514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515
4516 if (ret == NOTDONE)
4517 {
4518 /*
4519 * Must be a variable or function name.
4520 * Can also be a curly-braces kind of name: {expr}.
4521 */
4522 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004523 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 if (alias != NULL)
4525 s = alias;
4526
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004527 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004528 ret = FAIL;
4529 else
4530 {
4531 if (**arg == '(') /* recursive! */
4532 {
4533 /* If "s" is the name of a variable of type VAR_FUNC
4534 * use its contents. */
4535 s = deref_func_name(s, &len);
4536
4537 /* Invoke the function. */
4538 ret = get_func_tv(s, len, rettv, arg,
4539 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004540 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 /* Stop the expression evaluation when immediately
4542 * aborting on error, or when an interrupt occurred or
4543 * an exception was thrown but not caught. */
4544 if (aborting())
4545 {
4546 if (ret == OK)
4547 clear_tv(rettv);
4548 ret = FAIL;
4549 }
4550 }
4551 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004552 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004553 else
4554 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 }
4556
4557 if (alias != NULL)
4558 vim_free(alias);
4559 }
4560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 *arg = skipwhite(*arg);
4562
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004563 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4564 * expr(expr). */
4565 if (ret == OK)
4566 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 /*
4569 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4570 */
4571 if (ret == OK && evaluate && end_leader > start_leader)
4572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 int error = FALSE;
4574
4575 val = get_tv_number_chk(rettv, &error);
4576 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004578 clear_tv(rettv);
4579 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004581 else
4582 {
4583 while (end_leader > start_leader)
4584 {
4585 --end_leader;
4586 if (*end_leader == '!')
4587 val = !val;
4588 else if (*end_leader == '-')
4589 val = -val;
4590 }
4591 clear_tv(rettv);
4592 rettv->v_type = VAR_NUMBER;
4593 rettv->vval.v_number = val;
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596
4597 return ret;
4598}
4599
4600/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 * Evaluate an "[expr]" or "[expr:expr]" index.
4602 * "*arg" points to the '['.
4603 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4604 */
4605 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004606eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004608 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004610 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611{
4612 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004613 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004615 long len = -1;
4616 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004622 if (verbose)
4623 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624 return FAIL;
4625 }
4626
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 /*
4630 * dict.name
4631 */
4632 key = *arg + 1;
4633 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4634 ;
4635 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 }
4639 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641 /*
4642 * something[idx]
4643 *
4644 * Get the (first) variable from inside the [].
4645 */
4646 *arg = skipwhite(*arg + 1);
4647 if (**arg == ':')
4648 empty1 = TRUE;
4649 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4650 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4652 {
4653 /* not a number or string */
4654 clear_tv(&var1);
4655 return FAIL;
4656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657
4658 /*
4659 * Get the second variable from inside the [:].
4660 */
4661 if (**arg == ':')
4662 {
4663 range = TRUE;
4664 *arg = skipwhite(*arg + 1);
4665 if (**arg == ']')
4666 empty2 = TRUE;
4667 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 if (!empty1)
4670 clear_tv(&var1);
4671 return FAIL;
4672 }
4673 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4674 {
4675 /* not a number or string */
4676 if (!empty1)
4677 clear_tv(&var1);
4678 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 return FAIL;
4680 }
4681 }
4682
4683 /* Check for the ']'. */
4684 if (**arg != ']')
4685 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004686 if (verbose)
4687 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 clear_tv(&var1);
4689 if (range)
4690 clear_tv(&var2);
4691 return FAIL;
4692 }
4693 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 }
4695
4696 if (evaluate)
4697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004698 n1 = 0;
4699 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 n1 = get_tv_number(&var1);
4702 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 }
4704 if (range)
4705 {
4706 if (empty2)
4707 n2 = -1;
4708 else
4709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004710 n2 = get_tv_number(&var2);
4711 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 }
4713 }
4714
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 {
4717 case VAR_NUMBER:
4718 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 len = (long)STRLEN(s);
4721 if (range)
4722 {
4723 /* The resulting variable is a substring. If the indexes
4724 * are out of range the result is empty. */
4725 if (n1 < 0)
4726 {
4727 n1 = len + n1;
4728 if (n1 < 0)
4729 n1 = 0;
4730 }
4731 if (n2 < 0)
4732 n2 = len + n2;
4733 else if (n2 >= len)
4734 n2 = len;
4735 if (n1 >= len || n2 < 0 || n1 > n2)
4736 s = NULL;
4737 else
4738 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4739 }
4740 else
4741 {
4742 /* The resulting variable is a string of a single
4743 * character. If the index is too big or negative the
4744 * result is empty. */
4745 if (n1 >= len || n1 < 0)
4746 s = NULL;
4747 else
4748 s = vim_strnsave(s + n1, 1);
4749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
4751 rettv->v_type = VAR_STRING;
4752 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 break;
4754
4755 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 if (n1 < 0)
4758 n1 = len + n1;
4759 if (!empty1 && (n1 < 0 || n1 >= len))
4760 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004761 if (verbose)
4762 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 return FAIL;
4764 }
4765 if (range)
4766 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 list_T *l;
4768 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769
4770 if (n2 < 0)
4771 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004772 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004774 if (verbose)
4775 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 return FAIL;
4777 }
4778 l = list_alloc();
4779 if (l == NULL)
4780 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 n1 <= n2; ++n1)
4783 {
4784 if (list_append_tv(l, &item->li_tv) == FAIL)
4785 {
4786 list_free(l);
4787 return FAIL;
4788 }
4789 item = item->li_next;
4790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004791 clear_tv(rettv);
4792 rettv->v_type = VAR_LIST;
4793 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004794 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004795 }
4796 else
4797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 clear_tv(rettv);
4801 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 }
4803 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804
4805 case VAR_DICT:
4806 if (range)
4807 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004808 if (verbose)
4809 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810 if (len == -1)
4811 clear_tv(&var1);
4812 return FAIL;
4813 }
4814 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004815 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816
4817 if (len == -1)
4818 {
4819 key = get_tv_string(&var1);
4820 if (*key == NUL)
4821 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004822 if (verbose)
4823 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824 clear_tv(&var1);
4825 return FAIL;
4826 }
4827 }
4828
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004829 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004830
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004831 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004832 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004833 if (len == -1)
4834 clear_tv(&var1);
4835 if (item == NULL)
4836 return FAIL;
4837
4838 copy_tv(&item->di_tv, &var1);
4839 clear_tv(rettv);
4840 *rettv = var1;
4841 }
4842 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843 }
4844 }
4845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 return OK;
4847}
4848
4849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 * Get an option value.
4851 * "arg" points to the '&' or '+' before the option name.
4852 * "arg" is advanced to character after the option name.
4853 * Return OK or FAIL.
4854 */
4855 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004858 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 int evaluate;
4860{
4861 char_u *option_end;
4862 long numval;
4863 char_u *stringval;
4864 int opt_type;
4865 int c;
4866 int working = (**arg == '+'); /* has("+option") */
4867 int ret = OK;
4868 int opt_flags;
4869
4870 /*
4871 * Isolate the option name and find its value.
4872 */
4873 option_end = find_option_end(arg, &opt_flags);
4874 if (option_end == NULL)
4875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 EMSG2(_("E112: Option name missing: %s"), *arg);
4878 return FAIL;
4879 }
4880
4881 if (!evaluate)
4882 {
4883 *arg = option_end;
4884 return OK;
4885 }
4886
4887 c = *option_end;
4888 *option_end = NUL;
4889 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
4892 if (opt_type == -3) /* invalid name */
4893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 EMSG2(_("E113: Unknown option: %s"), *arg);
4896 ret = FAIL;
4897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
4900 if (opt_type == -2) /* hidden string option */
4901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 rettv->v_type = VAR_STRING;
4903 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 else if (opt_type == -1) /* hidden number option */
4906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 rettv->v_type = VAR_NUMBER;
4908 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
4910 else if (opt_type == 1) /* number option */
4911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 rettv->v_type = VAR_NUMBER;
4913 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else /* string option */
4916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 rettv->v_type = VAR_STRING;
4918 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920 }
4921 else if (working && (opt_type == -2 || opt_type == -1))
4922 ret = FAIL;
4923
4924 *option_end = c; /* put back for error messages */
4925 *arg = option_end;
4926
4927 return ret;
4928}
4929
4930/*
4931 * Allocate a variable for a string constant.
4932 * Return OK or FAIL.
4933 */
4934 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004935get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 int evaluate;
4939{
4940 char_u *p;
4941 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 int extra = 0;
4943
4944 /*
4945 * Find the end of the string, skipping backslashed characters.
4946 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004947 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 {
4949 if (*p == '\\' && p[1] != NUL)
4950 {
4951 ++p;
4952 /* A "\<x>" form occupies at least 4 characters, and produces up
4953 * to 6 characters: reserve space for 2 extra */
4954 if (*p == '<')
4955 extra += 2;
4956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958
4959 if (*p != '"')
4960 {
4961 EMSG2(_("E114: Missing quote: %s"), *arg);
4962 return FAIL;
4963 }
4964
4965 /* If only parsing, set *arg and return here */
4966 if (!evaluate)
4967 {
4968 *arg = p + 1;
4969 return OK;
4970 }
4971
4972 /*
4973 * Copy the string into allocated memory, handling backslashed
4974 * characters.
4975 */
4976 name = alloc((unsigned)(p - *arg + extra));
4977 if (name == NULL)
4978 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 rettv->v_type = VAR_STRING;
4980 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 if (*p == '\\')
4985 {
4986 switch (*++p)
4987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 case 'b': *name++ = BS; ++p; break;
4989 case 'e': *name++ = ESC; ++p; break;
4990 case 'f': *name++ = FF; ++p; break;
4991 case 'n': *name++ = NL; ++p; break;
4992 case 'r': *name++ = CAR; ++p; break;
4993 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 case 'X': /* hex: "\x1", "\x12" */
4996 case 'x':
4997 case 'u': /* Unicode: "\u0023" */
4998 case 'U':
4999 if (vim_isxdigit(p[1]))
5000 {
5001 int n, nr;
5002 int c = toupper(*p);
5003
5004 if (c == 'X')
5005 n = 2;
5006 else
5007 n = 4;
5008 nr = 0;
5009 while (--n >= 0 && vim_isxdigit(p[1]))
5010 {
5011 ++p;
5012 nr = (nr << 4) + hex2nr(*p);
5013 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#ifdef FEAT_MBYTE
5016 /* For "\u" store the number according to
5017 * 'encoding'. */
5018 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 else
5021#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 break;
5025
5026 /* octal: "\1", "\12", "\123" */
5027 case '0':
5028 case '1':
5029 case '2':
5030 case '3':
5031 case '4':
5032 case '5':
5033 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 case '7': *name = *p++ - '0';
5035 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 *name = (*name << 3) + *p++ - '0';
5038 if (*p >= '0' && *p <= '7')
5039 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043
5044 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (extra != 0)
5047 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 break;
5050 }
5051 /* FALLTHROUGH */
5052
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055 }
5056 }
5057 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 *arg = p + 1;
5063
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 return OK;
5065}
5066
5067/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 * Return OK or FAIL.
5070 */
5071 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 int evaluate;
5076{
5077 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 int reduce = 0;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 */
5084 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5085 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 break;
5090 ++reduce;
5091 ++p;
5092 }
5093 }
5094
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 return FAIL;
5099 }
5100
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 if (!evaluate)
5103 {
5104 *arg = p + 1;
5105 return OK;
5106 }
5107
5108 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 */
5111 str = alloc((unsigned)((p - *arg) - reduce));
5112 if (str == NULL)
5113 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 rettv->v_type = VAR_STRING;
5115 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005118 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 break;
5123 ++p;
5124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 *arg = p + 1;
5129
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 return OK;
5131}
5132
5133/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005134 * Allocate a variable for a List and fill it from "*arg".
5135 * Return OK or FAIL.
5136 */
5137 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 int evaluate;
5142{
Bram Moolenaar33570922005-01-25 22:26:29 +00005143 list_T *l = NULL;
5144 typval_T tv;
5145 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146
5147 if (evaluate)
5148 {
5149 l = list_alloc();
5150 if (l == NULL)
5151 return FAIL;
5152 }
5153
5154 *arg = skipwhite(*arg + 1);
5155 while (**arg != ']' && **arg != NUL)
5156 {
5157 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5158 goto failret;
5159 if (evaluate)
5160 {
5161 item = listitem_alloc();
5162 if (item != NULL)
5163 {
5164 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005165 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166 list_append(l, item);
5167 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005168 else
5169 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170 }
5171
5172 if (**arg == ']')
5173 break;
5174 if (**arg != ',')
5175 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 goto failret;
5178 }
5179 *arg = skipwhite(*arg + 1);
5180 }
5181
5182 if (**arg != ']')
5183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185failret:
5186 if (evaluate)
5187 list_free(l);
5188 return FAIL;
5189 }
5190
5191 *arg = skipwhite(*arg + 1);
5192 if (evaluate)
5193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 rettv->v_type = VAR_LIST;
5195 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005196 ++l->lv_refcount;
5197 }
5198
5199 return OK;
5200}
5201
5202/*
5203 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005204 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005206 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207list_alloc()
5208{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005209 list_T *l;
5210
5211 l = (list_T *)alloc_clear(sizeof(list_T));
5212 if (l != NULL)
5213 {
5214 /* Prepend the list to the list of lists for garbage collection. */
5215 if (first_list != NULL)
5216 first_list->lv_used_prev = l;
5217 l->lv_used_prev = NULL;
5218 l->lv_used_next = first_list;
5219 first_list = l;
5220 }
5221 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222}
5223
5224/*
5225 * Unreference a list: decrement the reference count and free it when it
5226 * becomes zero.
5227 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005228 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005230 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005232 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5233 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234}
5235
5236/*
5237 * Free a list, including all items it points to.
5238 * Ignores the reference count.
5239 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005240 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243{
Bram Moolenaar33570922005-01-25 22:26:29 +00005244 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245
Bram Moolenaard9fba312005-06-26 22:34:35 +00005246 /* Avoid that recursive reference to the list frees us again. */
5247 l->lv_refcount = DEL_REFCOUNT;
5248
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005249 /* Remove the list from the list of lists for garbage collection. */
5250 if (l->lv_used_prev == NULL)
5251 first_list = l->lv_used_next;
5252 else
5253 l->lv_used_prev->lv_used_next = l->lv_used_next;
5254 if (l->lv_used_next != NULL)
5255 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5256
Bram Moolenaard9fba312005-06-26 22:34:35 +00005257 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005259 /* Remove the item before deleting it. */
5260 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 listitem_free(item);
5262 }
5263 vim_free(l);
5264}
5265
5266/*
5267 * Allocate a list item.
5268 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005269 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270listitem_alloc()
5271{
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273}
5274
5275/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005276 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 */
5278 static void
5279listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 vim_free(item);
5284}
5285
5286/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005287 * Remove a list item from a List and free it. Also clears the value.
5288 */
5289 static void
5290listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 list_T *l;
5292 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005293{
5294 list_remove(l, item, item);
5295 listitem_free(item);
5296}
5297
5298/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 * Get the number of items in a list.
5300 */
5301 static long
5302list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005303 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 if (l == NULL)
5306 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005307 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308}
5309
5310/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005311 * Return TRUE when two lists have exactly the same values.
5312 */
5313 static int
5314list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005315 list_T *l1;
5316 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005317 int ic; /* ignore case for strings */
5318{
Bram Moolenaar33570922005-01-25 22:26:29 +00005319 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005320
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005321 if (list_len(l1) != list_len(l2))
5322 return FALSE;
5323
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005324 for (item1 = l1->lv_first, item2 = l2->lv_first;
5325 item1 != NULL && item2 != NULL;
5326 item1 = item1->li_next, item2 = item2->li_next)
5327 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5328 return FALSE;
5329 return item1 == NULL && item2 == NULL;
5330}
5331
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005332#if defined(FEAT_PYTHON) || defined(PROTO)
5333/*
5334 * Return the dictitem that an entry in a hashtable points to.
5335 */
5336 dictitem_T *
5337dict_lookup(hi)
5338 hashitem_T *hi;
5339{
5340 return HI2DI(hi);
5341}
5342#endif
5343
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005344/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005345 * Return TRUE when two dictionaries have exactly the same key/values.
5346 */
5347 static int
5348dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005349 dict_T *d1;
5350 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005351 int ic; /* ignore case for strings */
5352{
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 hashitem_T *hi;
5354 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005355 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005356
5357 if (dict_len(d1) != dict_len(d2))
5358 return FALSE;
5359
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 todo = d1->dv_hashtab.ht_used;
5361 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005363 if (!HASHITEM_EMPTY(hi))
5364 {
5365 item2 = dict_find(d2, hi->hi_key, -1);
5366 if (item2 == NULL)
5367 return FALSE;
5368 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5369 return FALSE;
5370 --todo;
5371 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005372 }
5373 return TRUE;
5374}
5375
5376/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005377 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005378 * Compares the items just like "==" would compare them, but strings and
5379 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005380 */
5381 static int
5382tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005383 typval_T *tv1;
5384 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005385 int ic; /* ignore case */
5386{
5387 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005388 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005389
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005390 if (tv1->v_type != tv2->v_type)
5391 return FALSE;
5392
5393 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005394 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005395 case VAR_LIST:
5396 /* recursive! */
5397 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5398
5399 case VAR_DICT:
5400 /* recursive! */
5401 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5402
5403 case VAR_FUNC:
5404 return (tv1->vval.v_string != NULL
5405 && tv2->vval.v_string != NULL
5406 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5407
5408 case VAR_NUMBER:
5409 return tv1->vval.v_number == tv2->vval.v_number;
5410
5411 case VAR_STRING:
5412 s1 = get_tv_string_buf(tv1, buf1);
5413 s2 = get_tv_string_buf(tv2, buf2);
5414 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005415 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005416
5417 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005418 return TRUE;
5419}
5420
5421/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 * Locate item with index "n" in list "l" and return it.
5423 * A negative index is counted from the end; -1 is the last item.
5424 * Returns NULL when "n" is out of range.
5425 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 long n;
5430{
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 long idx;
5433
5434 if (l == NULL)
5435 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005436
5437 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005439 n = l->lv_len + n;
5440
5441 /* Check for index out of range. */
5442 if (n < 0 || n >= l->lv_len)
5443 return NULL;
5444
5445 /* When there is a cached index may start search from there. */
5446 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005448 if (n < l->lv_idx / 2)
5449 {
5450 /* closest to the start of the list */
5451 item = l->lv_first;
5452 idx = 0;
5453 }
5454 else if (n > (l->lv_idx + l->lv_len) / 2)
5455 {
5456 /* closest to the end of the list */
5457 item = l->lv_last;
5458 idx = l->lv_len - 1;
5459 }
5460 else
5461 {
5462 /* closest to the cached index */
5463 item = l->lv_idx_item;
5464 idx = l->lv_idx;
5465 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 }
5467 else
5468 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005469 if (n < l->lv_len / 2)
5470 {
5471 /* closest to the start of the list */
5472 item = l->lv_first;
5473 idx = 0;
5474 }
5475 else
5476 {
5477 /* closest to the end of the list */
5478 item = l->lv_last;
5479 idx = l->lv_len - 1;
5480 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005482
5483 while (n > idx)
5484 {
5485 /* search forward */
5486 item = item->li_next;
5487 ++idx;
5488 }
5489 while (n < idx)
5490 {
5491 /* search backward */
5492 item = item->li_prev;
5493 --idx;
5494 }
5495
5496 /* cache the used index */
5497 l->lv_idx = idx;
5498 l->lv_idx_item = item;
5499
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 return item;
5501}
5502
5503/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005504 * Locate "item" list "l" and return its index.
5505 * Returns -1 when "item" is not in the list.
5506 */
5507 static long
5508list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 list_T *l;
5510 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005511{
5512 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005513 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005514
5515 if (l == NULL)
5516 return -1;
5517 idx = 0;
5518 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5519 ++idx;
5520 if (li == NULL)
5521 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005522 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005523}
5524
5525/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 * Append item "item" to the end of list "l".
5527 */
5528 static void
5529list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005530 list_T *l;
5531 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532{
5533 if (l->lv_last == NULL)
5534 {
5535 /* empty list */
5536 l->lv_first = item;
5537 l->lv_last = item;
5538 item->li_prev = NULL;
5539 }
5540 else
5541 {
5542 l->lv_last->li_next = item;
5543 item->li_prev = l->lv_last;
5544 l->lv_last = item;
5545 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005546 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 item->li_next = NULL;
5548}
5549
5550/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005551 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005552 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 */
5554 static int
5555list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005556 list_T *l;
5557 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005559 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560
Bram Moolenaar05159a02005-02-26 23:04:13 +00005561 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005563 copy_tv(tv, &li->li_tv);
5564 list_append(l, li);
5565 return OK;
5566}
5567
5568/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005569 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005570 * Return FAIL when out of memory.
5571 */
5572 int
5573list_append_dict(list, dict)
5574 list_T *list;
5575 dict_T *dict;
5576{
5577 listitem_T *li = listitem_alloc();
5578
5579 if (li == NULL)
5580 return FAIL;
5581 li->li_tv.v_type = VAR_DICT;
5582 li->li_tv.v_lock = 0;
5583 li->li_tv.vval.v_dict = dict;
5584 list_append(list, li);
5585 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 return OK;
5587}
5588
5589/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005590 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005591 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005592 * Returns FAIL when out of memory.
5593 */
5594 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005595list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005596 list_T *l;
5597 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005598 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005599{
5600 listitem_T *li = listitem_alloc();
5601
5602 if (li == NULL)
5603 return FAIL;
5604 list_append(l, li);
5605 li->li_tv.v_type = VAR_STRING;
5606 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005607 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5608 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005609 return FAIL;
5610 return OK;
5611}
5612
5613/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005614 * Append "n" to list "l".
5615 * Returns FAIL when out of memory.
5616 */
5617 static int
5618list_append_number(l, n)
5619 list_T *l;
5620 varnumber_T n;
5621{
5622 listitem_T *li;
5623
5624 li = listitem_alloc();
5625 if (li == NULL)
5626 return FAIL;
5627 li->li_tv.v_type = VAR_NUMBER;
5628 li->li_tv.v_lock = 0;
5629 li->li_tv.vval.v_number = n;
5630 list_append(l, li);
5631 return OK;
5632}
5633
5634/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005635 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005636 * If "item" is NULL append at the end.
5637 * Return FAIL when out of memory.
5638 */
5639 static int
5640list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005641 list_T *l;
5642 typval_T *tv;
5643 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005644{
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005646
5647 if (ni == NULL)
5648 return FAIL;
5649 copy_tv(tv, &ni->li_tv);
5650 if (item == NULL)
5651 /* Append new item at end of list. */
5652 list_append(l, ni);
5653 else
5654 {
5655 /* Insert new item before existing item. */
5656 ni->li_prev = item->li_prev;
5657 ni->li_next = item;
5658 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005659 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005660 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005661 ++l->lv_idx;
5662 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005663 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005664 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005665 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005666 l->lv_idx_item = NULL;
5667 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005668 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005669 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005670 }
5671 return OK;
5672}
5673
5674/*
5675 * Extend "l1" with "l2".
5676 * If "bef" is NULL append at the end, otherwise insert before this item.
5677 * Returns FAIL when out of memory.
5678 */
5679 static int
5680list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005681 list_T *l1;
5682 list_T *l2;
5683 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005684{
Bram Moolenaar33570922005-01-25 22:26:29 +00005685 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005686
5687 for (item = l2->lv_first; item != NULL; item = item->li_next)
5688 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5689 return FAIL;
5690 return OK;
5691}
5692
5693/*
5694 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5695 * Return FAIL when out of memory.
5696 */
5697 static int
5698list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 list_T *l1;
5700 list_T *l2;
5701 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005702{
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005704
5705 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005706 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005707 if (l == NULL)
5708 return FAIL;
5709 tv->v_type = VAR_LIST;
5710 tv->vval.v_list = l;
5711
5712 /* append all items from the second list */
5713 return list_extend(l, l2, NULL);
5714}
5715
5716/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005717 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005719 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 * Returns NULL when out of memory.
5721 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005722 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005723list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005726 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727{
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 list_T *copy;
5729 listitem_T *item;
5730 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731
5732 if (orig == NULL)
5733 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005734
5735 copy = list_alloc();
5736 if (copy != NULL)
5737 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005738 if (copyID != 0)
5739 {
5740 /* Do this before adding the items, because one of the items may
5741 * refer back to this list. */
5742 orig->lv_copyID = copyID;
5743 orig->lv_copylist = copy;
5744 }
5745 for (item = orig->lv_first; item != NULL && !got_int;
5746 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 {
5748 ni = listitem_alloc();
5749 if (ni == NULL)
5750 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005751 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005752 {
5753 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5754 {
5755 vim_free(ni);
5756 break;
5757 }
5758 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005760 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 list_append(copy, ni);
5762 }
5763 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005764 if (item != NULL)
5765 {
5766 list_unref(copy);
5767 copy = NULL;
5768 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 }
5770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 return copy;
5772}
5773
5774/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005775 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005776 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005778 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005779list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 list_T *l;
5781 listitem_T *item;
5782 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005783{
Bram Moolenaar33570922005-01-25 22:26:29 +00005784 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005786 /* notify watchers */
5787 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005789 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005790 list_fix_watch(l, ip);
5791 if (ip == item2)
5792 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005794
5795 if (item2->li_next == NULL)
5796 l->lv_last = item->li_prev;
5797 else
5798 item2->li_next->li_prev = item->li_prev;
5799 if (item->li_prev == NULL)
5800 l->lv_first = item2->li_next;
5801 else
5802 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005803 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804}
5805
5806/*
5807 * Return an allocated string with the string representation of a list.
5808 * May return NULL.
5809 */
5810 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005811list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005813 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814{
5815 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816
5817 if (tv->vval.v_list == NULL)
5818 return NULL;
5819 ga_init2(&ga, (int)sizeof(char), 80);
5820 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005821 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005822 {
5823 vim_free(ga.ga_data);
5824 return NULL;
5825 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 ga_append(&ga, ']');
5827 ga_append(&ga, NUL);
5828 return (char_u *)ga.ga_data;
5829}
5830
5831/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 * Join list "l" into a string in "*gap", using separator "sep".
5833 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005834 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005836 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005837list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005839 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 char_u *sep;
5841 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005842 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843{
5844 int first = TRUE;
5845 char_u *tofree;
5846 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005847 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 char_u *s;
5849
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005850 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 {
5852 if (first)
5853 first = FALSE;
5854 else
5855 ga_concat(gap, sep);
5856
5857 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005858 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005860 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 if (s != NULL)
5862 ga_concat(gap, s);
5863 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005864 if (s == NULL)
5865 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005867 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868}
5869
5870/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005871 * Garbage collection for lists and dictionaries.
5872 *
5873 * We use reference counts to be able to free most items right away when they
5874 * are no longer used. But for composite items it's possible that it becomes
5875 * unused while the reference count is > 0: When there is a recursive
5876 * reference. Example:
5877 * :let l = [1, 2, 3]
5878 * :let d = {9: l}
5879 * :let l[1] = d
5880 *
5881 * Since this is quite unusual we handle this with garbage collection: every
5882 * once in a while find out which lists and dicts are not referenced from any
5883 * variable.
5884 *
5885 * Here is a good reference text about garbage collection (refers to Python
5886 * but it applies to all reference-counting mechanisms):
5887 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005888 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005889
5890/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 * Do garbage collection for lists and dicts.
5892 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005893 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894 int
5895garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005896{
5897 dict_T *dd;
5898 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005899 int copyID = ++current_copyID;
5900 buf_T *buf;
5901 win_T *wp;
5902 int i;
5903 funccall_T *fc;
5904 int did_free = FALSE;
5905
5906 /*
5907 * 1. Go through all accessible variables and mark all lists and dicts
5908 * with copyID.
5909 */
5910 /* script-local variables */
5911 for (i = 1; i <= ga_scripts.ga_len; ++i)
5912 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5913
5914 /* buffer-local variables */
5915 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5916 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5917
5918 /* window-local variables */
5919 FOR_ALL_WINDOWS(wp)
5920 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5921
5922 /* global variables */
5923 set_ref_in_ht(&globvarht, copyID);
5924
5925 /* function-local variables */
5926 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5927 {
5928 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5929 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5930 }
5931
5932 /*
5933 * 2. Go through the list of dicts and free items without the copyID.
5934 */
5935 for (dd = first_dict; dd != NULL; )
5936 if (dd->dv_copyID != copyID)
5937 {
5938 dict_free(dd);
5939 did_free = TRUE;
5940
5941 /* restart, next dict may also have been freed */
5942 dd = first_dict;
5943 }
5944 else
5945 dd = dd->dv_used_next;
5946
5947 /*
5948 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005949 * But don't free a list that has a watcher (used in a for loop), these
5950 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951 */
5952 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005953 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005954 {
5955 list_free(ll);
5956 did_free = TRUE;
5957
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005958 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 ll = first_list;
5960 }
5961 else
5962 ll = ll->lv_used_next;
5963
5964 return did_free;
5965}
5966
5967/*
5968 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5969 */
5970 static void
5971set_ref_in_ht(ht, copyID)
5972 hashtab_T *ht;
5973 int copyID;
5974{
5975 int todo;
5976 hashitem_T *hi;
5977
5978 todo = ht->ht_used;
5979 for (hi = ht->ht_array; todo > 0; ++hi)
5980 if (!HASHITEM_EMPTY(hi))
5981 {
5982 --todo;
5983 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5984 }
5985}
5986
5987/*
5988 * Mark all lists and dicts referenced through list "l" with "copyID".
5989 */
5990 static void
5991set_ref_in_list(l, copyID)
5992 list_T *l;
5993 int copyID;
5994{
5995 listitem_T *li;
5996
5997 for (li = l->lv_first; li != NULL; li = li->li_next)
5998 set_ref_in_item(&li->li_tv, copyID);
5999}
6000
6001/*
6002 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6003 */
6004 static void
6005set_ref_in_item(tv, copyID)
6006 typval_T *tv;
6007 int copyID;
6008{
6009 dict_T *dd;
6010 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006011
6012 switch (tv->v_type)
6013 {
6014 case VAR_DICT:
6015 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006016 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006017 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 /* Didn't see this dict yet. */
6019 dd->dv_copyID = copyID;
6020 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006021 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006022 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006023
6024 case VAR_LIST:
6025 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006027 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028 /* Didn't see this list yet. */
6029 ll->lv_copyID = copyID;
6030 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006031 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006032 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006033 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006034 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006035}
6036
6037/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038 * Allocate an empty header for a dictionary.
6039 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006040 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006041dict_alloc()
6042{
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006044
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006046 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006047 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006048 /* Add the list to the hashtable for garbage collection. */
6049 if (first_dict != NULL)
6050 first_dict->dv_used_prev = d;
6051 d->dv_used_next = first_dict;
6052 d->dv_used_prev = NULL;
6053
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006055 d->dv_lock = 0;
6056 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006058 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006060}
6061
6062/*
6063 * Unreference a Dictionary: decrement the reference count and free it when it
6064 * becomes zero.
6065 */
6066 static void
6067dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006069{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006070 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6071 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006072}
6073
6074/*
6075 * Free a Dictionary, including all items it contains.
6076 * Ignores the reference count.
6077 */
6078 static void
6079dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006081{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006082 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006084 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006085
Bram Moolenaard9fba312005-06-26 22:34:35 +00006086 /* Avoid that recursive reference to the dict frees us again. */
6087 d->dv_refcount = DEL_REFCOUNT;
6088
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006089 /* Remove the dict from the list of dicts for garbage collection. */
6090 if (d->dv_used_prev == NULL)
6091 first_dict = d->dv_used_next;
6092 else
6093 d->dv_used_prev->dv_used_next = d->dv_used_next;
6094 if (d->dv_used_next != NULL)
6095 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6096
6097 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006098 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 todo = d->dv_hashtab.ht_used;
6100 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006101 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006102 if (!HASHITEM_EMPTY(hi))
6103 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006104 /* Remove the item before deleting it, just in case there is
6105 * something recursive causing trouble. */
6106 di = HI2DI(hi);
6107 hash_remove(&d->dv_hashtab, hi);
6108 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006109 --todo;
6110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006111 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006113 vim_free(d);
6114}
6115
6116/*
6117 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006118 * The "key" is copied to the new item.
6119 * Note that the value of the item "di_tv" still needs to be initialized!
6120 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006121 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123dictitem_alloc(key)
6124 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006125{
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006127
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006129 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006131 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006132 di->di_flags = 0;
6133 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006134 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006135}
6136
6137/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006138 * Make a copy of a Dictionary item.
6139 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006141dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006143{
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006145
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006147 if (di != NULL)
6148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006149 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006150 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006151 copy_tv(&org->di_tv, &di->di_tv);
6152 }
6153 return di;
6154}
6155
6156/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 * Remove item "item" from Dictionary "dict" and free it.
6158 */
6159 static void
6160dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 dict_T *dict;
6162 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006163{
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006167 if (HASHITEM_EMPTY(hi))
6168 EMSG2(_(e_intern2), "dictitem_remove()");
6169 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006171 dictitem_free(item);
6172}
6173
6174/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006175 * Free a dict item. Also clears the value.
6176 */
6177 static void
6178dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006180{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006181 clear_tv(&item->di_tv);
6182 vim_free(item);
6183}
6184
6185/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006186 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6187 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006188 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189 * Returns NULL when out of memory.
6190 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006191 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006192dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006195 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006196{
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 dict_T *copy;
6198 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006199 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006201
6202 if (orig == NULL)
6203 return NULL;
6204
6205 copy = dict_alloc();
6206 if (copy != NULL)
6207 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006208 if (copyID != 0)
6209 {
6210 orig->dv_copyID = copyID;
6211 orig->dv_copydict = copy;
6212 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006214 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006215 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006216 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006218 --todo;
6219
6220 di = dictitem_alloc(hi->hi_key);
6221 if (di == NULL)
6222 break;
6223 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006224 {
6225 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6226 copyID) == FAIL)
6227 {
6228 vim_free(di);
6229 break;
6230 }
6231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006232 else
6233 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6234 if (dict_add(copy, di) == FAIL)
6235 {
6236 dictitem_free(di);
6237 break;
6238 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006241
Bram Moolenaare9a41262005-01-15 22:18:47 +00006242 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006243 if (todo > 0)
6244 {
6245 dict_unref(copy);
6246 copy = NULL;
6247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006248 }
6249
6250 return copy;
6251}
6252
6253/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006254 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006255 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006257 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006258dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 dict_T *d;
6260 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263}
6264
Bram Moolenaar8c711452005-01-14 21:53:12 +00006265/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006266 * Add a number or string entry to dictionary "d".
6267 * When "str" is NULL use number "nr", otherwise use "str".
6268 * Returns FAIL when out of memory and when key already exists.
6269 */
6270 int
6271dict_add_nr_str(d, key, nr, str)
6272 dict_T *d;
6273 char *key;
6274 long nr;
6275 char_u *str;
6276{
6277 dictitem_T *item;
6278
6279 item = dictitem_alloc((char_u *)key);
6280 if (item == NULL)
6281 return FAIL;
6282 item->di_tv.v_lock = 0;
6283 if (str == NULL)
6284 {
6285 item->di_tv.v_type = VAR_NUMBER;
6286 item->di_tv.vval.v_number = nr;
6287 }
6288 else
6289 {
6290 item->di_tv.v_type = VAR_STRING;
6291 item->di_tv.vval.v_string = vim_strsave(str);
6292 }
6293 if (dict_add(d, item) == FAIL)
6294 {
6295 dictitem_free(item);
6296 return FAIL;
6297 }
6298 return OK;
6299}
6300
6301/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006302 * Get the number of items in a Dictionary.
6303 */
6304 static long
6305dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308 if (d == NULL)
6309 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006311}
6312
6313/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314 * Find item "key[len]" in Dictionary "d".
6315 * If "len" is negative use strlen(key).
6316 * Returns NULL when not found.
6317 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006319dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321 char_u *key;
6322 int len;
6323{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324#define AKEYLEN 200
6325 char_u buf[AKEYLEN];
6326 char_u *akey;
6327 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006328 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 if (len < 0)
6331 akey = key;
6332 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334 tofree = akey = vim_strnsave(key, len);
6335 if (akey == NULL)
6336 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006337 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006338 else
6339 {
6340 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006341 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006342 akey = buf;
6343 }
6344
Bram Moolenaar33570922005-01-25 22:26:29 +00006345 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 vim_free(tofree);
6347 if (HASHITEM_EMPTY(hi))
6348 return NULL;
6349 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350}
6351
6352/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006353 * Get a string item from a dictionary.
6354 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006355 * Returns NULL if the entry doesn't exist or out of memory.
6356 */
6357 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006358get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006359 dict_T *d;
6360 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006361 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006362{
6363 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006364 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006365
6366 di = dict_find(d, key, -1);
6367 if (di == NULL)
6368 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006369 s = get_tv_string(&di->di_tv);
6370 if (save && s != NULL)
6371 s = vim_strsave(s);
6372 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006373}
6374
6375/*
6376 * Get a number item from a dictionary.
6377 * Returns 0 if the entry doesn't exist or out of memory.
6378 */
6379 long
6380get_dict_number(d, key)
6381 dict_T *d;
6382 char_u *key;
6383{
6384 dictitem_T *di;
6385
6386 di = dict_find(d, key, -1);
6387 if (di == NULL)
6388 return 0;
6389 return get_tv_number(&di->di_tv);
6390}
6391
6392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006393 * Return an allocated string with the string representation of a Dictionary.
6394 * May return NULL.
6395 */
6396 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006397dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006399 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006400{
6401 garray_T ga;
6402 int first = TRUE;
6403 char_u *tofree;
6404 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006411 return NULL;
6412 ga_init2(&ga, (int)sizeof(char), 80);
6413 ga_append(&ga, '{');
6414
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006416 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006420 --todo;
6421
6422 if (first)
6423 first = FALSE;
6424 else
6425 ga_concat(&ga, (char_u *)", ");
6426
6427 tofree = string_quote(hi->hi_key, FALSE);
6428 if (tofree != NULL)
6429 {
6430 ga_concat(&ga, tofree);
6431 vim_free(tofree);
6432 }
6433 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006434 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006435 if (s != NULL)
6436 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006437 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 if (s == NULL)
6439 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006441 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006442 if (todo > 0)
6443 {
6444 vim_free(ga.ga_data);
6445 return NULL;
6446 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006447
6448 ga_append(&ga, '}');
6449 ga_append(&ga, NUL);
6450 return (char_u *)ga.ga_data;
6451}
6452
6453/*
6454 * Allocate a variable for a Dictionary and fill it from "*arg".
6455 * Return OK or FAIL. Returns NOTDONE for {expr}.
6456 */
6457 static int
6458get_dict_tv(arg, rettv, evaluate)
6459 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006460 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461 int evaluate;
6462{
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 dict_T *d = NULL;
6464 typval_T tvkey;
6465 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006469 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006470
6471 /*
6472 * First check if it's not a curly-braces thing: {expr}.
6473 * Must do this without evaluating, otherwise a function may be called
6474 * twice. Unfortunately this means we need to call eval1() twice for the
6475 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006476 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006478 if (*start != '}')
6479 {
6480 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6481 return FAIL;
6482 if (*start == '}')
6483 return NOTDONE;
6484 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006485
6486 if (evaluate)
6487 {
6488 d = dict_alloc();
6489 if (d == NULL)
6490 return FAIL;
6491 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006492 tvkey.v_type = VAR_UNKNOWN;
6493 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494
6495 *arg = skipwhite(*arg + 1);
6496 while (**arg != '}' && **arg != NUL)
6497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 goto failret;
6500 if (**arg != ':')
6501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006502 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006503 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504 goto failret;
6505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006506 key = get_tv_string_buf_chk(&tvkey, buf);
6507 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006509 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6510 if (key != NULL)
6511 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006512 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006513 goto failret;
6514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006515
6516 *arg = skipwhite(*arg + 1);
6517 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006519 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006520 goto failret;
6521 }
6522 if (evaluate)
6523 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006524 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 if (item != NULL)
6526 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006527 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006528 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529 clear_tv(&tv);
6530 goto failret;
6531 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006532 item = dictitem_alloc(key);
6533 clear_tv(&tvkey);
6534 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006536 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006537 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006538 if (dict_add(d, item) == FAIL)
6539 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006540 }
6541 }
6542
6543 if (**arg == '}')
6544 break;
6545 if (**arg != ',')
6546 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006547 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006548 goto failret;
6549 }
6550 *arg = skipwhite(*arg + 1);
6551 }
6552
6553 if (**arg != '}')
6554 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006555 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556failret:
6557 if (evaluate)
6558 dict_free(d);
6559 return FAIL;
6560 }
6561
6562 *arg = skipwhite(*arg + 1);
6563 if (evaluate)
6564 {
6565 rettv->v_type = VAR_DICT;
6566 rettv->vval.v_dict = d;
6567 ++d->dv_refcount;
6568 }
6569
6570 return OK;
6571}
6572
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * Return a string with the string representation of a variable.
6575 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006577 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * May return NULL;
6580 */
6581 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006582echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006588 static int recurse = 0;
6589 char_u *r = NULL;
6590
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006593 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006594 *tofree = NULL;
6595 return NULL;
6596 }
6597 ++recurse;
6598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 switch (tv->v_type)
6600 {
6601 case VAR_FUNC:
6602 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006603 r = tv->vval.v_string;
6604 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006605
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006607 if (tv->vval.v_list == NULL)
6608 {
6609 *tofree = NULL;
6610 r = NULL;
6611 }
6612 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6613 {
6614 *tofree = NULL;
6615 r = (char_u *)"[...]";
6616 }
6617 else
6618 {
6619 tv->vval.v_list->lv_copyID = copyID;
6620 *tofree = list2string(tv, copyID);
6621 r = *tofree;
6622 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006623 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006624
Bram Moolenaar8c711452005-01-14 21:53:12 +00006625 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626 if (tv->vval.v_dict == NULL)
6627 {
6628 *tofree = NULL;
6629 r = NULL;
6630 }
6631 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6632 {
6633 *tofree = NULL;
6634 r = (char_u *)"{...}";
6635 }
6636 else
6637 {
6638 tv->vval.v_dict->dv_copyID = copyID;
6639 *tofree = dict2string(tv, copyID);
6640 r = *tofree;
6641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006644 case VAR_STRING:
6645 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006646 *tofree = NULL;
6647 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006650 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006651 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006652 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006653 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006654
6655 --recurse;
6656 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006657}
6658
6659/*
6660 * Return a string with the string representation of a variable.
6661 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6662 * "numbuf" is used for a number.
6663 * Puts quotes around strings, so that they can be parsed back by eval().
6664 * May return NULL;
6665 */
6666 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006667tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006669 char_u **tofree;
6670 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006671 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672{
6673 switch (tv->v_type)
6674 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006675 case VAR_FUNC:
6676 *tofree = string_quote(tv->vval.v_string, TRUE);
6677 return *tofree;
6678 case VAR_STRING:
6679 *tofree = string_quote(tv->vval.v_string, FALSE);
6680 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006681 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006684 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006686 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006688 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006689}
6690
6691/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006692 * Return string "str" in ' quotes, doubling ' characters.
6693 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006694 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 */
6696 static char_u *
6697string_quote(str, function)
6698 char_u *str;
6699 int function;
6700{
Bram Moolenaar33570922005-01-25 22:26:29 +00006701 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702 char_u *p, *r, *s;
6703
Bram Moolenaar33570922005-01-25 22:26:29 +00006704 len = (function ? 13 : 3);
6705 if (str != NULL)
6706 {
6707 len += STRLEN(str);
6708 for (p = str; *p != NUL; mb_ptr_adv(p))
6709 if (*p == '\'')
6710 ++len;
6711 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712 s = r = alloc(len);
6713 if (r != NULL)
6714 {
6715 if (function)
6716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006717 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006718 r += 10;
6719 }
6720 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006721 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006722 if (str != NULL)
6723 for (p = str; *p != NUL; )
6724 {
6725 if (*p == '\'')
6726 *r++ = '\'';
6727 MB_COPY_CHAR(p, r);
6728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006729 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730 if (function)
6731 *r++ = ')';
6732 *r++ = NUL;
6733 }
6734 return s;
6735}
6736
6737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 * Get the value of an environment variable.
6739 * "arg" is pointing to the '$'. It is advanced to after the name.
6740 * If the environment variable was not set, silently assume it is empty.
6741 * Always return OK.
6742 */
6743 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006744get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 int evaluate;
6748{
6749 char_u *string = NULL;
6750 int len;
6751 int cc;
6752 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006753 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
6755 ++*arg;
6756 name = *arg;
6757 len = get_env_len(arg);
6758 if (evaluate)
6759 {
6760 if (len != 0)
6761 {
6762 cc = name[len];
6763 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006764 /* first try vim_getenv(), fast for normal environment vars */
6765 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006767 {
6768 if (!mustfree)
6769 string = vim_strsave(string);
6770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 else
6772 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006773 if (mustfree)
6774 vim_free(string);
6775
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 /* next try expanding things like $VIM and ${HOME} */
6777 string = expand_env_save(name - 1);
6778 if (string != NULL && *string == '$')
6779 {
6780 vim_free(string);
6781 string = NULL;
6782 }
6783 }
6784 name[len] = cc;
6785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006786 rettv->v_type = VAR_STRING;
6787 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 }
6789
6790 return OK;
6791}
6792
6793/*
6794 * Array with names and number of arguments of all internal functions
6795 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6796 */
6797static struct fst
6798{
6799 char *f_name; /* function name */
6800 char f_min_argc; /* minimal number of arguments */
6801 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006802 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006803 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804} functions[] =
6805{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006806 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {"append", 2, 2, f_append},
6808 {"argc", 0, 0, f_argc},
6809 {"argidx", 0, 0, f_argidx},
6810 {"argv", 1, 1, f_argv},
6811 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006812 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {"bufexists", 1, 1, f_bufexists},
6814 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6815 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6816 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6817 {"buflisted", 1, 1, f_buflisted},
6818 {"bufloaded", 1, 1, f_bufloaded},
6819 {"bufname", 1, 1, f_bufname},
6820 {"bufnr", 1, 1, f_bufnr},
6821 {"bufwinnr", 1, 1, f_bufwinnr},
6822 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006823 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {"char2nr", 1, 1, f_char2nr},
6826 {"cindent", 1, 1, f_cindent},
6827 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006828#if defined(FEAT_INS_EXPAND)
6829 {"complete_add", 1, 1, f_complete_add},
6830 {"complete_check", 0, 0, f_complete_check},
6831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006834 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {"cscope_connection",0,3, f_cscope_connection},
6836 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006837 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {"delete", 1, 1, f_delete},
6839 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006840 {"diff_filler", 1, 1, f_diff_filler},
6841 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006842 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006844 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {"eventhandler", 0, 0, f_eventhandler},
6846 {"executable", 1, 1, f_executable},
6847 {"exists", 1, 1, f_exists},
6848 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006849 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6851 {"filereadable", 1, 1, f_filereadable},
6852 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006853 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006854 {"finddir", 1, 3, f_finddir},
6855 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {"fnamemodify", 2, 2, f_fnamemodify},
6857 {"foldclosed", 1, 1, f_foldclosed},
6858 {"foldclosedend", 1, 1, f_foldclosedend},
6859 {"foldlevel", 1, 1, f_foldlevel},
6860 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006861 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006863 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006865 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006866 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {"getbufvar", 2, 2, f_getbufvar},
6868 {"getchar", 0, 1, f_getchar},
6869 {"getcharmod", 0, 0, f_getcharmod},
6870 {"getcmdline", 0, 0, f_getcmdline},
6871 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006872 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006874 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006875 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {"getfsize", 1, 1, f_getfsize},
6877 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006878 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006879 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006880 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006881 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006882 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {"getregtype", 0, 1, f_getregtype},
6884 {"getwinposx", 0, 0, f_getwinposx},
6885 {"getwinposy", 0, 0, f_getwinposy},
6886 {"getwinvar", 2, 2, f_getwinvar},
6887 {"glob", 1, 1, f_glob},
6888 {"globpath", 2, 2, f_globpath},
6889 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006890 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {"hasmapto", 1, 2, f_hasmapto},
6892 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6893 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6894 {"histadd", 2, 2, f_histadd},
6895 {"histdel", 1, 2, f_histdel},
6896 {"histget", 1, 2, f_histget},
6897 {"histnr", 1, 1, f_histnr},
6898 {"hlID", 1, 1, f_hlID},
6899 {"hlexists", 1, 1, f_hlexists},
6900 {"hostname", 0, 0, f_hostname},
6901 {"iconv", 3, 3, f_iconv},
6902 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006904 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006906 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {"inputrestore", 0, 0, f_inputrestore},
6908 {"inputsave", 0, 0, f_inputsave},
6909 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006910 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006912 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006913 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006914 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006915 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006917 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {"libcall", 3, 3, f_libcall},
6919 {"libcallnr", 3, 3, f_libcallnr},
6920 {"line", 1, 1, f_line},
6921 {"line2byte", 1, 1, f_line2byte},
6922 {"lispindent", 1, 1, f_lispindent},
6923 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006924 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {"maparg", 1, 2, f_maparg},
6926 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006927 {"match", 2, 4, f_match},
6928 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006929 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006930 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006931 {"max", 1, 1, f_max},
6932 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006933#ifdef vim_mkdir
6934 {"mkdir", 1, 3, f_mkdir},
6935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {"mode", 0, 0, f_mode},
6937 {"nextnonblank", 1, 1, f_nextnonblank},
6938 {"nr2char", 1, 1, f_nr2char},
6939 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006940 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006941 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006942 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006943 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {"remote_expr", 2, 3, f_remote_expr},
6945 {"remote_foreground", 1, 1, f_remote_foreground},
6946 {"remote_peek", 1, 2, f_remote_peek},
6947 {"remote_read", 1, 1, f_remote_read},
6948 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006949 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006951 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006953 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006955 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {"searchpair", 3, 5, f_searchpair},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006957 {"searchpairpos", 3, 5, f_searchpairpos},
6958 {"searchpos", 1, 2, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {"server2client", 2, 2, f_server2client},
6960 {"serverlist", 0, 0, f_serverlist},
6961 {"setbufvar", 3, 3, f_setbufvar},
6962 {"setcmdpos", 1, 1, f_setcmdpos},
6963 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00006964 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006965 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {"setreg", 2, 3, f_setreg},
6967 {"setwinvar", 3, 3, f_setwinvar},
6968 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006969 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006970 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006971 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006972 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006973 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974#ifdef HAVE_STRFTIME
6975 {"strftime", 1, 2, f_strftime},
6976#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006978 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"strlen", 1, 1, f_strlen},
6980 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006981 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {"strtrans", 1, 1, f_strtrans},
6983 {"submatch", 1, 1, f_submatch},
6984 {"substitute", 4, 4, f_substitute},
6985 {"synID", 3, 3, f_synID},
6986 {"synIDattr", 2, 3, f_synIDattr},
6987 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006988 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006989 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006990 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006991 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006992 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006993 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006995 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {"tolower", 1, 1, f_tolower},
6997 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006998 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007000 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {"virtcol", 1, 1, f_virtcol},
7002 {"visualmode", 0, 1, f_visualmode},
7003 {"winbufnr", 1, 1, f_winbufnr},
7004 {"wincol", 0, 0, f_wincol},
7005 {"winheight", 1, 1, f_winheight},
7006 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007007 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {"winrestcmd", 0, 0, f_winrestcmd},
7009 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007010 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011};
7012
7013#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7014
7015/*
7016 * Function given to ExpandGeneric() to obtain the list of internal
7017 * or user defined function names.
7018 */
7019 char_u *
7020get_function_name(xp, idx)
7021 expand_T *xp;
7022 int idx;
7023{
7024 static int intidx = -1;
7025 char_u *name;
7026
7027 if (idx == 0)
7028 intidx = -1;
7029 if (intidx < 0)
7030 {
7031 name = get_user_func_name(xp, idx);
7032 if (name != NULL)
7033 return name;
7034 }
7035 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7036 {
7037 STRCPY(IObuff, functions[intidx].f_name);
7038 STRCAT(IObuff, "(");
7039 if (functions[intidx].f_max_argc == 0)
7040 STRCAT(IObuff, ")");
7041 return IObuff;
7042 }
7043
7044 return NULL;
7045}
7046
7047/*
7048 * Function given to ExpandGeneric() to obtain the list of internal or
7049 * user defined variable or function names.
7050 */
7051/*ARGSUSED*/
7052 char_u *
7053get_expr_name(xp, idx)
7054 expand_T *xp;
7055 int idx;
7056{
7057 static int intidx = -1;
7058 char_u *name;
7059
7060 if (idx == 0)
7061 intidx = -1;
7062 if (intidx < 0)
7063 {
7064 name = get_function_name(xp, idx);
7065 if (name != NULL)
7066 return name;
7067 }
7068 return get_user_var_name(xp, ++intidx);
7069}
7070
7071#endif /* FEAT_CMDL_COMPL */
7072
7073/*
7074 * Find internal function in table above.
7075 * Return index, or -1 if not found
7076 */
7077 static int
7078find_internal_func(name)
7079 char_u *name; /* name of the function */
7080{
7081 int first = 0;
7082 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7083 int cmp;
7084 int x;
7085
7086 /*
7087 * Find the function name in the table. Binary search.
7088 */
7089 while (first <= last)
7090 {
7091 x = first + ((unsigned)(last - first) >> 1);
7092 cmp = STRCMP(name, functions[x].f_name);
7093 if (cmp < 0)
7094 last = x - 1;
7095 else if (cmp > 0)
7096 first = x + 1;
7097 else
7098 return x;
7099 }
7100 return -1;
7101}
7102
7103/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007104 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7105 * name it contains, otherwise return "name".
7106 */
7107 static char_u *
7108deref_func_name(name, lenp)
7109 char_u *name;
7110 int *lenp;
7111{
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007113 int cc;
7114
7115 cc = name[*lenp];
7116 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007117 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007118 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007120 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007122 {
7123 *lenp = 0;
7124 return (char_u *)""; /* just in case */
7125 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 *lenp = STRLEN(v->di_tv.vval.v_string);
7127 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007128 }
7129
7130 return name;
7131}
7132
7133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 * Allocate a variable for the result of a function.
7135 * Return OK or FAIL.
7136 */
7137 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7139 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 char_u *name; /* name of the function */
7141 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 char_u **arg; /* argument, pointing to the '(' */
7144 linenr_T firstline; /* first line of range */
7145 linenr_T lastline; /* last line of range */
7146 int *doesrange; /* return: function handled range */
7147 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 char_u *argp;
7151 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 int argcount = 0; /* number of arguments found */
7154
7155 /*
7156 * Get the arguments.
7157 */
7158 argp = *arg;
7159 while (argcount < MAX_FUNC_ARGS)
7160 {
7161 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7162 if (*argp == ')' || *argp == ',' || *argp == NUL)
7163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7165 {
7166 ret = FAIL;
7167 break;
7168 }
7169 ++argcount;
7170 if (*argp != ',')
7171 break;
7172 }
7173 if (*argp == ')')
7174 ++argp;
7175 else
7176 ret = FAIL;
7177
7178 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007179 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 {
7183 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007186 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
7189 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007190 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
7192 *arg = skipwhite(argp);
7193 return ret;
7194}
7195
7196
7197/*
7198 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007199 * Return OK when the function can't be called, FAIL otherwise.
7200 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 */
7202 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007203call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 char_u *name; /* name of the function */
7206 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 linenr_T firstline; /* first line of range */
7211 linenr_T lastline; /* last line of range */
7212 int *doesrange; /* return: function handled range */
7213 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217#define ERROR_UNKNOWN 0
7218#define ERROR_TOOMANY 1
7219#define ERROR_TOOFEW 2
7220#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007221#define ERROR_DICT 4
7222#define ERROR_NONE 5
7223#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 int error = ERROR_NONE;
7225 int i;
7226 int llen;
7227 ufunc_T *fp;
7228 int cc;
7229#define FLEN_FIXED 40
7230 char_u fname_buf[FLEN_FIXED + 1];
7231 char_u *fname;
7232
7233 /*
7234 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7235 * Change <SNR>123_name() to K_SNR 123_name().
7236 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7237 */
7238 cc = name[len];
7239 name[len] = NUL;
7240 llen = eval_fname_script(name);
7241 if (llen > 0)
7242 {
7243 fname_buf[0] = K_SPECIAL;
7244 fname_buf[1] = KS_EXTRA;
7245 fname_buf[2] = (int)KE_SNR;
7246 i = 3;
7247 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7248 {
7249 if (current_SID <= 0)
7250 error = ERROR_SCRIPT;
7251 else
7252 {
7253 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7254 i = (int)STRLEN(fname_buf);
7255 }
7256 }
7257 if (i + STRLEN(name + llen) < FLEN_FIXED)
7258 {
7259 STRCPY(fname_buf + i, name + llen);
7260 fname = fname_buf;
7261 }
7262 else
7263 {
7264 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7265 if (fname == NULL)
7266 error = ERROR_OTHER;
7267 else
7268 {
7269 mch_memmove(fname, fname_buf, (size_t)i);
7270 STRCPY(fname + i, name + llen);
7271 }
7272 }
7273 }
7274 else
7275 fname = name;
7276
7277 *doesrange = FALSE;
7278
7279
7280 /* execute the function if no errors detected and executing */
7281 if (evaluate && error == ERROR_NONE)
7282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007283 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 error = ERROR_UNKNOWN;
7285
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007286 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
7288 /*
7289 * User defined function.
7290 */
7291 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007292
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007294 /* Trigger FuncUndefined event, may load the function. */
7295 if (fp == NULL
7296 && apply_autocmds(EVENT_FUNCUNDEFINED,
7297 fname, fname, TRUE, NULL)
7298 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007300 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 fp = find_func(fname);
7302 }
7303#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007304 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007305 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007306 {
7307 /* loaded a package, search for the function again */
7308 fp = find_func(fname);
7309 }
7310
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 if (fp != NULL)
7312 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007313 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007315 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007317 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007319 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 else
7322 {
7323 /*
7324 * Call the user function.
7325 * Save and restore search patterns, script variables and
7326 * redo buffer.
7327 */
7328 save_search_patterns();
7329 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007330 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007331 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007333 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7334 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7335 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007336 /* Function was unreferenced while being used, free it
7337 * now. */
7338 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 restoreRedobuff();
7340 restore_search_patterns();
7341 error = ERROR_NONE;
7342 }
7343 }
7344 }
7345 else
7346 {
7347 /*
7348 * Find the function name in the table, call its implementation.
7349 */
7350 i = find_internal_func(fname);
7351 if (i >= 0)
7352 {
7353 if (argcount < functions[i].f_min_argc)
7354 error = ERROR_TOOFEW;
7355 else if (argcount > functions[i].f_max_argc)
7356 error = ERROR_TOOMANY;
7357 else
7358 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007359 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007360 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 error = ERROR_NONE;
7362 }
7363 }
7364 }
7365 /*
7366 * The function call (or "FuncUndefined" autocommand sequence) might
7367 * have been aborted by an error, an interrupt, or an explicitly thrown
7368 * exception that has not been caught so far. This situation can be
7369 * tested for by calling aborting(). For an error in an internal
7370 * function or for the "E132" error in call_user_func(), however, the
7371 * throw point at which the "force_abort" flag (temporarily reset by
7372 * emsg()) is normally updated has not been reached yet. We need to
7373 * update that flag first to make aborting() reliable.
7374 */
7375 update_force_abort();
7376 }
7377 if (error == ERROR_NONE)
7378 ret = OK;
7379
7380 /*
7381 * Report an error unless the argument evaluation or function call has been
7382 * cancelled due to an aborting error, an interrupt, or an exception.
7383 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007384 if (!aborting())
7385 {
7386 switch (error)
7387 {
7388 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 break;
7391 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007392 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 break;
7394 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 name);
7397 break;
7398 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 name);
7401 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007403 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 name);
7405 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 }
7407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
7409 name[len] = cc;
7410 if (fname != name && fname != fname_buf)
7411 vim_free(fname);
7412
7413 return ret;
7414}
7415
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007416/*
7417 * Give an error message with a function name. Handle <SNR> things.
7418 */
7419 static void
7420emsg_funcname(msg, name)
7421 char *msg;
7422 char_u *name;
7423{
7424 char_u *p;
7425
7426 if (*name == K_SPECIAL)
7427 p = concat_str((char_u *)"<SNR>", name + 3);
7428 else
7429 p = name;
7430 EMSG2(_(msg), p);
7431 if (p != name)
7432 vim_free(p);
7433}
7434
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435/*********************************************
7436 * Implementation of the built-in functions
7437 */
7438
7439/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007440 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 */
7442 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007443f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 typval_T *argvars;
7445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446{
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007449 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007450 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007452 if ((l = argvars[0].vval.v_list) != NULL
7453 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7454 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 }
7457 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007458 EMSG(_(e_listreq));
7459}
7460
7461/*
7462 * "append(lnum, string/list)" function
7463 */
7464 static void
7465f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007466 typval_T *argvars;
7467 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007468{
7469 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 list_T *l = NULL;
7472 listitem_T *li = NULL;
7473 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007474 long added = 0;
7475
Bram Moolenaar0d660222005-01-07 21:51:51 +00007476 lnum = get_tv_lnum(argvars);
7477 if (lnum >= 0
7478 && lnum <= curbuf->b_ml.ml_line_count
7479 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007480 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007481 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007483 l = argvars[1].vval.v_list;
7484 if (l == NULL)
7485 return;
7486 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007488 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007489 for (;;)
7490 {
7491 if (l == NULL)
7492 tv = &argvars[1]; /* append a string */
7493 else if (li == NULL)
7494 break; /* end of list */
7495 else
7496 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007497 line = get_tv_string_chk(tv);
7498 if (line == NULL) /* type error */
7499 {
7500 rettv->vval.v_number = 1; /* Failed */
7501 break;
7502 }
7503 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007504 ++added;
7505 if (l == NULL)
7506 break;
7507 li = li->li_next;
7508 }
7509
7510 appended_lines_mark(lnum, added);
7511 if (curwin->w_cursor.lnum > lnum)
7512 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007514 else
7515 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516}
7517
7518/*
7519 * "argc()" function
7520 */
7521/* ARGSUSED */
7522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007523f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 typval_T *argvars;
7525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007527 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528}
7529
7530/*
7531 * "argidx()" function
7532 */
7533/* ARGSUSED */
7534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007535f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007536 typval_T *argvars;
7537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540}
7541
7542/*
7543 * "argv(nr)" function
7544 */
7545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007546f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 typval_T *argvars;
7548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
7550 int idx;
7551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007552 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007554 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556 rettv->vval.v_string = NULL;
7557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558}
7559
7560/*
7561 * "browse(save, title, initdir, default)" function
7562 */
7563/* ARGSUSED */
7564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *argvars;
7567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
7569#ifdef FEAT_BROWSE
7570 int save;
7571 char_u *title;
7572 char_u *initdir;
7573 char_u *defname;
7574 char_u buf[NUMBUFLEN];
7575 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007576 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007578 save = get_tv_number_chk(&argvars[0], &error);
7579 title = get_tv_string_chk(&argvars[1]);
7580 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7581 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007583 if (error || title == NULL || initdir == NULL || defname == NULL)
7584 rettv->vval.v_string = NULL;
7585 else
7586 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007587 do_browse(save ? BROWSE_SAVE : 0,
7588 title, defname, NULL, initdir, NULL, curbuf);
7589#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007590 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007591#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007593}
7594
7595/*
7596 * "browsedir(title, initdir)" function
7597 */
7598/* ARGSUSED */
7599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 typval_T *argvars;
7602 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007603{
7604#ifdef FEAT_BROWSE
7605 char_u *title;
7606 char_u *initdir;
7607 char_u buf[NUMBUFLEN];
7608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007609 title = get_tv_string_chk(&argvars[0]);
7610 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007612 if (title == NULL || initdir == NULL)
7613 rettv->vval.v_string = NULL;
7614 else
7615 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007616 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621}
7622
Bram Moolenaar33570922005-01-25 22:26:29 +00007623static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007624
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625/*
7626 * Find a buffer by number or exact name.
7627 */
7628 static buf_T *
7629find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
7632 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 if (avar->v_type == VAR_NUMBER)
7635 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007636 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007638 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007639 if (buf == NULL)
7640 {
7641 /* No full path name match, try a match with a URL or a "nofile"
7642 * buffer, these don't use the full path. */
7643 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7644 if (buf->b_fname != NULL
7645 && (path_with_url(buf->b_fname)
7646#ifdef FEAT_QUICKFIX
7647 || bt_nofile(buf)
7648#endif
7649 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007651 break;
7652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 }
7654 return buf;
7655}
7656
7657/*
7658 * "bufexists(expr)" function
7659 */
7660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 typval_T *argvars;
7663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
7668/*
7669 * "buflisted(expr)" function
7670 */
7671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *argvars;
7674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 buf_T *buf;
7677
7678 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680}
7681
7682/*
7683 * "bufloaded(expr)" function
7684 */
7685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 typval_T *argvars;
7688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
7690 buf_T *buf;
7691
7692 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694}
7695
Bram Moolenaar33570922005-01-25 22:26:29 +00007696static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007697
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698/*
7699 * Get buffer by number or pattern.
7700 */
7701 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007705 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 int save_magic;
7707 char_u *save_cpo;
7708 buf_T *buf;
7709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710 if (tv->v_type == VAR_NUMBER)
7711 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007712 if (tv->v_type != VAR_STRING)
7713 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 if (name == NULL || *name == NUL)
7715 return curbuf;
7716 if (name[0] == '$' && name[1] == NUL)
7717 return lastbuf;
7718
7719 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7720 save_magic = p_magic;
7721 p_magic = TRUE;
7722 save_cpo = p_cpo;
7723 p_cpo = (char_u *)"";
7724
7725 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7726 TRUE, FALSE));
7727
7728 p_magic = save_magic;
7729 p_cpo = save_cpo;
7730
7731 /* If not found, try expanding the name, like done for bufexists(). */
7732 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734
7735 return buf;
7736}
7737
7738/*
7739 * "bufname(expr)" function
7740 */
7741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 typval_T *argvars;
7744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
7746 buf_T *buf;
7747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007748 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750 buf = get_buf_tv(&argvars[0]);
7751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 --emsg_off;
7757}
7758
7759/*
7760 * "bufnr(expr)" function
7761 */
7762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *argvars;
7765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 buf_T *buf;
7768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007773 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 --emsg_off;
7777}
7778
7779/*
7780 * "bufwinnr(nr)" function
7781 */
7782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007784 typval_T *argvars;
7785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787#ifdef FEAT_WINDOWS
7788 win_T *wp;
7789 int winnr = 0;
7790#endif
7791 buf_T *buf;
7792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007793 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#ifdef FEAT_WINDOWS
7797 for (wp = firstwin; wp; wp = wp->w_next)
7798 {
7799 ++winnr;
7800 if (wp->w_buffer == buf)
7801 break;
7802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007803 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806#endif
7807 --emsg_off;
7808}
7809
7810/*
7811 * "byte2line(byte)" function
7812 */
7813/*ARGSUSED*/
7814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 typval_T *argvars;
7817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821#else
7822 long boff = 0;
7823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007824 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 (linenr_T)0, &boff);
7830#endif
7831}
7832
7833/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007834 * "byteidx()" function
7835 */
7836/*ARGSUSED*/
7837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007838f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 typval_T *argvars;
7840 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007841{
7842#ifdef FEAT_MBYTE
7843 char_u *t;
7844#endif
7845 char_u *str;
7846 long idx;
7847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007848 str = get_tv_string_chk(&argvars[0]);
7849 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007851 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007852 return;
7853
7854#ifdef FEAT_MBYTE
7855 t = str;
7856 for ( ; idx > 0; idx--)
7857 {
7858 if (*t == NUL) /* EOL reached */
7859 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007860 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007863#else
7864 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007866#endif
7867}
7868
7869/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007870 * "call(func, arglist)" function
7871 */
7872 static void
7873f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007874 typval_T *argvars;
7875 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007876{
7877 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007879 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007881 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007883
7884 rettv->vval.v_number = 0;
7885 if (argvars[1].v_type != VAR_LIST)
7886 {
7887 EMSG(_(e_listreq));
7888 return;
7889 }
7890 if (argvars[1].vval.v_list == NULL)
7891 return;
7892
7893 if (argvars[0].v_type == VAR_FUNC)
7894 func = argvars[0].vval.v_string;
7895 else
7896 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007897 if (*func == NUL)
7898 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007899
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 if (argvars[2].v_type != VAR_UNKNOWN)
7901 {
7902 if (argvars[2].v_type != VAR_DICT)
7903 {
7904 EMSG(_(e_dictreq));
7905 return;
7906 }
7907 selfdict = argvars[2].vval.v_dict;
7908 }
7909
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007910 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7911 item = item->li_next)
7912 {
7913 if (argc == MAX_FUNC_ARGS)
7914 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007915 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007916 break;
7917 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007918 /* Make a copy of each argument. This is needed to be able to set
7919 * v_lock to VAR_FIXED in the copy without changing the original list.
7920 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007921 copy_tv(&item->li_tv, &argv[argc++]);
7922 }
7923
7924 if (item == NULL)
7925 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007926 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7927 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007928
7929 /* Free the arguments. */
7930 while (argc > 0)
7931 clear_tv(&argv[--argc]);
7932}
7933
7934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 * "char2nr(string)" function
7936 */
7937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 typval_T *argvars;
7940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
7942#ifdef FEAT_MBYTE
7943 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007944 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 else
7946#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948}
7949
7950/*
7951 * "cindent(lnum)" function
7952 */
7953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007954f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 typval_T *argvars;
7956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957{
7958#ifdef FEAT_CINDENT
7959 pos_T pos;
7960 linenr_T lnum;
7961
7962 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7965 {
7966 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007967 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 curwin->w_cursor = pos;
7969 }
7970 else
7971#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973}
7974
7975/*
7976 * "col(string)" function
7977 */
7978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 typval_T *argvars;
7981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 colnr_T col = 0;
7984 pos_T *fp;
7985
7986 fp = var2fpos(&argvars[0], FALSE);
7987 if (fp != NULL)
7988 {
7989 if (fp->col == MAXCOL)
7990 {
7991 /* '> can be MAXCOL, get the length of the line then */
7992 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7993 col = STRLEN(ml_get(fp->lnum)) + 1;
7994 else
7995 col = MAXCOL;
7996 }
7997 else
7998 {
7999 col = fp->col + 1;
8000#ifdef FEAT_VIRTUALEDIT
8001 /* col(".") when the cursor is on the NUL at the end of the line
8002 * because of "coladd" can be seen as an extra column. */
8003 if (virtual_active() && fp == &curwin->w_cursor)
8004 {
8005 char_u *p = ml_get_cursor();
8006
8007 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8008 curwin->w_virtcol - curwin->w_cursor.coladd))
8009 {
8010# ifdef FEAT_MBYTE
8011 int l;
8012
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008013 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 col += l;
8015# else
8016 if (*p != NUL && p[1] == NUL)
8017 ++col;
8018# endif
8019 }
8020 }
8021#endif
8022 }
8023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025}
8026
Bram Moolenaar572cb562005-08-05 21:35:02 +00008027#if defined(FEAT_INS_EXPAND)
8028/*
8029 * "complete_add()" function
8030 */
8031/*ARGSUSED*/
8032 static void
8033f_complete_add(argvars, rettv)
8034 typval_T *argvars;
8035 typval_T *rettv;
8036{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008037 char_u *word;
8038 char_u *extra = NULL;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008039
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008040 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8041 {
8042 word = get_dict_string(argvars[0].vval.v_dict,
8043 (char_u *)"word", FALSE);
8044 extra = get_dict_string(argvars[0].vval.v_dict,
8045 (char_u *)"menu", FALSE);
8046 }
8047 else
8048 word = get_tv_string_chk(&argvars[0]);
8049 if (word != NULL)
8050 rettv->vval.v_number = ins_compl_add(word, -1, NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008051}
8052
8053/*
8054 * "complete_check()" function
8055 */
8056/*ARGSUSED*/
8057 static void
8058f_complete_check(argvars, rettv)
8059 typval_T *argvars;
8060 typval_T *rettv;
8061{
8062 int saved = RedrawingDisabled;
8063
8064 RedrawingDisabled = 0;
8065 ins_compl_check_keys(0);
8066 rettv->vval.v_number = compl_interrupted;
8067 RedrawingDisabled = saved;
8068}
8069#endif
8070
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071/*
8072 * "confirm(message, buttons[, default [, type]])" function
8073 */
8074/*ARGSUSED*/
8075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008076f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 typval_T *argvars;
8078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
8080#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8081 char_u *message;
8082 char_u *buttons = NULL;
8083 char_u buf[NUMBUFLEN];
8084 char_u buf2[NUMBUFLEN];
8085 int def = 1;
8086 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008087 char_u *typestr;
8088 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008090 message = get_tv_string_chk(&argvars[0]);
8091 if (message == NULL)
8092 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008093 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008095 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8096 if (buttons == NULL)
8097 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008098 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008100 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008103 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8104 if (typestr == NULL)
8105 error = TRUE;
8106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008108 switch (TOUPPER_ASC(*typestr))
8109 {
8110 case 'E': type = VIM_ERROR; break;
8111 case 'Q': type = VIM_QUESTION; break;
8112 case 'I': type = VIM_INFO; break;
8113 case 'W': type = VIM_WARNING; break;
8114 case 'G': type = VIM_GENERIC; break;
8115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
8117 }
8118 }
8119 }
8120
8121 if (buttons == NULL || *buttons == NUL)
8122 buttons = (char_u *)_("&Ok");
8123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008124 if (error)
8125 rettv->vval.v_number = 0;
8126 else
8127 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 def, NULL);
8129#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008130 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131#endif
8132}
8133
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134/*
8135 * "copy()" function
8136 */
8137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008138f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008139 typval_T *argvars;
8140 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008142 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
8145/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008146 * "count()" function
8147 */
8148 static void
8149f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008150 typval_T *argvars;
8151 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008152{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008153 long n = 0;
8154 int ic = FALSE;
8155
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008157 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 listitem_T *li;
8159 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 if ((l = argvars[0].vval.v_list) != NULL)
8163 {
8164 li = l->lv_first;
8165 if (argvars[2].v_type != VAR_UNKNOWN)
8166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008167 int error = FALSE;
8168
8169 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008170 if (argvars[3].v_type != VAR_UNKNOWN)
8171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008172 idx = get_tv_number_chk(&argvars[3], &error);
8173 if (!error)
8174 {
8175 li = list_find(l, idx);
8176 if (li == NULL)
8177 EMSGN(_(e_listidx), idx);
8178 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008180 if (error)
8181 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008182 }
8183
8184 for ( ; li != NULL; li = li->li_next)
8185 if (tv_equal(&li->li_tv, &argvars[1], ic))
8186 ++n;
8187 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 else if (argvars[0].v_type == VAR_DICT)
8190 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 int todo;
8192 dict_T *d;
8193 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008195 if ((d = argvars[0].vval.v_dict) != NULL)
8196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008197 int error = FALSE;
8198
Bram Moolenaare9a41262005-01-15 22:18:47 +00008199 if (argvars[2].v_type != VAR_UNKNOWN)
8200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008201 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008202 if (argvars[3].v_type != VAR_UNKNOWN)
8203 EMSG(_(e_invarg));
8204 }
8205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008206 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008208 {
8209 if (!HASHITEM_EMPTY(hi))
8210 {
8211 --todo;
8212 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8213 ++n;
8214 }
8215 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008216 }
8217 }
8218 else
8219 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008220 rettv->vval.v_number = n;
8221}
8222
8223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8225 *
8226 * Checks the existence of a cscope connection.
8227 */
8228/*ARGSUSED*/
8229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008230f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008231 typval_T *argvars;
8232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234#ifdef FEAT_CSCOPE
8235 int num = 0;
8236 char_u *dbpath = NULL;
8237 char_u *prepend = NULL;
8238 char_u buf[NUMBUFLEN];
8239
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 if (argvars[0].v_type != VAR_UNKNOWN
8241 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008243 num = (int)get_tv_number(&argvars[0]);
8244 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008245 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008249 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252#endif
8253}
8254
8255/*
8256 * "cursor(lnum, col)" function
8257 *
8258 * Moves the cursor to the specified line and column
8259 */
8260/*ARGSUSED*/
8261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 typval_T *argvars;
8264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 long line, col;
8267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008269 col = get_tv_number_chk(&argvars[1], NULL);
8270 if (line < 0 || col < 0)
8271 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 if (line > 0)
8273 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 if (col > 0)
8275 curwin->w_cursor.col = col - 1;
8276#ifdef FEAT_VIRTUALEDIT
8277 curwin->w_cursor.coladd = 0;
8278#endif
8279
8280 /* Make sure the cursor is in a valid position. */
8281 check_cursor();
8282#ifdef FEAT_MBYTE
8283 /* Correct cursor for multi-byte character. */
8284 if (has_mbyte)
8285 mb_adjust_cursor();
8286#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008287
8288 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289}
8290
8291/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 */
8294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008299 int noref = 0;
8300
8301 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008302 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008303 if (noref < 0 || noref > 1)
8304 EMSG(_(e_invarg));
8305 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008306 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307}
8308
8309/*
8310 * "delete()" function
8311 */
8312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008313f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 typval_T *argvars;
8315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
8317 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321}
8322
8323/*
8324 * "did_filetype()" function
8325 */
8326/*ARGSUSED*/
8327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008328f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 typval_T *argvars;
8330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008335 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336#endif
8337}
8338
8339/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008340 * "diff_filler()" function
8341 */
8342/*ARGSUSED*/
8343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 typval_T *argvars;
8346 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008347{
8348#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008350#endif
8351}
8352
8353/*
8354 * "diff_hlID()" function
8355 */
8356/*ARGSUSED*/
8357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 typval_T *argvars;
8360 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008361{
8362#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008364 static linenr_T prev_lnum = 0;
8365 static int changedtick = 0;
8366 static int fnum = 0;
8367 static int change_start = 0;
8368 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008369 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008370 int filler_lines;
8371 int col;
8372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008373 if (lnum < 0) /* ignore type error in {lnum} arg */
8374 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008375 if (lnum != prev_lnum
8376 || changedtick != curbuf->b_changedtick
8377 || fnum != curbuf->b_fnum)
8378 {
8379 /* New line, buffer, change: need to get the values. */
8380 filler_lines = diff_check(curwin, lnum);
8381 if (filler_lines < 0)
8382 {
8383 if (filler_lines == -1)
8384 {
8385 change_start = MAXCOL;
8386 change_end = -1;
8387 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8388 hlID = HLF_ADD; /* added line */
8389 else
8390 hlID = HLF_CHD; /* changed line */
8391 }
8392 else
8393 hlID = HLF_ADD; /* added line */
8394 }
8395 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008396 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008397 prev_lnum = lnum;
8398 changedtick = curbuf->b_changedtick;
8399 fnum = curbuf->b_fnum;
8400 }
8401
8402 if (hlID == HLF_CHD || hlID == HLF_TXD)
8403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008404 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008405 if (col >= change_start && col <= change_end)
8406 hlID = HLF_TXD; /* changed text */
8407 else
8408 hlID = HLF_CHD; /* changed line */
8409 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008410 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008411#endif
8412}
8413
8414/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008415 * "empty({expr})" function
8416 */
8417 static void
8418f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008419 typval_T *argvars;
8420 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008421{
8422 int n;
8423
8424 switch (argvars[0].v_type)
8425 {
8426 case VAR_STRING:
8427 case VAR_FUNC:
8428 n = argvars[0].vval.v_string == NULL
8429 || *argvars[0].vval.v_string == NUL;
8430 break;
8431 case VAR_NUMBER:
8432 n = argvars[0].vval.v_number == 0;
8433 break;
8434 case VAR_LIST:
8435 n = argvars[0].vval.v_list == NULL
8436 || argvars[0].vval.v_list->lv_first == NULL;
8437 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008438 case VAR_DICT:
8439 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008441 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008442 default:
8443 EMSG2(_(e_intern2), "f_empty()");
8444 n = 0;
8445 }
8446
8447 rettv->vval.v_number = n;
8448}
8449
8450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 * "escape({string}, {chars})" function
8452 */
8453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008455 typval_T *argvars;
8456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
8458 char_u buf[NUMBUFLEN];
8459
Bram Moolenaar758711c2005-02-02 23:11:38 +00008460 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8461 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463}
8464
8465/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008466 * "eval()" function
8467 */
8468/*ARGSUSED*/
8469 static void
8470f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 typval_T *argvars;
8472 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008473{
8474 char_u *s;
8475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 s = get_tv_string_chk(&argvars[0]);
8477 if (s != NULL)
8478 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8481 {
8482 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008483 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008485 else if (*s != NUL)
8486 EMSG(_(e_trailing));
8487}
8488
8489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 * "eventhandler()" function
8491 */
8492/*ARGSUSED*/
8493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 typval_T *argvars;
8496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499}
8500
8501/*
8502 * "executable()" function
8503 */
8504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 typval_T *argvars;
8507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510}
8511
8512/*
8513 * "exists()" function
8514 */
8515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008517 typval_T *argvars;
8518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519{
8520 char_u *p;
8521 char_u *name;
8522 int n = FALSE;
8523 int len = 0;
8524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (*p == '$') /* environment variable */
8527 {
8528 /* first try "normal" environment variables (fast) */
8529 if (mch_getenv(p + 1) != NULL)
8530 n = TRUE;
8531 else
8532 {
8533 /* try expanding things like $VIM and ${HOME} */
8534 p = expand_env_save(p);
8535 if (p != NULL && *p != '$')
8536 n = TRUE;
8537 vim_free(p);
8538 }
8539 }
8540 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 else if (*p == '*') /* internal or user defined function */
8543 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008544 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 }
8546 else if (*p == ':')
8547 {
8548 n = cmd_exists(p + 1);
8549 }
8550 else if (*p == '#')
8551 {
8552#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008553 if (p[1] == '#')
8554 n = autocmd_supported(p + 2);
8555 else
8556 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557#endif
8558 }
8559 else /* internal variable */
8560 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008561 char_u *tofree;
8562 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008564 /* get_name_len() takes care of expanding curly braces */
8565 name = p;
8566 len = get_name_len(&p, &tofree, TRUE, FALSE);
8567 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008569 if (tofree != NULL)
8570 name = tofree;
8571 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8572 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008574 /* handle d.key, l[idx], f(expr) */
8575 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8576 if (n)
8577 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
8579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008581 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 }
8583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585}
8586
8587/*
8588 * "expand()" function
8589 */
8590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 typval_T *argvars;
8593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 char_u *s;
8596 int len;
8597 char_u *errormsg;
8598 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8599 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008600 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 rettv->v_type = VAR_STRING;
8603 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (*s == '%' || *s == '#' || *s == '<')
8605 {
8606 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 --emsg_off;
8609 }
8610 else
8611 {
8612 /* When the optional second argument is non-zero, don't remove matches
8613 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008614 if (argvars[1].v_type != VAR_UNKNOWN
8615 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617 if (!error)
8618 {
8619 ExpandInit(&xpc);
8620 xpc.xp_context = EXPAND_FILES;
8621 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8622 ExpandCleanup(&xpc);
8623 }
8624 else
8625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 }
8627}
8628
8629/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008630 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008631 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008632 */
8633 static void
8634f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 typval_T *argvars;
8636 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008637{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008638 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008639 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008640 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 list_T *l1, *l2;
8642 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008643 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008645
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 l1 = argvars[0].vval.v_list;
8647 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008648 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8649 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008650 {
8651 if (argvars[2].v_type != VAR_UNKNOWN)
8652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008653 before = get_tv_number_chk(&argvars[2], &error);
8654 if (error)
8655 return; /* type error; errmsg already given */
8656
Bram Moolenaar758711c2005-02-02 23:11:38 +00008657 if (before == l1->lv_len)
8658 item = NULL;
8659 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008660 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008661 item = list_find(l1, before);
8662 if (item == NULL)
8663 {
8664 EMSGN(_(e_listidx), before);
8665 return;
8666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008667 }
8668 }
8669 else
8670 item = NULL;
8671 list_extend(l1, l2, item);
8672
Bram Moolenaare9a41262005-01-15 22:18:47 +00008673 copy_tv(&argvars[0], rettv);
8674 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008675 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008676 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8677 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 dict_T *d1, *d2;
8679 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008680 char_u *action;
8681 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008683 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684
8685 d1 = argvars[0].vval.v_dict;
8686 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008687 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8688 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008689 {
8690 /* Check the third argument. */
8691 if (argvars[2].v_type != VAR_UNKNOWN)
8692 {
8693 static char *(av[]) = {"keep", "force", "error"};
8694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 action = get_tv_string_chk(&argvars[2]);
8696 if (action == NULL)
8697 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008698 for (i = 0; i < 3; ++i)
8699 if (STRCMP(action, av[i]) == 0)
8700 break;
8701 if (i == 3)
8702 {
8703 EMSGN(_(e_invarg2), action);
8704 return;
8705 }
8706 }
8707 else
8708 action = (char_u *)"force";
8709
8710 /* Go over all entries in the second dict and add them to the
8711 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008712 todo = d2->dv_hashtab.ht_used;
8713 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008715 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008716 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008717 --todo;
8718 di1 = dict_find(d1, hi2->hi_key, -1);
8719 if (di1 == NULL)
8720 {
8721 di1 = dictitem_copy(HI2DI(hi2));
8722 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8723 dictitem_free(di1);
8724 }
8725 else if (*action == 'e')
8726 {
8727 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8728 break;
8729 }
8730 else if (*action == 'f')
8731 {
8732 clear_tv(&di1->di_tv);
8733 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008735 }
8736 }
8737
Bram Moolenaare9a41262005-01-15 22:18:47 +00008738 copy_tv(&argvars[0], rettv);
8739 }
8740 }
8741 else
8742 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008743}
8744
8745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 * "filereadable()" function
8747 */
8748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *argvars;
8751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
8753 FILE *fd;
8754 char_u *p;
8755 int n;
8756
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8759 {
8760 n = TRUE;
8761 fclose(fd);
8762 }
8763 else
8764 n = FALSE;
8765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767}
8768
8769/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008770 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 * rights to write into.
8772 */
8773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *argvars;
8776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008778 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
Bram Moolenaar33570922005-01-25 22:26:29 +00008781static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008782
8783 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008784findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 typval_T *argvars;
8786 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008787 int dir;
8788{
8789#ifdef FEAT_SEARCHPATH
8790 char_u *fname;
8791 char_u *fresult = NULL;
8792 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8793 char_u *p;
8794 char_u pathbuf[NUMBUFLEN];
8795 int count = 1;
8796 int first = TRUE;
8797
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008799
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008800 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008802 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8803 if (p == NULL)
8804 count = -1; /* error */
8805 else
8806 {
8807 if (*p != NUL)
8808 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008810 if (argvars[2].v_type != VAR_UNKNOWN)
8811 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8812 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008813 }
8814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008815 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008817 do
8818 {
8819 vim_free(fresult);
8820 fresult = find_file_in_path_option(first ? fname : NULL,
8821 first ? (int)STRLEN(fname) : 0,
8822 0, first, path, dir, NULL);
8823 first = FALSE;
8824 } while (--count > 0 && fresult != NULL);
8825 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008828#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008830#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008832}
8833
Bram Moolenaar33570922005-01-25 22:26:29 +00008834static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8835static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836
8837/*
8838 * Implementation of map() and filter().
8839 */
8840 static void
8841filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *argvars;
8843 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008844 int map;
8845{
8846 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008847 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 listitem_T *li, *nli;
8849 list_T *l = NULL;
8850 dictitem_T *di;
8851 hashtab_T *ht;
8852 hashitem_T *hi;
8853 dict_T *d = NULL;
8854 typval_T save_val;
8855 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008857 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008858 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008859 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008860
8861 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008862 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008863 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008864 if ((l = argvars[0].vval.v_list) == NULL
8865 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008866 return;
8867 }
8868 else if (argvars[0].v_type == VAR_DICT)
8869 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008870 if ((d = argvars[0].vval.v_dict) == NULL
8871 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008872 return;
8873 }
8874 else
8875 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008876 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 return;
8878 }
8879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008880 expr = get_tv_string_buf_chk(&argvars[1], buf);
8881 /* On type errors, the preceding call has already displayed an error
8882 * message. Avoid a misleading error message for an empty string that
8883 * was not passed as argument. */
8884 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008886 prepare_vimvar(VV_VAL, &save_val);
8887 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008888
Bram Moolenaar280f1262006-01-30 00:14:18 +00008889 /* We reset "called_emsg" to be able to detect whether an error
8890 * occurred during evaluation of the expression. "did_emsg" can't be
8891 * used, because it is reset when calling a function. */
8892 save_called_emsg = called_emsg;
8893 called_emsg = FALSE;
8894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897 prepare_vimvar(VV_KEY, &save_key);
8898 vimvars[VV_KEY].vv_type = VAR_STRING;
8899
8900 ht = &d->dv_hashtab;
8901 hash_lock(ht);
8902 todo = ht->ht_used;
8903 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 if (!HASHITEM_EMPTY(hi))
8906 {
8907 --todo;
8908 di = HI2DI(hi);
8909 if (tv_check_lock(di->di_tv.v_lock, msg))
8910 break;
8911 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008912 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
8913 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008914 break;
8915 if (!map && rem)
8916 dictitem_remove(d, di);
8917 clear_tv(&vimvars[VV_KEY].vv_tv);
8918 }
8919 }
8920 hash_unlock(ht);
8921
8922 restore_vimvar(VV_KEY, &save_key);
8923 }
8924 else
8925 {
8926 for (li = l->lv_first; li != NULL; li = nli)
8927 {
8928 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008929 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008930 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008931 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
8932 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008933 break;
8934 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008936 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00008940
8941 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008943
8944 copy_tv(&argvars[0], rettv);
8945}
8946
8947 static int
8948filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008950 char_u *expr;
8951 int map;
8952 int *remp;
8953{
Bram Moolenaar33570922005-01-25 22:26:29 +00008954 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008955 char_u *s;
8956
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008958 s = expr;
8959 if (eval1(&s, &rettv, TRUE) == FAIL)
8960 return FAIL;
8961 if (*s != NUL) /* check for trailing chars after expr */
8962 {
8963 EMSG2(_(e_invexpr2), s);
8964 return FAIL;
8965 }
8966 if (map)
8967 {
8968 /* map(): replace the list item value */
8969 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008970 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 *tv = rettv;
8972 }
8973 else
8974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 int error = FALSE;
8976
Bram Moolenaare9a41262005-01-15 22:18:47 +00008977 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 /* On type error, nothing has been removed; return FAIL to stop the
8981 * loop. The error message was given by get_tv_number_chk(). */
8982 if (error)
8983 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008984 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008985 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008986 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008987}
8988
8989/*
8990 * "filter()" function
8991 */
8992 static void
8993f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 typval_T *argvars;
8995 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008996{
8997 filter_map(argvars, rettv, FALSE);
8998}
8999
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001 * "finddir({fname}[, {path}[, {count}]])" function
9002 */
9003 static void
9004f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009007{
9008 findfilendir(argvars, rettv, TRUE);
9009}
9010
9011/*
9012 * "findfile({fname}[, {path}[, {count}]])" function
9013 */
9014 static void
9015f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009016 typval_T *argvars;
9017 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009018{
9019 findfilendir(argvars, rettv, FALSE);
9020}
9021
9022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 * "fnamemodify({fname}, {mods})" function
9024 */
9025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 typval_T *argvars;
9028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
9030 char_u *fname;
9031 char_u *mods;
9032 int usedlen = 0;
9033 int len;
9034 char_u *fbuf = NULL;
9035 char_u buf[NUMBUFLEN];
9036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009037 fname = get_tv_string_chk(&argvars[0]);
9038 mods = get_tv_string_buf_chk(&argvars[1], buf);
9039 if (fname == NULL || mods == NULL)
9040 fname = NULL;
9041 else
9042 {
9043 len = (int)STRLEN(fname);
9044 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 vim_free(fbuf);
9053}
9054
Bram Moolenaar33570922005-01-25 22:26:29 +00009055static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
9057/*
9058 * "foldclosed()" function
9059 */
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 int end;
9065{
9066#ifdef FEAT_FOLDING
9067 linenr_T lnum;
9068 linenr_T first, last;
9069
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9072 {
9073 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9074 {
9075 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 return;
9080 }
9081 }
9082#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084}
9085
9086/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009087 * "foldclosed()" function
9088 */
9089 static void
9090f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 typval_T *argvars;
9092 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009093{
9094 foldclosed_both(argvars, rettv, FALSE);
9095}
9096
9097/*
9098 * "foldclosedend()" function
9099 */
9100 static void
9101f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 typval_T *argvars;
9103 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009104{
9105 foldclosed_both(argvars, rettv, TRUE);
9106}
9107
9108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 * "foldlevel()" function
9110 */
9111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009113 typval_T *argvars;
9114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
9116#ifdef FEAT_FOLDING
9117 linenr_T lnum;
9118
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 else
9123#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * "foldtext()" function
9129 */
9130/*ARGSUSED*/
9131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132f_foldtext(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{
9136#ifdef FEAT_FOLDING
9137 linenr_T lnum;
9138 char_u *s;
9139 char_u *r;
9140 int len;
9141 char *txt;
9142#endif
9143
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->v_type = VAR_STRING;
9145 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009147 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9148 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9149 <= curbuf->b_ml.ml_line_count
9150 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 {
9152 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009153 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9154 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 {
9156 if (!linewhite(lnum))
9157 break;
9158 ++lnum;
9159 }
9160
9161 /* Find interesting text in this line. */
9162 s = skipwhite(ml_get(lnum));
9163 /* skip C comment-start */
9164 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009167 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009168 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009169 {
9170 s = skipwhite(ml_get(lnum + 1));
9171 if (*s == '*')
9172 s = skipwhite(s + 1);
9173 }
9174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 txt = _("+-%s%3ld lines: ");
9176 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009177 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 + 20 /* for %3ld */
9179 + STRLEN(s))); /* concatenated */
9180 if (r != NULL)
9181 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009182 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9183 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9184 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 len = (int)STRLEN(r);
9186 STRCAT(r, s);
9187 /* remove 'foldmarker' and 'commentstring' */
9188 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 }
9191 }
9192#endif
9193}
9194
9195/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009196 * "foldtextresult(lnum)" function
9197 */
9198/*ARGSUSED*/
9199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009201 typval_T *argvars;
9202 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009203{
9204#ifdef FEAT_FOLDING
9205 linenr_T lnum;
9206 char_u *text;
9207 char_u buf[51];
9208 foldinfo_T foldinfo;
9209 int fold_count;
9210#endif
9211
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212 rettv->v_type = VAR_STRING;
9213 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009214#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009216 /* treat illegal types and illegal string values for {lnum} the same */
9217 if (lnum < 0)
9218 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009219 fold_count = foldedCount(curwin, lnum, &foldinfo);
9220 if (fold_count > 0)
9221 {
9222 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9223 &foldinfo, buf);
9224 if (text == buf)
9225 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009227 }
9228#endif
9229}
9230
9231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 * "foreground()" function
9233 */
9234/*ARGSUSED*/
9235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009236f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009237 typval_T *argvars;
9238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241#ifdef FEAT_GUI
9242 if (gui.in_use)
9243 gui_mch_set_foreground();
9244#else
9245# ifdef WIN32
9246 win32_set_foreground();
9247# endif
9248#endif
9249}
9250
9251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009252 * "function()" function
9253 */
9254/*ARGSUSED*/
9255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009257 typval_T *argvars;
9258 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009259{
9260 char_u *s;
9261
Bram Moolenaara7043832005-01-21 11:56:39 +00009262 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009263 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009264 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009265 EMSG2(_(e_invarg2), s);
9266 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009267 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009268 else
9269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009270 rettv->vval.v_string = vim_strsave(s);
9271 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009272 }
9273}
9274
9275/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009276 * "garbagecollect()" function
9277 */
9278/*ARGSUSED*/
9279 static void
9280f_garbagecollect(argvars, rettv)
9281 typval_T *argvars;
9282 typval_T *rettv;
9283{
9284 garbage_collect();
9285}
9286
9287/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009288 * "get()" function
9289 */
9290 static void
9291f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009292 typval_T *argvars;
9293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009294{
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 listitem_T *li;
9296 list_T *l;
9297 dictitem_T *di;
9298 dict_T *d;
9299 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009300
Bram Moolenaare9a41262005-01-15 22:18:47 +00009301 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009302 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 int error = FALSE;
9306
9307 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9308 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009311 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009312 else if (argvars[0].v_type == VAR_DICT)
9313 {
9314 if ((d = argvars[0].vval.v_dict) != NULL)
9315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009316 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009317 if (di != NULL)
9318 tv = &di->di_tv;
9319 }
9320 }
9321 else
9322 EMSG2(_(e_listdictarg), "get()");
9323
9324 if (tv == NULL)
9325 {
9326 if (argvars[2].v_type == VAR_UNKNOWN)
9327 rettv->vval.v_number = 0;
9328 else
9329 copy_tv(&argvars[2], rettv);
9330 }
9331 else
9332 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009333}
9334
Bram Moolenaar342337a2005-07-21 21:11:17 +00009335static 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 +00009336
9337/*
9338 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009339 * Return a range (from start to end) of lines in rettv from the specified
9340 * buffer.
9341 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009342 */
9343 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009344get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009345 buf_T *buf;
9346 linenr_T start;
9347 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009348 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009349 typval_T *rettv;
9350{
9351 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009352 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009353
Bram Moolenaar342337a2005-07-21 21:11:17 +00009354 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009355 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009356 l = list_alloc();
9357 if (l == NULL)
9358 return;
9359
9360 rettv->vval.v_list = l;
9361 rettv->v_type = VAR_LIST;
9362 ++l->lv_refcount;
9363 }
9364 else
9365 rettv->vval.v_number = 0;
9366
9367 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9368 return;
9369
9370 if (!retlist)
9371 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009372 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9373 p = ml_get_buf(buf, start, FALSE);
9374 else
9375 p = (char_u *)"";
9376
9377 rettv->v_type = VAR_STRING;
9378 rettv->vval.v_string = vim_strsave(p);
9379 }
9380 else
9381 {
9382 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009383 return;
9384
9385 if (start < 1)
9386 start = 1;
9387 if (end > buf->b_ml.ml_line_count)
9388 end = buf->b_ml.ml_line_count;
9389 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009390 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9391 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009392 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009393 }
9394}
9395
9396/*
9397 * "getbufline()" function
9398 */
9399 static void
9400f_getbufline(argvars, rettv)
9401 typval_T *argvars;
9402 typval_T *rettv;
9403{
9404 linenr_T lnum;
9405 linenr_T end;
9406 buf_T *buf;
9407
9408 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9409 ++emsg_off;
9410 buf = get_buf_tv(&argvars[0]);
9411 --emsg_off;
9412
Bram Moolenaar661b1822005-07-28 22:36:45 +00009413 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009414 if (argvars[2].v_type == VAR_UNKNOWN)
9415 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009416 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009417 end = get_tv_lnum_buf(&argvars[2], buf);
9418
Bram Moolenaar342337a2005-07-21 21:11:17 +00009419 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009420}
9421
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422/*
9423 * "getbufvar()" function
9424 */
9425 static void
9426f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009427 typval_T *argvars;
9428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009429{
9430 buf_T *buf;
9431 buf_T *save_curbuf;
9432 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009433 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009435 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9436 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437 ++emsg_off;
9438 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009439
9440 rettv->v_type = VAR_STRING;
9441 rettv->vval.v_string = NULL;
9442
9443 if (buf != NULL && varname != NULL)
9444 {
9445 if (*varname == '&') /* buffer-local-option */
9446 {
9447 /* set curbuf to be our buf, temporarily */
9448 save_curbuf = curbuf;
9449 curbuf = buf;
9450
9451 get_option_tv(&varname, rettv, TRUE);
9452
9453 /* restore previous notion of curbuf */
9454 curbuf = save_curbuf;
9455 }
9456 else
9457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009458 if (*varname == NUL)
9459 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9460 * scope prefix before the NUL byte is required by
9461 * find_var_in_ht(). */
9462 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009464 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009466 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009467 }
9468 }
9469
9470 --emsg_off;
9471}
9472
9473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 * "getchar()" function
9475 */
9476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009478 typval_T *argvars;
9479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480{
9481 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483
9484 ++no_mapping;
9485 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009486 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 /* getchar(): blocking wait. */
9488 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009489 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 /* getchar(1): only check if char avail */
9491 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009492 else if (error || vpeekc() == NUL)
9493 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 n = 0;
9495 else
9496 /* getchar(0) and char avail: return char */
9497 n = safe_vgetc();
9498 --no_mapping;
9499 --allow_keys;
9500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 if (IS_SPECIAL(n) || mod_mask != 0)
9503 {
9504 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9505 int i = 0;
9506
9507 /* Turn a special key into three bytes, plus modifier. */
9508 if (mod_mask != 0)
9509 {
9510 temp[i++] = K_SPECIAL;
9511 temp[i++] = KS_MODIFIER;
9512 temp[i++] = mod_mask;
9513 }
9514 if (IS_SPECIAL(n))
9515 {
9516 temp[i++] = K_SPECIAL;
9517 temp[i++] = K_SECOND(n);
9518 temp[i++] = K_THIRD(n);
9519 }
9520#ifdef FEAT_MBYTE
9521 else if (has_mbyte)
9522 i += (*mb_char2bytes)(n, temp + i);
9523#endif
9524 else
9525 temp[i++] = n;
9526 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->v_type = VAR_STRING;
9528 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 }
9530}
9531
9532/*
9533 * "getcharmod()" function
9534 */
9535/*ARGSUSED*/
9536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009537f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009538 typval_T *argvars;
9539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542}
9543
9544/*
9545 * "getcmdline()" function
9546 */
9547/*ARGSUSED*/
9548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 typval_T *argvars;
9551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553 rettv->v_type = VAR_STRING;
9554 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
9556
9557/*
9558 * "getcmdpos()" function
9559 */
9560/*ARGSUSED*/
9561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009563 typval_T *argvars;
9564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
9569/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009570 * "getcmdtype()" function
9571 */
9572/*ARGSUSED*/
9573 static void
9574f_getcmdtype(argvars, rettv)
9575 typval_T *argvars;
9576 typval_T *rettv;
9577{
9578 rettv->v_type = VAR_STRING;
9579 rettv->vval.v_string = alloc(2);
9580 if (rettv->vval.v_string != NULL)
9581 {
9582 rettv->vval.v_string[0] = get_cmdline_type();
9583 rettv->vval.v_string[1] = NUL;
9584 }
9585}
9586
9587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 * "getcwd()" function
9589 */
9590/*ARGSUSED*/
9591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009593 typval_T *argvars;
9594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 char_u cwd[MAXPATHL];
9597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 else
9602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009605 if (rettv->vval.v_string != NULL)
9606 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607#endif
9608 }
9609}
9610
9611/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009612 * "getfontname()" function
9613 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009614/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009617 typval_T *argvars;
9618 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->v_type = VAR_STRING;
9621 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009622#ifdef FEAT_GUI
9623 if (gui.in_use)
9624 {
9625 GuiFont font;
9626 char_u *name = NULL;
9627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009629 {
9630 /* Get the "Normal" font. Either the name saved by
9631 * hl_set_font_name() or from the font ID. */
9632 font = gui.norm_font;
9633 name = hl_get_font_name();
9634 }
9635 else
9636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009638 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9639 return;
9640 font = gui_mch_get_font(name, FALSE);
9641 if (font == NOFONT)
9642 return; /* Invalid font name, return empty string. */
9643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009644 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009645 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009646 gui_mch_free_font(font);
9647 }
9648#endif
9649}
9650
9651/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009652 * "getfperm({fname})" function
9653 */
9654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 typval_T *argvars;
9657 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009658{
9659 char_u *fname;
9660 struct stat st;
9661 char_u *perm = NULL;
9662 char_u flags[] = "rwx";
9663 int i;
9664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009666
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009668 if (mch_stat((char *)fname, &st) >= 0)
9669 {
9670 perm = vim_strsave((char_u *)"---------");
9671 if (perm != NULL)
9672 {
9673 for (i = 0; i < 9; i++)
9674 {
9675 if (st.st_mode & (1 << (8 - i)))
9676 perm[i] = flags[i % 3];
9677 }
9678 }
9679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009681}
9682
9683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 * "getfsize({fname})" function
9685 */
9686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009687f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009688 typval_T *argvars;
9689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690{
9691 char_u *fname;
9692 struct stat st;
9693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697
9698 if (mch_stat((char *)fname, &st) >= 0)
9699 {
9700 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 }
9705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707}
9708
9709/*
9710 * "getftime({fname})" function
9711 */
9712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 typval_T *argvars;
9715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
9717 char_u *fname;
9718 struct stat st;
9719
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
9722 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727
9728/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009729 * "getftype({fname})" function
9730 */
9731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009733 typval_T *argvars;
9734 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009735{
9736 char_u *fname;
9737 struct stat st;
9738 char_u *type = NULL;
9739 char *t;
9740
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009742
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009744 if (mch_lstat((char *)fname, &st) >= 0)
9745 {
9746#ifdef S_ISREG
9747 if (S_ISREG(st.st_mode))
9748 t = "file";
9749 else if (S_ISDIR(st.st_mode))
9750 t = "dir";
9751# ifdef S_ISLNK
9752 else if (S_ISLNK(st.st_mode))
9753 t = "link";
9754# endif
9755# ifdef S_ISBLK
9756 else if (S_ISBLK(st.st_mode))
9757 t = "bdev";
9758# endif
9759# ifdef S_ISCHR
9760 else if (S_ISCHR(st.st_mode))
9761 t = "cdev";
9762# endif
9763# ifdef S_ISFIFO
9764 else if (S_ISFIFO(st.st_mode))
9765 t = "fifo";
9766# endif
9767# ifdef S_ISSOCK
9768 else if (S_ISSOCK(st.st_mode))
9769 t = "fifo";
9770# endif
9771 else
9772 t = "other";
9773#else
9774# ifdef S_IFMT
9775 switch (st.st_mode & S_IFMT)
9776 {
9777 case S_IFREG: t = "file"; break;
9778 case S_IFDIR: t = "dir"; break;
9779# ifdef S_IFLNK
9780 case S_IFLNK: t = "link"; break;
9781# endif
9782# ifdef S_IFBLK
9783 case S_IFBLK: t = "bdev"; break;
9784# endif
9785# ifdef S_IFCHR
9786 case S_IFCHR: t = "cdev"; break;
9787# endif
9788# ifdef S_IFIFO
9789 case S_IFIFO: t = "fifo"; break;
9790# endif
9791# ifdef S_IFSOCK
9792 case S_IFSOCK: t = "socket"; break;
9793# endif
9794 default: t = "other";
9795 }
9796# else
9797 if (mch_isdir(fname))
9798 t = "dir";
9799 else
9800 t = "file";
9801# endif
9802#endif
9803 type = vim_strsave((char_u *)t);
9804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009806}
9807
9808/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009809 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009810 */
9811 static void
9812f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009813 typval_T *argvars;
9814 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009815{
9816 linenr_T lnum;
9817 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009818 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009819
9820 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009821 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009822 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009823 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009824 retlist = FALSE;
9825 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009826 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009827 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009828 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009829 retlist = TRUE;
9830 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009831
Bram Moolenaar342337a2005-07-21 21:11:17 +00009832 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009833}
9834
9835/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009836 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009837 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009838/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009839 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009840f_getqflist(argvars, rettv)
9841 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009842 typval_T *rettv;
9843{
9844#ifdef FEAT_QUICKFIX
9845 list_T *l;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009846 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009847#endif
9848
9849 rettv->vval.v_number = FALSE;
9850#ifdef FEAT_QUICKFIX
9851 l = list_alloc();
9852 if (l != NULL)
9853 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009854 rettv->vval.v_list = l;
9855 rettv->v_type = VAR_LIST;
9856 ++l->lv_refcount;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009857 wp = NULL;
9858 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9859 {
9860 wp = find_win_by_nr(&argvars[0]);
9861 if (wp == NULL)
9862 return;
9863 }
9864
Bram Moolenaar17c7c012006-01-26 22:25:15 +00009865 (void)get_errorlist(wp, l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009866 }
9867#endif
9868}
9869
9870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 * "getreg()" function
9872 */
9873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 typval_T *argvars;
9876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877{
9878 char_u *strregname;
9879 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009880 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009883 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 strregname = get_tv_string_chk(&argvars[0]);
9886 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009887 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009891 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 regname = (strregname == NULL ? '"' : *strregname);
9893 if (regname == 0)
9894 regname = '"';
9895
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009897 rettv->vval.v_string = error ? NULL :
9898 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899}
9900
9901/*
9902 * "getregtype()" function
9903 */
9904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009906 typval_T *argvars;
9907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908{
9909 char_u *strregname;
9910 int regname;
9911 char_u buf[NUMBUFLEN + 2];
9912 long reglen = 0;
9913
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009914 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009915 {
9916 strregname = get_tv_string_chk(&argvars[0]);
9917 if (strregname == NULL) /* type error; errmsg already given */
9918 {
9919 rettv->v_type = VAR_STRING;
9920 rettv->vval.v_string = NULL;
9921 return;
9922 }
9923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 else
9925 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
9928 regname = (strregname == NULL ? '"' : *strregname);
9929 if (regname == 0)
9930 regname = '"';
9931
9932 buf[0] = NUL;
9933 buf[1] = NUL;
9934 switch (get_reg_type(regname, &reglen))
9935 {
9936 case MLINE: buf[0] = 'V'; break;
9937 case MCHAR: buf[0] = 'v'; break;
9938#ifdef FEAT_VISUAL
9939 case MBLOCK:
9940 buf[0] = Ctrl_V;
9941 sprintf((char *)buf + 1, "%ld", reglen + 1);
9942 break;
9943#endif
9944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945 rettv->v_type = VAR_STRING;
9946 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947}
9948
9949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 * "getwinposx()" function
9951 */
9952/*ARGSUSED*/
9953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009954f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 typval_T *argvars;
9956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959#ifdef FEAT_GUI
9960 if (gui.in_use)
9961 {
9962 int x, y;
9963
9964 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 }
9967#endif
9968}
9969
9970/*
9971 * "getwinposy()" function
9972 */
9973/*ARGSUSED*/
9974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *argvars;
9977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980#ifdef FEAT_GUI
9981 if (gui.in_use)
9982 {
9983 int x, y;
9984
9985 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 }
9988#endif
9989}
9990
Bram Moolenaara40058a2005-07-11 22:42:07 +00009991 static win_T *
9992find_win_by_nr(vp)
9993 typval_T *vp;
9994{
9995#ifdef FEAT_WINDOWS
9996 win_T *wp;
9997#endif
9998 int nr;
9999
10000 nr = get_tv_number_chk(vp, NULL);
10001
10002#ifdef FEAT_WINDOWS
10003 if (nr < 0)
10004 return NULL;
10005 if (nr == 0)
10006 return curwin;
10007
10008 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10009 if (--nr <= 0)
10010 break;
10011 return wp;
10012#else
10013 if (nr == 0 || nr == 1)
10014 return curwin;
10015 return NULL;
10016#endif
10017}
10018
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019/*
10020 * "getwinvar()" function
10021 */
10022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010024 typval_T *argvars;
10025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 win_T *win, *oldcurwin;
10028 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010029 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 varname = get_tv_string_chk(&argvars[1]);
10033 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035 rettv->v_type = VAR_STRING;
10036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
10038 if (win != NULL && varname != NULL)
10039 {
10040 if (*varname == '&') /* window-local-option */
10041 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010042 /* Set curwin to be our win, temporarily. Also set curbuf, so
10043 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 oldcurwin = curwin;
10045 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010046 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
10050 /* restore previous notion of curwin */
10051 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010052 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054 else
10055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010056 if (*varname == NUL)
10057 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10058 * scope prefix before the NUL byte is required by
10059 * find_var_in_ht(). */
10060 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010062 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010064 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 }
10066 }
10067
10068 --emsg_off;
10069}
10070
10071/*
10072 * "glob()" function
10073 */
10074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010076 typval_T *argvars;
10077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
10079 expand_T xpc;
10080
10081 ExpandInit(&xpc);
10082 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->v_type = VAR_STRING;
10084 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10086 ExpandCleanup(&xpc);
10087}
10088
10089/*
10090 * "globpath()" function
10091 */
10092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010094 typval_T *argvars;
10095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 if (file == NULL)
10102 rettv->vval.v_string = NULL;
10103 else
10104 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105}
10106
10107/*
10108 * "has()" function
10109 */
10110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010112 typval_T *argvars;
10113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
10115 int i;
10116 char_u *name;
10117 int n = FALSE;
10118 static char *(has_list[]) =
10119 {
10120#ifdef AMIGA
10121 "amiga",
10122# ifdef FEAT_ARP
10123 "arp",
10124# endif
10125#endif
10126#ifdef __BEOS__
10127 "beos",
10128#endif
10129#ifdef MSDOS
10130# ifdef DJGPP
10131 "dos32",
10132# else
10133 "dos16",
10134# endif
10135#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010136#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 "mac",
10138#endif
10139#if defined(MACOS_X_UNIX)
10140 "macunix",
10141#endif
10142#ifdef OS2
10143 "os2",
10144#endif
10145#ifdef __QNX__
10146 "qnx",
10147#endif
10148#ifdef RISCOS
10149 "riscos",
10150#endif
10151#ifdef UNIX
10152 "unix",
10153#endif
10154#ifdef VMS
10155 "vms",
10156#endif
10157#ifdef WIN16
10158 "win16",
10159#endif
10160#ifdef WIN32
10161 "win32",
10162#endif
10163#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10164 "win32unix",
10165#endif
10166#ifdef WIN64
10167 "win64",
10168#endif
10169#ifdef EBCDIC
10170 "ebcdic",
10171#endif
10172#ifndef CASE_INSENSITIVE_FILENAME
10173 "fname_case",
10174#endif
10175#ifdef FEAT_ARABIC
10176 "arabic",
10177#endif
10178#ifdef FEAT_AUTOCMD
10179 "autocmd",
10180#endif
10181#ifdef FEAT_BEVAL
10182 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010183# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10184 "balloon_multiline",
10185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186#endif
10187#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10188 "builtin_terms",
10189# ifdef ALL_BUILTIN_TCAPS
10190 "all_builtin_terms",
10191# endif
10192#endif
10193#ifdef FEAT_BYTEOFF
10194 "byte_offset",
10195#endif
10196#ifdef FEAT_CINDENT
10197 "cindent",
10198#endif
10199#ifdef FEAT_CLIENTSERVER
10200 "clientserver",
10201#endif
10202#ifdef FEAT_CLIPBOARD
10203 "clipboard",
10204#endif
10205#ifdef FEAT_CMDL_COMPL
10206 "cmdline_compl",
10207#endif
10208#ifdef FEAT_CMDHIST
10209 "cmdline_hist",
10210#endif
10211#ifdef FEAT_COMMENTS
10212 "comments",
10213#endif
10214#ifdef FEAT_CRYPT
10215 "cryptv",
10216#endif
10217#ifdef FEAT_CSCOPE
10218 "cscope",
10219#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010220#ifdef CURSOR_SHAPE
10221 "cursorshape",
10222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223#ifdef DEBUG
10224 "debug",
10225#endif
10226#ifdef FEAT_CON_DIALOG
10227 "dialog_con",
10228#endif
10229#ifdef FEAT_GUI_DIALOG
10230 "dialog_gui",
10231#endif
10232#ifdef FEAT_DIFF
10233 "diff",
10234#endif
10235#ifdef FEAT_DIGRAPHS
10236 "digraphs",
10237#endif
10238#ifdef FEAT_DND
10239 "dnd",
10240#endif
10241#ifdef FEAT_EMACS_TAGS
10242 "emacs_tags",
10243#endif
10244 "eval", /* always present, of course! */
10245#ifdef FEAT_EX_EXTRA
10246 "ex_extra",
10247#endif
10248#ifdef FEAT_SEARCH_EXTRA
10249 "extra_search",
10250#endif
10251#ifdef FEAT_FKMAP
10252 "farsi",
10253#endif
10254#ifdef FEAT_SEARCHPATH
10255 "file_in_path",
10256#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010257#if defined(UNIX) && !defined(USE_SYSTEM)
10258 "filterpipe",
10259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260#ifdef FEAT_FIND_ID
10261 "find_in_path",
10262#endif
10263#ifdef FEAT_FOLDING
10264 "folding",
10265#endif
10266#ifdef FEAT_FOOTER
10267 "footer",
10268#endif
10269#if !defined(USE_SYSTEM) && defined(UNIX)
10270 "fork",
10271#endif
10272#ifdef FEAT_GETTEXT
10273 "gettext",
10274#endif
10275#ifdef FEAT_GUI
10276 "gui",
10277#endif
10278#ifdef FEAT_GUI_ATHENA
10279# ifdef FEAT_GUI_NEXTAW
10280 "gui_neXtaw",
10281# else
10282 "gui_athena",
10283# endif
10284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285#ifdef FEAT_GUI_GTK
10286 "gui_gtk",
10287# ifdef HAVE_GTK2
10288 "gui_gtk2",
10289# endif
10290#endif
10291#ifdef FEAT_GUI_MAC
10292 "gui_mac",
10293#endif
10294#ifdef FEAT_GUI_MOTIF
10295 "gui_motif",
10296#endif
10297#ifdef FEAT_GUI_PHOTON
10298 "gui_photon",
10299#endif
10300#ifdef FEAT_GUI_W16
10301 "gui_win16",
10302#endif
10303#ifdef FEAT_GUI_W32
10304 "gui_win32",
10305#endif
10306#ifdef FEAT_HANGULIN
10307 "hangul_input",
10308#endif
10309#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10310 "iconv",
10311#endif
10312#ifdef FEAT_INS_EXPAND
10313 "insert_expand",
10314#endif
10315#ifdef FEAT_JUMPLIST
10316 "jumplist",
10317#endif
10318#ifdef FEAT_KEYMAP
10319 "keymap",
10320#endif
10321#ifdef FEAT_LANGMAP
10322 "langmap",
10323#endif
10324#ifdef FEAT_LIBCALL
10325 "libcall",
10326#endif
10327#ifdef FEAT_LINEBREAK
10328 "linebreak",
10329#endif
10330#ifdef FEAT_LISP
10331 "lispindent",
10332#endif
10333#ifdef FEAT_LISTCMDS
10334 "listcmds",
10335#endif
10336#ifdef FEAT_LOCALMAP
10337 "localmap",
10338#endif
10339#ifdef FEAT_MENU
10340 "menu",
10341#endif
10342#ifdef FEAT_SESSION
10343 "mksession",
10344#endif
10345#ifdef FEAT_MODIFY_FNAME
10346 "modify_fname",
10347#endif
10348#ifdef FEAT_MOUSE
10349 "mouse",
10350#endif
10351#ifdef FEAT_MOUSESHAPE
10352 "mouseshape",
10353#endif
10354#if defined(UNIX) || defined(VMS)
10355# ifdef FEAT_MOUSE_DEC
10356 "mouse_dec",
10357# endif
10358# ifdef FEAT_MOUSE_GPM
10359 "mouse_gpm",
10360# endif
10361# ifdef FEAT_MOUSE_JSB
10362 "mouse_jsbterm",
10363# endif
10364# ifdef FEAT_MOUSE_NET
10365 "mouse_netterm",
10366# endif
10367# ifdef FEAT_MOUSE_PTERM
10368 "mouse_pterm",
10369# endif
10370# ifdef FEAT_MOUSE_XTERM
10371 "mouse_xterm",
10372# endif
10373#endif
10374#ifdef FEAT_MBYTE
10375 "multi_byte",
10376#endif
10377#ifdef FEAT_MBYTE_IME
10378 "multi_byte_ime",
10379#endif
10380#ifdef FEAT_MULTI_LANG
10381 "multi_lang",
10382#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010383#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010384#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010385 "mzscheme",
10386#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388#ifdef FEAT_OLE
10389 "ole",
10390#endif
10391#ifdef FEAT_OSFILETYPE
10392 "osfiletype",
10393#endif
10394#ifdef FEAT_PATH_EXTRA
10395 "path_extra",
10396#endif
10397#ifdef FEAT_PERL
10398#ifndef DYNAMIC_PERL
10399 "perl",
10400#endif
10401#endif
10402#ifdef FEAT_PYTHON
10403#ifndef DYNAMIC_PYTHON
10404 "python",
10405#endif
10406#endif
10407#ifdef FEAT_POSTSCRIPT
10408 "postscript",
10409#endif
10410#ifdef FEAT_PRINTER
10411 "printer",
10412#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010413#ifdef FEAT_PROFILE
10414 "profile",
10415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416#ifdef FEAT_QUICKFIX
10417 "quickfix",
10418#endif
10419#ifdef FEAT_RIGHTLEFT
10420 "rightleft",
10421#endif
10422#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10423 "ruby",
10424#endif
10425#ifdef FEAT_SCROLLBIND
10426 "scrollbind",
10427#endif
10428#ifdef FEAT_CMDL_INFO
10429 "showcmd",
10430 "cmdline_info",
10431#endif
10432#ifdef FEAT_SIGNS
10433 "signs",
10434#endif
10435#ifdef FEAT_SMARTINDENT
10436 "smartindent",
10437#endif
10438#ifdef FEAT_SNIFF
10439 "sniff",
10440#endif
10441#ifdef FEAT_STL_OPT
10442 "statusline",
10443#endif
10444#ifdef FEAT_SUN_WORKSHOP
10445 "sun_workshop",
10446#endif
10447#ifdef FEAT_NETBEANS_INTG
10448 "netbeans_intg",
10449#endif
10450#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010451 "spell",
10452#endif
10453#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 "syntax",
10455#endif
10456#if defined(USE_SYSTEM) || !defined(UNIX)
10457 "system",
10458#endif
10459#ifdef FEAT_TAG_BINS
10460 "tag_binary",
10461#endif
10462#ifdef FEAT_TAG_OLDSTATIC
10463 "tag_old_static",
10464#endif
10465#ifdef FEAT_TAG_ANYWHITE
10466 "tag_any_white",
10467#endif
10468#ifdef FEAT_TCL
10469# ifndef DYNAMIC_TCL
10470 "tcl",
10471# endif
10472#endif
10473#ifdef TERMINFO
10474 "terminfo",
10475#endif
10476#ifdef FEAT_TERMRESPONSE
10477 "termresponse",
10478#endif
10479#ifdef FEAT_TEXTOBJ
10480 "textobjects",
10481#endif
10482#ifdef HAVE_TGETENT
10483 "tgetent",
10484#endif
10485#ifdef FEAT_TITLE
10486 "title",
10487#endif
10488#ifdef FEAT_TOOLBAR
10489 "toolbar",
10490#endif
10491#ifdef FEAT_USR_CMDS
10492 "user-commands", /* was accidentally included in 5.4 */
10493 "user_commands",
10494#endif
10495#ifdef FEAT_VIMINFO
10496 "viminfo",
10497#endif
10498#ifdef FEAT_VERTSPLIT
10499 "vertsplit",
10500#endif
10501#ifdef FEAT_VIRTUALEDIT
10502 "virtualedit",
10503#endif
10504#ifdef FEAT_VISUAL
10505 "visual",
10506#endif
10507#ifdef FEAT_VISUALEXTRA
10508 "visualextra",
10509#endif
10510#ifdef FEAT_VREPLACE
10511 "vreplace",
10512#endif
10513#ifdef FEAT_WILDIGN
10514 "wildignore",
10515#endif
10516#ifdef FEAT_WILDMENU
10517 "wildmenu",
10518#endif
10519#ifdef FEAT_WINDOWS
10520 "windows",
10521#endif
10522#ifdef FEAT_WAK
10523 "winaltkeys",
10524#endif
10525#ifdef FEAT_WRITEBACKUP
10526 "writebackup",
10527#endif
10528#ifdef FEAT_XIM
10529 "xim",
10530#endif
10531#ifdef FEAT_XFONTSET
10532 "xfontset",
10533#endif
10534#ifdef USE_XSMP
10535 "xsmp",
10536#endif
10537#ifdef USE_XSMP_INTERACT
10538 "xsmp_interact",
10539#endif
10540#ifdef FEAT_XCLIPBOARD
10541 "xterm_clipboard",
10542#endif
10543#ifdef FEAT_XTERM_SAVE
10544 "xterm_save",
10545#endif
10546#if defined(UNIX) && defined(FEAT_X11)
10547 "X11",
10548#endif
10549 NULL
10550 };
10551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 for (i = 0; has_list[i] != NULL; ++i)
10554 if (STRICMP(name, has_list[i]) == 0)
10555 {
10556 n = TRUE;
10557 break;
10558 }
10559
10560 if (n == FALSE)
10561 {
10562 if (STRNICMP(name, "patch", 5) == 0)
10563 n = has_patch(atoi((char *)name + 5));
10564 else if (STRICMP(name, "vim_starting") == 0)
10565 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010566#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10567 else if (STRICMP(name, "balloon_multiline") == 0)
10568 n = multiline_balloon_available();
10569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570#ifdef DYNAMIC_TCL
10571 else if (STRICMP(name, "tcl") == 0)
10572 n = tcl_enabled(FALSE);
10573#endif
10574#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10575 else if (STRICMP(name, "iconv") == 0)
10576 n = iconv_enabled(FALSE);
10577#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010578#ifdef DYNAMIC_MZSCHEME
10579 else if (STRICMP(name, "mzscheme") == 0)
10580 n = mzscheme_enabled(FALSE);
10581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582#ifdef DYNAMIC_RUBY
10583 else if (STRICMP(name, "ruby") == 0)
10584 n = ruby_enabled(FALSE);
10585#endif
10586#ifdef DYNAMIC_PYTHON
10587 else if (STRICMP(name, "python") == 0)
10588 n = python_enabled(FALSE);
10589#endif
10590#ifdef DYNAMIC_PERL
10591 else if (STRICMP(name, "perl") == 0)
10592 n = perl_enabled(FALSE);
10593#endif
10594#ifdef FEAT_GUI
10595 else if (STRICMP(name, "gui_running") == 0)
10596 n = (gui.in_use || gui.starting);
10597# ifdef FEAT_GUI_W32
10598 else if (STRICMP(name, "gui_win32s") == 0)
10599 n = gui_is_win32s();
10600# endif
10601# ifdef FEAT_BROWSE
10602 else if (STRICMP(name, "browse") == 0)
10603 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10604# endif
10605#endif
10606#ifdef FEAT_SYN_HL
10607 else if (STRICMP(name, "syntax_items") == 0)
10608 n = syntax_present(curbuf);
10609#endif
10610#if defined(WIN3264)
10611 else if (STRICMP(name, "win95") == 0)
10612 n = mch_windows95();
10613#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010614#ifdef FEAT_NETBEANS_INTG
10615 else if (STRICMP(name, "netbeans_enabled") == 0)
10616 n = usingNetbeans;
10617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 }
10619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621}
10622
10623/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 * "has_key()" function
10625 */
10626 static void
10627f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010628 typval_T *argvars;
10629 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630{
10631 rettv->vval.v_number = 0;
10632 if (argvars[0].v_type != VAR_DICT)
10633 {
10634 EMSG(_(e_dictreq));
10635 return;
10636 }
10637 if (argvars[0].vval.v_dict == NULL)
10638 return;
10639
10640 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010641 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010642}
10643
10644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 * "hasmapto()" function
10646 */
10647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 typval_T *argvars;
10650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651{
10652 char_u *name;
10653 char_u *mode;
10654 char_u buf[NUMBUFLEN];
10655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010657 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 mode = (char_u *)"nvo";
10659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661
10662 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666}
10667
10668/*
10669 * "histadd()" function
10670 */
10671/*ARGSUSED*/
10672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010674 typval_T *argvars;
10675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676{
10677#ifdef FEAT_CMDHIST
10678 int histype;
10679 char_u *str;
10680 char_u buf[NUMBUFLEN];
10681#endif
10682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 if (check_restricted() || check_secure())
10685 return;
10686#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10688 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 if (histype >= 0)
10690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 if (*str != NUL)
10693 {
10694 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 return;
10697 }
10698 }
10699#endif
10700}
10701
10702/*
10703 * "histdel()" function
10704 */
10705/*ARGSUSED*/
10706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010708 typval_T *argvars;
10709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710{
10711#ifdef FEAT_CMDHIST
10712 int n;
10713 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010714 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010716 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10717 if (str == NULL)
10718 n = 0;
10719 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010722 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010724 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 else
10727 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 get_tv_string_buf(&argvars[1], buf));
10730 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733#endif
10734}
10735
10736/*
10737 * "histget()" function
10738 */
10739/*ARGSUSED*/
10740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 typval_T *argvars;
10743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744{
10745#ifdef FEAT_CMDHIST
10746 int type;
10747 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010748 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10751 if (str == NULL)
10752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 {
10755 type = get_histtype(str);
10756 if (argvars[1].v_type == VAR_UNKNOWN)
10757 idx = get_history_idx(type);
10758 else
10759 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10760 /* -1 on type error */
10761 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767}
10768
10769/*
10770 * "histnr()" function
10771 */
10772/*ARGSUSED*/
10773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010775 typval_T *argvars;
10776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777{
10778 int i;
10779
10780#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010781 char_u *history = get_tv_string_chk(&argvars[0]);
10782
10783 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 if (i >= HIST_CMD && i < HIST_COUNT)
10785 i = get_history_idx(i);
10786 else
10787#endif
10788 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790}
10791
10792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 * "highlightID(name)" function
10794 */
10795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010797 typval_T *argvars;
10798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801}
10802
10803/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010804 * "highlight_exists()" function
10805 */
10806 static void
10807f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010808 typval_T *argvars;
10809 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010810{
10811 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10812}
10813
10814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 * "hostname()" function
10816 */
10817/*ARGSUSED*/
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
10823 char_u hostname[256];
10824
10825 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 rettv->v_type = VAR_STRING;
10827 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830/*
10831 * iconv() function
10832 */
10833/*ARGSUSED*/
10834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 typval_T *argvars;
10837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
10839#ifdef FEAT_MBYTE
10840 char_u buf1[NUMBUFLEN];
10841 char_u buf2[NUMBUFLEN];
10842 char_u *from, *to, *str;
10843 vimconv_T vimconv;
10844#endif
10845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->v_type = VAR_STRING;
10847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848
10849#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 str = get_tv_string(&argvars[0]);
10851 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10852 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 vimconv.vc_type = CONV_NONE;
10854 convert_setup(&vimconv, from, to);
10855
10856 /* If the encodings are equal, no conversion needed. */
10857 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
10862 convert_setup(&vimconv, NULL, NULL);
10863 vim_free(from);
10864 vim_free(to);
10865#endif
10866}
10867
10868/*
10869 * "indent()" function
10870 */
10871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010873 typval_T *argvars;
10874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875{
10876 linenr_T lnum;
10877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883}
10884
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010885/*
10886 * "index()" function
10887 */
10888 static void
10889f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010890 typval_T *argvars;
10891 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010892{
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 list_T *l;
10894 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010895 long idx = 0;
10896 int ic = FALSE;
10897
10898 rettv->vval.v_number = -1;
10899 if (argvars[0].v_type != VAR_LIST)
10900 {
10901 EMSG(_(e_listreq));
10902 return;
10903 }
10904 l = argvars[0].vval.v_list;
10905 if (l != NULL)
10906 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010907 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010908 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 int error = FALSE;
10911
Bram Moolenaar758711c2005-02-02 23:11:38 +000010912 /* Start at specified item. Use the cached index that list_find()
10913 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010915 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010916 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 ic = get_tv_number_chk(&argvars[3], &error);
10918 if (error)
10919 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010920 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010921
Bram Moolenaar758711c2005-02-02 23:11:38 +000010922 for ( ; item != NULL; item = item->li_next, ++idx)
10923 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010924 {
10925 rettv->vval.v_number = idx;
10926 break;
10927 }
10928 }
10929}
10930
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931static int inputsecret_flag = 0;
10932
10933/*
10934 * "input()" function
10935 * Also handles inputsecret() when inputsecret is set.
10936 */
10937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010939 typval_T *argvars;
10940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 char_u *p = NULL;
10944 int c;
10945 char_u buf[NUMBUFLEN];
10946 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010948 int xp_type = EXPAND_NOTHING;
10949 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952
10953#ifdef NO_CONSOLE_INPUT
10954 /* While starting up, there is no place to enter text. */
10955 if (no_console_input())
10956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 return;
10959 }
10960#endif
10961
10962 cmd_silent = FALSE; /* Want to see the prompt. */
10963 if (prompt != NULL)
10964 {
10965 /* Only the part of the message after the last NL is considered as
10966 * prompt for the command line */
10967 p = vim_strrchr(prompt, '\n');
10968 if (p == NULL)
10969 p = prompt;
10970 else
10971 {
10972 ++p;
10973 c = *p;
10974 *p = NUL;
10975 msg_start();
10976 msg_clr_eos();
10977 msg_puts_attr(prompt, echo_attr);
10978 msg_didout = FALSE;
10979 msg_starthere();
10980 *p = c;
10981 }
10982 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010984 if (argvars[1].v_type != VAR_UNKNOWN)
10985 {
10986 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10987 if (defstr != NULL)
10988 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989
Bram Moolenaar4463f292005-09-25 22:20:24 +000010990 if (argvars[2].v_type != VAR_UNKNOWN)
10991 {
10992 char_u *xp_name;
10993 int xp_namelen;
10994 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010995
Bram Moolenaar4463f292005-09-25 22:20:24 +000010996 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010997
Bram Moolenaar4463f292005-09-25 22:20:24 +000010998 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10999 if (xp_name == NULL)
11000 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011001
Bram Moolenaar4463f292005-09-25 22:20:24 +000011002 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011003
Bram Moolenaar4463f292005-09-25 22:20:24 +000011004 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11005 &xp_arg) == FAIL)
11006 return;
11007 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011008 }
11009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011010 if (defstr != NULL)
11011 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011012 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11013 xp_type, xp_arg);
11014
11015 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 /* since the user typed this, no need to wait for return */
11018 need_wait_return = FALSE;
11019 msg_didout = FALSE;
11020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 cmd_silent = cmd_silent_save;
11022}
11023
11024/*
11025 * "inputdialog()" function
11026 */
11027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011029 typval_T *argvars;
11030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031{
11032#if defined(FEAT_GUI_TEXTDIALOG)
11033 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11034 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11035 {
11036 char_u *message;
11037 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011040 message = get_tv_string_chk(&argvars[0]);
11041 if (argvars[1].v_type != VAR_UNKNOWN
11042 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011043 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 else
11045 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 if (message != NULL && defstr != NULL
11047 && do_dialog(VIM_QUESTION, NULL, message,
11048 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 else
11051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 if (message != NULL && defstr != NULL
11053 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011054 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 rettv->vval.v_string = vim_strsave(
11056 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 }
11062 else
11063#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065}
11066
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011067/*
11068 * "inputlist()" function
11069 */
11070 static void
11071f_inputlist(argvars, rettv)
11072 typval_T *argvars;
11073 typval_T *rettv;
11074{
11075 listitem_T *li;
11076 int selected;
11077 int mouse_used;
11078
11079 rettv->vval.v_number = 0;
11080#ifdef NO_CONSOLE_INPUT
11081 /* While starting up, there is no place to enter text. */
11082 if (no_console_input())
11083 return;
11084#endif
11085 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11086 {
11087 EMSG2(_(e_listarg), "inputlist()");
11088 return;
11089 }
11090
11091 msg_start();
11092 lines_left = Rows; /* avoid more prompt */
11093 msg_scroll = TRUE;
11094 msg_clr_eos();
11095
11096 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11097 {
11098 msg_puts(get_tv_string(&li->li_tv));
11099 msg_putchar('\n');
11100 }
11101
11102 /* Ask for choice. */
11103 selected = prompt_for_number(&mouse_used);
11104 if (mouse_used)
11105 selected -= lines_left;
11106
11107 rettv->vval.v_number = selected;
11108}
11109
11110
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11112
11113/*
11114 * "inputrestore()" function
11115 */
11116/*ARGSUSED*/
11117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 typval_T *argvars;
11120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121{
11122 if (ga_userinput.ga_len > 0)
11123 {
11124 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11126 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 }
11129 else if (p_verbose > 1)
11130 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011131 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 }
11134}
11135
11136/*
11137 * "inputsave()" function
11138 */
11139/*ARGSUSED*/
11140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 typval_T *argvars;
11143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144{
11145 /* Add an entry to the stack of typehead storage. */
11146 if (ga_grow(&ga_userinput, 1) == OK)
11147 {
11148 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11149 + ga_userinput.ga_len);
11150 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 }
11153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155}
11156
11157/*
11158 * "inputsecret()" function
11159 */
11160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 typval_T *argvars;
11163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164{
11165 ++cmdline_star;
11166 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 --cmdline_star;
11169 --inputsecret_flag;
11170}
11171
11172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173 * "insert()" function
11174 */
11175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 typval_T *argvars;
11178 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011179{
11180 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 listitem_T *item;
11182 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011184
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011185 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011187 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011188 else if ((l = argvars[0].vval.v_list) != NULL
11189 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011190 {
11191 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 before = get_tv_number_chk(&argvars[2], &error);
11193 if (error)
11194 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011195
Bram Moolenaar758711c2005-02-02 23:11:38 +000011196 if (before == l->lv_len)
11197 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011198 else
11199 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011200 item = list_find(l, before);
11201 if (item == NULL)
11202 {
11203 EMSGN(_(e_listidx), before);
11204 l = NULL;
11205 }
11206 }
11207 if (l != NULL)
11208 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011209 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011210 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011211 }
11212 }
11213}
11214
11215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 * "isdirectory()" function
11217 */
11218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011226/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011227 * "islocked()" function
11228 */
11229 static void
11230f_islocked(argvars, rettv)
11231 typval_T *argvars;
11232 typval_T *rettv;
11233{
11234 lval_T lv;
11235 char_u *end;
11236 dictitem_T *di;
11237
11238 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011239 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11240 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011241 if (end != NULL && lv.ll_name != NULL)
11242 {
11243 if (*end != NUL)
11244 EMSG(_(e_trailing));
11245 else
11246 {
11247 if (lv.ll_tv == NULL)
11248 {
11249 if (check_changedtick(lv.ll_name))
11250 rettv->vval.v_number = 1; /* always locked */
11251 else
11252 {
11253 di = find_var(lv.ll_name, NULL);
11254 if (di != NULL)
11255 {
11256 /* Consider a variable locked when:
11257 * 1. the variable itself is locked
11258 * 2. the value of the variable is locked.
11259 * 3. the List or Dict value is locked.
11260 */
11261 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11262 || tv_islocked(&di->di_tv));
11263 }
11264 }
11265 }
11266 else if (lv.ll_range)
11267 EMSG(_("E745: Range not allowed"));
11268 else if (lv.ll_newkey != NULL)
11269 EMSG2(_(e_dictkey), lv.ll_newkey);
11270 else if (lv.ll_list != NULL)
11271 /* List item. */
11272 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11273 else
11274 /* Dictionary item. */
11275 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11276 }
11277 }
11278
11279 clear_lval(&lv);
11280}
11281
Bram Moolenaar33570922005-01-25 22:26:29 +000011282static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011283
11284/*
11285 * Turn a dict into a list:
11286 * "what" == 0: list of keys
11287 * "what" == 1: list of values
11288 * "what" == 2: list of items
11289 */
11290 static void
11291dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *argvars;
11293 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011294 int what;
11295{
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 list_T *l;
11297 list_T *l2;
11298 dictitem_T *di;
11299 hashitem_T *hi;
11300 listitem_T *li;
11301 listitem_T *li2;
11302 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011303 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011304
11305 rettv->vval.v_number = 0;
11306 if (argvars[0].v_type != VAR_DICT)
11307 {
11308 EMSG(_(e_dictreq));
11309 return;
11310 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011311 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011312 return;
11313
11314 l = list_alloc();
11315 if (l == NULL)
11316 return;
11317 rettv->v_type = VAR_LIST;
11318 rettv->vval.v_list = l;
11319 ++l->lv_refcount;
11320
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 todo = d->dv_hashtab.ht_used;
11322 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011324 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011325 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011326 --todo;
11327 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011328
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011329 li = listitem_alloc();
11330 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011331 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011332 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011333
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011334 if (what == 0)
11335 {
11336 /* keys() */
11337 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011338 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011339 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11340 }
11341 else if (what == 1)
11342 {
11343 /* values() */
11344 copy_tv(&di->di_tv, &li->li_tv);
11345 }
11346 else
11347 {
11348 /* items() */
11349 l2 = list_alloc();
11350 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011351 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011352 li->li_tv.vval.v_list = l2;
11353 if (l2 == NULL)
11354 break;
11355 ++l2->lv_refcount;
11356
11357 li2 = listitem_alloc();
11358 if (li2 == NULL)
11359 break;
11360 list_append(l2, li2);
11361 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011362 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011363 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11364
11365 li2 = listitem_alloc();
11366 if (li2 == NULL)
11367 break;
11368 list_append(l2, li2);
11369 copy_tv(&di->di_tv, &li2->li_tv);
11370 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011371 }
11372 }
11373}
11374
11375/*
11376 * "items(dict)" function
11377 */
11378 static void
11379f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011382{
11383 dict_list(argvars, rettv, 2);
11384}
11385
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011387 * "join()" function
11388 */
11389 static void
11390f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *argvars;
11392 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011393{
11394 garray_T ga;
11395 char_u *sep;
11396
11397 rettv->vval.v_number = 0;
11398 if (argvars[0].v_type != VAR_LIST)
11399 {
11400 EMSG(_(e_listreq));
11401 return;
11402 }
11403 if (argvars[0].vval.v_list == NULL)
11404 return;
11405 if (argvars[1].v_type == VAR_UNKNOWN)
11406 sep = (char_u *)" ";
11407 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011409
11410 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411
11412 if (sep != NULL)
11413 {
11414 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011415 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 ga_append(&ga, NUL);
11417 rettv->vval.v_string = (char_u *)ga.ga_data;
11418 }
11419 else
11420 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011421}
11422
11423/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011424 * "keys()" function
11425 */
11426 static void
11427f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011428 typval_T *argvars;
11429 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011430{
11431 dict_list(argvars, rettv, 0);
11432}
11433
11434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 * "last_buffer_nr()" function.
11436 */
11437/*ARGSUSED*/
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443 int n = 0;
11444 buf_T *buf;
11445
11446 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11447 if (n < buf->b_fnum)
11448 n = buf->b_fnum;
11449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011451}
11452
11453/*
11454 * "len()" function
11455 */
11456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011458 typval_T *argvars;
11459 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011460{
11461 switch (argvars[0].v_type)
11462 {
11463 case VAR_STRING:
11464 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->vval.v_number = (varnumber_T)STRLEN(
11466 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011467 break;
11468 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011470 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471 case VAR_DICT:
11472 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11473 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011474 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011475 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011476 break;
11477 }
11478}
11479
Bram Moolenaar33570922005-01-25 22:26:29 +000011480static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011481
11482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011484 typval_T *argvars;
11485 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011486 int type;
11487{
11488#ifdef FEAT_LIBCALL
11489 char_u *string_in;
11490 char_u **string_result;
11491 int nr_result;
11492#endif
11493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011495 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011499
11500 if (check_restricted() || check_secure())
11501 return;
11502
11503#ifdef FEAT_LIBCALL
11504 /* The first two args must be strings, otherwise its meaningless */
11505 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11506 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011507 string_in = NULL;
11508 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011509 string_in = argvars[2].vval.v_string;
11510 if (type == VAR_NUMBER)
11511 string_result = NULL;
11512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011514 if (mch_libcall(argvars[0].vval.v_string,
11515 argvars[1].vval.v_string,
11516 string_in,
11517 argvars[2].vval.v_number,
11518 string_result,
11519 &nr_result) == OK
11520 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011522 }
11523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524}
11525
11526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527 * "libcall()" function
11528 */
11529 static void
11530f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 typval_T *argvars;
11532 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533{
11534 libcall_common(argvars, rettv, VAR_STRING);
11535}
11536
11537/*
11538 * "libcallnr()" function
11539 */
11540 static void
11541f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *argvars;
11543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544{
11545 libcall_common(argvars, rettv, VAR_NUMBER);
11546}
11547
11548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 * "line(string)" function
11550 */
11551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 typval_T *argvars;
11554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555{
11556 linenr_T lnum = 0;
11557 pos_T *fp;
11558
11559 fp = var2fpos(&argvars[0], TRUE);
11560 if (fp != NULL)
11561 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563}
11564
11565/*
11566 * "line2byte(lnum)" function
11567 */
11568/*ARGSUSED*/
11569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576#else
11577 linenr_T lnum;
11578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11584 if (rettv->vval.v_number >= 0)
11585 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586#endif
11587}
11588
11589/*
11590 * "lispindent(lnum)" function
11591 */
11592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011594 typval_T *argvars;
11595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596{
11597#ifdef FEAT_LISP
11598 pos_T pos;
11599 linenr_T lnum;
11600
11601 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11604 {
11605 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 curwin->w_cursor = pos;
11608 }
11609 else
11610#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612}
11613
11614/*
11615 * "localtime()" function
11616 */
11617/*ARGSUSED*/
11618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011620 typval_T *argvars;
11621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624}
11625
Bram Moolenaar33570922005-01-25 22:26:29 +000011626static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627
11628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011630 typval_T *argvars;
11631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 int exact;
11633{
11634 char_u *keys;
11635 char_u *which;
11636 char_u buf[NUMBUFLEN];
11637 char_u *keys_buf = NULL;
11638 char_u *rhs;
11639 int mode;
11640 garray_T ga;
11641
11642 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->v_type = VAR_STRING;
11644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 if (*keys == NUL)
11648 return;
11649
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011650 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 else
11653 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 if (which == NULL)
11655 return;
11656
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 mode = get_map_mode(&which, 0);
11658
11659 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011660 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 vim_free(keys_buf);
11662 if (rhs != NULL)
11663 {
11664 ga_init(&ga);
11665 ga.ga_itemsize = 1;
11666 ga.ga_growsize = 40;
11667
11668 while (*rhs != NUL)
11669 ga_concat(&ga, str2special(&rhs, FALSE));
11670
11671 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 }
11674}
11675
11676/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011677 * "map()" function
11678 */
11679 static void
11680f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *argvars;
11682 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011683{
11684 filter_map(argvars, rettv, TRUE);
11685}
11686
11687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011688 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 */
11690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011691f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 typval_T *argvars;
11693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011695 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696}
11697
11698/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 */
11701 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011702f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 typval_T *argvars;
11704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011706 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707}
11708
Bram Moolenaar33570922005-01-25 22:26:29 +000011709static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710
11711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011713 typval_T *argvars;
11714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 int type;
11716{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011717 char_u *str = NULL;
11718 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 char_u *pat;
11720 regmatch_T regmatch;
11721 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011722 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 char_u *save_cpo;
11724 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011725 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011726 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011727 list_T *l = NULL;
11728 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011729 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011730 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731
11732 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11733 save_cpo = p_cpo;
11734 p_cpo = (char_u *)"";
11735
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011736 rettv->vval.v_number = -1;
11737 if (type == 3)
11738 {
11739 /* return empty list when there are no matches */
11740 if ((rettv->vval.v_list = list_alloc()) == NULL)
11741 goto theend;
11742 rettv->v_type = VAR_LIST;
11743 ++rettv->vval.v_list->lv_refcount;
11744 }
11745 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747 rettv->v_type = VAR_STRING;
11748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011751 if (argvars[0].v_type == VAR_LIST)
11752 {
11753 if ((l = argvars[0].vval.v_list) == NULL)
11754 goto theend;
11755 li = l->lv_first;
11756 }
11757 else
11758 expr = str = get_tv_string(&argvars[0]);
11759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011760 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11761 if (pat == NULL)
11762 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011763
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011764 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 int error = FALSE;
11767
11768 start = get_tv_number_chk(&argvars[2], &error);
11769 if (error)
11770 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011771 if (l != NULL)
11772 {
11773 li = list_find(l, start);
11774 if (li == NULL)
11775 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011776 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011777 }
11778 else
11779 {
11780 if (start < 0)
11781 start = 0;
11782 if (start > (long)STRLEN(str))
11783 goto theend;
11784 str += start;
11785 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011786
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011787 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788 nth = get_tv_number_chk(&argvars[3], &error);
11789 if (error)
11790 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 }
11792
11793 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11794 if (regmatch.regprog != NULL)
11795 {
11796 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011797
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011798 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011799 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011800 if (l != NULL)
11801 {
11802 if (li == NULL)
11803 {
11804 match = FALSE;
11805 break;
11806 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011807 vim_free(tofree);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011808 str = echo_string(&li->li_tv, &tofree, strbuf,0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011809 if (str == NULL)
11810 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011811 }
11812
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011813 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011814
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011815 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011816 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011817 if (l == NULL && !match)
11818 break;
11819
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011820 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011821 if (l != NULL)
11822 {
11823 li = li->li_next;
11824 ++idx;
11825 }
11826 else
11827 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011828#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011829 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011830#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011831 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011832#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011833 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011834 }
11835
11836 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011838 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011839 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011840 int i;
11841
11842 /* return list with matched string and submatches */
11843 for (i = 0; i < NSUBEXP; ++i)
11844 {
11845 if (regmatch.endp[i] == NULL)
11846 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011847 if (list_append_string(rettv->vval.v_list,
11848 regmatch.startp[i],
11849 (int)(regmatch.endp[i] - regmatch.startp[i]))
11850 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011851 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011852 }
11853 }
11854 else if (type == 2)
11855 {
11856 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011857 if (l != NULL)
11858 copy_tv(&li->li_tv, rettv);
11859 else
11860 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011862 }
11863 else if (l != NULL)
11864 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 else
11866 {
11867 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 (varnumber_T)(regmatch.startp[0] - str);
11870 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 }
11875 }
11876 vim_free(regmatch.regprog);
11877 }
11878
11879theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011880 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 p_cpo = save_cpo;
11882}
11883
11884/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011885 * "match()" function
11886 */
11887 static void
11888f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011889 typval_T *argvars;
11890 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891{
11892 find_some_match(argvars, rettv, 1);
11893}
11894
11895/*
11896 * "matchend()" function
11897 */
11898 static void
11899f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011900 typval_T *argvars;
11901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902{
11903 find_some_match(argvars, rettv, 0);
11904}
11905
11906/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011907 * "matchlist()" function
11908 */
11909 static void
11910f_matchlist(argvars, rettv)
11911 typval_T *argvars;
11912 typval_T *rettv;
11913{
11914 find_some_match(argvars, rettv, 3);
11915}
11916
11917/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011918 * "matchstr()" function
11919 */
11920 static void
11921f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011922 typval_T *argvars;
11923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011924{
11925 find_some_match(argvars, rettv, 2);
11926}
11927
Bram Moolenaar33570922005-01-25 22:26:29 +000011928static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011929
11930 static void
11931max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011932 typval_T *argvars;
11933 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011934 int domax;
11935{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011936 long n = 0;
11937 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011939
11940 if (argvars[0].v_type == VAR_LIST)
11941 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011942 list_T *l;
11943 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011944
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011945 l = argvars[0].vval.v_list;
11946 if (l != NULL)
11947 {
11948 li = l->lv_first;
11949 if (li != NULL)
11950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011952 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011953 {
11954 li = li->li_next;
11955 if (li == NULL)
11956 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011958 if (domax ? i > n : i < n)
11959 n = i;
11960 }
11961 }
11962 }
11963 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011964 else if (argvars[0].v_type == VAR_DICT)
11965 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011966 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011967 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011969 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970
11971 d = argvars[0].vval.v_dict;
11972 if (d != NULL)
11973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011974 todo = d->dv_hashtab.ht_used;
11975 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011977 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011978 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011979 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011981 if (first)
11982 {
11983 n = i;
11984 first = FALSE;
11985 }
11986 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011987 n = i;
11988 }
11989 }
11990 }
11991 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011992 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011993 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011995}
11996
11997/*
11998 * "max()" function
11999 */
12000 static void
12001f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012004{
12005 max_min(argvars, rettv, TRUE);
12006}
12007
12008/*
12009 * "min()" function
12010 */
12011 static void
12012f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012013 typval_T *argvars;
12014 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012015{
12016 max_min(argvars, rettv, FALSE);
12017}
12018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012019static int mkdir_recurse __ARGS((char_u *dir, int prot));
12020
12021/*
12022 * Create the directory in which "dir" is located, and higher levels when
12023 * needed.
12024 */
12025 static int
12026mkdir_recurse(dir, prot)
12027 char_u *dir;
12028 int prot;
12029{
12030 char_u *p;
12031 char_u *updir;
12032 int r = FAIL;
12033
12034 /* Get end of directory name in "dir".
12035 * We're done when it's "/" or "c:/". */
12036 p = gettail_sep(dir);
12037 if (p <= get_past_head(dir))
12038 return OK;
12039
12040 /* If the directory exists we're done. Otherwise: create it.*/
12041 updir = vim_strnsave(dir, (int)(p - dir));
12042 if (updir == NULL)
12043 return FAIL;
12044 if (mch_isdir(updir))
12045 r = OK;
12046 else if (mkdir_recurse(updir, prot) == OK)
12047 r = vim_mkdir_emsg(updir, prot);
12048 vim_free(updir);
12049 return r;
12050}
12051
12052#ifdef vim_mkdir
12053/*
12054 * "mkdir()" function
12055 */
12056 static void
12057f_mkdir(argvars, rettv)
12058 typval_T *argvars;
12059 typval_T *rettv;
12060{
12061 char_u *dir;
12062 char_u buf[NUMBUFLEN];
12063 int prot = 0755;
12064
12065 rettv->vval.v_number = FAIL;
12066 if (check_restricted() || check_secure())
12067 return;
12068
12069 dir = get_tv_string_buf(&argvars[0], buf);
12070 if (argvars[1].v_type != VAR_UNKNOWN)
12071 {
12072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 prot = get_tv_number_chk(&argvars[2], NULL);
12074 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012075 mkdir_recurse(dir, prot);
12076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012078}
12079#endif
12080
Bram Moolenaar0d660222005-01-07 21:51:51 +000012081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 * "mode()" function
12083 */
12084/*ARGSUSED*/
12085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012087 typval_T *argvars;
12088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
12090 char_u buf[2];
12091
12092#ifdef FEAT_VISUAL
12093 if (VIsual_active)
12094 {
12095 if (VIsual_select)
12096 buf[0] = VIsual_mode + 's' - 'v';
12097 else
12098 buf[0] = VIsual_mode;
12099 }
12100 else
12101#endif
12102 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12103 buf[0] = 'r';
12104 else if (State & INSERT)
12105 {
12106 if (State & REPLACE_FLAG)
12107 buf[0] = 'R';
12108 else
12109 buf[0] = 'i';
12110 }
12111 else if (State & CMDLINE)
12112 buf[0] = 'c';
12113 else
12114 buf[0] = 'n';
12115
12116 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->vval.v_string = vim_strsave(buf);
12118 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119}
12120
12121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122 * "nextnonblank()" function
12123 */
12124 static void
12125f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 typval_T *argvars;
12127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128{
12129 linenr_T lnum;
12130
12131 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012133 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134 {
12135 lnum = 0;
12136 break;
12137 }
12138 if (*skipwhite(ml_get(lnum)) != NUL)
12139 break;
12140 }
12141 rettv->vval.v_number = lnum;
12142}
12143
12144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 * "nr2char()" function
12146 */
12147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012149 typval_T *argvars;
12150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151{
12152 char_u buf[NUMBUFLEN];
12153
12154#ifdef FEAT_MBYTE
12155 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 else
12158#endif
12159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 buf[1] = NUL;
12162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163 rettv->v_type = VAR_STRING;
12164 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012165}
12166
12167/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012168 * "prevnonblank()" function
12169 */
12170 static void
12171f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012172 typval_T *argvars;
12173 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012174{
12175 linenr_T lnum;
12176
12177 lnum = get_tv_lnum(argvars);
12178 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12179 lnum = 0;
12180 else
12181 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12182 --lnum;
12183 rettv->vval.v_number = lnum;
12184}
12185
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012186#ifdef HAVE_STDARG_H
12187/* This dummy va_list is here because:
12188 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12189 * - locally in the function results in a "used before set" warning
12190 * - using va_start() to initialize it gives "function with fixed args" error */
12191static va_list ap;
12192#endif
12193
Bram Moolenaar8c711452005-01-14 21:53:12 +000012194/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012195 * "printf()" function
12196 */
12197 static void
12198f_printf(argvars, rettv)
12199 typval_T *argvars;
12200 typval_T *rettv;
12201{
12202 rettv->v_type = VAR_STRING;
12203 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012204#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012205 {
12206 char_u buf[NUMBUFLEN];
12207 int len;
12208 char_u *s;
12209 int saved_did_emsg = did_emsg;
12210 char *fmt;
12211
12212 /* Get the required length, allocate the buffer and do it for real. */
12213 did_emsg = FALSE;
12214 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012215 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012216 if (!did_emsg)
12217 {
12218 s = alloc(len + 1);
12219 if (s != NULL)
12220 {
12221 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012222 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012223 }
12224 }
12225 did_emsg |= saved_did_emsg;
12226 }
12227#endif
12228}
12229
12230/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012231 * "pumvisible()" function
12232 */
12233/*ARGSUSED*/
12234 static void
12235f_pumvisible(argvars, rettv)
12236 typval_T *argvars;
12237 typval_T *rettv;
12238{
12239 rettv->vval.v_number = 0;
12240#ifdef FEAT_INS_EXPAND
12241 if (pum_visible())
12242 rettv->vval.v_number = 1;
12243#endif
12244}
12245
12246/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012247 * "range()" function
12248 */
12249 static void
12250f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012251 typval_T *argvars;
12252 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012253{
12254 long start;
12255 long end;
12256 long stride = 1;
12257 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012258 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012259 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012261 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012262 if (argvars[1].v_type == VAR_UNKNOWN)
12263 {
12264 end = start - 1;
12265 start = 0;
12266 }
12267 else
12268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012272 }
12273
12274 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 if (error)
12276 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012277 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012278 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012279 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012280 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012281 else
12282 {
12283 l = list_alloc();
12284 if (l != NULL)
12285 {
12286 rettv->v_type = VAR_LIST;
12287 rettv->vval.v_list = l;
12288 ++l->lv_refcount;
12289
12290 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012291 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012292 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012293 }
12294 }
12295}
12296
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012297/*
12298 * "readfile()" function
12299 */
12300 static void
12301f_readfile(argvars, rettv)
12302 typval_T *argvars;
12303 typval_T *rettv;
12304{
12305 int binary = FALSE;
12306 char_u *fname;
12307 FILE *fd;
12308 list_T *l;
12309 listitem_T *li;
12310#define FREAD_SIZE 200 /* optimized for text lines */
12311 char_u buf[FREAD_SIZE];
12312 int readlen; /* size of last fread() */
12313 int buflen; /* nr of valid chars in buf[] */
12314 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12315 int tolist; /* first byte in buf[] still to be put in list */
12316 int chop; /* how many CR to chop off */
12317 char_u *prev = NULL; /* previously read bytes, if any */
12318 int prevlen = 0; /* length of "prev" if not NULL */
12319 char_u *s;
12320 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012321 long maxline = MAXLNUM;
12322 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012323
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012324 if (argvars[1].v_type != VAR_UNKNOWN)
12325 {
12326 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12327 binary = TRUE;
12328 if (argvars[2].v_type != VAR_UNKNOWN)
12329 maxline = get_tv_number(&argvars[2]);
12330 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012331
12332 l = list_alloc();
12333 if (l == NULL)
12334 return;
12335 rettv->v_type = VAR_LIST;
12336 rettv->vval.v_list = l;
12337 l->lv_refcount = 1;
12338
12339 /* Always open the file in binary mode, library functions have a mind of
12340 * their own about CR-LF conversion. */
12341 fname = get_tv_string(&argvars[0]);
12342 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12343 {
12344 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12345 return;
12346 }
12347
12348 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012349 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012350 {
12351 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12352 buflen = filtd + readlen;
12353 tolist = 0;
12354 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12355 {
12356 if (buf[filtd] == '\n' || readlen <= 0)
12357 {
12358 /* Only when in binary mode add an empty list item when the
12359 * last line ends in a '\n'. */
12360 if (!binary && readlen == 0 && filtd == 0)
12361 break;
12362
12363 /* Found end-of-line or end-of-file: add a text line to the
12364 * list. */
12365 chop = 0;
12366 if (!binary)
12367 while (filtd - chop - 1 >= tolist
12368 && buf[filtd - chop - 1] == '\r')
12369 ++chop;
12370 len = filtd - tolist - chop;
12371 if (prev == NULL)
12372 s = vim_strnsave(buf + tolist, len);
12373 else
12374 {
12375 s = alloc((unsigned)(prevlen + len + 1));
12376 if (s != NULL)
12377 {
12378 mch_memmove(s, prev, prevlen);
12379 vim_free(prev);
12380 prev = NULL;
12381 mch_memmove(s + prevlen, buf + tolist, len);
12382 s[prevlen + len] = NUL;
12383 }
12384 }
12385 tolist = filtd + 1;
12386
12387 li = listitem_alloc();
12388 if (li == NULL)
12389 {
12390 vim_free(s);
12391 break;
12392 }
12393 li->li_tv.v_type = VAR_STRING;
12394 li->li_tv.v_lock = 0;
12395 li->li_tv.vval.v_string = s;
12396 list_append(l, li);
12397
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012398 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012399 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012400 if (readlen <= 0)
12401 break;
12402 }
12403 else if (buf[filtd] == NUL)
12404 buf[filtd] = '\n';
12405 }
12406 if (readlen <= 0)
12407 break;
12408
12409 if (tolist == 0)
12410 {
12411 /* "buf" is full, need to move text to an allocated buffer */
12412 if (prev == NULL)
12413 {
12414 prev = vim_strnsave(buf, buflen);
12415 prevlen = buflen;
12416 }
12417 else
12418 {
12419 s = alloc((unsigned)(prevlen + buflen));
12420 if (s != NULL)
12421 {
12422 mch_memmove(s, prev, prevlen);
12423 mch_memmove(s + prevlen, buf, buflen);
12424 vim_free(prev);
12425 prev = s;
12426 prevlen += buflen;
12427 }
12428 }
12429 filtd = 0;
12430 }
12431 else
12432 {
12433 mch_memmove(buf, buf + tolist, buflen - tolist);
12434 filtd -= tolist;
12435 }
12436 }
12437
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012438 /*
12439 * For a negative line count use only the lines at the end of the file,
12440 * free the rest.
12441 */
12442 if (maxline < 0)
12443 while (cnt > -maxline)
12444 {
12445 listitem_remove(l, l->lv_first);
12446 --cnt;
12447 }
12448
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012449 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012450 fclose(fd);
12451}
12452
12453
Bram Moolenaar0d660222005-01-07 21:51:51 +000012454#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12455static void make_connection __ARGS((void));
12456static int check_connection __ARGS((void));
12457
12458 static void
12459make_connection()
12460{
12461 if (X_DISPLAY == NULL
12462# ifdef FEAT_GUI
12463 && !gui.in_use
12464# endif
12465 )
12466 {
12467 x_force_connect = TRUE;
12468 setup_term_clip();
12469 x_force_connect = FALSE;
12470 }
12471}
12472
12473 static int
12474check_connection()
12475{
12476 make_connection();
12477 if (X_DISPLAY == NULL)
12478 {
12479 EMSG(_("E240: No connection to Vim server"));
12480 return FAIL;
12481 }
12482 return OK;
12483}
12484#endif
12485
12486#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012487static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012488
12489 static void
12490remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012491 typval_T *argvars;
12492 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012493 int expr;
12494{
12495 char_u *server_name;
12496 char_u *keys;
12497 char_u *r = NULL;
12498 char_u buf[NUMBUFLEN];
12499# ifdef WIN32
12500 HWND w;
12501# else
12502 Window w;
12503# endif
12504
12505 if (check_restricted() || check_secure())
12506 return;
12507
12508# ifdef FEAT_X11
12509 if (check_connection() == FAIL)
12510 return;
12511# endif
12512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 server_name = get_tv_string_chk(&argvars[0]);
12514 if (server_name == NULL)
12515 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516 keys = get_tv_string_buf(&argvars[1], buf);
12517# ifdef WIN32
12518 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12519# else
12520 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12521 < 0)
12522# endif
12523 {
12524 if (r != NULL)
12525 EMSG(r); /* sending worked but evaluation failed */
12526 else
12527 EMSG2(_("E241: Unable to send to %s"), server_name);
12528 return;
12529 }
12530
12531 rettv->vval.v_string = r;
12532
12533 if (argvars[2].v_type != VAR_UNKNOWN)
12534 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012536 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538
12539 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012540 v.di_tv.v_type = VAR_STRING;
12541 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 idvar = get_tv_string_chk(&argvars[2]);
12543 if (idvar != NULL)
12544 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012545 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012546 }
12547}
12548#endif
12549
12550/*
12551 * "remote_expr()" function
12552 */
12553/*ARGSUSED*/
12554 static void
12555f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 typval_T *argvars;
12557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558{
12559 rettv->v_type = VAR_STRING;
12560 rettv->vval.v_string = NULL;
12561#ifdef FEAT_CLIENTSERVER
12562 remote_common(argvars, rettv, TRUE);
12563#endif
12564}
12565
12566/*
12567 * "remote_foreground()" function
12568 */
12569/*ARGSUSED*/
12570 static void
12571f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012572 typval_T *argvars;
12573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574{
12575 rettv->vval.v_number = 0;
12576#ifdef FEAT_CLIENTSERVER
12577# ifdef WIN32
12578 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012579 {
12580 char_u *server_name = get_tv_string_chk(&argvars[0]);
12581
12582 if (server_name != NULL)
12583 serverForeground(server_name);
12584 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585# else
12586 /* Send a foreground() expression to the server. */
12587 argvars[1].v_type = VAR_STRING;
12588 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12589 argvars[2].v_type = VAR_UNKNOWN;
12590 remote_common(argvars, rettv, TRUE);
12591 vim_free(argvars[1].vval.v_string);
12592# endif
12593#endif
12594}
12595
12596/*ARGSUSED*/
12597 static void
12598f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 typval_T *argvars;
12600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012601{
12602#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012603 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012604 char_u *s = NULL;
12605# ifdef WIN32
12606 int n = 0;
12607# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012608 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609
12610 if (check_restricted() || check_secure())
12611 {
12612 rettv->vval.v_number = -1;
12613 return;
12614 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 serverid = get_tv_string_chk(&argvars[0]);
12616 if (serverid == NULL)
12617 {
12618 rettv->vval.v_number = -1;
12619 return; /* type error; errmsg already given */
12620 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012621# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012622 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012623 if (n == 0)
12624 rettv->vval.v_number = -1;
12625 else
12626 {
12627 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12628 rettv->vval.v_number = (s != NULL);
12629 }
12630# else
12631 rettv->vval.v_number = 0;
12632 if (check_connection() == FAIL)
12633 return;
12634
12635 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012637# endif
12638
12639 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 char_u *retvar;
12642
Bram Moolenaar33570922005-01-25 22:26:29 +000012643 v.di_tv.v_type = VAR_STRING;
12644 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645 retvar = get_tv_string_chk(&argvars[1]);
12646 if (retvar != NULL)
12647 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012648 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012649 }
12650#else
12651 rettv->vval.v_number = -1;
12652#endif
12653}
12654
12655/*ARGSUSED*/
12656 static void
12657f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 typval_T *argvars;
12659 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012660{
12661 char_u *r = NULL;
12662
12663#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664 char_u *serverid = get_tv_string_chk(&argvars[0]);
12665
12666 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 {
12668# ifdef WIN32
12669 /* The server's HWND is encoded in the 'id' parameter */
12670 int n = 0;
12671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012673 if (n != 0)
12674 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12675 if (r == NULL)
12676# else
12677 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012678 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012679# endif
12680 EMSG(_("E277: Unable to read a server reply"));
12681 }
12682#endif
12683 rettv->v_type = VAR_STRING;
12684 rettv->vval.v_string = r;
12685}
12686
12687/*
12688 * "remote_send()" function
12689 */
12690/*ARGSUSED*/
12691 static void
12692f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 typval_T *argvars;
12694 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012695{
12696 rettv->v_type = VAR_STRING;
12697 rettv->vval.v_string = NULL;
12698#ifdef FEAT_CLIENTSERVER
12699 remote_common(argvars, rettv, FALSE);
12700#endif
12701}
12702
12703/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012704 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012705 */
12706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012708 typval_T *argvars;
12709 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710{
Bram Moolenaar33570922005-01-25 22:26:29 +000012711 list_T *l;
12712 listitem_T *item, *item2;
12713 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012714 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012715 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012716 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012717 dict_T *d;
12718 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012719
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012720 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012721 if (argvars[0].v_type == VAR_DICT)
12722 {
12723 if (argvars[2].v_type != VAR_UNKNOWN)
12724 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012725 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012726 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 key = get_tv_string_chk(&argvars[1]);
12729 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 di = dict_find(d, key, -1);
12732 if (di == NULL)
12733 EMSG2(_(e_dictkey), key);
12734 else
12735 {
12736 *rettv = di->di_tv;
12737 init_tv(&di->di_tv);
12738 dictitem_remove(d, di);
12739 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012741 }
12742 }
12743 else if (argvars[0].v_type != VAR_LIST)
12744 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012745 else if ((l = argvars[0].vval.v_list) != NULL
12746 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012748 int error = FALSE;
12749
12750 idx = get_tv_number_chk(&argvars[1], &error);
12751 if (error)
12752 ; /* type error: do nothing, errmsg already given */
12753 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012754 EMSGN(_(e_listidx), idx);
12755 else
12756 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012757 if (argvars[2].v_type == VAR_UNKNOWN)
12758 {
12759 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012760 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012761 *rettv = item->li_tv;
12762 vim_free(item);
12763 }
12764 else
12765 {
12766 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 end = get_tv_number_chk(&argvars[2], &error);
12768 if (error)
12769 ; /* type error: do nothing */
12770 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012771 EMSGN(_(e_listidx), end);
12772 else
12773 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012774 int cnt = 0;
12775
12776 for (li = item; li != NULL; li = li->li_next)
12777 {
12778 ++cnt;
12779 if (li == item2)
12780 break;
12781 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012782 if (li == NULL) /* didn't find "item2" after "item" */
12783 EMSG(_(e_invrange));
12784 else
12785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012786 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012787 l = list_alloc();
12788 if (l != NULL)
12789 {
12790 rettv->v_type = VAR_LIST;
12791 rettv->vval.v_list = l;
12792 l->lv_first = item;
12793 l->lv_last = item2;
12794 l->lv_refcount = 1;
12795 item->li_prev = NULL;
12796 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012797 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012798 }
12799 }
12800 }
12801 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012802 }
12803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804}
12805
12806/*
12807 * "rename({from}, {to})" function
12808 */
12809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 typval_T *argvars;
12812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813{
12814 char_u buf[NUMBUFLEN];
12815
12816 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12820 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821}
12822
12823/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012824 * "repeat()" function
12825 */
12826/*ARGSUSED*/
12827 static void
12828f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *argvars;
12830 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012831{
12832 char_u *p;
12833 int n;
12834 int slen;
12835 int len;
12836 char_u *r;
12837 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012839
12840 n = get_tv_number(&argvars[1]);
12841 if (argvars[0].v_type == VAR_LIST)
12842 {
12843 l = list_alloc();
12844 if (l != NULL && argvars[0].vval.v_list != NULL)
12845 {
12846 l->lv_refcount = 1;
12847 while (n-- > 0)
12848 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12849 break;
12850 }
12851 rettv->v_type = VAR_LIST;
12852 rettv->vval.v_list = l;
12853 }
12854 else
12855 {
12856 p = get_tv_string(&argvars[0]);
12857 rettv->v_type = VAR_STRING;
12858 rettv->vval.v_string = NULL;
12859
12860 slen = (int)STRLEN(p);
12861 len = slen * n;
12862 if (len <= 0)
12863 return;
12864
12865 r = alloc(len + 1);
12866 if (r != NULL)
12867 {
12868 for (i = 0; i < n; i++)
12869 mch_memmove(r + i * slen, p, (size_t)slen);
12870 r[len] = NUL;
12871 }
12872
12873 rettv->vval.v_string = r;
12874 }
12875}
12876
12877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 * "resolve()" function
12879 */
12880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012882 typval_T *argvars;
12883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884{
12885 char_u *p;
12886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888#ifdef FEAT_SHORTCUT
12889 {
12890 char_u *v = NULL;
12891
12892 v = mch_resolve_shortcut(p);
12893 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 }
12898#else
12899# ifdef HAVE_READLINK
12900 {
12901 char_u buf[MAXPATHL + 1];
12902 char_u *cpy;
12903 int len;
12904 char_u *remain = NULL;
12905 char_u *q;
12906 int is_relative_to_current = FALSE;
12907 int has_trailing_pathsep = FALSE;
12908 int limit = 100;
12909
12910 p = vim_strsave(p);
12911
12912 if (p[0] == '.' && (vim_ispathsep(p[1])
12913 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12914 is_relative_to_current = TRUE;
12915
12916 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012917 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 has_trailing_pathsep = TRUE;
12919
12920 q = getnextcomp(p);
12921 if (*q != NUL)
12922 {
12923 /* Separate the first path component in "p", and keep the
12924 * remainder (beginning with the path separator). */
12925 remain = vim_strsave(q - 1);
12926 q[-1] = NUL;
12927 }
12928
12929 for (;;)
12930 {
12931 for (;;)
12932 {
12933 len = readlink((char *)p, (char *)buf, MAXPATHL);
12934 if (len <= 0)
12935 break;
12936 buf[len] = NUL;
12937
12938 if (limit-- == 0)
12939 {
12940 vim_free(p);
12941 vim_free(remain);
12942 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944 goto fail;
12945 }
12946
12947 /* Ensure that the result will have a trailing path separator
12948 * if the argument has one. */
12949 if (remain == NULL && has_trailing_pathsep)
12950 add_pathsep(buf);
12951
12952 /* Separate the first path component in the link value and
12953 * concatenate the remainders. */
12954 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12955 if (*q != NUL)
12956 {
12957 if (remain == NULL)
12958 remain = vim_strsave(q - 1);
12959 else
12960 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000012961 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 if (cpy != NULL)
12963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 vim_free(remain);
12965 remain = cpy;
12966 }
12967 }
12968 q[-1] = NUL;
12969 }
12970
12971 q = gettail(p);
12972 if (q > p && *q == NUL)
12973 {
12974 /* Ignore trailing path separator. */
12975 q[-1] = NUL;
12976 q = gettail(p);
12977 }
12978 if (q > p && !mch_isFullName(buf))
12979 {
12980 /* symlink is relative to directory of argument */
12981 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12982 if (cpy != NULL)
12983 {
12984 STRCPY(cpy, p);
12985 STRCPY(gettail(cpy), buf);
12986 vim_free(p);
12987 p = cpy;
12988 }
12989 }
12990 else
12991 {
12992 vim_free(p);
12993 p = vim_strsave(buf);
12994 }
12995 }
12996
12997 if (remain == NULL)
12998 break;
12999
13000 /* Append the first path component of "remain" to "p". */
13001 q = getnextcomp(remain + 1);
13002 len = q - remain - (*q != NUL);
13003 cpy = vim_strnsave(p, STRLEN(p) + len);
13004 if (cpy != NULL)
13005 {
13006 STRNCAT(cpy, remain, len);
13007 vim_free(p);
13008 p = cpy;
13009 }
13010 /* Shorten "remain". */
13011 if (*q != NUL)
13012 STRCPY(remain, q - 1);
13013 else
13014 {
13015 vim_free(remain);
13016 remain = NULL;
13017 }
13018 }
13019
13020 /* If the result is a relative path name, make it explicitly relative to
13021 * the current directory if and only if the argument had this form. */
13022 if (!vim_ispathsep(*p))
13023 {
13024 if (is_relative_to_current
13025 && *p != NUL
13026 && !(p[0] == '.'
13027 && (p[1] == NUL
13028 || vim_ispathsep(p[1])
13029 || (p[1] == '.'
13030 && (p[2] == NUL
13031 || vim_ispathsep(p[2]))))))
13032 {
13033 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013034 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 if (cpy != NULL)
13036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 vim_free(p);
13038 p = cpy;
13039 }
13040 }
13041 else if (!is_relative_to_current)
13042 {
13043 /* Strip leading "./". */
13044 q = p;
13045 while (q[0] == '.' && vim_ispathsep(q[1]))
13046 q += 2;
13047 if (q > p)
13048 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13049 }
13050 }
13051
13052 /* Ensure that the result will have no trailing path separator
13053 * if the argument had none. But keep "/" or "//". */
13054 if (!has_trailing_pathsep)
13055 {
13056 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013057 if (after_pathsep(p, q))
13058 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 }
13060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 }
13063# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065# endif
13066#endif
13067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069
13070#ifdef HAVE_READLINK
13071fail:
13072#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074}
13075
13076/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013077 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 */
13079 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013080f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013081 typval_T *argvars;
13082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083{
Bram Moolenaar33570922005-01-25 22:26:29 +000013084 list_T *l;
13085 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086
Bram Moolenaar0d660222005-01-07 21:51:51 +000013087 rettv->vval.v_number = 0;
13088 if (argvars[0].v_type != VAR_LIST)
13089 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013090 else if ((l = argvars[0].vval.v_list) != NULL
13091 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013092 {
13093 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013094 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013095 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013096 while (li != NULL)
13097 {
13098 ni = li->li_prev;
13099 list_append(l, li);
13100 li = ni;
13101 }
13102 rettv->vval.v_list = l;
13103 rettv->v_type = VAR_LIST;
13104 ++l->lv_refcount;
13105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106}
13107
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013108#define SP_NOMOVE 1 /* don't move cursor */
13109#define SP_REPEAT 2 /* repeat to find outer pair */
13110#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000013111#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013112
Bram Moolenaar33570922005-01-25 22:26:29 +000013113static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013114
13115/*
13116 * Get flags for a search function.
13117 * Possibly sets "p_ws".
13118 * Returns BACKWARD, FORWARD or zero (for an error).
13119 */
13120 static int
13121get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013122 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013123 int *flagsp;
13124{
13125 int dir = FORWARD;
13126 char_u *flags;
13127 char_u nbuf[NUMBUFLEN];
13128 int mask;
13129
13130 if (varp->v_type != VAR_UNKNOWN)
13131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013132 flags = get_tv_string_buf_chk(varp, nbuf);
13133 if (flags == NULL)
13134 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135 while (*flags != NUL)
13136 {
13137 switch (*flags)
13138 {
13139 case 'b': dir = BACKWARD; break;
13140 case 'w': p_ws = TRUE; break;
13141 case 'W': p_ws = FALSE; break;
13142 default: mask = 0;
13143 if (flagsp != NULL)
13144 switch (*flags)
13145 {
13146 case 'n': mask = SP_NOMOVE; break;
13147 case 'r': mask = SP_REPEAT; break;
13148 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013149 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013150 }
13151 if (mask == 0)
13152 {
13153 EMSG2(_(e_invarg2), flags);
13154 dir = 0;
13155 }
13156 else
13157 *flagsp |= mask;
13158 }
13159 if (dir == 0)
13160 break;
13161 ++flags;
13162 }
13163 }
13164 return dir;
13165}
13166
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013168 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013170 static int
13171search_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013172 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013173 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174{
13175 char_u *pat;
13176 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013177 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 int save_p_ws = p_ws;
13179 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013180 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013181 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013183 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013184 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13185 if (dir == 0)
13186 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013187 /*
13188 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13189 * Check to make sure only those flags are set.
13190 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13191 * flags cannot be set. Check for that condition also.
13192 */
13193 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13194 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013197 goto theend;
13198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013200 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13202 SEARCH_KEEP, RE_SEARCH) != FAIL)
13203 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013204 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013205 if (flags & SP_SETPCMARK)
13206 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013208 if (match_pos != NULL)
13209 {
13210 /* Store the match cursor position */
13211 match_pos->lnum = pos.lnum;
13212 match_pos->col = pos.col + 1;
13213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 /* "/$" will put the cursor after the end of the line, may need to
13215 * correct that here */
13216 check_cursor();
13217 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013218
13219 /* If 'n' flag is used: restore cursor position. */
13220 if (flags & SP_NOMOVE)
13221 curwin->w_cursor = save_cursor;
13222theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013224
13225 return retval;
13226}
13227
13228/*
13229 * "search()" function
13230 */
13231 static void
13232f_search(argvars, rettv)
13233 typval_T *argvars;
13234 typval_T *rettv;
13235{
13236 rettv->vval.v_number = search_cmn(argvars, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237}
13238
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013240 * "searchdecl()" function
13241 */
13242 static void
13243f_searchdecl(argvars, rettv)
13244 typval_T *argvars;
13245 typval_T *rettv;
13246{
13247 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013248 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013249 int error = FALSE;
13250 char_u *name;
13251
13252 rettv->vval.v_number = 1; /* default: FAIL */
13253
13254 name = get_tv_string_chk(&argvars[0]);
13255 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013256 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013257 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013258 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13259 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13260 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013261 if (!error && name != NULL)
13262 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013263 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013264}
13265
13266/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013267 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013269 static int
13270searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013272 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273{
13274 char_u *spat, *mpat, *epat;
13275 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 int dir;
13278 int flags = 0;
13279 char_u nbuf1[NUMBUFLEN];
13280 char_u nbuf2[NUMBUFLEN];
13281 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013282 int retval = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 spat = get_tv_string_chk(&argvars[0]);
13286 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13287 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13288 if (spat == NULL || mpat == NULL || epat == NULL)
13289 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 /* Handle the optional fourth argument: flags */
13292 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013293 if (dir == 0)
13294 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013295 /*
13296 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13297 */
13298 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13299 {
13300 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13301 goto theend;
13302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013304 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013305 if (argvars[3].v_type == VAR_UNKNOWN
13306 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 skip = (char_u *)"";
13308 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013309 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13310 if (skip == NULL)
13311 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013313 retval = do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013314
13315theend:
13316 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013317
13318 return retval;
13319}
13320
13321/*
13322 * "searchpair()" function
13323 */
13324 static void
13325f_searchpair(argvars, rettv)
13326 typval_T *argvars;
13327 typval_T *rettv;
13328{
13329 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13330}
13331
13332/*
13333 * "searchpairpos()" function
13334 */
13335 static void
13336f_searchpairpos(argvars, rettv)
13337 typval_T *argvars;
13338 typval_T *rettv;
13339{
13340 list_T *l;
13341 pos_T match_pos;
13342 int lnum = 0;
13343 int col = 0;
13344
13345 rettv->vval.v_number = 0;
13346
13347 l = list_alloc();
13348 if (l == NULL)
13349 return;
13350 rettv->v_type = VAR_LIST;
13351 rettv->vval.v_list = l;
13352 ++l->lv_refcount;
13353
13354 if (searchpair_cmn(argvars, &match_pos) > 0)
13355 {
13356 lnum = match_pos.lnum;
13357 col = match_pos.col;
13358 }
13359
13360 list_append_number(l, (varnumber_T)lnum);
13361 list_append_number(l, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013362}
13363
13364/*
13365 * Search for a start/middle/end thing.
13366 * Used by searchpair(), see its documentation for the details.
13367 * Returns 0 or -1 for no match,
13368 */
13369 long
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013370do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013371 char_u *spat; /* start pattern */
13372 char_u *mpat; /* middle pattern */
13373 char_u *epat; /* end pattern */
13374 int dir; /* BACKWARD or FORWARD */
13375 char_u *skip; /* skip expression */
13376 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013377 pos_T *match_pos;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013378{
13379 char_u *save_cpo;
13380 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13381 long retval = 0;
13382 pos_T pos;
13383 pos_T firstpos;
13384 pos_T foundpos;
13385 pos_T save_cursor;
13386 pos_T save_pos;
13387 int n;
13388 int r;
13389 int nest = 1;
13390 int err;
13391
13392 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13393 save_cpo = p_cpo;
13394 p_cpo = (char_u *)"";
13395
13396 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13397 * start/middle/end (pat3, for the top pair). */
13398 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13399 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13400 if (pat2 == NULL || pat3 == NULL)
13401 goto theend;
13402 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13403 if (*mpat == NUL)
13404 STRCPY(pat3, pat2);
13405 else
13406 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13407 spat, epat, mpat);
13408
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 save_cursor = curwin->w_cursor;
13410 pos = curwin->w_cursor;
13411 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013412 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 pat = pat3;
13414 for (;;)
13415 {
13416 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13417 SEARCH_KEEP, RE_SEARCH);
13418 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13419 /* didn't find it or found the first match again: FAIL */
13420 break;
13421
13422 if (firstpos.lnum == 0)
13423 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013424 if (equalpos(pos, foundpos))
13425 {
13426 /* Found the same position again. Can happen with a pattern that
13427 * has "\zs" at the end and searching backwards. Advance one
13428 * character and try again. */
13429 if (dir == BACKWARD)
13430 decl(&pos);
13431 else
13432 incl(&pos);
13433 }
13434 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435
13436 /* If the skip pattern matches, ignore this match. */
13437 if (*skip != NUL)
13438 {
13439 save_pos = curwin->w_cursor;
13440 curwin->w_cursor = pos;
13441 r = eval_to_bool(skip, &err, NULL, FALSE);
13442 curwin->w_cursor = save_pos;
13443 if (err)
13444 {
13445 /* Evaluating {skip} caused an error, break here. */
13446 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013447 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 break;
13449 }
13450 if (r)
13451 continue;
13452 }
13453
13454 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13455 {
13456 /* Found end when searching backwards or start when searching
13457 * forward: nested pair. */
13458 ++nest;
13459 pat = pat2; /* nested, don't search for middle */
13460 }
13461 else
13462 {
13463 /* Found end when searching forward or start when searching
13464 * backward: end of (nested) pair; or found middle in outer pair. */
13465 if (--nest == 1)
13466 pat = pat3; /* outer level, search for middle */
13467 }
13468
13469 if (nest == 0)
13470 {
13471 /* Found the match: return matchcount or line number. */
13472 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013473 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013475 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013476 if (flags & SP_SETPCMARK)
13477 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 curwin->w_cursor = pos;
13479 if (!(flags & SP_REPEAT))
13480 break;
13481 nest = 1; /* search for next unmatched */
13482 }
13483 }
13484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013485 if (match_pos != NULL)
13486 {
13487 /* Store the match cursor position */
13488 match_pos->lnum = curwin->w_cursor.lnum;
13489 match_pos->col = curwin->w_cursor.col + 1;
13490 }
13491
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013493 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 curwin->w_cursor = save_cursor;
13495
13496theend:
13497 vim_free(pat2);
13498 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013500
13501 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502}
13503
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013504/*
13505 * "searchpos()" function
13506 */
13507 static void
13508f_searchpos(argvars, rettv)
13509 typval_T *argvars;
13510 typval_T *rettv;
13511{
13512 list_T *l;
13513 pos_T match_pos;
13514 int lnum = 0;
13515 int col = 0;
13516
13517 rettv->vval.v_number = 0;
13518
13519 l = list_alloc();
13520 if (l == NULL)
13521 return;
13522 rettv->v_type = VAR_LIST;
13523 rettv->vval.v_list = l;
13524 ++l->lv_refcount;
13525
13526 if (search_cmn(argvars, &match_pos) > 0)
13527 {
13528 lnum = match_pos.lnum;
13529 col = match_pos.col;
13530 }
13531
13532 list_append_number(l, (varnumber_T)lnum);
13533 list_append_number(l, (varnumber_T)col);
13534
13535}
13536
13537
Bram Moolenaar0d660222005-01-07 21:51:51 +000013538/*ARGSUSED*/
13539 static void
13540f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 typval_T *argvars;
13542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013544#ifdef FEAT_CLIENTSERVER
13545 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 char_u *server = get_tv_string_chk(&argvars[0]);
13547 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550 if (server == NULL || reply == NULL)
13551 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013552 if (check_restricted() || check_secure())
13553 return;
13554# ifdef FEAT_X11
13555 if (check_connection() == FAIL)
13556 return;
13557# endif
13558
13559 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013561 EMSG(_("E258: Unable to send to client"));
13562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013564 rettv->vval.v_number = 0;
13565#else
13566 rettv->vval.v_number = -1;
13567#endif
13568}
13569
13570/*ARGSUSED*/
13571 static void
13572f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *argvars;
13574 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013575{
13576 char_u *r = NULL;
13577
13578#ifdef FEAT_CLIENTSERVER
13579# ifdef WIN32
13580 r = serverGetVimNames();
13581# else
13582 make_connection();
13583 if (X_DISPLAY != NULL)
13584 r = serverGetVimNames(X_DISPLAY);
13585# endif
13586#endif
13587 rettv->v_type = VAR_STRING;
13588 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589}
13590
13591/*
13592 * "setbufvar()" function
13593 */
13594/*ARGSUSED*/
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013597 typval_T *argvars;
13598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
13600 buf_T *buf;
13601#ifdef FEAT_AUTOCMD
13602 aco_save_T aco;
13603#else
13604 buf_T *save_curbuf;
13605#endif
13606 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013607 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 char_u nbuf[NUMBUFLEN];
13609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013610 rettv->vval.v_number = 0;
13611
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 if (check_restricted() || check_secure())
13613 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13615 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 varp = &argvars[2];
13618
13619 if (buf != NULL && varname != NULL && varp != NULL)
13620 {
13621 /* set curbuf to be our buf, temporarily */
13622#ifdef FEAT_AUTOCMD
13623 aucmd_prepbuf(&aco, buf);
13624#else
13625 save_curbuf = curbuf;
13626 curbuf = buf;
13627#endif
13628
13629 if (*varname == '&')
13630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 long numval;
13632 char_u *strval;
13633 int error = FALSE;
13634
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 numval = get_tv_number_chk(varp, &error);
13637 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013638 if (!error && strval != NULL)
13639 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 }
13641 else
13642 {
13643 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13644 if (bufvarname != NULL)
13645 {
13646 STRCPY(bufvarname, "b:");
13647 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013648 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 vim_free(bufvarname);
13650 }
13651 }
13652
13653 /* reset notion of buffer */
13654#ifdef FEAT_AUTOCMD
13655 aucmd_restbuf(&aco);
13656#else
13657 curbuf = save_curbuf;
13658#endif
13659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
13662/*
13663 * "setcmdpos()" function
13664 */
13665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013667 typval_T *argvars;
13668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013670 int pos = (int)get_tv_number(&argvars[0]) - 1;
13671
13672 if (pos >= 0)
13673 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674}
13675
13676/*
13677 * "setline()" function
13678 */
13679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *argvars;
13682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683{
13684 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013685 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013686 list_T *l = NULL;
13687 listitem_T *li = NULL;
13688 long added = 0;
13689 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013691 lnum = get_tv_lnum(&argvars[0]);
13692 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013694 l = argvars[1].vval.v_list;
13695 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013697 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013699
13700 rettv->vval.v_number = 0; /* OK */
13701 for (;;)
13702 {
13703 if (l != NULL)
13704 {
13705 /* list argument, get next string */
13706 if (li == NULL)
13707 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013709 li = li->li_next;
13710 }
13711
13712 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013713 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013714 break;
13715 if (lnum <= curbuf->b_ml.ml_line_count)
13716 {
13717 /* existing line, replace it */
13718 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13719 {
13720 changed_bytes(lnum, 0);
13721 check_cursor_col();
13722 rettv->vval.v_number = 0; /* OK */
13723 }
13724 }
13725 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13726 {
13727 /* lnum is one past the last line, append the line */
13728 ++added;
13729 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13730 rettv->vval.v_number = 0; /* OK */
13731 }
13732
13733 if (l == NULL) /* only one string argument */
13734 break;
13735 ++lnum;
13736 }
13737
13738 if (added > 0)
13739 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740}
13741
13742/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013743 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013744 */
13745/*ARGSUSED*/
13746 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013747set_qf_ll_list(wp, list_arg, action_arg, rettv)
13748 win_T *wp;
13749 typval_T *list_arg;
13750 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013751 typval_T *rettv;
13752{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013753#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013754 char_u *act;
13755 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013756#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013757
Bram Moolenaar2641f772005-03-25 21:58:17 +000013758 rettv->vval.v_number = -1;
13759
13760#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013761 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013762 EMSG(_(e_listreq));
13763 else
13764 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013765 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013766
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013767 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013768 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013769 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 if (act == NULL)
13771 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013772 if (*act == 'a' || *act == 'r')
13773 action = *act;
13774 }
13775
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013776 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013777 rettv->vval.v_number = 0;
13778 }
13779#endif
13780}
13781
13782/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013783 * "setloclist()" function
13784 */
13785/*ARGSUSED*/
13786 static void
13787f_setloclist(argvars, rettv)
13788 typval_T *argvars;
13789 typval_T *rettv;
13790{
13791 win_T *win;
13792
13793 rettv->vval.v_number = -1;
13794
13795 win = find_win_by_nr(&argvars[0]);
13796 if (win != NULL)
13797 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13798}
13799
13800/*
13801 * "setqflist()" function
13802 */
13803/*ARGSUSED*/
13804 static void
13805f_setqflist(argvars, rettv)
13806 typval_T *argvars;
13807 typval_T *rettv;
13808{
13809 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13810}
13811
13812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 * "setreg()" function
13814 */
13815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013817 typval_T *argvars;
13818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819{
13820 int regname;
13821 char_u *strregname;
13822 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 int append;
13825 char_u yank_type;
13826 long block_len;
13827
13828 block_len = -1;
13829 yank_type = MAUTO;
13830 append = FALSE;
13831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 if (strregname == NULL)
13836 return; /* type error; errmsg already given */
13837 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 if (regname == 0 || regname == '@')
13839 regname = '"';
13840 else if (regname == '=')
13841 return;
13842
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013843 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 stropt = get_tv_string_chk(&argvars[2]);
13846 if (stropt == NULL)
13847 return; /* type error */
13848 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 switch (*stropt)
13850 {
13851 case 'a': case 'A': /* append */
13852 append = TRUE;
13853 break;
13854 case 'v': case 'c': /* character-wise selection */
13855 yank_type = MCHAR;
13856 break;
13857 case 'V': case 'l': /* line-wise selection */
13858 yank_type = MLINE;
13859 break;
13860#ifdef FEAT_VISUAL
13861 case 'b': case Ctrl_V: /* block-wise selection */
13862 yank_type = MBLOCK;
13863 if (VIM_ISDIGIT(stropt[1]))
13864 {
13865 ++stropt;
13866 block_len = getdigits(&stropt) - 1;
13867 --stropt;
13868 }
13869 break;
13870#endif
13871 }
13872 }
13873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013874 strval = get_tv_string_chk(&argvars[1]);
13875 if (strval != NULL)
13876 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879}
13880
13881
13882/*
13883 * "setwinvar(expr)" function
13884 */
13885/*ARGSUSED*/
13886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013888 typval_T *argvars;
13889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890{
13891 win_T *win;
13892#ifdef FEAT_WINDOWS
13893 win_T *save_curwin;
13894#endif
13895 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013896 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 char_u nbuf[NUMBUFLEN];
13898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013899 rettv->vval.v_number = 0;
13900
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 if (check_restricted() || check_secure())
13902 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 varp = &argvars[2];
13906
13907 if (win != NULL && varname != NULL && varp != NULL)
13908 {
13909#ifdef FEAT_WINDOWS
13910 /* set curwin to be our win, temporarily */
13911 save_curwin = curwin;
13912 curwin = win;
13913 curbuf = curwin->w_buffer;
13914#endif
13915
13916 if (*varname == '&')
13917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013918 long numval;
13919 char_u *strval;
13920 int error = FALSE;
13921
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013923 numval = get_tv_number_chk(varp, &error);
13924 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 if (!error && strval != NULL)
13926 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 }
13928 else
13929 {
13930 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13931 if (winvarname != NULL)
13932 {
13933 STRCPY(winvarname, "w:");
13934 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013935 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 vim_free(winvarname);
13937 }
13938 }
13939
13940#ifdef FEAT_WINDOWS
13941 /* Restore current window, if it's still valid (autocomands can make
13942 * it invalid). */
13943 if (win_valid(save_curwin))
13944 {
13945 curwin = save_curwin;
13946 curbuf = curwin->w_buffer;
13947 }
13948#endif
13949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950}
13951
13952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 */
13955 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013957 typval_T *argvars;
13958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013960 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
Bram Moolenaar0d660222005-01-07 21:51:51 +000013962 p = get_tv_string(&argvars[0]);
13963 rettv->vval.v_string = vim_strsave(p);
13964 simplify_filename(rettv->vval.v_string); /* simplify in place */
13965 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966}
13967
Bram Moolenaar0d660222005-01-07 21:51:51 +000013968static int
13969#ifdef __BORLANDC__
13970 _RTLENTRYF
13971#endif
13972 item_compare __ARGS((const void *s1, const void *s2));
13973static int
13974#ifdef __BORLANDC__
13975 _RTLENTRYF
13976#endif
13977 item_compare2 __ARGS((const void *s1, const void *s2));
13978
13979static int item_compare_ic;
13980static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982#define ITEM_COMPARE_FAIL 999
13983
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013985 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013987 static int
13988#ifdef __BORLANDC__
13989_RTLENTRYF
13990#endif
13991item_compare(s1, s2)
13992 const void *s1;
13993 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013995 char_u *p1, *p2;
13996 char_u *tofree1, *tofree2;
13997 int res;
13998 char_u numbuf1[NUMBUFLEN];
13999 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014001 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14002 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014003 if (item_compare_ic)
14004 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014006 res = STRCMP(p1, p2);
14007 vim_free(tofree1);
14008 vim_free(tofree2);
14009 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010}
14011
14012 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013#ifdef __BORLANDC__
14014_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014016item_compare2(s1, s2)
14017 const void *s1;
14018 const void *s2;
14019{
14020 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014021 typval_T rettv;
14022 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014023 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014025 /* shortcut after failure in previous call; compare all items equal */
14026 if (item_compare_func_err)
14027 return 0;
14028
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014029 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14030 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014031 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14032 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014033
14034 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14035 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014036 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037 clear_tv(&argv[0]);
14038 clear_tv(&argv[1]);
14039
14040 if (res == FAIL)
14041 res = ITEM_COMPARE_FAIL;
14042 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014043 /* return value has wrong type */
14044 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14045 if (item_compare_func_err)
14046 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014047 clear_tv(&rettv);
14048 return res;
14049}
14050
14051/*
14052 * "sort({list})" function
14053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014055f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014056 typval_T *argvars;
14057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058{
Bram Moolenaar33570922005-01-25 22:26:29 +000014059 list_T *l;
14060 listitem_T *li;
14061 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014062 long len;
14063 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064
Bram Moolenaar0d660222005-01-07 21:51:51 +000014065 rettv->vval.v_number = 0;
14066 if (argvars[0].v_type != VAR_LIST)
14067 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 else
14069 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014070 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014071 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014072 return;
14073 rettv->vval.v_list = l;
14074 rettv->v_type = VAR_LIST;
14075 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076
Bram Moolenaar0d660222005-01-07 21:51:51 +000014077 len = list_len(l);
14078 if (len <= 1)
14079 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081 item_compare_ic = FALSE;
14082 item_compare_func = NULL;
14083 if (argvars[1].v_type != VAR_UNKNOWN)
14084 {
14085 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014087 else
14088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 int error = FALSE;
14090
14091 i = get_tv_number_chk(&argvars[1], &error);
14092 if (error)
14093 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094 if (i == 1)
14095 item_compare_ic = TRUE;
14096 else
14097 item_compare_func = get_tv_string(&argvars[1]);
14098 }
14099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103 if (ptrs == NULL)
14104 return;
14105 i = 0;
14106 for (li = l->lv_first; li != NULL; li = li->li_next)
14107 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014110 /* test the compare function */
14111 if (item_compare_func != NULL
14112 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14113 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014114 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116 {
14117 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014118 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119 item_compare_func == NULL ? item_compare : item_compare2);
14120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014121 if (!item_compare_func_err)
14122 {
14123 /* Clear the List and append the items in the sorted order. */
14124 l->lv_first = l->lv_last = NULL;
14125 l->lv_len = 0;
14126 for (i = 0; i < len; ++i)
14127 list_append(l, ptrs[i]);
14128 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129 }
14130
14131 vim_free(ptrs);
14132 }
14133}
14134
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014135/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014136 * "soundfold({word})" function
14137 */
14138 static void
14139f_soundfold(argvars, rettv)
14140 typval_T *argvars;
14141 typval_T *rettv;
14142{
14143 char_u *s;
14144
14145 rettv->v_type = VAR_STRING;
14146 s = get_tv_string(&argvars[0]);
14147#ifdef FEAT_SYN_HL
14148 rettv->vval.v_string = eval_soundfold(s);
14149#else
14150 rettv->vval.v_string = vim_strsave(s);
14151#endif
14152}
14153
14154/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014155 * "spellbadword()" function
14156 */
14157/* ARGSUSED */
14158 static void
14159f_spellbadword(argvars, rettv)
14160 typval_T *argvars;
14161 typval_T *rettv;
14162{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014163 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014164 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014165 int len = 0;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014166 list_T *l;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014167
Bram Moolenaar4463f292005-09-25 22:20:24 +000014168 l = list_alloc();
14169 if (l == NULL)
14170 return;
14171 rettv->v_type = VAR_LIST;
14172 rettv->vval.v_list = l;
14173 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014174
14175#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014176 if (argvars[0].v_type == VAR_UNKNOWN)
14177 {
14178 /* Find the start and length of the badly spelled word. */
14179 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14180 if (len != 0)
14181 word = ml_get_cursor();
14182 }
14183 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14184 {
14185 char_u *str = get_tv_string_chk(&argvars[0]);
14186 int capcol = -1;
14187
14188 if (str != NULL)
14189 {
14190 /* Check the argument for spelling. */
14191 while (*str != NUL)
14192 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014193 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014194 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014195 {
14196 word = str;
14197 break;
14198 }
14199 str += len;
14200 }
14201 }
14202 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014203#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014204
14205 list_append_string(l, word, len);
14206 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014207 attr == HLF_SPB ? "bad" :
14208 attr == HLF_SPR ? "rare" :
14209 attr == HLF_SPL ? "local" :
14210 attr == HLF_SPC ? "caps" :
14211 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014212}
14213
14214/*
14215 * "spellsuggest()" function
14216 */
14217 static void
14218f_spellsuggest(argvars, rettv)
14219 typval_T *argvars;
14220 typval_T *rettv;
14221{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014222 list_T *l;
14223#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014224 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014225 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014226 int maxcount;
14227 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014228 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014229 listitem_T *li;
14230 int need_capital = FALSE;
14231#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014232
14233 l = list_alloc();
14234 if (l == NULL)
14235 return;
14236 rettv->v_type = VAR_LIST;
14237 rettv->vval.v_list = l;
14238 ++l->lv_refcount;
14239
14240#ifdef FEAT_SYN_HL
14241 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14242 {
14243 str = get_tv_string(&argvars[0]);
14244 if (argvars[1].v_type != VAR_UNKNOWN)
14245 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014246 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014247 if (maxcount <= 0)
14248 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014249 if (argvars[2].v_type != VAR_UNKNOWN)
14250 {
14251 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14252 if (typeerr)
14253 return;
14254 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014255 }
14256 else
14257 maxcount = 25;
14258
Bram Moolenaar4770d092006-01-12 23:22:24 +000014259 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014260
14261 for (i = 0; i < ga.ga_len; ++i)
14262 {
14263 str = ((char_u **)ga.ga_data)[i];
14264
14265 li = listitem_alloc();
14266 if (li == NULL)
14267 vim_free(str);
14268 else
14269 {
14270 li->li_tv.v_type = VAR_STRING;
14271 li->li_tv.v_lock = 0;
14272 li->li_tv.vval.v_string = str;
14273 list_append(l, li);
14274 }
14275 }
14276 ga_clear(&ga);
14277 }
14278#endif
14279}
14280
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014282f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014283 typval_T *argvars;
14284 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285{
14286 char_u *str;
14287 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014288 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014289 regmatch_T regmatch;
14290 char_u patbuf[NUMBUFLEN];
14291 char_u *save_cpo;
14292 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014294 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014295 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014297
14298 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14299 save_cpo = p_cpo;
14300 p_cpo = (char_u *)"";
14301
14302 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014303 if (argvars[1].v_type != VAR_UNKNOWN)
14304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14306 if (pat == NULL)
14307 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014308 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014310 }
14311 if (pat == NULL || *pat == NUL)
14312 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014313
14314 l = list_alloc();
14315 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317 rettv->v_type = VAR_LIST;
14318 rettv->vval.v_list = l;
14319 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014320 if (typeerr)
14321 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14324 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014326 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014327 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014328 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014329 if (*str == NUL)
14330 match = FALSE; /* empty item at the end */
14331 else
14332 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014333 if (match)
14334 end = regmatch.startp[0];
14335 else
14336 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014337 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14338 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014339 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014340 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014341 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014342 }
14343 if (!match)
14344 break;
14345 /* Advance to just after the match. */
14346 if (regmatch.endp[0] > str)
14347 col = 0;
14348 else
14349 {
14350 /* Don't get stuck at the same match. */
14351#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014352 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014353#else
14354 col = 1;
14355#endif
14356 }
14357 str = regmatch.endp[0];
14358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359
Bram Moolenaar0d660222005-01-07 21:51:51 +000014360 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364}
14365
14366#ifdef HAVE_STRFTIME
14367/*
14368 * "strftime({format}[, {time}])" function
14369 */
14370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014372 typval_T *argvars;
14373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
14375 char_u result_buf[256];
14376 struct tm *curtime;
14377 time_t seconds;
14378 char_u *p;
14379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014383 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 seconds = time(NULL);
14385 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 curtime = localtime(&seconds);
14388 /* MSVC returns NULL for an invalid value of seconds. */
14389 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 else
14392 {
14393# ifdef FEAT_MBYTE
14394 vimconv_T conv;
14395 char_u *enc;
14396
14397 conv.vc_type = CONV_NONE;
14398 enc = enc_locale();
14399 convert_setup(&conv, p_enc, enc);
14400 if (conv.vc_type != CONV_NONE)
14401 p = string_convert(&conv, p, NULL);
14402# endif
14403 if (p != NULL)
14404 (void)strftime((char *)result_buf, sizeof(result_buf),
14405 (char *)p, curtime);
14406 else
14407 result_buf[0] = NUL;
14408
14409# ifdef FEAT_MBYTE
14410 if (conv.vc_type != CONV_NONE)
14411 vim_free(p);
14412 convert_setup(&conv, enc, p_enc);
14413 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014414 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 else
14416# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418
14419# ifdef FEAT_MBYTE
14420 /* Release conversion descriptors */
14421 convert_setup(&conv, NULL, NULL);
14422 vim_free(enc);
14423# endif
14424 }
14425}
14426#endif
14427
14428/*
14429 * "stridx()" function
14430 */
14431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014433 typval_T *argvars;
14434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
14436 char_u buf[NUMBUFLEN];
14437 char_u *needle;
14438 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014441 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014443 needle = get_tv_string_chk(&argvars[1]);
14444 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014445 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 if (needle == NULL || haystack == NULL)
14447 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 if (argvars[2].v_type != VAR_UNKNOWN)
14450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 int error = FALSE;
14452
14453 start_idx = get_tv_number_chk(&argvars[2], &error);
14454 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014455 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014456 if (start_idx >= 0)
14457 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014458 }
14459
14460 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14461 if (pos != NULL)
14462 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463}
14464
14465/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014466 * "string()" function
14467 */
14468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *argvars;
14471 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472{
14473 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014474 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014477 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014478 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480}
14481
14482/*
14483 * "strlen()" function
14484 */
14485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014487 typval_T *argvars;
14488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 rettv->vval.v_number = (varnumber_T)(STRLEN(
14491 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492}
14493
14494/*
14495 * "strpart()" function
14496 */
14497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014499 typval_T *argvars;
14500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501{
14502 char_u *p;
14503 int n;
14504 int len;
14505 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 slen = (int)STRLEN(p);
14510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 n = get_tv_number_chk(&argvars[1], &error);
14512 if (error)
14513 len = 0;
14514 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 else
14517 len = slen - n; /* default len: all bytes that are available. */
14518
14519 /*
14520 * Only return the overlap between the specified part and the actual
14521 * string.
14522 */
14523 if (n < 0)
14524 {
14525 len += n;
14526 n = 0;
14527 }
14528 else if (n > slen)
14529 n = slen;
14530 if (len < 0)
14531 len = 0;
14532 else if (n + len > slen)
14533 len = slen - n;
14534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->v_type = VAR_STRING;
14536 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537}
14538
14539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540 * "strridx()" function
14541 */
14542 static void
14543f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 typval_T *argvars;
14545 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546{
14547 char_u buf[NUMBUFLEN];
14548 char_u *needle;
14549 char_u *haystack;
14550 char_u *rest;
14551 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014552 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 needle = get_tv_string_chk(&argvars[1]);
14555 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014556 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557
14558 rettv->vval.v_number = -1;
14559 if (needle == NULL || haystack == NULL)
14560 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014561 if (argvars[2].v_type != VAR_UNKNOWN)
14562 {
14563 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014564 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014565 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014566 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014567 }
14568 else
14569 end_idx = haystack_len;
14570
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014572 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014574 lastmatch = haystack + end_idx;
14575 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014578 for (rest = haystack; *rest != '\0'; ++rest)
14579 {
14580 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014581 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582 break;
14583 lastmatch = rest;
14584 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014585 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586
14587 if (lastmatch == NULL)
14588 rettv->vval.v_number = -1;
14589 else
14590 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14591}
14592
14593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 * "strtrans()" function
14595 */
14596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014598 typval_T *argvars;
14599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601 rettv->v_type = VAR_STRING;
14602 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603}
14604
14605/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606 * "submatch()" function
14607 */
14608 static void
14609f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 typval_T *argvars;
14611 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612{
14613 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014614 rettv->vval.v_string =
14615 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616}
14617
14618/*
14619 * "substitute()" function
14620 */
14621 static void
14622f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014623 typval_T *argvars;
14624 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625{
14626 char_u patbuf[NUMBUFLEN];
14627 char_u subbuf[NUMBUFLEN];
14628 char_u flagsbuf[NUMBUFLEN];
14629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 char_u *str = get_tv_string_chk(&argvars[0]);
14631 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14632 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14633 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14634
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14637 rettv->vval.v_string = NULL;
14638 else
14639 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640}
14641
14642/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014643 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 */
14645/*ARGSUSED*/
14646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 typval_T *argvars;
14649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650{
14651 int id = 0;
14652#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014653 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 long col;
14655 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014656 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014658 lnum = get_tv_lnum(argvars); /* -1 on type error */
14659 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14660 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014663 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014664 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665#endif
14666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014667 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668}
14669
14670/*
14671 * "synIDattr(id, what [, mode])" function
14672 */
14673/*ARGSUSED*/
14674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014675f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014676 typval_T *argvars;
14677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678{
14679 char_u *p = NULL;
14680#ifdef FEAT_SYN_HL
14681 int id;
14682 char_u *what;
14683 char_u *mode;
14684 char_u modebuf[NUMBUFLEN];
14685 int modec;
14686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 id = get_tv_number(&argvars[0]);
14688 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014691 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 modec = TOLOWER_ASC(mode[0]);
14693 if (modec != 't' && modec != 'c'
14694#ifdef FEAT_GUI
14695 && modec != 'g'
14696#endif
14697 )
14698 modec = 0; /* replace invalid with current */
14699 }
14700 else
14701 {
14702#ifdef FEAT_GUI
14703 if (gui.in_use)
14704 modec = 'g';
14705 else
14706#endif
14707 if (t_colors > 1)
14708 modec = 'c';
14709 else
14710 modec = 't';
14711 }
14712
14713
14714 switch (TOLOWER_ASC(what[0]))
14715 {
14716 case 'b':
14717 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14718 p = highlight_color(id, what, modec);
14719 else /* bold */
14720 p = highlight_has_attr(id, HL_BOLD, modec);
14721 break;
14722
14723 case 'f': /* fg[#] */
14724 p = highlight_color(id, what, modec);
14725 break;
14726
14727 case 'i':
14728 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14729 p = highlight_has_attr(id, HL_INVERSE, modec);
14730 else /* italic */
14731 p = highlight_has_attr(id, HL_ITALIC, modec);
14732 break;
14733
14734 case 'n': /* name */
14735 p = get_highlight_name(NULL, id - 1);
14736 break;
14737
14738 case 'r': /* reverse */
14739 p = highlight_has_attr(id, HL_INVERSE, modec);
14740 break;
14741
14742 case 's': /* standout */
14743 p = highlight_has_attr(id, HL_STANDOUT, modec);
14744 break;
14745
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014746 case 'u':
14747 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14748 /* underline */
14749 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14750 else
14751 /* undercurl */
14752 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 break;
14754 }
14755
14756 if (p != NULL)
14757 p = vim_strsave(p);
14758#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014759 rettv->v_type = VAR_STRING;
14760 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761}
14762
14763/*
14764 * "synIDtrans(id)" function
14765 */
14766/*ARGSUSED*/
14767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014769 typval_T *argvars;
14770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771{
14772 int id;
14773
14774#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776
14777 if (id > 0)
14778 id = syn_get_final_id(id);
14779 else
14780#endif
14781 id = 0;
14782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784}
14785
14786/*
14787 * "system()" function
14788 */
14789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014790f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014791 typval_T *argvars;
14792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014794 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014796 char_u *infile = NULL;
14797 char_u buf[NUMBUFLEN];
14798 int err = FALSE;
14799 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014802 {
14803 /*
14804 * Write the string to a temp file, to be used for input of the shell
14805 * command.
14806 */
14807 if ((infile = vim_tempname('i')) == NULL)
14808 {
14809 EMSG(_(e_notmp));
14810 return;
14811 }
14812
14813 fd = mch_fopen((char *)infile, WRITEBIN);
14814 if (fd == NULL)
14815 {
14816 EMSG2(_(e_notopen), infile);
14817 goto done;
14818 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014819 p = get_tv_string_buf_chk(&argvars[1], buf);
14820 if (p == NULL)
14821 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014822 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14823 err = TRUE;
14824 if (fclose(fd) != 0)
14825 err = TRUE;
14826 if (err)
14827 {
14828 EMSG(_("E677: Error writing temp file"));
14829 goto done;
14830 }
14831 }
14832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014834
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835#ifdef USE_CR
14836 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014837 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 {
14839 char_u *s;
14840
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014841 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 {
14843 if (*s == CAR)
14844 *s = NL;
14845 }
14846 }
14847#else
14848# ifdef USE_CRNL
14849 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014850 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 {
14852 char_u *s, *d;
14853
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014854 d = res;
14855 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 {
14857 if (s[0] == CAR && s[1] == NL)
14858 ++s;
14859 *d++ = *s;
14860 }
14861 *d = NUL;
14862 }
14863# endif
14864#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014865
14866done:
14867 if (infile != NULL)
14868 {
14869 mch_remove(infile);
14870 vim_free(infile);
14871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872 rettv->v_type = VAR_STRING;
14873 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874}
14875
14876/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014877 * "tabpagebuflist()" function
14878 */
14879/* ARGSUSED */
14880 static void
14881f_tabpagebuflist(argvars, rettv)
14882 typval_T *argvars;
14883 typval_T *rettv;
14884{
14885#ifndef FEAT_WINDOWS
14886 rettv->vval.v_number = 0;
14887#else
14888 tabpage_T *tp;
14889 win_T *wp = NULL;
14890 list_T *l;
14891
14892 if (argvars[0].v_type == VAR_UNKNOWN)
14893 wp = firstwin;
14894 else
14895 {
14896 tp = find_tabpage((int)get_tv_number(&argvars[0]));
14897 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000014898 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014899 }
14900 if (wp == NULL)
14901 rettv->vval.v_number = 0;
14902 else
14903 {
14904 l = list_alloc();
14905 if (l == NULL)
14906 rettv->vval.v_number = 0;
14907 else
14908 {
14909 rettv->vval.v_list = l;
14910 rettv->v_type = VAR_LIST;
14911 ++l->lv_refcount;
14912
14913 for (; wp != NULL; wp = wp->w_next)
14914 if (list_append_number(l, wp->w_buffer->b_fnum) == FAIL)
14915 break;
14916 }
14917 }
14918#endif
14919}
14920
14921
14922/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000014923 * "tabpagenr()" function
14924 */
14925/* ARGSUSED */
14926 static void
14927f_tabpagenr(argvars, rettv)
14928 typval_T *argvars;
14929 typval_T *rettv;
14930{
14931 int nr = 1;
14932#ifdef FEAT_WINDOWS
14933 tabpage_T *tp;
14934 char_u *arg;
14935
14936 if (argvars[0].v_type != VAR_UNKNOWN)
14937 {
14938 arg = get_tv_string_chk(&argvars[0]);
14939 nr = 0;
14940 if (arg != NULL)
14941 {
14942 if (STRCMP(arg, "$") == 0)
14943 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
14944 ++nr;
14945 else
14946 EMSG2(_(e_invexpr2), arg);
14947 }
14948 }
14949 else
14950 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
14951 ++nr;
14952#endif
14953 rettv->vval.v_number = nr;
14954}
14955
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000014956
14957#ifdef FEAT_WINDOWS
14958static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
14959
14960/*
14961 * Common code for tabpagewinnr() and winnr().
14962 */
14963 static int
14964get_winnr(tp, argvar)
14965 tabpage_T *tp;
14966 typval_T *argvar;
14967{
14968 win_T *twin;
14969 int nr = 1;
14970 win_T *wp;
14971 char_u *arg;
14972
14973 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14974 if (argvar->v_type != VAR_UNKNOWN)
14975 {
14976 arg = get_tv_string_chk(argvar);
14977 if (arg == NULL)
14978 nr = 0; /* type error; errmsg already given */
14979 else if (STRCMP(arg, "$") == 0)
14980 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14981 else if (STRCMP(arg, "#") == 0)
14982 {
14983 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14984 if (twin == NULL)
14985 nr = 0;
14986 }
14987 else
14988 {
14989 EMSG2(_(e_invexpr2), arg);
14990 nr = 0;
14991 }
14992 }
14993
14994 if (nr > 0)
14995 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14996 wp != twin; wp = wp->w_next)
14997 ++nr;
14998 return nr;
14999}
15000#endif
15001
15002/*
15003 * "tabpagewinnr()" function
15004 */
15005/* ARGSUSED */
15006 static void
15007f_tabpagewinnr(argvars, rettv)
15008 typval_T *argvars;
15009 typval_T *rettv;
15010{
15011 int nr = 1;
15012#ifdef FEAT_WINDOWS
15013 tabpage_T *tp;
15014
15015 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15016 if (tp == NULL)
15017 nr = 0;
15018 else
15019 nr = get_winnr(tp, &argvars[1]);
15020#endif
15021 rettv->vval.v_number = nr;
15022}
15023
15024
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015025/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015026 * "tagfiles()" function
15027 */
15028/*ARGSUSED*/
15029 static void
15030f_tagfiles(argvars, rettv)
15031 typval_T *argvars;
15032 typval_T *rettv;
15033{
15034 char_u fname[MAXPATHL + 1];
15035 list_T *l;
15036
15037 l = list_alloc();
15038 if (l == NULL)
15039 {
15040 rettv->vval.v_number = 0;
15041 return;
15042 }
15043 rettv->vval.v_list = l;
15044 rettv->v_type = VAR_LIST;
15045 ++l->lv_refcount;
15046
15047 get_tagfname(TRUE, NULL);
15048 for (;;)
15049 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015050 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015051 break;
15052}
15053
15054/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015055 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015056 */
15057 static void
15058f_taglist(argvars, rettv)
15059 typval_T *argvars;
15060 typval_T *rettv;
15061{
15062 char_u *tag_pattern;
15063 list_T *l;
15064
15065 tag_pattern = get_tv_string(&argvars[0]);
15066
15067 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 if (*tag_pattern == NUL)
15069 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015070
15071 l = list_alloc();
15072 if (l != NULL)
15073 {
15074 if (get_tags(l, tag_pattern) != FAIL)
15075 {
15076 rettv->vval.v_list = l;
15077 rettv->v_type = VAR_LIST;
15078 ++l->lv_refcount;
15079 }
15080 else
15081 list_free(l);
15082 }
15083}
15084
15085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086 * "tempname()" function
15087 */
15088/*ARGSUSED*/
15089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 typval_T *argvars;
15092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093{
15094 static int x = 'A';
15095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015096 rettv->v_type = VAR_STRING;
15097 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098
15099 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15100 * names. Skip 'I' and 'O', they are used for shell redirection. */
15101 do
15102 {
15103 if (x == 'Z')
15104 x = '0';
15105 else if (x == '9')
15106 x = 'A';
15107 else
15108 {
15109#ifdef EBCDIC
15110 if (x == 'I')
15111 x = 'J';
15112 else if (x == 'R')
15113 x = 'S';
15114 else
15115#endif
15116 ++x;
15117 }
15118 } while (x == 'I' || x == 'O');
15119}
15120
15121/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015122 * "test(list)" function: Just checking the walls...
15123 */
15124/*ARGSUSED*/
15125 static void
15126f_test(argvars, rettv)
15127 typval_T *argvars;
15128 typval_T *rettv;
15129{
15130 /* Used for unit testing. Change the code below to your liking. */
15131#if 0
15132 listitem_T *li;
15133 list_T *l;
15134 char_u *bad, *good;
15135
15136 if (argvars[0].v_type != VAR_LIST)
15137 return;
15138 l = argvars[0].vval.v_list;
15139 if (l == NULL)
15140 return;
15141 li = l->lv_first;
15142 if (li == NULL)
15143 return;
15144 bad = get_tv_string(&li->li_tv);
15145 li = li->li_next;
15146 if (li == NULL)
15147 return;
15148 good = get_tv_string(&li->li_tv);
15149 rettv->vval.v_number = test_edit_score(bad, good);
15150#endif
15151}
15152
15153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154 * "tolower(string)" function
15155 */
15156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015157f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015158 typval_T *argvars;
15159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160{
15161 char_u *p;
15162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 p = vim_strsave(get_tv_string(&argvars[0]));
15164 rettv->v_type = VAR_STRING;
15165 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166
15167 if (p != NULL)
15168 while (*p != NUL)
15169 {
15170#ifdef FEAT_MBYTE
15171 int l;
15172
15173 if (enc_utf8)
15174 {
15175 int c, lc;
15176
15177 c = utf_ptr2char(p);
15178 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015179 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 /* TODO: reallocate string when byte count changes. */
15181 if (utf_char2len(lc) == l)
15182 utf_char2bytes(lc, p);
15183 p += l;
15184 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015185 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 p += l; /* skip multi-byte character */
15187 else
15188#endif
15189 {
15190 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15191 ++p;
15192 }
15193 }
15194}
15195
15196/*
15197 * "toupper(string)" function
15198 */
15199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 typval_T *argvars;
15202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206}
15207
15208/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015209 * "tr(string, fromstr, tostr)" function
15210 */
15211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015212f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 typval_T *argvars;
15214 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015215{
15216 char_u *instr;
15217 char_u *fromstr;
15218 char_u *tostr;
15219 char_u *p;
15220#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015221 int inlen;
15222 int fromlen;
15223 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015224 int idx;
15225 char_u *cpstr;
15226 int cplen;
15227 int first = TRUE;
15228#endif
15229 char_u buf[NUMBUFLEN];
15230 char_u buf2[NUMBUFLEN];
15231 garray_T ga;
15232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015233 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015234 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15235 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015236
15237 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238 rettv->v_type = VAR_STRING;
15239 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015240 if (fromstr == NULL || tostr == NULL)
15241 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015242 ga_init2(&ga, (int)sizeof(char), 80);
15243
15244#ifdef FEAT_MBYTE
15245 if (!has_mbyte)
15246#endif
15247 /* not multi-byte: fromstr and tostr must be the same length */
15248 if (STRLEN(fromstr) != STRLEN(tostr))
15249 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015250#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015251error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015252#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015253 EMSG2(_(e_invarg2), fromstr);
15254 ga_clear(&ga);
15255 return;
15256 }
15257
15258 /* fromstr and tostr have to contain the same number of chars */
15259 while (*instr != NUL)
15260 {
15261#ifdef FEAT_MBYTE
15262 if (has_mbyte)
15263 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015264 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015265 cpstr = instr;
15266 cplen = inlen;
15267 idx = 0;
15268 for (p = fromstr; *p != NUL; p += fromlen)
15269 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015270 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015271 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15272 {
15273 for (p = tostr; *p != NUL; p += tolen)
15274 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015275 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015276 if (idx-- == 0)
15277 {
15278 cplen = tolen;
15279 cpstr = p;
15280 break;
15281 }
15282 }
15283 if (*p == NUL) /* tostr is shorter than fromstr */
15284 goto error;
15285 break;
15286 }
15287 ++idx;
15288 }
15289
15290 if (first && cpstr == instr)
15291 {
15292 /* Check that fromstr and tostr have the same number of
15293 * (multi-byte) characters. Done only once when a character
15294 * of instr doesn't appear in fromstr. */
15295 first = FALSE;
15296 for (p = tostr; *p != NUL; p += tolen)
15297 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015298 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015299 --idx;
15300 }
15301 if (idx != 0)
15302 goto error;
15303 }
15304
15305 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015306 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015307 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015308
15309 instr += inlen;
15310 }
15311 else
15312#endif
15313 {
15314 /* When not using multi-byte chars we can do it faster. */
15315 p = vim_strchr(fromstr, *instr);
15316 if (p != NULL)
15317 ga_append(&ga, tostr[p - fromstr]);
15318 else
15319 ga_append(&ga, *instr);
15320 ++instr;
15321 }
15322 }
15323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015324 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015325}
15326
15327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 * "type(expr)" function
15329 */
15330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015332 typval_T *argvars;
15333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015335 int n;
15336
15337 switch (argvars[0].v_type)
15338 {
15339 case VAR_NUMBER: n = 0; break;
15340 case VAR_STRING: n = 1; break;
15341 case VAR_FUNC: n = 2; break;
15342 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015343 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015344 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15345 }
15346 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347}
15348
15349/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015350 * "values(dict)" function
15351 */
15352 static void
15353f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015354 typval_T *argvars;
15355 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015356{
15357 dict_list(argvars, rettv, 1);
15358}
15359
15360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 * "virtcol(string)" function
15362 */
15363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 typval_T *argvars;
15366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367{
15368 colnr_T vcol = 0;
15369 pos_T *fp;
15370
15371 fp = var2fpos(&argvars[0], FALSE);
15372 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
15373 {
15374 getvvcol(curwin, fp, NULL, NULL, &vcol);
15375 ++vcol;
15376 }
15377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379}
15380
15381/*
15382 * "visualmode()" function
15383 */
15384/*ARGSUSED*/
15385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015387 typval_T *argvars;
15388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389{
15390#ifdef FEAT_VISUAL
15391 char_u str[2];
15392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 str[0] = curbuf->b_visual_mode_eval;
15395 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397
15398 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015399 if ((argvars[0].v_type == VAR_NUMBER
15400 && argvars[0].vval.v_number != 0)
15401 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 curbuf->b_visual_mode_eval = NUL;
15404#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406#endif
15407}
15408
15409/*
15410 * "winbufnr(nr)" function
15411 */
15412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015414 typval_T *argvars;
15415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416{
15417 win_T *wp;
15418
15419 wp = find_win_by_nr(&argvars[0]);
15420 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424}
15425
15426/*
15427 * "wincol()" function
15428 */
15429/*ARGSUSED*/
15430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015431f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015432 typval_T *argvars;
15433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
15435 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437}
15438
15439/*
15440 * "winheight(nr)" function
15441 */
15442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015444 typval_T *argvars;
15445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446{
15447 win_T *wp;
15448
15449 wp = find_win_by_nr(&argvars[0]);
15450 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015451 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454}
15455
15456/*
15457 * "winline()" function
15458 */
15459/*ARGSUSED*/
15460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015462 typval_T *argvars;
15463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464{
15465 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467}
15468
15469/*
15470 * "winnr()" function
15471 */
15472/* ARGSUSED */
15473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015475 typval_T *argvars;
15476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477{
15478 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015479
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015481 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484}
15485
15486/*
15487 * "winrestcmd()" function
15488 */
15489/* ARGSUSED */
15490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 typval_T *argvars;
15493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494{
15495#ifdef FEAT_WINDOWS
15496 win_T *wp;
15497 int winnr = 1;
15498 garray_T ga;
15499 char_u buf[50];
15500
15501 ga_init2(&ga, (int)sizeof(char), 70);
15502 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15503 {
15504 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15505 ga_concat(&ga, buf);
15506# ifdef FEAT_VERTSPLIT
15507 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15508 ga_concat(&ga, buf);
15509# endif
15510 ++winnr;
15511 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015512 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519}
15520
15521/*
15522 * "winwidth(nr)" function
15523 */
15524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015525f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015526 typval_T *argvars;
15527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
15529 win_T *wp;
15530
15531 wp = find_win_by_nr(&argvars[0]);
15532 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 else
15535#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539#endif
15540}
15541
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 * "writefile()" function
15544 */
15545 static void
15546f_writefile(argvars, rettv)
15547 typval_T *argvars;
15548 typval_T *rettv;
15549{
15550 int binary = FALSE;
15551 char_u *fname;
15552 FILE *fd;
15553 listitem_T *li;
15554 char_u *s;
15555 int ret = 0;
15556 int c;
15557
15558 if (argvars[0].v_type != VAR_LIST)
15559 {
15560 EMSG2(_(e_listarg), "writefile()");
15561 return;
15562 }
15563 if (argvars[0].vval.v_list == NULL)
15564 return;
15565
15566 if (argvars[2].v_type != VAR_UNKNOWN
15567 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15568 binary = TRUE;
15569
15570 /* Always open the file in binary mode, library functions have a mind of
15571 * their own about CR-LF conversion. */
15572 fname = get_tv_string(&argvars[1]);
15573 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15574 {
15575 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15576 ret = -1;
15577 }
15578 else
15579 {
15580 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15581 li = li->li_next)
15582 {
15583 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15584 {
15585 if (*s == '\n')
15586 c = putc(NUL, fd);
15587 else
15588 c = putc(*s, fd);
15589 if (c == EOF)
15590 {
15591 ret = -1;
15592 break;
15593 }
15594 }
15595 if (!binary || li->li_next != NULL)
15596 if (putc('\n', fd) == EOF)
15597 {
15598 ret = -1;
15599 break;
15600 }
15601 if (ret < 0)
15602 {
15603 EMSG(_(e_write));
15604 break;
15605 }
15606 }
15607 fclose(fd);
15608 }
15609
15610 rettv->vval.v_number = ret;
15611}
15612
15613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 * Translate a String variable into a position.
15615 */
15616 static pos_T *
15617var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 int lnum; /* TRUE when $ is last line */
15620{
15621 char_u *name;
15622 static pos_T pos;
15623 pos_T *pp;
15624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015625 name = get_tv_string_chk(varp);
15626 if (name == NULL)
15627 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 if (name[0] == '.') /* cursor */
15629 return &curwin->w_cursor;
15630 if (name[0] == '\'') /* mark */
15631 {
15632 pp = getmark(name[1], FALSE);
15633 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15634 return NULL;
15635 return pp;
15636 }
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015637 if (name[0] == 'w' && lnum)
15638 {
15639 pos.col = 0;
15640 if (name[1] == '0') /* "w0": first visible line */
15641 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015642 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015643 pos.lnum = curwin->w_topline;
15644 return &pos;
15645 }
15646 else if (name[1] == '$') /* "w$": last visible line */
15647 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015648 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015649 pos.lnum = curwin->w_botline - 1;
15650 return &pos;
15651 }
15652 }
15653 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 {
15655 if (lnum)
15656 {
15657 pos.lnum = curbuf->b_ml.ml_line_count;
15658 pos.col = 0;
15659 }
15660 else
15661 {
15662 pos.lnum = curwin->w_cursor.lnum;
15663 pos.col = (colnr_T)STRLEN(ml_get_curline());
15664 }
15665 return &pos;
15666 }
15667 return NULL;
15668}
15669
15670/*
15671 * Get the length of an environment variable name.
15672 * Advance "arg" to the first character after the name.
15673 * Return 0 for error.
15674 */
15675 static int
15676get_env_len(arg)
15677 char_u **arg;
15678{
15679 char_u *p;
15680 int len;
15681
15682 for (p = *arg; vim_isIDc(*p); ++p)
15683 ;
15684 if (p == *arg) /* no name found */
15685 return 0;
15686
15687 len = (int)(p - *arg);
15688 *arg = p;
15689 return len;
15690}
15691
15692/*
15693 * Get the length of the name of a function or internal variable.
15694 * "arg" is advanced to the first non-white character after the name.
15695 * Return 0 if something is wrong.
15696 */
15697 static int
15698get_id_len(arg)
15699 char_u **arg;
15700{
15701 char_u *p;
15702 int len;
15703
15704 /* Find the end of the name. */
15705 for (p = *arg; eval_isnamec(*p); ++p)
15706 ;
15707 if (p == *arg) /* no name found */
15708 return 0;
15709
15710 len = (int)(p - *arg);
15711 *arg = skipwhite(p);
15712
15713 return len;
15714}
15715
15716/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015717 * Get the length of the name of a variable or function.
15718 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015720 * Return -1 if curly braces expansion failed.
15721 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 * If the name contains 'magic' {}'s, expand them and return the
15723 * expanded name in an allocated string via 'alias' - caller must free.
15724 */
15725 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 char_u **arg;
15728 char_u **alias;
15729 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731{
15732 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 char_u *p;
15734 char_u *expr_start;
15735 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736
15737 *alias = NULL; /* default to no alias */
15738
15739 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15740 && (*arg)[2] == (int)KE_SNR)
15741 {
15742 /* hard coded <SNR>, already translated */
15743 *arg += 3;
15744 return get_id_len(arg) + 3;
15745 }
15746 len = eval_fname_script(*arg);
15747 if (len > 0)
15748 {
15749 /* literal "<SID>", "s:" or "<SNR>" */
15750 *arg += len;
15751 }
15752
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015754 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015756 p = find_name_end(*arg, &expr_start, &expr_end,
15757 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 if (expr_start != NULL)
15759 {
15760 char_u *temp_string;
15761
15762 if (!evaluate)
15763 {
15764 len += (int)(p - *arg);
15765 *arg = skipwhite(p);
15766 return len;
15767 }
15768
15769 /*
15770 * Include any <SID> etc in the expanded string:
15771 * Thus the -len here.
15772 */
15773 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15774 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015775 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 *alias = temp_string;
15777 *arg = skipwhite(p);
15778 return (int)STRLEN(temp_string);
15779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780
15781 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015782 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 EMSG2(_(e_invexpr2), *arg);
15784
15785 return len;
15786}
15787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788/*
15789 * Find the end of a variable or function name, taking care of magic braces.
15790 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15791 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015792 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015793 * Return a pointer to just after the name. Equal to "arg" if there is no
15794 * valid name.
15795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015797find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 char_u *arg;
15799 char_u **expr_start;
15800 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015801 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015803 int mb_nest = 0;
15804 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 char_u *p;
15806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015807 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809 *expr_start = NULL;
15810 *expr_end = NULL;
15811 }
15812
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015813 /* Quick check for valid starting character. */
15814 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15815 return arg;
15816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015817 for (p = arg; *p != NUL
15818 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015819 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015820 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015821 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015822 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015824 if (*p == '\'')
15825 {
15826 /* skip over 'string' to avoid counting [ and ] inside it. */
15827 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15828 ;
15829 if (*p == NUL)
15830 break;
15831 }
15832 else if (*p == '"')
15833 {
15834 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15835 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15836 if (*p == '\\' && p[1] != NUL)
15837 ++p;
15838 if (*p == NUL)
15839 break;
15840 }
15841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015842 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015844 if (*p == '[')
15845 ++br_nest;
15846 else if (*p == ']')
15847 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015850 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015852 if (*p == '{')
15853 {
15854 mb_nest++;
15855 if (expr_start != NULL && *expr_start == NULL)
15856 *expr_start = p;
15857 }
15858 else if (*p == '}')
15859 {
15860 mb_nest--;
15861 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15862 *expr_end = p;
15863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 }
15866
15867 return p;
15868}
15869
15870/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015871 * Expands out the 'magic' {}'s in a variable/function name.
15872 * Note that this can call itself recursively, to deal with
15873 * constructs like foo{bar}{baz}{bam}
15874 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15875 * "in_start" ^
15876 * "expr_start" ^
15877 * "expr_end" ^
15878 * "in_end" ^
15879 *
15880 * Returns a new allocated string, which the caller must free.
15881 * Returns NULL for failure.
15882 */
15883 static char_u *
15884make_expanded_name(in_start, expr_start, expr_end, in_end)
15885 char_u *in_start;
15886 char_u *expr_start;
15887 char_u *expr_end;
15888 char_u *in_end;
15889{
15890 char_u c1;
15891 char_u *retval = NULL;
15892 char_u *temp_result;
15893 char_u *nextcmd = NULL;
15894
15895 if (expr_end == NULL || in_end == NULL)
15896 return NULL;
15897 *expr_start = NUL;
15898 *expr_end = NUL;
15899 c1 = *in_end;
15900 *in_end = NUL;
15901
15902 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15903 if (temp_result != NULL && nextcmd == NULL)
15904 {
15905 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15906 + (in_end - expr_end) + 1));
15907 if (retval != NULL)
15908 {
15909 STRCPY(retval, in_start);
15910 STRCAT(retval, temp_result);
15911 STRCAT(retval, expr_end + 1);
15912 }
15913 }
15914 vim_free(temp_result);
15915
15916 *in_end = c1; /* put char back for error messages */
15917 *expr_start = '{';
15918 *expr_end = '}';
15919
15920 if (retval != NULL)
15921 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015922 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015923 if (expr_start != NULL)
15924 {
15925 /* Further expansion! */
15926 temp_result = make_expanded_name(retval, expr_start,
15927 expr_end, temp_result);
15928 vim_free(retval);
15929 retval = temp_result;
15930 }
15931 }
15932
15933 return retval;
15934}
15935
15936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015938 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 */
15940 static int
15941eval_isnamec(c)
15942 int c;
15943{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015944 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15945}
15946
15947/*
15948 * Return TRUE if character "c" can be used as the first character in a
15949 * variable or function name (excluding '{' and '}').
15950 */
15951 static int
15952eval_isnamec1(c)
15953 int c;
15954{
15955 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956}
15957
15958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 * Set number v: variable to "val".
15960 */
15961 void
15962set_vim_var_nr(idx, val)
15963 int idx;
15964 long val;
15965{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015966 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967}
15968
15969/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015970 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971 */
15972 long
15973get_vim_var_nr(idx)
15974 int idx;
15975{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015976 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977}
15978
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015979#if defined(FEAT_AUTOCMD) || defined(PROTO)
15980/*
15981 * Get string v: variable value. Uses a static buffer, can only be used once.
15982 */
15983 char_u *
15984get_vim_var_str(idx)
15985 int idx;
15986{
15987 return get_tv_string(&vimvars[idx].vv_tv);
15988}
15989#endif
15990
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991/*
15992 * Set v:count, v:count1 and v:prevcount.
15993 */
15994 void
15995set_vcount(count, count1)
15996 long count;
15997 long count1;
15998{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015999 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16000 vimvars[VV_COUNT].vv_nr = count;
16001 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002}
16003
16004/*
16005 * Set string v: variable to a copy of "val".
16006 */
16007 void
16008set_vim_var_string(idx, val, len)
16009 int idx;
16010 char_u *val;
16011 int len; /* length of "val" to use or -1 (whole string) */
16012{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016013 /* Need to do this (at least) once, since we can't initialize a union.
16014 * Will always be invoked when "v:progname" is set. */
16015 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16016
Bram Moolenaare9a41262005-01-15 22:18:47 +000016017 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016019 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016021 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016023 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024}
16025
16026/*
16027 * Set v:register if needed.
16028 */
16029 void
16030set_reg_var(c)
16031 int c;
16032{
16033 char_u regname;
16034
16035 if (c == 0 || c == ' ')
16036 regname = '"';
16037 else
16038 regname = c;
16039 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016040 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 set_vim_var_string(VV_REG, &regname, 1);
16042}
16043
16044/*
16045 * Get or set v:exception. If "oldval" == NULL, return the current value.
16046 * Otherwise, restore the value to "oldval" and return NULL.
16047 * Must always be called in pairs to save and restore v:exception! Does not
16048 * take care of memory allocations.
16049 */
16050 char_u *
16051v_exception(oldval)
16052 char_u *oldval;
16053{
16054 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016055 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056
Bram Moolenaare9a41262005-01-15 22:18:47 +000016057 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 return NULL;
16059}
16060
16061/*
16062 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16063 * Otherwise, restore the value to "oldval" and return NULL.
16064 * Must always be called in pairs to save and restore v:throwpoint! Does not
16065 * take care of memory allocations.
16066 */
16067 char_u *
16068v_throwpoint(oldval)
16069 char_u *oldval;
16070{
16071 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016072 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073
Bram Moolenaare9a41262005-01-15 22:18:47 +000016074 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 return NULL;
16076}
16077
16078#if defined(FEAT_AUTOCMD) || defined(PROTO)
16079/*
16080 * Set v:cmdarg.
16081 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16082 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16083 * Must always be called in pairs!
16084 */
16085 char_u *
16086set_cmdarg(eap, oldarg)
16087 exarg_T *eap;
16088 char_u *oldarg;
16089{
16090 char_u *oldval;
16091 char_u *newval;
16092 unsigned len;
16093
Bram Moolenaare9a41262005-01-15 22:18:47 +000016094 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016095 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016097 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016098 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016099 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 }
16101
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016102 if (eap->force_bin == FORCE_BIN)
16103 len = 6;
16104 else if (eap->force_bin == FORCE_NOBIN)
16105 len = 8;
16106 else
16107 len = 0;
16108 if (eap->force_ff != 0)
16109 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16110# ifdef FEAT_MBYTE
16111 if (eap->force_enc != 0)
16112 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016113 if (eap->bad_char != 0)
16114 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016115# endif
16116
16117 newval = alloc(len + 1);
16118 if (newval == NULL)
16119 return NULL;
16120
16121 if (eap->force_bin == FORCE_BIN)
16122 sprintf((char *)newval, " ++bin");
16123 else if (eap->force_bin == FORCE_NOBIN)
16124 sprintf((char *)newval, " ++nobin");
16125 else
16126 *newval = NUL;
16127 if (eap->force_ff != 0)
16128 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16129 eap->cmd + eap->force_ff);
16130# ifdef FEAT_MBYTE
16131 if (eap->force_enc != 0)
16132 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16133 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016134 if (eap->bad_char != 0)
16135 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16136 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016137# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016138 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016139 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140}
16141#endif
16142
16143/*
16144 * Get the value of internal variable "name".
16145 * Return OK or FAIL.
16146 */
16147 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016148get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 char_u *name;
16150 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016151 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016152 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153{
16154 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016155 typval_T *tv = NULL;
16156 typval_T atv;
16157 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
16160 /* truncate the name, so that we can use strcmp() */
16161 cc = name[len];
16162 name[len] = NUL;
16163
16164 /*
16165 * Check for "b:changedtick".
16166 */
16167 if (STRCMP(name, "b:changedtick") == 0)
16168 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016169 atv.v_type = VAR_NUMBER;
16170 atv.vval.v_number = curbuf->b_changedtick;
16171 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 }
16173
16174 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 * Check for user-defined variables.
16176 */
16177 else
16178 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016179 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 }
16183
Bram Moolenaare9a41262005-01-15 22:18:47 +000016184 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016186 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016187 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 ret = FAIL;
16189 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016190 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016191 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192
16193 name[len] = cc;
16194
16195 return ret;
16196}
16197
16198/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016199 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16200 * Also handle function call with Funcref variable: func(expr)
16201 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16202 */
16203 static int
16204handle_subscript(arg, rettv, evaluate, verbose)
16205 char_u **arg;
16206 typval_T *rettv;
16207 int evaluate; /* do more than finding the end */
16208 int verbose; /* give error messages */
16209{
16210 int ret = OK;
16211 dict_T *selfdict = NULL;
16212 char_u *s;
16213 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016214 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016215
16216 while (ret == OK
16217 && (**arg == '['
16218 || (**arg == '.' && rettv->v_type == VAR_DICT)
16219 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16220 && !vim_iswhite(*(*arg - 1)))
16221 {
16222 if (**arg == '(')
16223 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016224 /* need to copy the funcref so that we can clear rettv */
16225 functv = *rettv;
16226 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016227
16228 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016229 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016230 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016231 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16232 &len, evaluate, selfdict);
16233
16234 /* Clear the funcref afterwards, so that deleting it while
16235 * evaluating the arguments is possible (see test55). */
16236 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016237
16238 /* Stop the expression evaluation when immediately aborting on
16239 * error, or when an interrupt occurred or an exception was thrown
16240 * but not caught. */
16241 if (aborting())
16242 {
16243 if (ret == OK)
16244 clear_tv(rettv);
16245 ret = FAIL;
16246 }
16247 dict_unref(selfdict);
16248 selfdict = NULL;
16249 }
16250 else /* **arg == '[' || **arg == '.' */
16251 {
16252 dict_unref(selfdict);
16253 if (rettv->v_type == VAR_DICT)
16254 {
16255 selfdict = rettv->vval.v_dict;
16256 if (selfdict != NULL)
16257 ++selfdict->dv_refcount;
16258 }
16259 else
16260 selfdict = NULL;
16261 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16262 {
16263 clear_tv(rettv);
16264 ret = FAIL;
16265 }
16266 }
16267 }
16268 dict_unref(selfdict);
16269 return ret;
16270}
16271
16272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016273 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16274 * value).
16275 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016276 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016278{
Bram Moolenaar33570922005-01-25 22:26:29 +000016279 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016280}
16281
16282/*
16283 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 * The string "s" must have been allocated, it is consumed.
16285 * Return NULL for out of memory, the variable otherwise.
16286 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016287 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 char_u *s;
16290{
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016293 rettv = alloc_tv();
16294 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016296 rettv->v_type = VAR_STRING;
16297 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 }
16299 else
16300 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016301 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302}
16303
16304/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016305 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016307 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016308free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
16311 if (varp != NULL)
16312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016313 switch (varp->v_type)
16314 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016315 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016316 func_unref(varp->vval.v_string);
16317 /*FALLTHROUGH*/
16318 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016319 vim_free(varp->vval.v_string);
16320 break;
16321 case VAR_LIST:
16322 list_unref(varp->vval.v_list);
16323 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016324 case VAR_DICT:
16325 dict_unref(varp->vval.v_dict);
16326 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016327 case VAR_NUMBER:
16328 case VAR_UNKNOWN:
16329 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016330 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016331 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016332 break;
16333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 vim_free(varp);
16335 }
16336}
16337
16338/*
16339 * Free the memory for a variable value and set the value to NULL or 0.
16340 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016341 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016342clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344{
16345 if (varp != NULL)
16346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016347 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016349 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016350 func_unref(varp->vval.v_string);
16351 /*FALLTHROUGH*/
16352 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016353 vim_free(varp->vval.v_string);
16354 varp->vval.v_string = NULL;
16355 break;
16356 case VAR_LIST:
16357 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016358 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016359 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016360 case VAR_DICT:
16361 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016362 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016363 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016365 varp->vval.v_number = 0;
16366 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016367 case VAR_UNKNOWN:
16368 break;
16369 default:
16370 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016372 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373 }
16374}
16375
16376/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016377 * Set the value of a variable to NULL without freeing items.
16378 */
16379 static void
16380init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016381 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016382{
16383 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016384 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016385}
16386
16387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 * Get the number value of a variable.
16389 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016390 * For incompatible types, return 0.
16391 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16392 * caller of incompatible types: it sets *denote to TRUE if "denote"
16393 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 */
16395 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016397 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016399 int error = FALSE;
16400
16401 return get_tv_number_chk(varp, &error); /* return 0L on error */
16402}
16403
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016404 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016405get_tv_number_chk(varp, denote)
16406 typval_T *varp;
16407 int *denote;
16408{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016409 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016411 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016413 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016414 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016415 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016416 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016417 break;
16418 case VAR_STRING:
16419 if (varp->vval.v_string != NULL)
16420 vim_str2nr(varp->vval.v_string, NULL, NULL,
16421 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016423 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016424 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016425 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016426 case VAR_DICT:
16427 EMSG(_("E728: Using a Dictionary as a number"));
16428 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016429 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016430 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016433 if (denote == NULL) /* useful for values that must be unsigned */
16434 n = -1;
16435 else
16436 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016437 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438}
16439
16440/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016441 * Get the lnum from the first argument.
16442 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 */
16445 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016446get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016447 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448{
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 linenr_T lnum;
16451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016452 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 if (lnum == 0) /* no valid number, try using line() */
16454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016455 rettv.v_type = VAR_NUMBER;
16456 f_line(argvars, &rettv);
16457 lnum = rettv.vval.v_number;
16458 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 }
16460 return lnum;
16461}
16462
16463/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016464 * Get the lnum from the first argument.
16465 * Also accepts "$", then "buf" is used.
16466 * Returns 0 on error.
16467 */
16468 static linenr_T
16469get_tv_lnum_buf(argvars, buf)
16470 typval_T *argvars;
16471 buf_T *buf;
16472{
16473 if (argvars[0].v_type == VAR_STRING
16474 && argvars[0].vval.v_string != NULL
16475 && argvars[0].vval.v_string[0] == '$'
16476 && buf != NULL)
16477 return buf->b_ml.ml_line_count;
16478 return get_tv_number_chk(&argvars[0], NULL);
16479}
16480
16481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 * Get the string value of a variable.
16483 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016484 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16485 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 * If the String variable has never been set, return an empty string.
16487 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016488 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16489 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 */
16491 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016492get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016493 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494{
16495 static char_u mybuf[NUMBUFLEN];
16496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016497 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498}
16499
16500 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016501get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016502 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016503 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016505 char_u *res = get_tv_string_buf_chk(varp, buf);
16506
16507 return res != NULL ? res : (char_u *)"";
16508}
16509
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016510 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016511get_tv_string_chk(varp)
16512 typval_T *varp;
16513{
16514 static char_u mybuf[NUMBUFLEN];
16515
16516 return get_tv_string_buf_chk(varp, mybuf);
16517}
16518
16519 static char_u *
16520get_tv_string_buf_chk(varp, buf)
16521 typval_T *varp;
16522 char_u *buf;
16523{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016524 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016526 case VAR_NUMBER:
16527 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16528 return buf;
16529 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016530 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016531 break;
16532 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016533 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016534 break;
16535 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016536 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537 break;
16538 case VAR_STRING:
16539 if (varp->vval.v_string != NULL)
16540 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016542 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016543 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016546 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547}
16548
16549/*
16550 * Find variable "name" in the list of variables.
16551 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016552 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016553 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016556 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016557find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016559 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016562 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563
Bram Moolenaara7043832005-01-21 11:56:39 +000016564 ht = find_var_ht(name, &varname);
16565 if (htp != NULL)
16566 *htp = ht;
16567 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016569 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570}
16571
16572/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016573 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016574 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016576 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016577find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016578 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016579 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016580 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016581{
Bram Moolenaar33570922005-01-25 22:26:29 +000016582 hashitem_T *hi;
16583
16584 if (*varname == NUL)
16585 {
16586 /* Must be something like "s:", otherwise "ht" would be NULL. */
16587 switch (varname[-2])
16588 {
16589 case 's': return &SCRIPT_SV(current_SID).sv_var;
16590 case 'g': return &globvars_var;
16591 case 'v': return &vimvars_var;
16592 case 'b': return &curbuf->b_bufvar;
16593 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016594 case 'l': return current_funccal == NULL
16595 ? NULL : &current_funccal->l_vars_var;
16596 case 'a': return current_funccal == NULL
16597 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016598 }
16599 return NULL;
16600 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016601
16602 hi = hash_find(ht, varname);
16603 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016604 {
16605 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016606 * worked find the variable again. Don't auto-load a script if it was
16607 * loaded already, otherwise it would be loaded every time when
16608 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016609 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016610 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016611 hi = hash_find(ht, varname);
16612 if (HASHITEM_EMPTY(hi))
16613 return NULL;
16614 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016616}
16617
16618/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016619 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016620 * Set "varname" to the start of name without ':'.
16621 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016622 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016623find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 char_u *name;
16625 char_u **varname;
16626{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016627 hashitem_T *hi;
16628
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 if (name[1] != ':')
16630 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016631 /* The name must not start with a colon or #. */
16632 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 return NULL;
16634 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016635
16636 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016637 hi = hash_find(&compat_hashtab, name);
16638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016639 return &compat_hashtab;
16640
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016642 return &globvarht; /* global variable */
16643 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 }
16645 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016646 if (*name == 'g') /* global variable */
16647 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016648 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16649 */
16650 if (vim_strchr(name + 2, ':') != NULL
16651 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016652 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016654 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016656 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016657 if (*name == 'v') /* v: variable */
16658 return &vimvarht;
16659 if (*name == 'a' && current_funccal != NULL) /* function argument */
16660 return &current_funccal->l_avars.dv_hashtab;
16661 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16662 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 if (*name == 's' /* script variable */
16664 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16665 return &SCRIPT_VARS(current_SID);
16666 return NULL;
16667}
16668
16669/*
16670 * Get the string value of a (global/local) variable.
16671 * Returns NULL when it doesn't exist.
16672 */
16673 char_u *
16674get_var_value(name)
16675 char_u *name;
16676{
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678
Bram Moolenaara7043832005-01-21 11:56:39 +000016679 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 if (v == NULL)
16681 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016682 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683}
16684
16685/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016686 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 * sourcing this script and when executing functions defined in the script.
16688 */
16689 void
16690new_script_vars(id)
16691 scid_T id;
16692{
Bram Moolenaara7043832005-01-21 11:56:39 +000016693 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016694 hashtab_T *ht;
16695 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016696
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16698 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016699 /* Re-allocating ga_data means that an ht_array pointing to
16700 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016702 for (i = 1; i <= ga_scripts.ga_len; ++i)
16703 {
16704 ht = &SCRIPT_VARS(i);
16705 if (ht->ht_mask == HT_INIT_SIZE - 1)
16706 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016707 sv = &SCRIPT_SV(i);
16708 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016709 }
16710
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 while (ga_scripts.ga_len < id)
16712 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016713 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16714 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 }
16717 }
16718}
16719
16720/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016721 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16722 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 */
16724 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016725init_var_dict(dict, dict_var)
16726 dict_T *dict;
16727 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728{
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 hash_init(&dict->dv_hashtab);
16730 dict->dv_refcount = 99999;
16731 dict_var->di_tv.vval.v_dict = dict;
16732 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016733 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016734 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16735 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736}
16737
16738/*
16739 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016740 * Frees all allocated variables and the value they contain.
16741 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742 */
16743 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016744vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016745 hashtab_T *ht;
16746{
16747 vars_clear_ext(ht, TRUE);
16748}
16749
16750/*
16751 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16752 */
16753 static void
16754vars_clear_ext(ht, free_val)
16755 hashtab_T *ht;
16756 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757{
Bram Moolenaara7043832005-01-21 11:56:39 +000016758 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016759 hashitem_T *hi;
16760 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761
Bram Moolenaar33570922005-01-25 22:26:29 +000016762 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016763 todo = ht->ht_used;
16764 for (hi = ht->ht_array; todo > 0; ++hi)
16765 {
16766 if (!HASHITEM_EMPTY(hi))
16767 {
16768 --todo;
16769
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016771 * ht_array might change then. hash_clear() takes care of it
16772 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016773 v = HI2DI(hi);
16774 if (free_val)
16775 clear_tv(&v->di_tv);
16776 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16777 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016778 }
16779 }
16780 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016781 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782}
16783
Bram Moolenaara7043832005-01-21 11:56:39 +000016784/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 * Delete a variable from hashtab "ht" at item "hi".
16786 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016789delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016790 hashtab_T *ht;
16791 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792{
Bram Moolenaar33570922005-01-25 22:26:29 +000016793 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016794
16795 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016796 clear_tv(&di->di_tv);
16797 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798}
16799
16800/*
16801 * List the value of one internal variable.
16802 */
16803 static void
16804list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016805 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 char_u *prefix;
16807{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016808 char_u *tofree;
16809 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016810 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016811
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016812 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000016813 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016814 s == NULL ? (char_u *)"" : s);
16815 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816}
16817
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 static void
16819list_one_var_a(prefix, name, type, string)
16820 char_u *prefix;
16821 char_u *name;
16822 int type;
16823 char_u *string;
16824{
16825 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16826 if (name != NULL) /* "a:" vars don't have a name stored */
16827 msg_puts(name);
16828 msg_putchar(' ');
16829 msg_advance(22);
16830 if (type == VAR_NUMBER)
16831 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016832 else if (type == VAR_FUNC)
16833 msg_putchar('*');
16834 else if (type == VAR_LIST)
16835 {
16836 msg_putchar('[');
16837 if (*string == '[')
16838 ++string;
16839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016840 else if (type == VAR_DICT)
16841 {
16842 msg_putchar('{');
16843 if (*string == '{')
16844 ++string;
16845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 else
16847 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016848
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016850
16851 if (type == VAR_FUNC)
16852 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853}
16854
16855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016856 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 * If the variable already exists, the value is updated.
16858 * Otherwise the variable is created.
16859 */
16860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016861set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016864 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865{
Bram Moolenaar33570922005-01-25 22:26:29 +000016866 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016868 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016869 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016871 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016872 {
16873 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16874 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16875 ? name[2] : name[0]))
16876 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016877 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016878 return;
16879 }
16880 if (function_exists(name))
16881 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016882 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016883 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016884 return;
16885 }
16886 }
16887
Bram Moolenaara7043832005-01-21 11:56:39 +000016888 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016889 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016890 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016891 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016892 return;
16893 }
16894
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016895 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016896 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016898 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016899 if (var_check_ro(v->di_flags, name)
16900 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016901 return;
16902 if (v->di_tv.v_type != tv->v_type
16903 && !((v->di_tv.v_type == VAR_STRING
16904 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016905 && (tv->v_type == VAR_STRING
16906 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016907 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016908 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016909 return;
16910 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016911
16912 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016913 * Handle setting internal v: variables separately: we don't change
16914 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016915 */
16916 if (ht == &vimvarht)
16917 {
16918 if (v->di_tv.v_type == VAR_STRING)
16919 {
16920 vim_free(v->di_tv.vval.v_string);
16921 if (copy || tv->v_type != VAR_STRING)
16922 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16923 else
16924 {
16925 /* Take over the string to avoid an extra alloc/free. */
16926 v->di_tv.vval.v_string = tv->vval.v_string;
16927 tv->vval.v_string = NULL;
16928 }
16929 }
16930 else if (v->di_tv.v_type != VAR_NUMBER)
16931 EMSG2(_(e_intern2), "set_var()");
16932 else
16933 v->di_tv.vval.v_number = get_tv_number(tv);
16934 return;
16935 }
16936
16937 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 }
16939 else /* add a new variable */
16940 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016941 /* Make sure the variable name is valid. */
16942 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016943 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16944 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016945 {
16946 EMSG2(_(e_illvar), varname);
16947 return;
16948 }
16949
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016950 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16951 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016952 if (v == NULL)
16953 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016954 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016955 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016957 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 return;
16959 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016960 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016963 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016964 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016965 else
16966 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016967 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016968 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016969 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971}
16972
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016973/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016974 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16975 * Also give an error message.
16976 */
16977 static int
16978var_check_ro(flags, name)
16979 int flags;
16980 char_u *name;
16981{
16982 if (flags & DI_FLAGS_RO)
16983 {
16984 EMSG2(_(e_readonlyvar), name);
16985 return TRUE;
16986 }
16987 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16988 {
16989 EMSG2(_(e_readonlysbx), name);
16990 return TRUE;
16991 }
16992 return FALSE;
16993}
16994
16995/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016996 * Return TRUE if typeval "tv" is set to be locked (immutable).
16997 * Also give an error message, using "name".
16998 */
16999 static int
17000tv_check_lock(lock, name)
17001 int lock;
17002 char_u *name;
17003{
17004 if (lock & VAR_LOCKED)
17005 {
17006 EMSG2(_("E741: Value is locked: %s"),
17007 name == NULL ? (char_u *)_("Unknown") : name);
17008 return TRUE;
17009 }
17010 if (lock & VAR_FIXED)
17011 {
17012 EMSG2(_("E742: Cannot change value of %s"),
17013 name == NULL ? (char_u *)_("Unknown") : name);
17014 return TRUE;
17015 }
17016 return FALSE;
17017}
17018
17019/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017020 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017021 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017022 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017025copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017026 typval_T *from;
17027 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017029 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017030 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017031 switch (from->v_type)
17032 {
17033 case VAR_NUMBER:
17034 to->vval.v_number = from->vval.v_number;
17035 break;
17036 case VAR_STRING:
17037 case VAR_FUNC:
17038 if (from->vval.v_string == NULL)
17039 to->vval.v_string = NULL;
17040 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017041 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017042 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017043 if (from->v_type == VAR_FUNC)
17044 func_ref(to->vval.v_string);
17045 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017046 break;
17047 case VAR_LIST:
17048 if (from->vval.v_list == NULL)
17049 to->vval.v_list = NULL;
17050 else
17051 {
17052 to->vval.v_list = from->vval.v_list;
17053 ++to->vval.v_list->lv_refcount;
17054 }
17055 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017056 case VAR_DICT:
17057 if (from->vval.v_dict == NULL)
17058 to->vval.v_dict = NULL;
17059 else
17060 {
17061 to->vval.v_dict = from->vval.v_dict;
17062 ++to->vval.v_dict->dv_refcount;
17063 }
17064 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017065 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017066 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017067 break;
17068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069}
17070
17071/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017072 * Make a copy of an item.
17073 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017074 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17075 * reference to an already copied list/dict can be used.
17076 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017077 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017078 static int
17079item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017080 typval_T *from;
17081 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017082 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017083 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017084{
17085 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017086 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017087
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017089 {
17090 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017091 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017092 }
17093 ++recurse;
17094
17095 switch (from->v_type)
17096 {
17097 case VAR_NUMBER:
17098 case VAR_STRING:
17099 case VAR_FUNC:
17100 copy_tv(from, to);
17101 break;
17102 case VAR_LIST:
17103 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017104 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017105 if (from->vval.v_list == NULL)
17106 to->vval.v_list = NULL;
17107 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17108 {
17109 /* use the copy made earlier */
17110 to->vval.v_list = from->vval.v_list->lv_copylist;
17111 ++to->vval.v_list->lv_refcount;
17112 }
17113 else
17114 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17115 if (to->vval.v_list == NULL)
17116 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017117 break;
17118 case VAR_DICT:
17119 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017120 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017121 if (from->vval.v_dict == NULL)
17122 to->vval.v_dict = NULL;
17123 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17124 {
17125 /* use the copy made earlier */
17126 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17127 ++to->vval.v_dict->dv_refcount;
17128 }
17129 else
17130 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17131 if (to->vval.v_dict == NULL)
17132 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017133 break;
17134 default:
17135 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017136 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017137 }
17138 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017139 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017140}
17141
17142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 * ":echo expr1 ..." print each argument separated with a space, add a
17144 * newline at the end.
17145 * ":echon expr1 ..." print each argument plain.
17146 */
17147 void
17148ex_echo(eap)
17149 exarg_T *eap;
17150{
17151 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017152 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017153 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 char_u *p;
17155 int needclr = TRUE;
17156 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017157 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158
17159 if (eap->skip)
17160 ++emsg_skip;
17161 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17162 {
17163 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017164 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 {
17166 /*
17167 * Report the invalid expression unless the expression evaluation
17168 * has been cancelled due to an aborting error, an interrupt, or an
17169 * exception.
17170 */
17171 if (!aborting())
17172 EMSG2(_(e_invexpr2), p);
17173 break;
17174 }
17175 if (!eap->skip)
17176 {
17177 if (atstart)
17178 {
17179 atstart = FALSE;
17180 /* Call msg_start() after eval1(), evaluating the expression
17181 * may cause a message to appear. */
17182 if (eap->cmdidx == CMD_echo)
17183 msg_start();
17184 }
17185 else if (eap->cmdidx == CMD_echo)
17186 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017187 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017188 if (p != NULL)
17189 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017191 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017193 if (*p != TAB && needclr)
17194 {
17195 /* remove any text still there from the command */
17196 msg_clr_eos();
17197 needclr = FALSE;
17198 }
17199 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 }
17201 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017202 {
17203#ifdef FEAT_MBYTE
17204 if (has_mbyte)
17205 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017206 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017207
17208 (void)msg_outtrans_len_attr(p, i, echo_attr);
17209 p += i - 1;
17210 }
17211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017213 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017216 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017218 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 arg = skipwhite(arg);
17220 }
17221 eap->nextcmd = check_nextcmd(arg);
17222
17223 if (eap->skip)
17224 --emsg_skip;
17225 else
17226 {
17227 /* remove text that may still be there from the command */
17228 if (needclr)
17229 msg_clr_eos();
17230 if (eap->cmdidx == CMD_echo)
17231 msg_end();
17232 }
17233}
17234
17235/*
17236 * ":echohl {name}".
17237 */
17238 void
17239ex_echohl(eap)
17240 exarg_T *eap;
17241{
17242 int id;
17243
17244 id = syn_name2id(eap->arg);
17245 if (id == 0)
17246 echo_attr = 0;
17247 else
17248 echo_attr = syn_id2attr(id);
17249}
17250
17251/*
17252 * ":execute expr1 ..." execute the result of an expression.
17253 * ":echomsg expr1 ..." Print a message
17254 * ":echoerr expr1 ..." Print an error
17255 * Each gets spaces around each argument and a newline at the end for
17256 * echo commands
17257 */
17258 void
17259ex_execute(eap)
17260 exarg_T *eap;
17261{
17262 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 int ret = OK;
17265 char_u *p;
17266 garray_T ga;
17267 int len;
17268 int save_did_emsg;
17269
17270 ga_init2(&ga, 1, 80);
17271
17272 if (eap->skip)
17273 ++emsg_skip;
17274 while (*arg != NUL && *arg != '|' && *arg != '\n')
17275 {
17276 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017277 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 {
17279 /*
17280 * Report the invalid expression unless the expression evaluation
17281 * has been cancelled due to an aborting error, an interrupt, or an
17282 * exception.
17283 */
17284 if (!aborting())
17285 EMSG2(_(e_invexpr2), p);
17286 ret = FAIL;
17287 break;
17288 }
17289
17290 if (!eap->skip)
17291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017292 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 len = (int)STRLEN(p);
17294 if (ga_grow(&ga, len + 2) == FAIL)
17295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 ret = FAIL;
17298 break;
17299 }
17300 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 ga.ga_len += len;
17304 }
17305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 arg = skipwhite(arg);
17308 }
17309
17310 if (ret != FAIL && ga.ga_data != NULL)
17311 {
17312 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017315 out_flush();
17316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 else if (eap->cmdidx == CMD_echoerr)
17318 {
17319 /* We don't want to abort following commands, restore did_emsg. */
17320 save_did_emsg = did_emsg;
17321 EMSG((char_u *)ga.ga_data);
17322 if (!force_abort)
17323 did_emsg = save_did_emsg;
17324 }
17325 else if (eap->cmdidx == CMD_execute)
17326 do_cmdline((char_u *)ga.ga_data,
17327 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17328 }
17329
17330 ga_clear(&ga);
17331
17332 if (eap->skip)
17333 --emsg_skip;
17334
17335 eap->nextcmd = check_nextcmd(arg);
17336}
17337
17338/*
17339 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17340 * "arg" points to the "&" or '+' when called, to "option" when returning.
17341 * Returns NULL when no option name found. Otherwise pointer to the char
17342 * after the option name.
17343 */
17344 static char_u *
17345find_option_end(arg, opt_flags)
17346 char_u **arg;
17347 int *opt_flags;
17348{
17349 char_u *p = *arg;
17350
17351 ++p;
17352 if (*p == 'g' && p[1] == ':')
17353 {
17354 *opt_flags = OPT_GLOBAL;
17355 p += 2;
17356 }
17357 else if (*p == 'l' && p[1] == ':')
17358 {
17359 *opt_flags = OPT_LOCAL;
17360 p += 2;
17361 }
17362 else
17363 *opt_flags = 0;
17364
17365 if (!ASCII_ISALPHA(*p))
17366 return NULL;
17367 *arg = p;
17368
17369 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17370 p += 4; /* termcap option */
17371 else
17372 while (ASCII_ISALPHA(*p))
17373 ++p;
17374 return p;
17375}
17376
17377/*
17378 * ":function"
17379 */
17380 void
17381ex_function(eap)
17382 exarg_T *eap;
17383{
17384 char_u *theline;
17385 int j;
17386 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 char_u *name = NULL;
17389 char_u *p;
17390 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017391 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 garray_T newargs;
17393 garray_T newlines;
17394 int varargs = FALSE;
17395 int mustend = FALSE;
17396 int flags = 0;
17397 ufunc_T *fp;
17398 int indent;
17399 int nesting;
17400 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017401 dictitem_T *v;
17402 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017403 static int func_nr = 0; /* number for nameless function */
17404 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017405 hashtab_T *ht;
17406 int todo;
17407 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408
17409 /*
17410 * ":function" without argument: list functions.
17411 */
17412 if (ends_excmd(*eap->arg))
17413 {
17414 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017415 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017416 todo = func_hashtab.ht_used;
17417 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017418 {
17419 if (!HASHITEM_EMPTY(hi))
17420 {
17421 --todo;
17422 fp = HI2UF(hi);
17423 if (!isdigit(*fp->uf_name))
17424 list_func_head(fp, FALSE);
17425 }
17426 }
17427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 eap->nextcmd = check_nextcmd(eap->arg);
17429 return;
17430 }
17431
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017432 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017433 * ":function /pat": list functions matching pattern.
17434 */
17435 if (*eap->arg == '/')
17436 {
17437 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17438 if (!eap->skip)
17439 {
17440 regmatch_T regmatch;
17441
17442 c = *p;
17443 *p = NUL;
17444 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17445 *p = c;
17446 if (regmatch.regprog != NULL)
17447 {
17448 regmatch.rm_ic = p_ic;
17449
17450 todo = func_hashtab.ht_used;
17451 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17452 {
17453 if (!HASHITEM_EMPTY(hi))
17454 {
17455 --todo;
17456 fp = HI2UF(hi);
17457 if (!isdigit(*fp->uf_name)
17458 && vim_regexec(&regmatch, fp->uf_name, 0))
17459 list_func_head(fp, FALSE);
17460 }
17461 }
17462 }
17463 }
17464 if (*p == '/')
17465 ++p;
17466 eap->nextcmd = check_nextcmd(p);
17467 return;
17468 }
17469
17470 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017471 * Get the function name. There are these situations:
17472 * func normal function name
17473 * "name" == func, "fudi.fd_dict" == NULL
17474 * dict.func new dictionary entry
17475 * "name" == NULL, "fudi.fd_dict" set,
17476 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17477 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017478 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017479 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17480 * dict.func existing dict entry that's not a Funcref
17481 * "name" == NULL, "fudi.fd_dict" set,
17482 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017485 name = trans_function_name(&p, eap->skip, 0, &fudi);
17486 paren = (vim_strchr(p, '(') != NULL);
17487 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 {
17489 /*
17490 * Return on an invalid expression in braces, unless the expression
17491 * evaluation has been cancelled due to an aborting error, an
17492 * interrupt, or an exception.
17493 */
17494 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017496 if (!eap->skip && fudi.fd_newkey != NULL)
17497 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017498 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 else
17502 eap->skip = TRUE;
17503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 /* An error in a function call during evaluation of an expression in magic
17505 * braces should not cause the function not to be defined. */
17506 saved_did_emsg = did_emsg;
17507 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508
17509 /*
17510 * ":function func" with only function name: list function.
17511 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017512 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 {
17514 if (!ends_excmd(*skipwhite(p)))
17515 {
17516 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017517 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 }
17519 eap->nextcmd = check_nextcmd(p);
17520 if (eap->nextcmd != NULL)
17521 *p = NUL;
17522 if (!eap->skip && !got_int)
17523 {
17524 fp = find_func(name);
17525 if (fp != NULL)
17526 {
17527 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017528 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529 {
17530 msg_putchar('\n');
17531 msg_outnum((long)(j + 1));
17532 if (j < 9)
17533 msg_putchar(' ');
17534 if (j < 99)
17535 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017536 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 out_flush(); /* show a line at a time */
17538 ui_breakcheck();
17539 }
17540 if (!got_int)
17541 {
17542 msg_putchar('\n');
17543 msg_puts((char_u *)" endfunction");
17544 }
17545 }
17546 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017547 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017549 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 }
17551
17552 /*
17553 * ":function name(arg1, arg2)" Define function.
17554 */
17555 p = skipwhite(p);
17556 if (*p != '(')
17557 {
17558 if (!eap->skip)
17559 {
17560 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017561 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 }
17563 /* attempt to continue by skipping some text */
17564 if (vim_strchr(p, '(') != NULL)
17565 p = vim_strchr(p, '(');
17566 }
17567 p = skipwhite(p + 1);
17568
17569 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17570 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17571
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017572 if (!eap->skip)
17573 {
17574 /* Check the name of the function. */
17575 if (name != NULL)
17576 arg = name;
17577 else
17578 arg = fudi.fd_newkey;
17579 if (arg != NULL)
17580 {
17581 if (*arg == K_SPECIAL)
17582 j = 3;
17583 else
17584 j = 0;
17585 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17586 : eval_isnamec(arg[j])))
17587 ++j;
17588 if (arg[j] != NUL)
17589 emsg_funcname(_(e_invarg2), arg);
17590 }
17591 }
17592
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 /*
17594 * Isolate the arguments: "arg1, arg2, ...)"
17595 */
17596 while (*p != ')')
17597 {
17598 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17599 {
17600 varargs = TRUE;
17601 p += 3;
17602 mustend = TRUE;
17603 }
17604 else
17605 {
17606 arg = p;
17607 while (ASCII_ISALNUM(*p) || *p == '_')
17608 ++p;
17609 if (arg == p || isdigit(*arg)
17610 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17611 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17612 {
17613 if (!eap->skip)
17614 EMSG2(_("E125: Illegal argument: %s"), arg);
17615 break;
17616 }
17617 if (ga_grow(&newargs, 1) == FAIL)
17618 goto erret;
17619 c = *p;
17620 *p = NUL;
17621 arg = vim_strsave(arg);
17622 if (arg == NULL)
17623 goto erret;
17624 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17625 *p = c;
17626 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 if (*p == ',')
17628 ++p;
17629 else
17630 mustend = TRUE;
17631 }
17632 p = skipwhite(p);
17633 if (mustend && *p != ')')
17634 {
17635 if (!eap->skip)
17636 EMSG2(_(e_invarg2), eap->arg);
17637 break;
17638 }
17639 }
17640 ++p; /* skip the ')' */
17641
Bram Moolenaare9a41262005-01-15 22:18:47 +000017642 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 for (;;)
17644 {
17645 p = skipwhite(p);
17646 if (STRNCMP(p, "range", 5) == 0)
17647 {
17648 flags |= FC_RANGE;
17649 p += 5;
17650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017651 else if (STRNCMP(p, "dict", 4) == 0)
17652 {
17653 flags |= FC_DICT;
17654 p += 4;
17655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 else if (STRNCMP(p, "abort", 5) == 0)
17657 {
17658 flags |= FC_ABORT;
17659 p += 5;
17660 }
17661 else
17662 break;
17663 }
17664
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017665 /* When there is a line break use what follows for the function body.
17666 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17667 if (*p == '\n')
17668 line_arg = p + 1;
17669 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 EMSG(_(e_trailing));
17671
17672 /*
17673 * Read the body of the function, until ":endfunction" is found.
17674 */
17675 if (KeyTyped)
17676 {
17677 /* Check if the function already exists, don't let the user type the
17678 * whole function before telling him it doesn't work! For a script we
17679 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017680 if (!eap->skip && !eap->forceit)
17681 {
17682 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17683 EMSG(_(e_funcdict));
17684 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017685 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017688 if (!eap->skip && did_emsg)
17689 goto erret;
17690
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 msg_putchar('\n'); /* don't overwrite the function name */
17692 cmdline_row = msg_row;
17693 }
17694
17695 indent = 2;
17696 nesting = 0;
17697 for (;;)
17698 {
17699 msg_scroll = TRUE;
17700 need_wait_return = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017701 if (line_arg != NULL)
17702 {
17703 /* Use eap->arg, split up in parts by line breaks. */
17704 theline = line_arg;
17705 p = vim_strchr(theline, '\n');
17706 if (p == NULL)
17707 line_arg += STRLEN(line_arg);
17708 else
17709 {
17710 *p = NUL;
17711 line_arg = p + 1;
17712 }
17713 }
17714 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 theline = getcmdline(':', 0L, indent);
17716 else
17717 theline = eap->getline(':', eap->cookie, indent);
17718 if (KeyTyped)
17719 lines_left = Rows - 1;
17720 if (theline == NULL)
17721 {
17722 EMSG(_("E126: Missing :endfunction"));
17723 goto erret;
17724 }
17725
17726 if (skip_until != NULL)
17727 {
17728 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17729 * don't check for ":endfunc". */
17730 if (STRCMP(theline, skip_until) == 0)
17731 {
17732 vim_free(skip_until);
17733 skip_until = NULL;
17734 }
17735 }
17736 else
17737 {
17738 /* skip ':' and blanks*/
17739 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17740 ;
17741
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017742 /* Check for "endfunction". */
17743 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017745 if (line_arg == NULL)
17746 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 break;
17748 }
17749
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017750 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 * at "end". */
17752 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17753 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017754 else if (STRNCMP(p, "if", 2) == 0
17755 || STRNCMP(p, "wh", 2) == 0
17756 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 || STRNCMP(p, "try", 3) == 0)
17758 indent += 2;
17759
17760 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017761 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017763 if (*p == '!')
17764 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 p += eval_fname_script(p);
17766 if (ASCII_ISALPHA(*p))
17767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017768 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 if (*skipwhite(p) == '(')
17770 {
17771 ++nesting;
17772 indent += 2;
17773 }
17774 }
17775 }
17776
17777 /* Check for ":append" or ":insert". */
17778 p = skip_range(p, NULL);
17779 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17780 || (p[0] == 'i'
17781 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17782 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17783 skip_until = vim_strsave((char_u *)".");
17784
17785 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17786 arg = skipwhite(skiptowhite(p));
17787 if (arg[0] == '<' && arg[1] =='<'
17788 && ((p[0] == 'p' && p[1] == 'y'
17789 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17790 || (p[0] == 'p' && p[1] == 'e'
17791 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17792 || (p[0] == 't' && p[1] == 'c'
17793 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17794 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17795 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017796 || (p[0] == 'm' && p[1] == 'z'
17797 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 ))
17799 {
17800 /* ":python <<" continues until a dot, like ":append" */
17801 p = skipwhite(arg + 2);
17802 if (*p == NUL)
17803 skip_until = vim_strsave((char_u *)".");
17804 else
17805 skip_until = vim_strsave(p);
17806 }
17807 }
17808
17809 /* Add the line to the function. */
17810 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017811 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017812 if (line_arg == NULL)
17813 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017815 }
17816
17817 /* Copy the line to newly allocated memory. get_one_sourceline()
17818 * allocates 250 bytes per line, this saves 80% on average. The cost
17819 * is an extra alloc/free. */
17820 p = vim_strsave(theline);
17821 if (p != NULL)
17822 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017823 if (line_arg == NULL)
17824 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017825 theline = p;
17826 }
17827
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17829 newlines.ga_len++;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017830
17831 /* Check for end of eap->arg. */
17832 if (line_arg != NULL && *line_arg == NUL)
17833 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 }
17835
17836 /* Don't define the function when skipping commands or when an error was
17837 * detected. */
17838 if (eap->skip || did_emsg)
17839 goto erret;
17840
17841 /*
17842 * If there are no errors, add the function
17843 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017844 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017845 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017846 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017848 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017849 emsg_funcname("E707: Function name conflicts with variable: %s",
17850 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017851 goto erret;
17852 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017853
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017854 fp = find_func(name);
17855 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017857 if (!eap->forceit)
17858 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017859 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017860 goto erret;
17861 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017862 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017863 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017864 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017865 name);
17866 goto erret;
17867 }
17868 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017869 ga_clear_strings(&(fp->uf_args));
17870 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017871 vim_free(name);
17872 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 }
17875 else
17876 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017877 char numbuf[20];
17878
17879 fp = NULL;
17880 if (fudi.fd_newkey == NULL && !eap->forceit)
17881 {
17882 EMSG(_(e_funcdict));
17883 goto erret;
17884 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017885 if (fudi.fd_di == NULL)
17886 {
17887 /* Can't add a function to a locked dictionary */
17888 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17889 goto erret;
17890 }
17891 /* Can't change an existing function if it is locked */
17892 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17893 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017894
17895 /* Give the function a sequential number. Can only be used with a
17896 * Funcref! */
17897 vim_free(name);
17898 sprintf(numbuf, "%d", ++func_nr);
17899 name = vim_strsave((char_u *)numbuf);
17900 if (name == NULL)
17901 goto erret;
17902 }
17903
17904 if (fp == NULL)
17905 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017906 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017907 {
17908 int slen, plen;
17909 char_u *scriptname;
17910
17911 /* Check that the autoload name matches the script name. */
17912 j = FAIL;
17913 if (sourcing_name != NULL)
17914 {
17915 scriptname = autoload_name(name);
17916 if (scriptname != NULL)
17917 {
17918 p = vim_strchr(scriptname, '/');
17919 plen = STRLEN(p);
17920 slen = STRLEN(sourcing_name);
17921 if (slen > plen && fnamecmp(p,
17922 sourcing_name + slen - plen) == 0)
17923 j = OK;
17924 vim_free(scriptname);
17925 }
17926 }
17927 if (j == FAIL)
17928 {
17929 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17930 goto erret;
17931 }
17932 }
17933
17934 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 if (fp == NULL)
17936 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017937
17938 if (fudi.fd_dict != NULL)
17939 {
17940 if (fudi.fd_di == NULL)
17941 {
17942 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017943 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017944 if (fudi.fd_di == NULL)
17945 {
17946 vim_free(fp);
17947 goto erret;
17948 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017949 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17950 {
17951 vim_free(fudi.fd_di);
17952 goto erret;
17953 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017954 }
17955 else
17956 /* overwrite existing dict entry */
17957 clear_tv(&fudi.fd_di->di_tv);
17958 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017959 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017960 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017961 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017962 }
17963
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017965 STRCPY(fp->uf_name, name);
17966 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017968 fp->uf_args = newargs;
17969 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017970#ifdef FEAT_PROFILE
17971 fp->uf_tml_count = NULL;
17972 fp->uf_tml_total = NULL;
17973 fp->uf_tml_self = NULL;
17974 fp->uf_profiling = FALSE;
17975 if (prof_def_func())
17976 func_do_profile(fp);
17977#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017978 fp->uf_varargs = varargs;
17979 fp->uf_flags = flags;
17980 fp->uf_calls = 0;
17981 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017982 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983
17984erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985 ga_clear_strings(&newargs);
17986 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017987ret_free:
17988 vim_free(skip_until);
17989 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992}
17993
17994/*
17995 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017996 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017998 * flags:
17999 * TFN_INT: internal function name OK
18000 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 * Advances "pp" to just after the function name (if no error).
18002 */
18003 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018004trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 char_u **pp;
18006 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018007 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018008 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018010 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 char_u *start;
18012 char_u *end;
18013 int lead;
18014 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018017
18018 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018019 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018021
18022 /* Check for hard coded <SNR>: already translated function ID (from a user
18023 * command). */
18024 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18025 && (*pp)[2] == (int)KE_SNR)
18026 {
18027 *pp += 3;
18028 len = get_id_len(pp) + 3;
18029 return vim_strnsave(start, len);
18030 }
18031
18032 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18033 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018035 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018037
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018038 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18039 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018040 if (end == start)
18041 {
18042 if (!skip)
18043 EMSG(_("E129: Function name required"));
18044 goto theend;
18045 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018046 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018047 {
18048 /*
18049 * Report an invalid expression in braces, unless the expression
18050 * evaluation has been cancelled due to an aborting error, an
18051 * interrupt, or an exception.
18052 */
18053 if (!aborting())
18054 {
18055 if (end != NULL)
18056 EMSG2(_(e_invarg2), start);
18057 }
18058 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018059 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018060 goto theend;
18061 }
18062
18063 if (lv.ll_tv != NULL)
18064 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018065 if (fdp != NULL)
18066 {
18067 fdp->fd_dict = lv.ll_dict;
18068 fdp->fd_newkey = lv.ll_newkey;
18069 lv.ll_newkey = NULL;
18070 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018071 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018072 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18073 {
18074 name = vim_strsave(lv.ll_tv->vval.v_string);
18075 *pp = end;
18076 }
18077 else
18078 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018079 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18080 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018081 EMSG(_(e_funcref));
18082 else
18083 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018084 name = NULL;
18085 }
18086 goto theend;
18087 }
18088
18089 if (lv.ll_name == NULL)
18090 {
18091 /* Error found, but continue after the function name. */
18092 *pp = end;
18093 goto theend;
18094 }
18095
18096 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018097 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018098 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018099 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18100 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18101 {
18102 /* When there was "s:" already or the name expanded to get a
18103 * leading "s:" then remove it. */
18104 lv.ll_name += 2;
18105 len -= 2;
18106 lead = 2;
18107 }
18108 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018109 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018110 {
18111 if (lead == 2) /* skip over "s:" */
18112 lv.ll_name += 2;
18113 len = (int)(end - lv.ll_name);
18114 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018115
18116 /*
18117 * Copy the function name to allocated memory.
18118 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18119 * Accept <SNR>123_name() outside a script.
18120 */
18121 if (skip)
18122 lead = 0; /* do nothing */
18123 else if (lead > 0)
18124 {
18125 lead = 3;
18126 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18127 {
18128 if (current_SID <= 0)
18129 {
18130 EMSG(_(e_usingsid));
18131 goto theend;
18132 }
18133 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18134 lead += (int)STRLEN(sid_buf);
18135 }
18136 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018137 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018138 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018139 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018140 goto theend;
18141 }
18142 name = alloc((unsigned)(len + lead + 1));
18143 if (name != NULL)
18144 {
18145 if (lead > 0)
18146 {
18147 name[0] = K_SPECIAL;
18148 name[1] = KS_EXTRA;
18149 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018150 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018151 STRCPY(name + 3, sid_buf);
18152 }
18153 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18154 name[len + lead] = NUL;
18155 }
18156 *pp = end;
18157
18158theend:
18159 clear_lval(&lv);
18160 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161}
18162
18163/*
18164 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18165 * Return 2 if "p" starts with "s:".
18166 * Return 0 otherwise.
18167 */
18168 static int
18169eval_fname_script(p)
18170 char_u *p;
18171{
18172 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18173 || STRNICMP(p + 1, "SNR>", 4) == 0))
18174 return 5;
18175 if (p[0] == 's' && p[1] == ':')
18176 return 2;
18177 return 0;
18178}
18179
18180/*
18181 * Return TRUE if "p" starts with "<SID>" or "s:".
18182 * Only works if eval_fname_script() returned non-zero for "p"!
18183 */
18184 static int
18185eval_fname_sid(p)
18186 char_u *p;
18187{
18188 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18189}
18190
18191/*
18192 * List the head of the function: "name(arg1, arg2)".
18193 */
18194 static void
18195list_func_head(fp, indent)
18196 ufunc_T *fp;
18197 int indent;
18198{
18199 int j;
18200
18201 msg_start();
18202 if (indent)
18203 MSG_PUTS(" ");
18204 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018205 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 {
18207 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018208 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 }
18210 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018211 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018213 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 {
18215 if (j)
18216 MSG_PUTS(", ");
18217 msg_puts(FUNCARG(fp, j));
18218 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018219 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 {
18221 if (j)
18222 MSG_PUTS(", ");
18223 MSG_PUTS("...");
18224 }
18225 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018226 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018227 if (p_verbose > 0)
18228 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229}
18230
18231/*
18232 * Find a function by name, return pointer to it in ufuncs.
18233 * Return NULL for unknown function.
18234 */
18235 static ufunc_T *
18236find_func(name)
18237 char_u *name;
18238{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018239 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018241 hi = hash_find(&func_hashtab, name);
18242 if (!HASHITEM_EMPTY(hi))
18243 return HI2UF(hi);
18244 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245}
18246
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018247#if defined(EXITFREE) || defined(PROTO)
18248 void
18249free_all_functions()
18250{
18251 hashitem_T *hi;
18252
18253 /* Need to start all over every time, because func_free() may change the
18254 * hash table. */
18255 while (func_hashtab.ht_used > 0)
18256 for (hi = func_hashtab.ht_array; ; ++hi)
18257 if (!HASHITEM_EMPTY(hi))
18258 {
18259 func_free(HI2UF(hi));
18260 break;
18261 }
18262}
18263#endif
18264
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018265/*
18266 * Return TRUE if a function "name" exists.
18267 */
18268 static int
18269function_exists(name)
18270 char_u *name;
18271{
18272 char_u *p = name;
18273 int n = FALSE;
18274
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018275 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018276 if (p != NULL)
18277 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018278 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018279 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018280 else
18281 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018282 vim_free(p);
18283 }
18284 return n;
18285}
18286
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018287/*
18288 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018289 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018290 */
18291 static int
18292builtin_function(name)
18293 char_u *name;
18294{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018295 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18296 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018297}
18298
Bram Moolenaar05159a02005-02-26 23:04:13 +000018299#if defined(FEAT_PROFILE) || defined(PROTO)
18300/*
18301 * Start profiling function "fp".
18302 */
18303 static void
18304func_do_profile(fp)
18305 ufunc_T *fp;
18306{
18307 fp->uf_tm_count = 0;
18308 profile_zero(&fp->uf_tm_self);
18309 profile_zero(&fp->uf_tm_total);
18310 if (fp->uf_tml_count == NULL)
18311 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18312 (sizeof(int) * fp->uf_lines.ga_len));
18313 if (fp->uf_tml_total == NULL)
18314 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18315 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18316 if (fp->uf_tml_self == NULL)
18317 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18318 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18319 fp->uf_tml_idx = -1;
18320 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18321 || fp->uf_tml_self == NULL)
18322 return; /* out of memory */
18323
18324 fp->uf_profiling = TRUE;
18325}
18326
18327/*
18328 * Dump the profiling results for all functions in file "fd".
18329 */
18330 void
18331func_dump_profile(fd)
18332 FILE *fd;
18333{
18334 hashitem_T *hi;
18335 int todo;
18336 ufunc_T *fp;
18337 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018338 ufunc_T **sorttab;
18339 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018340
18341 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018342 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18343
Bram Moolenaar05159a02005-02-26 23:04:13 +000018344 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18345 {
18346 if (!HASHITEM_EMPTY(hi))
18347 {
18348 --todo;
18349 fp = HI2UF(hi);
18350 if (fp->uf_profiling)
18351 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018352 if (sorttab != NULL)
18353 sorttab[st_len++] = fp;
18354
Bram Moolenaar05159a02005-02-26 23:04:13 +000018355 if (fp->uf_name[0] == K_SPECIAL)
18356 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18357 else
18358 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18359 if (fp->uf_tm_count == 1)
18360 fprintf(fd, "Called 1 time\n");
18361 else
18362 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18363 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18364 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18365 fprintf(fd, "\n");
18366 fprintf(fd, "count total (s) self (s)\n");
18367
18368 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18369 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018370 prof_func_line(fd, fp->uf_tml_count[i],
18371 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018372 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18373 }
18374 fprintf(fd, "\n");
18375 }
18376 }
18377 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018378
18379 if (sorttab != NULL && st_len > 0)
18380 {
18381 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18382 prof_total_cmp);
18383 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18384 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18385 prof_self_cmp);
18386 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18387 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018388}
Bram Moolenaar73830342005-02-28 22:48:19 +000018389
18390 static void
18391prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18392 FILE *fd;
18393 ufunc_T **sorttab;
18394 int st_len;
18395 char *title;
18396 int prefer_self; /* when equal print only self time */
18397{
18398 int i;
18399 ufunc_T *fp;
18400
18401 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18402 fprintf(fd, "count total (s) self (s) function\n");
18403 for (i = 0; i < 20 && i < st_len; ++i)
18404 {
18405 fp = sorttab[i];
18406 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18407 prefer_self);
18408 if (fp->uf_name[0] == K_SPECIAL)
18409 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18410 else
18411 fprintf(fd, " %s()\n", fp->uf_name);
18412 }
18413 fprintf(fd, "\n");
18414}
18415
18416/*
18417 * Print the count and times for one function or function line.
18418 */
18419 static void
18420prof_func_line(fd, count, total, self, prefer_self)
18421 FILE *fd;
18422 int count;
18423 proftime_T *total;
18424 proftime_T *self;
18425 int prefer_self; /* when equal print only self time */
18426{
18427 if (count > 0)
18428 {
18429 fprintf(fd, "%5d ", count);
18430 if (prefer_self && profile_equal(total, self))
18431 fprintf(fd, " ");
18432 else
18433 fprintf(fd, "%s ", profile_msg(total));
18434 if (!prefer_self && profile_equal(total, self))
18435 fprintf(fd, " ");
18436 else
18437 fprintf(fd, "%s ", profile_msg(self));
18438 }
18439 else
18440 fprintf(fd, " ");
18441}
18442
18443/*
18444 * Compare function for total time sorting.
18445 */
18446 static int
18447#ifdef __BORLANDC__
18448_RTLENTRYF
18449#endif
18450prof_total_cmp(s1, s2)
18451 const void *s1;
18452 const void *s2;
18453{
18454 ufunc_T *p1, *p2;
18455
18456 p1 = *(ufunc_T **)s1;
18457 p2 = *(ufunc_T **)s2;
18458 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18459}
18460
18461/*
18462 * Compare function for self time sorting.
18463 */
18464 static int
18465#ifdef __BORLANDC__
18466_RTLENTRYF
18467#endif
18468prof_self_cmp(s1, s2)
18469 const void *s1;
18470 const void *s2;
18471{
18472 ufunc_T *p1, *p2;
18473
18474 p1 = *(ufunc_T **)s1;
18475 p2 = *(ufunc_T **)s2;
18476 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18477}
18478
Bram Moolenaar05159a02005-02-26 23:04:13 +000018479#endif
18480
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018481/* The names of packages that once were loaded is remembered. */
18482static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18483
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018484/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018485 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018486 * Return TRUE if a package was loaded.
18487 */
18488 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018489script_autoload(name, reload)
18490 char_u *name;
18491 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018492{
18493 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018494 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018495 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018496 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018497
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018498 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018499 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018500 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018501 return FALSE;
18502
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018503 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018504
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018505 /* Find the name in the list of previously loaded package names. Skip
18506 * "autoload/", it's always the same. */
18507 for (i = 0; i < ga_loaded.ga_len; ++i)
18508 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18509 break;
18510 if (!reload && i < ga_loaded.ga_len)
18511 ret = FALSE; /* was loaded already */
18512 else
18513 {
18514 /* Remember the name if it wasn't loaded already. */
18515 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18516 {
18517 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18518 tofree = NULL;
18519 }
18520
18521 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018522 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018523 ret = TRUE;
18524 }
18525
18526 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018527 return ret;
18528}
18529
18530/*
18531 * Return the autoload script name for a function or variable name.
18532 * Returns NULL when out of memory.
18533 */
18534 static char_u *
18535autoload_name(name)
18536 char_u *name;
18537{
18538 char_u *p;
18539 char_u *scriptname;
18540
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018541 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018542 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18543 if (scriptname == NULL)
18544 return FALSE;
18545 STRCPY(scriptname, "autoload/");
18546 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018547 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018548 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018549 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018550 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018551 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018552}
18553
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18555
18556/*
18557 * Function given to ExpandGeneric() to obtain the list of user defined
18558 * function names.
18559 */
18560 char_u *
18561get_user_func_name(xp, idx)
18562 expand_T *xp;
18563 int idx;
18564{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018565 static long_u done;
18566 static hashitem_T *hi;
18567 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568
18569 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018571 done = 0;
18572 hi = func_hashtab.ht_array;
18573 }
18574 if (done < func_hashtab.ht_used)
18575 {
18576 if (done++ > 0)
18577 ++hi;
18578 while (HASHITEM_EMPTY(hi))
18579 ++hi;
18580 fp = HI2UF(hi);
18581
18582 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18583 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
18585 cat_func_name(IObuff, fp);
18586 if (xp->xp_context != EXPAND_USER_FUNC)
18587 {
18588 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018589 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 STRCAT(IObuff, ")");
18591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 return IObuff;
18593 }
18594 return NULL;
18595}
18596
18597#endif /* FEAT_CMDL_COMPL */
18598
18599/*
18600 * Copy the function name of "fp" to buffer "buf".
18601 * "buf" must be able to hold the function name plus three bytes.
18602 * Takes care of script-local function names.
18603 */
18604 static void
18605cat_func_name(buf, fp)
18606 char_u *buf;
18607 ufunc_T *fp;
18608{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018609 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 {
18611 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018612 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 }
18614 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018615 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616}
18617
18618/*
18619 * ":delfunction {name}"
18620 */
18621 void
18622ex_delfunction(eap)
18623 exarg_T *eap;
18624{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018625 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 char_u *p;
18627 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018628 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629
18630 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018631 name = trans_function_name(&p, eap->skip, 0, &fudi);
18632 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018634 {
18635 if (fudi.fd_dict != NULL && !eap->skip)
18636 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 if (!ends_excmd(*skipwhite(p)))
18640 {
18641 vim_free(name);
18642 EMSG(_(e_trailing));
18643 return;
18644 }
18645 eap->nextcmd = check_nextcmd(p);
18646 if (eap->nextcmd != NULL)
18647 *p = NUL;
18648
18649 if (!eap->skip)
18650 fp = find_func(name);
18651 vim_free(name);
18652
18653 if (!eap->skip)
18654 {
18655 if (fp == NULL)
18656 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018657 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 return;
18659 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018660 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 {
18662 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18663 return;
18664 }
18665
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018666 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018668 /* Delete the dict item that refers to the function, it will
18669 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018670 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018672 else
18673 func_free(fp);
18674 }
18675}
18676
18677/*
18678 * Free a function and remove it from the list of functions.
18679 */
18680 static void
18681func_free(fp)
18682 ufunc_T *fp;
18683{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018684 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018685
18686 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018687 ga_clear_strings(&(fp->uf_args));
18688 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018689#ifdef FEAT_PROFILE
18690 vim_free(fp->uf_tml_count);
18691 vim_free(fp->uf_tml_total);
18692 vim_free(fp->uf_tml_self);
18693#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018694
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018695 /* remove the function from the function hashtable */
18696 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18697 if (HASHITEM_EMPTY(hi))
18698 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018699 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018700 hash_remove(&func_hashtab, hi);
18701
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018702 vim_free(fp);
18703}
18704
18705/*
18706 * Unreference a Function: decrement the reference count and free it when it
18707 * becomes zero. Only for numbered functions.
18708 */
18709 static void
18710func_unref(name)
18711 char_u *name;
18712{
18713 ufunc_T *fp;
18714
18715 if (name != NULL && isdigit(*name))
18716 {
18717 fp = find_func(name);
18718 if (fp == NULL)
18719 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018720 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018721 {
18722 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018723 * when "uf_calls" becomes zero. */
18724 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018725 func_free(fp);
18726 }
18727 }
18728}
18729
18730/*
18731 * Count a reference to a Function.
18732 */
18733 static void
18734func_ref(name)
18735 char_u *name;
18736{
18737 ufunc_T *fp;
18738
18739 if (name != NULL && isdigit(*name))
18740 {
18741 fp = find_func(name);
18742 if (fp == NULL)
18743 EMSG2(_(e_intern2), "func_ref()");
18744 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018745 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 }
18747}
18748
18749/*
18750 * Call a user function.
18751 */
18752 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018753call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 ufunc_T *fp; /* pointer to function */
18755 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018756 typval_T *argvars; /* arguments */
18757 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 linenr_T firstline; /* first line of range */
18759 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761{
Bram Moolenaar33570922005-01-25 22:26:29 +000018762 char_u *save_sourcing_name;
18763 linenr_T save_sourcing_lnum;
18764 scid_T save_current_SID;
18765 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 int save_did_emsg;
18767 static int depth = 0;
18768 dictitem_T *v;
18769 int fixvar_idx = 0; /* index in fixvar[] */
18770 int i;
18771 int ai;
18772 char_u numbuf[NUMBUFLEN];
18773 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018774#ifdef FEAT_PROFILE
18775 proftime_T wait_start;
18776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777
18778 /* If depth of calling is getting too high, don't execute the function */
18779 if (depth >= p_mfd)
18780 {
18781 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018782 rettv->v_type = VAR_NUMBER;
18783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 return;
18785 }
18786 ++depth;
18787
18788 line_breakcheck(); /* check for CTRL-C hit */
18789
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018790 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793 fc.rettv = rettv;
18794 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 fc.linenr = 0;
18796 fc.returned = FALSE;
18797 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018799 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 fc.dbg_tick = debug_tick;
18801
Bram Moolenaar33570922005-01-25 22:26:29 +000018802 /*
18803 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18804 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18805 * each argument variable and saves a lot of time.
18806 */
18807 /*
18808 * Init l: variables.
18809 */
18810 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018811 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018812 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018813 /* Set l:self to "selfdict". */
18814 v = &fc.fixvar[fixvar_idx++].var;
18815 STRCPY(v->di_key, "self");
18816 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18817 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18818 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018819 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018820 v->di_tv.vval.v_dict = selfdict;
18821 ++selfdict->dv_refcount;
18822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018823
Bram Moolenaar33570922005-01-25 22:26:29 +000018824 /*
18825 * Init a: variables.
18826 * Set a:0 to "argcount".
18827 * Set a:000 to a list with room for the "..." arguments.
18828 */
18829 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18830 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018831 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018832 v = &fc.fixvar[fixvar_idx++].var;
18833 STRCPY(v->di_key, "000");
18834 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18835 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18836 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018837 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 v->di_tv.vval.v_list = &fc.l_varlist;
18839 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18840 fc.l_varlist.lv_refcount = 99999;
18841
18842 /*
18843 * Set a:firstline to "firstline" and a:lastline to "lastline".
18844 * Set a:name to named arguments.
18845 * Set a:N to the "..." arguments.
18846 */
18847 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18848 (varnumber_T)firstline);
18849 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18850 (varnumber_T)lastline);
18851 for (i = 0; i < argcount; ++i)
18852 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018853 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 if (ai < 0)
18855 /* named argument a:name */
18856 name = FUNCARG(fp, i);
18857 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018858 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018859 /* "..." argument a:1, a:2, etc. */
18860 sprintf((char *)numbuf, "%d", ai + 1);
18861 name = numbuf;
18862 }
18863 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18864 {
18865 v = &fc.fixvar[fixvar_idx++].var;
18866 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18867 }
18868 else
18869 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018870 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18871 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018872 if (v == NULL)
18873 break;
18874 v->di_flags = DI_FLAGS_RO;
18875 }
18876 STRCPY(v->di_key, name);
18877 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18878
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018879 /* Note: the values are copied directly to avoid alloc/free.
18880 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018882 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018883
18884 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18885 {
18886 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18887 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018888 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018889 }
18890 }
18891
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 /* Don't redraw while executing the function. */
18893 ++RedrawingDisabled;
18894 save_sourcing_name = sourcing_name;
18895 save_sourcing_lnum = sourcing_lnum;
18896 sourcing_lnum = 1;
18897 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018898 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 if (sourcing_name != NULL)
18900 {
18901 if (save_sourcing_name != NULL
18902 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18903 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18904 else
18905 STRCPY(sourcing_name, "function ");
18906 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18907
18908 if (p_verbose >= 12)
18909 {
18910 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018911 verbose_enter_scroll();
18912
Bram Moolenaar555b2802005-05-19 21:08:39 +000018913 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 if (p_verbose >= 14)
18915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018917 char_u numbuf[NUMBUFLEN];
18918 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919
18920 msg_puts((char_u *)"(");
18921 for (i = 0; i < argcount; ++i)
18922 {
18923 if (i > 0)
18924 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018925 if (argvars[i].v_type == VAR_NUMBER)
18926 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927 else
18928 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018929 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018930 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018932 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 }
18934 }
18935 msg_puts((char_u *)")");
18936 }
18937 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018938
18939 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 --no_wait_return;
18941 }
18942 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018943#ifdef FEAT_PROFILE
18944 if (do_profiling)
18945 {
18946 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18947 func_do_profile(fp);
18948 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018949 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018950 {
18951 ++fp->uf_tm_count;
18952 profile_start(&fp->uf_tm_start);
18953 profile_zero(&fp->uf_tm_children);
18954 }
18955 script_prof_save(&wait_start);
18956 }
18957#endif
18958
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018960 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 save_did_emsg = did_emsg;
18962 did_emsg = FALSE;
18963
18964 /* call do_cmdline() to execute the lines */
18965 do_cmdline(NULL, get_func_line, (void *)&fc,
18966 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18967
18968 --RedrawingDisabled;
18969
18970 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018971 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018973 clear_tv(rettv);
18974 rettv->v_type = VAR_NUMBER;
18975 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 }
18977
Bram Moolenaar05159a02005-02-26 23:04:13 +000018978#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018979 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018980 {
18981 profile_end(&fp->uf_tm_start);
18982 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18983 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18984 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18985 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018986 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018988 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18989 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018990 }
18991 }
18992#endif
18993
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 /* when being verbose, mention the return value */
18995 if (p_verbose >= 12)
18996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018998 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019001 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019002 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019003 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19004 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019007 char_u buf[MSG_BUF_LEN];
19008 char_u numbuf[NUMBUFLEN];
19009 char_u *tofree;
19010
Bram Moolenaar555b2802005-05-19 21:08:39 +000019011 /* The value may be very long. Skip the middle part, so that we
19012 * have some idea how it starts and ends. smsg() would always
19013 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019014 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019015 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019016 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019017 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 }
19019 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019020
19021 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 --no_wait_return;
19023 }
19024
19025 vim_free(sourcing_name);
19026 sourcing_name = save_sourcing_name;
19027 sourcing_lnum = save_sourcing_lnum;
19028 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019029#ifdef FEAT_PROFILE
19030 if (do_profiling)
19031 script_prof_restore(&wait_start);
19032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033
19034 if (p_verbose >= 12 && sourcing_name != NULL)
19035 {
19036 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019037 verbose_enter_scroll();
19038
Bram Moolenaar555b2802005-05-19 21:08:39 +000019039 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019041
19042 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043 --no_wait_return;
19044 }
19045
19046 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019047 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048
Bram Moolenaar33570922005-01-25 22:26:29 +000019049 /* The a: variables typevals were not alloced, only free the allocated
19050 * variables. */
19051 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19052
19053 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 --depth;
19055}
19056
19057/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019058 * Add a number variable "name" to dict "dp" with value "nr".
19059 */
19060 static void
19061add_nr_var(dp, v, name, nr)
19062 dict_T *dp;
19063 dictitem_T *v;
19064 char *name;
19065 varnumber_T nr;
19066{
19067 STRCPY(v->di_key, name);
19068 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19069 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19070 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019071 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 v->di_tv.vval.v_number = nr;
19073}
19074
19075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 * ":return [expr]"
19077 */
19078 void
19079ex_return(eap)
19080 exarg_T *eap;
19081{
19082 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019083 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 int returning = FALSE;
19085
19086 if (current_funccal == NULL)
19087 {
19088 EMSG(_("E133: :return not inside a function"));
19089 return;
19090 }
19091
19092 if (eap->skip)
19093 ++emsg_skip;
19094
19095 eap->nextcmd = NULL;
19096 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019097 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 {
19099 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 }
19104 /* It's safer to return also on error. */
19105 else if (!eap->skip)
19106 {
19107 /*
19108 * Return unless the expression evaluation has been cancelled due to an
19109 * aborting error, an interrupt, or an exception.
19110 */
19111 if (!aborting())
19112 returning = do_return(eap, FALSE, TRUE, NULL);
19113 }
19114
19115 /* When skipping or the return gets pending, advance to the next command
19116 * in this line (!returning). Otherwise, ignore the rest of the line.
19117 * Following lines will be ignored by get_func_line(). */
19118 if (returning)
19119 eap->nextcmd = NULL;
19120 else if (eap->nextcmd == NULL) /* no argument */
19121 eap->nextcmd = check_nextcmd(arg);
19122
19123 if (eap->skip)
19124 --emsg_skip;
19125}
19126
19127/*
19128 * Return from a function. Possibly makes the return pending. Also called
19129 * for a pending return at the ":endtry" or after returning from an extra
19130 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019131 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019132 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 * FALSE when the return gets pending.
19134 */
19135 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 exarg_T *eap;
19138 int reanimate;
19139 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141{
19142 int idx;
19143 struct condstack *cstack = eap->cstack;
19144
19145 if (reanimate)
19146 /* Undo the return. */
19147 current_funccal->returned = FALSE;
19148
19149 /*
19150 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19151 * not in its finally clause (which then is to be executed next) is found.
19152 * In this case, make the ":return" pending for execution at the ":endtry".
19153 * Otherwise, return normally.
19154 */
19155 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19156 if (idx >= 0)
19157 {
19158 cstack->cs_pending[idx] = CSTP_RETURN;
19159
19160 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161 /* A pending return again gets pending. "rettv" points to an
19162 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019164 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 else
19166 {
19167 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 {
19174 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019176 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 else
19178 EMSG(_(e_outofmem));
19179 }
19180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182
19183 if (reanimate)
19184 {
19185 /* The pending return value could be overwritten by a ":return"
19186 * without argument in a finally clause; reset the default
19187 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019188 current_funccal->rettv->v_type = VAR_NUMBER;
19189 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 }
19191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019192 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 }
19194 else
19195 {
19196 current_funccal->returned = TRUE;
19197
19198 /* If the return is carried out now, store the return value. For
19199 * a return immediately after reanimation, the value is already
19200 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019204 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019206 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 }
19208 }
19209
19210 return idx < 0;
19211}
19212
19213/*
19214 * Free the variable with a pending return value.
19215 */
19216 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217discard_pending_return(rettv)
19218 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219{
Bram Moolenaar33570922005-01-25 22:26:29 +000019220 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221}
19222
19223/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 * is an allocated string. Used by report_pending() for verbose messages.
19226 */
19227 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019228get_return_cmd(rettv)
19229 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019231 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019233 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019235 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019236 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019237 if (s == NULL)
19238 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019239
19240 STRCPY(IObuff, ":return ");
19241 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19242 if (STRLEN(s) + 8 >= IOSIZE)
19243 STRCPY(IObuff + IOSIZE - 4, "...");
19244 vim_free(tofree);
19245 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
19248/*
19249 * Get next function line.
19250 * Called by do_cmdline() to get the next line.
19251 * Returns allocated string, or NULL for end of function.
19252 */
19253/* ARGSUSED */
19254 char_u *
19255get_func_line(c, cookie, indent)
19256 int c; /* not used */
19257 void *cookie;
19258 int indent; /* not used */
19259{
Bram Moolenaar33570922005-01-25 22:26:29 +000019260 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019261 ufunc_T *fp = fcp->func;
19262 char_u *retval;
19263 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264
19265 /* If breakpoints have been added/deleted need to check for it. */
19266 if (fcp->dbg_tick != debug_tick)
19267 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019268 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 sourcing_lnum);
19270 fcp->dbg_tick = debug_tick;
19271 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019272#ifdef FEAT_PROFILE
19273 if (do_profiling)
19274 func_line_end(cookie);
19275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276
Bram Moolenaar05159a02005-02-26 23:04:13 +000019277 gap = &fp->uf_lines;
19278 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 retval = NULL;
19280 else if (fcp->returned || fcp->linenr >= gap->ga_len)
19281 retval = NULL;
19282 else
19283 {
19284 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19285 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019286#ifdef FEAT_PROFILE
19287 if (do_profiling)
19288 func_line_start(cookie);
19289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 }
19291
19292 /* Did we encounter a breakpoint? */
19293 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19294 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019295 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019297 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 sourcing_lnum);
19299 fcp->dbg_tick = debug_tick;
19300 }
19301
19302 return retval;
19303}
19304
Bram Moolenaar05159a02005-02-26 23:04:13 +000019305#if defined(FEAT_PROFILE) || defined(PROTO)
19306/*
19307 * Called when starting to read a function line.
19308 * "sourcing_lnum" must be correct!
19309 * When skipping lines it may not actually be executed, but we won't find out
19310 * until later and we need to store the time now.
19311 */
19312 void
19313func_line_start(cookie)
19314 void *cookie;
19315{
19316 funccall_T *fcp = (funccall_T *)cookie;
19317 ufunc_T *fp = fcp->func;
19318
19319 if (fp->uf_profiling && sourcing_lnum >= 1
19320 && sourcing_lnum <= fp->uf_lines.ga_len)
19321 {
19322 fp->uf_tml_idx = sourcing_lnum - 1;
19323 fp->uf_tml_execed = FALSE;
19324 profile_start(&fp->uf_tml_start);
19325 profile_zero(&fp->uf_tml_children);
19326 profile_get_wait(&fp->uf_tml_wait);
19327 }
19328}
19329
19330/*
19331 * Called when actually executing a function line.
19332 */
19333 void
19334func_line_exec(cookie)
19335 void *cookie;
19336{
19337 funccall_T *fcp = (funccall_T *)cookie;
19338 ufunc_T *fp = fcp->func;
19339
19340 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19341 fp->uf_tml_execed = TRUE;
19342}
19343
19344/*
19345 * Called when done with a function line.
19346 */
19347 void
19348func_line_end(cookie)
19349 void *cookie;
19350{
19351 funccall_T *fcp = (funccall_T *)cookie;
19352 ufunc_T *fp = fcp->func;
19353
19354 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19355 {
19356 if (fp->uf_tml_execed)
19357 {
19358 ++fp->uf_tml_count[fp->uf_tml_idx];
19359 profile_end(&fp->uf_tml_start);
19360 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19361 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19362 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19363 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19364 }
19365 fp->uf_tml_idx = -1;
19366 }
19367}
19368#endif
19369
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370/*
19371 * Return TRUE if the currently active function should be ended, because a
19372 * return was encountered or an error occured. Used inside a ":while".
19373 */
19374 int
19375func_has_ended(cookie)
19376 void *cookie;
19377{
Bram Moolenaar33570922005-01-25 22:26:29 +000019378 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379
19380 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19381 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019382 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 || fcp->returned);
19384}
19385
19386/*
19387 * return TRUE if cookie indicates a function which "abort"s on errors.
19388 */
19389 int
19390func_has_abort(cookie)
19391 void *cookie;
19392{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019393 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394}
19395
19396#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19397typedef enum
19398{
19399 VAR_FLAVOUR_DEFAULT,
19400 VAR_FLAVOUR_SESSION,
19401 VAR_FLAVOUR_VIMINFO
19402} var_flavour_T;
19403
19404static var_flavour_T var_flavour __ARGS((char_u *varname));
19405
19406 static var_flavour_T
19407var_flavour(varname)
19408 char_u *varname;
19409{
19410 char_u *p = varname;
19411
19412 if (ASCII_ISUPPER(*p))
19413 {
19414 while (*(++p))
19415 if (ASCII_ISLOWER(*p))
19416 return VAR_FLAVOUR_SESSION;
19417 return VAR_FLAVOUR_VIMINFO;
19418 }
19419 else
19420 return VAR_FLAVOUR_DEFAULT;
19421}
19422#endif
19423
19424#if defined(FEAT_VIMINFO) || defined(PROTO)
19425/*
19426 * Restore global vars that start with a capital from the viminfo file
19427 */
19428 int
19429read_viminfo_varlist(virp, writing)
19430 vir_T *virp;
19431 int writing;
19432{
19433 char_u *tab;
19434 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019435 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436
19437 if (!writing && (find_viminfo_parameter('!') != NULL))
19438 {
19439 tab = vim_strchr(virp->vir_line + 1, '\t');
19440 if (tab != NULL)
19441 {
19442 *tab++ = '\0'; /* isolate the variable name */
19443 if (*tab == 'S') /* string var */
19444 is_string = TRUE;
19445
19446 tab = vim_strchr(tab, '\t');
19447 if (tab != NULL)
19448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 if (is_string)
19450 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019451 tv.v_type = VAR_STRING;
19452 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 }
19455 else
19456 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019457 tv.v_type = VAR_NUMBER;
19458 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019460 set_var(virp->vir_line + 1, &tv, FALSE);
19461 if (is_string)
19462 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 }
19464 }
19465 }
19466
19467 return viminfo_readline(virp);
19468}
19469
19470/*
19471 * Write global vars that start with a capital to the viminfo file
19472 */
19473 void
19474write_viminfo_varlist(fp)
19475 FILE *fp;
19476{
Bram Moolenaar33570922005-01-25 22:26:29 +000019477 hashitem_T *hi;
19478 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019479 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019481 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019482 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019483 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484
19485 if (find_viminfo_parameter('!') == NULL)
19486 return;
19487
19488 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019489
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 todo = globvarht.ht_used;
19491 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019493 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019495 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019496 this_var = HI2DI(hi);
19497 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019499 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019500 {
19501 case VAR_STRING: s = "STR"; break;
19502 case VAR_NUMBER: s = "NUM"; break;
19503 default: continue;
19504 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019505 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019506 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019507 if (p != NULL)
19508 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019509 vim_free(tofree);
19510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 }
19512 }
19513}
19514#endif
19515
19516#if defined(FEAT_SESSION) || defined(PROTO)
19517 int
19518store_session_globals(fd)
19519 FILE *fd;
19520{
Bram Moolenaar33570922005-01-25 22:26:29 +000019521 hashitem_T *hi;
19522 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019523 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 char_u *p, *t;
19525
Bram Moolenaar33570922005-01-25 22:26:29 +000019526 todo = globvarht.ht_used;
19527 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019529 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019531 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019532 this_var = HI2DI(hi);
19533 if ((this_var->di_tv.v_type == VAR_NUMBER
19534 || this_var->di_tv.v_type == VAR_STRING)
19535 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019536 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019537 /* Escape special characters with a backslash. Turn a LF and
19538 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019539 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019540 (char_u *)"\\\"\n\r");
19541 if (p == NULL) /* out of memory */
19542 break;
19543 for (t = p; *t != NUL; ++t)
19544 if (*t == '\n')
19545 *t = 'n';
19546 else if (*t == '\r')
19547 *t = 'r';
19548 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019549 this_var->di_key,
19550 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19551 : ' ',
19552 p,
19553 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19554 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019555 || put_eol(fd) == FAIL)
19556 {
19557 vim_free(p);
19558 return FAIL;
19559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 vim_free(p);
19561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 }
19563 }
19564 return OK;
19565}
19566#endif
19567
Bram Moolenaar661b1822005-07-28 22:36:45 +000019568/*
19569 * Display script name where an item was last set.
19570 * Should only be invoked when 'verbose' is non-zero.
19571 */
19572 void
19573last_set_msg(scriptID)
19574 scid_T scriptID;
19575{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019576 char_u *p;
19577
Bram Moolenaar661b1822005-07-28 22:36:45 +000019578 if (scriptID != 0)
19579 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019580 p = home_replace_save(NULL, get_scriptname(scriptID));
19581 if (p != NULL)
19582 {
19583 verbose_enter();
19584 MSG_PUTS(_("\n\tLast set from "));
19585 MSG_PUTS(p);
19586 vim_free(p);
19587 verbose_leave();
19588 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019589 }
19590}
19591
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592#endif /* FEAT_EVAL */
19593
19594#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19595
19596
19597#ifdef WIN3264
19598/*
19599 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19600 */
19601static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19602static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19603static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19604
19605/*
19606 * Get the short pathname of a file.
19607 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19608 */
19609 static int
19610get_short_pathname(fnamep, bufp, fnamelen)
19611 char_u **fnamep;
19612 char_u **bufp;
19613 int *fnamelen;
19614{
19615 int l,len;
19616 char_u *newbuf;
19617
19618 len = *fnamelen;
19619
19620 l = GetShortPathName(*fnamep, *fnamep, len);
19621 if (l > len - 1)
19622 {
19623 /* If that doesn't work (not enough space), then save the string
19624 * and try again with a new buffer big enough
19625 */
19626 newbuf = vim_strnsave(*fnamep, l);
19627 if (newbuf == NULL)
19628 return 0;
19629
19630 vim_free(*bufp);
19631 *fnamep = *bufp = newbuf;
19632
19633 l = GetShortPathName(*fnamep,*fnamep,l+1);
19634
19635 /* Really should always succeed, as the buffer is big enough */
19636 }
19637
19638 *fnamelen = l;
19639 return 1;
19640}
19641
19642/*
19643 * Create a short path name. Returns the length of the buffer it needs.
19644 * Doesn't copy over the end of the buffer passed in.
19645 */
19646 static int
19647shortpath_for_invalid_fname(fname, bufp, fnamelen)
19648 char_u **fname;
19649 char_u **bufp;
19650 int *fnamelen;
19651{
19652 char_u *s, *p, *pbuf2, *pbuf3;
19653 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019654 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655
19656 /* Make a copy */
19657 len2 = *fnamelen;
19658 pbuf2 = vim_strnsave(*fname, len2);
19659 pbuf3 = NULL;
19660
19661 s = pbuf2 + len2 - 1; /* Find the end */
19662 slen = 1;
19663 plen = len2;
19664
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019665 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 {
19667 --s;
19668 ++slen;
19669 --plen;
19670 }
19671
19672 do
19673 {
19674 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019675 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 {
19677 --s;
19678 ++slen;
19679 --plen;
19680 }
19681 if (s <= pbuf2)
19682 break;
19683
19684 /* Remeber the character that is about to be blatted */
19685 ch = *s;
19686 *s = 0; /* get_short_pathname requires a null-terminated string */
19687
19688 /* Try it in situ */
19689 p = pbuf2;
19690 if (!get_short_pathname(&p, &pbuf3, &plen))
19691 {
19692 vim_free(pbuf2);
19693 return -1;
19694 }
19695 *s = ch; /* Preserve the string */
19696 } while (plen == 0);
19697
19698 if (plen > 0)
19699 {
19700 /* Remeber the length of the new string. */
19701 *fnamelen = len = plen + slen;
19702 vim_free(*bufp);
19703 if (len > len2)
19704 {
19705 /* If there's not enough space in the currently allocated string,
19706 * then copy it to a buffer big enough.
19707 */
19708 *fname= *bufp = vim_strnsave(p, len);
19709 if (*fname == NULL)
19710 return -1;
19711 }
19712 else
19713 {
19714 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19715 *fname = *bufp = pbuf2;
19716 if (p != pbuf2)
19717 strncpy(*fname, p, plen);
19718 pbuf2 = NULL;
19719 }
19720 /* Concat the next bit */
19721 strncpy(*fname + plen, s, slen);
19722 (*fname)[len] = '\0';
19723 }
19724 vim_free(pbuf3);
19725 vim_free(pbuf2);
19726 return 0;
19727}
19728
19729/*
19730 * Get a pathname for a partial path.
19731 */
19732 static int
19733shortpath_for_partial(fnamep, bufp, fnamelen)
19734 char_u **fnamep;
19735 char_u **bufp;
19736 int *fnamelen;
19737{
19738 int sepcount, len, tflen;
19739 char_u *p;
19740 char_u *pbuf, *tfname;
19741 int hasTilde;
19742
19743 /* Count up the path seperators from the RHS.. so we know which part
19744 * of the path to return.
19745 */
19746 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019747 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 if (vim_ispathsep(*p))
19749 ++sepcount;
19750
19751 /* Need full path first (use expand_env() to remove a "~/") */
19752 hasTilde = (**fnamep == '~');
19753 if (hasTilde)
19754 pbuf = tfname = expand_env_save(*fnamep);
19755 else
19756 pbuf = tfname = FullName_save(*fnamep, FALSE);
19757
19758 len = tflen = STRLEN(tfname);
19759
19760 if (!get_short_pathname(&tfname, &pbuf, &len))
19761 return -1;
19762
19763 if (len == 0)
19764 {
19765 /* Don't have a valid filename, so shorten the rest of the
19766 * path if we can. This CAN give us invalid 8.3 filenames, but
19767 * there's not a lot of point in guessing what it might be.
19768 */
19769 len = tflen;
19770 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19771 return -1;
19772 }
19773
19774 /* Count the paths backward to find the beginning of the desired string. */
19775 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019776 {
19777#ifdef FEAT_MBYTE
19778 if (has_mbyte)
19779 p -= mb_head_off(tfname, p);
19780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 if (vim_ispathsep(*p))
19782 {
19783 if (sepcount == 0 || (hasTilde && sepcount == 1))
19784 break;
19785 else
19786 sepcount --;
19787 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 if (hasTilde)
19790 {
19791 --p;
19792 if (p >= tfname)
19793 *p = '~';
19794 else
19795 return -1;
19796 }
19797 else
19798 ++p;
19799
19800 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19801 vim_free(*bufp);
19802 *fnamelen = (int)STRLEN(p);
19803 *bufp = pbuf;
19804 *fnamep = p;
19805
19806 return 0;
19807}
19808#endif /* WIN3264 */
19809
19810/*
19811 * Adjust a filename, according to a string of modifiers.
19812 * *fnamep must be NUL terminated when called. When returning, the length is
19813 * determined by *fnamelen.
19814 * Returns valid flags.
19815 * When there is an error, *fnamep is set to NULL.
19816 */
19817 int
19818modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19819 char_u *src; /* string with modifiers */
19820 int *usedlen; /* characters after src that are used */
19821 char_u **fnamep; /* file name so far */
19822 char_u **bufp; /* buffer for allocated file name or NULL */
19823 int *fnamelen; /* length of fnamep */
19824{
19825 int valid = 0;
19826 char_u *tail;
19827 char_u *s, *p, *pbuf;
19828 char_u dirname[MAXPATHL];
19829 int c;
19830 int has_fullname = 0;
19831#ifdef WIN3264
19832 int has_shortname = 0;
19833#endif
19834
19835repeat:
19836 /* ":p" - full path/file_name */
19837 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19838 {
19839 has_fullname = 1;
19840
19841 valid |= VALID_PATH;
19842 *usedlen += 2;
19843
19844 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19845 if ((*fnamep)[0] == '~'
19846#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19847 && ((*fnamep)[1] == '/'
19848# ifdef BACKSLASH_IN_FILENAME
19849 || (*fnamep)[1] == '\\'
19850# endif
19851 || (*fnamep)[1] == NUL)
19852
19853#endif
19854 )
19855 {
19856 *fnamep = expand_env_save(*fnamep);
19857 vim_free(*bufp); /* free any allocated file name */
19858 *bufp = *fnamep;
19859 if (*fnamep == NULL)
19860 return -1;
19861 }
19862
19863 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019864 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 {
19866 if (vim_ispathsep(*p)
19867 && p[1] == '.'
19868 && (p[2] == NUL
19869 || vim_ispathsep(p[2])
19870 || (p[2] == '.'
19871 && (p[3] == NUL || vim_ispathsep(p[3])))))
19872 break;
19873 }
19874
19875 /* FullName_save() is slow, don't use it when not needed. */
19876 if (*p != NUL || !vim_isAbsName(*fnamep))
19877 {
19878 *fnamep = FullName_save(*fnamep, *p != NUL);
19879 vim_free(*bufp); /* free any allocated file name */
19880 *bufp = *fnamep;
19881 if (*fnamep == NULL)
19882 return -1;
19883 }
19884
19885 /* Append a path separator to a directory. */
19886 if (mch_isdir(*fnamep))
19887 {
19888 /* Make room for one or two extra characters. */
19889 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19890 vim_free(*bufp); /* free any allocated file name */
19891 *bufp = *fnamep;
19892 if (*fnamep == NULL)
19893 return -1;
19894 add_pathsep(*fnamep);
19895 }
19896 }
19897
19898 /* ":." - path relative to the current directory */
19899 /* ":~" - path relative to the home directory */
19900 /* ":8" - shortname path - postponed till after */
19901 while (src[*usedlen] == ':'
19902 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19903 {
19904 *usedlen += 2;
19905 if (c == '8')
19906 {
19907#ifdef WIN3264
19908 has_shortname = 1; /* Postpone this. */
19909#endif
19910 continue;
19911 }
19912 pbuf = NULL;
19913 /* Need full path first (use expand_env() to remove a "~/") */
19914 if (!has_fullname)
19915 {
19916 if (c == '.' && **fnamep == '~')
19917 p = pbuf = expand_env_save(*fnamep);
19918 else
19919 p = pbuf = FullName_save(*fnamep, FALSE);
19920 }
19921 else
19922 p = *fnamep;
19923
19924 has_fullname = 0;
19925
19926 if (p != NULL)
19927 {
19928 if (c == '.')
19929 {
19930 mch_dirname(dirname, MAXPATHL);
19931 s = shorten_fname(p, dirname);
19932 if (s != NULL)
19933 {
19934 *fnamep = s;
19935 if (pbuf != NULL)
19936 {
19937 vim_free(*bufp); /* free any allocated file name */
19938 *bufp = pbuf;
19939 pbuf = NULL;
19940 }
19941 }
19942 }
19943 else
19944 {
19945 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19946 /* Only replace it when it starts with '~' */
19947 if (*dirname == '~')
19948 {
19949 s = vim_strsave(dirname);
19950 if (s != NULL)
19951 {
19952 *fnamep = s;
19953 vim_free(*bufp);
19954 *bufp = s;
19955 }
19956 }
19957 }
19958 vim_free(pbuf);
19959 }
19960 }
19961
19962 tail = gettail(*fnamep);
19963 *fnamelen = (int)STRLEN(*fnamep);
19964
19965 /* ":h" - head, remove "/file_name", can be repeated */
19966 /* Don't remove the first "/" or "c:\" */
19967 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19968 {
19969 valid |= VALID_HEAD;
19970 *usedlen += 2;
19971 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019972 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 --tail;
19974 *fnamelen = (int)(tail - *fnamep);
19975#ifdef VMS
19976 if (*fnamelen > 0)
19977 *fnamelen += 1; /* the path separator is part of the path */
19978#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019979 while (tail > s && !after_pathsep(s, tail))
19980 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 }
19982
19983 /* ":8" - shortname */
19984 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19985 {
19986 *usedlen += 2;
19987#ifdef WIN3264
19988 has_shortname = 1;
19989#endif
19990 }
19991
19992#ifdef WIN3264
19993 /* Check shortname after we have done 'heads' and before we do 'tails'
19994 */
19995 if (has_shortname)
19996 {
19997 pbuf = NULL;
19998 /* Copy the string if it is shortened by :h */
19999 if (*fnamelen < (int)STRLEN(*fnamep))
20000 {
20001 p = vim_strnsave(*fnamep, *fnamelen);
20002 if (p == 0)
20003 return -1;
20004 vim_free(*bufp);
20005 *bufp = *fnamep = p;
20006 }
20007
20008 /* Split into two implementations - makes it easier. First is where
20009 * there isn't a full name already, second is where there is.
20010 */
20011 if (!has_fullname && !vim_isAbsName(*fnamep))
20012 {
20013 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20014 return -1;
20015 }
20016 else
20017 {
20018 int l;
20019
20020 /* Simple case, already have the full-name
20021 * Nearly always shorter, so try first time. */
20022 l = *fnamelen;
20023 if (!get_short_pathname(fnamep, bufp, &l))
20024 return -1;
20025
20026 if (l == 0)
20027 {
20028 /* Couldn't find the filename.. search the paths.
20029 */
20030 l = *fnamelen;
20031 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20032 return -1;
20033 }
20034 *fnamelen = l;
20035 }
20036 }
20037#endif /* WIN3264 */
20038
20039 /* ":t" - tail, just the basename */
20040 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20041 {
20042 *usedlen += 2;
20043 *fnamelen -= (int)(tail - *fnamep);
20044 *fnamep = tail;
20045 }
20046
20047 /* ":e" - extension, can be repeated */
20048 /* ":r" - root, without extension, can be repeated */
20049 while (src[*usedlen] == ':'
20050 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20051 {
20052 /* find a '.' in the tail:
20053 * - for second :e: before the current fname
20054 * - otherwise: The last '.'
20055 */
20056 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20057 s = *fnamep - 2;
20058 else
20059 s = *fnamep + *fnamelen - 1;
20060 for ( ; s > tail; --s)
20061 if (s[0] == '.')
20062 break;
20063 if (src[*usedlen + 1] == 'e') /* :e */
20064 {
20065 if (s > tail)
20066 {
20067 *fnamelen += (int)(*fnamep - (s + 1));
20068 *fnamep = s + 1;
20069#ifdef VMS
20070 /* cut version from the extension */
20071 s = *fnamep + *fnamelen - 1;
20072 for ( ; s > *fnamep; --s)
20073 if (s[0] == ';')
20074 break;
20075 if (s > *fnamep)
20076 *fnamelen = s - *fnamep;
20077#endif
20078 }
20079 else if (*fnamep <= tail)
20080 *fnamelen = 0;
20081 }
20082 else /* :r */
20083 {
20084 if (s > tail) /* remove one extension */
20085 *fnamelen = (int)(s - *fnamep);
20086 }
20087 *usedlen += 2;
20088 }
20089
20090 /* ":s?pat?foo?" - substitute */
20091 /* ":gs?pat?foo?" - global substitute */
20092 if (src[*usedlen] == ':'
20093 && (src[*usedlen + 1] == 's'
20094 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20095 {
20096 char_u *str;
20097 char_u *pat;
20098 char_u *sub;
20099 int sep;
20100 char_u *flags;
20101 int didit = FALSE;
20102
20103 flags = (char_u *)"";
20104 s = src + *usedlen + 2;
20105 if (src[*usedlen + 1] == 'g')
20106 {
20107 flags = (char_u *)"g";
20108 ++s;
20109 }
20110
20111 sep = *s++;
20112 if (sep)
20113 {
20114 /* find end of pattern */
20115 p = vim_strchr(s, sep);
20116 if (p != NULL)
20117 {
20118 pat = vim_strnsave(s, (int)(p - s));
20119 if (pat != NULL)
20120 {
20121 s = p + 1;
20122 /* find end of substitution */
20123 p = vim_strchr(s, sep);
20124 if (p != NULL)
20125 {
20126 sub = vim_strnsave(s, (int)(p - s));
20127 str = vim_strnsave(*fnamep, *fnamelen);
20128 if (sub != NULL && str != NULL)
20129 {
20130 *usedlen = (int)(p + 1 - src);
20131 s = do_string_sub(str, pat, sub, flags);
20132 if (s != NULL)
20133 {
20134 *fnamep = s;
20135 *fnamelen = (int)STRLEN(s);
20136 vim_free(*bufp);
20137 *bufp = s;
20138 didit = TRUE;
20139 }
20140 }
20141 vim_free(sub);
20142 vim_free(str);
20143 }
20144 vim_free(pat);
20145 }
20146 }
20147 /* after using ":s", repeat all the modifiers */
20148 if (didit)
20149 goto repeat;
20150 }
20151 }
20152
20153 return valid;
20154}
20155
20156/*
20157 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20158 * "flags" can be "g" to do a global substitute.
20159 * Returns an allocated string, NULL for error.
20160 */
20161 char_u *
20162do_string_sub(str, pat, sub, flags)
20163 char_u *str;
20164 char_u *pat;
20165 char_u *sub;
20166 char_u *flags;
20167{
20168 int sublen;
20169 regmatch_T regmatch;
20170 int i;
20171 int do_all;
20172 char_u *tail;
20173 garray_T ga;
20174 char_u *ret;
20175 char_u *save_cpo;
20176
20177 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20178 save_cpo = p_cpo;
20179 p_cpo = (char_u *)"";
20180
20181 ga_init2(&ga, 1, 200);
20182
20183 do_all = (flags[0] == 'g');
20184
20185 regmatch.rm_ic = p_ic;
20186 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20187 if (regmatch.regprog != NULL)
20188 {
20189 tail = str;
20190 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20191 {
20192 /*
20193 * Get some space for a temporary buffer to do the substitution
20194 * into. It will contain:
20195 * - The text up to where the match is.
20196 * - The substituted text.
20197 * - The text after the match.
20198 */
20199 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20200 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20201 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20202 {
20203 ga_clear(&ga);
20204 break;
20205 }
20206
20207 /* copy the text up to where the match is */
20208 i = (int)(regmatch.startp[0] - tail);
20209 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20210 /* add the substituted text */
20211 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20212 + ga.ga_len + i, TRUE, TRUE, FALSE);
20213 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 /* avoid getting stuck on a match with an empty string */
20215 if (tail == regmatch.endp[0])
20216 {
20217 if (*tail == NUL)
20218 break;
20219 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20220 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 }
20222 else
20223 {
20224 tail = regmatch.endp[0];
20225 if (*tail == NUL)
20226 break;
20227 }
20228 if (!do_all)
20229 break;
20230 }
20231
20232 if (ga.ga_data != NULL)
20233 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20234
20235 vim_free(regmatch.regprog);
20236 }
20237
20238 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20239 ga_clear(&ga);
20240 p_cpo = save_cpo;
20241
20242 return ret;
20243}
20244
20245#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */