blob: 54b5a53bb9b740e91cdfc040cf25063c54652d01 [file] [log] [blame]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001/* 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 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
27#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
51static void f_assert_match(typval_T *argvars, typval_T *rettv);
52static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
53static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
54static void f_assert_true(typval_T *argvars, typval_T *rettv);
55#ifdef FEAT_FLOAT
56static void f_asin(typval_T *argvars, typval_T *rettv);
57static void f_atan(typval_T *argvars, typval_T *rettv);
58static void f_atan2(typval_T *argvars, typval_T *rettv);
59#endif
60static void f_browse(typval_T *argvars, typval_T *rettv);
61static void f_browsedir(typval_T *argvars, typval_T *rettv);
62static void f_bufexists(typval_T *argvars, typval_T *rettv);
63static void f_buflisted(typval_T *argvars, typval_T *rettv);
64static void f_bufloaded(typval_T *argvars, typval_T *rettv);
65static void f_bufname(typval_T *argvars, typval_T *rettv);
66static void f_bufnr(typval_T *argvars, typval_T *rettv);
67static void f_bufwinid(typval_T *argvars, typval_T *rettv);
68static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
69static void f_byte2line(typval_T *argvars, typval_T *rettv);
70static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
71static void f_byteidx(typval_T *argvars, typval_T *rettv);
72static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
73static void f_call(typval_T *argvars, typval_T *rettv);
74#ifdef FEAT_FLOAT
75static void f_ceil(typval_T *argvars, typval_T *rettv);
76#endif
77#ifdef FEAT_JOB_CHANNEL
78static void f_ch_close(typval_T *argvars, typval_T *rettv);
79static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
80static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
81static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
82static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
83static void f_ch_info(typval_T *argvars, typval_T *rettv);
84static void f_ch_log(typval_T *argvars, typval_T *rettv);
85static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
86static void f_ch_open(typval_T *argvars, typval_T *rettv);
87static void f_ch_read(typval_T *argvars, typval_T *rettv);
88static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
89static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
90static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
91static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
92static void f_ch_status(typval_T *argvars, typval_T *rettv);
93#endif
94static void f_changenr(typval_T *argvars, typval_T *rettv);
95static void f_char2nr(typval_T *argvars, typval_T *rettv);
96static void f_cindent(typval_T *argvars, typval_T *rettv);
97static void f_clearmatches(typval_T *argvars, typval_T *rettv);
98static void f_col(typval_T *argvars, typval_T *rettv);
99#if defined(FEAT_INS_EXPAND)
100static void f_complete(typval_T *argvars, typval_T *rettv);
101static void f_complete_add(typval_T *argvars, typval_T *rettv);
102static void f_complete_check(typval_T *argvars, typval_T *rettv);
103#endif
104static void f_confirm(typval_T *argvars, typval_T *rettv);
105static void f_copy(typval_T *argvars, typval_T *rettv);
106#ifdef FEAT_FLOAT
107static void f_cos(typval_T *argvars, typval_T *rettv);
108static void f_cosh(typval_T *argvars, typval_T *rettv);
109#endif
110static void f_count(typval_T *argvars, typval_T *rettv);
111static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
112static void f_cursor(typval_T *argsvars, typval_T *rettv);
113static void f_deepcopy(typval_T *argvars, typval_T *rettv);
114static void f_delete(typval_T *argvars, typval_T *rettv);
115static void f_did_filetype(typval_T *argvars, typval_T *rettv);
116static void f_diff_filler(typval_T *argvars, typval_T *rettv);
117static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
118static void f_empty(typval_T *argvars, typval_T *rettv);
119static void f_escape(typval_T *argvars, typval_T *rettv);
120static void f_eval(typval_T *argvars, typval_T *rettv);
121static void f_eventhandler(typval_T *argvars, typval_T *rettv);
122static void f_executable(typval_T *argvars, typval_T *rettv);
123static void f_execute(typval_T *argvars, typval_T *rettv);
124static void f_exepath(typval_T *argvars, typval_T *rettv);
125static void f_exists(typval_T *argvars, typval_T *rettv);
126#ifdef FEAT_FLOAT
127static void f_exp(typval_T *argvars, typval_T *rettv);
128#endif
129static void f_expand(typval_T *argvars, typval_T *rettv);
130static void f_extend(typval_T *argvars, typval_T *rettv);
131static void f_feedkeys(typval_T *argvars, typval_T *rettv);
132static void f_filereadable(typval_T *argvars, typval_T *rettv);
133static void f_filewritable(typval_T *argvars, typval_T *rettv);
134static void f_filter(typval_T *argvars, typval_T *rettv);
135static void f_finddir(typval_T *argvars, typval_T *rettv);
136static void f_findfile(typval_T *argvars, typval_T *rettv);
137#ifdef FEAT_FLOAT
138static void f_float2nr(typval_T *argvars, typval_T *rettv);
139static void f_floor(typval_T *argvars, typval_T *rettv);
140static void f_fmod(typval_T *argvars, typval_T *rettv);
141#endif
142static void f_fnameescape(typval_T *argvars, typval_T *rettv);
143static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
144static void f_foldclosed(typval_T *argvars, typval_T *rettv);
145static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
146static void f_foldlevel(typval_T *argvars, typval_T *rettv);
147static void f_foldtext(typval_T *argvars, typval_T *rettv);
148static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
149static void f_foreground(typval_T *argvars, typval_T *rettv);
150static void f_function(typval_T *argvars, typval_T *rettv);
151static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
152static void f_get(typval_T *argvars, typval_T *rettv);
153static void f_getbufline(typval_T *argvars, typval_T *rettv);
154static void f_getbufvar(typval_T *argvars, typval_T *rettv);
155static void f_getchar(typval_T *argvars, typval_T *rettv);
156static void f_getcharmod(typval_T *argvars, typval_T *rettv);
157static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
158static void f_getcmdline(typval_T *argvars, typval_T *rettv);
159#if defined(FEAT_CMDL_COMPL)
160static void f_getcompletion(typval_T *argvars, typval_T *rettv);
161#endif
162static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
163static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
164static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
165static void f_getcwd(typval_T *argvars, typval_T *rettv);
166static void f_getfontname(typval_T *argvars, typval_T *rettv);
167static void f_getfperm(typval_T *argvars, typval_T *rettv);
168static void f_getfsize(typval_T *argvars, typval_T *rettv);
169static void f_getftime(typval_T *argvars, typval_T *rettv);
170static void f_getftype(typval_T *argvars, typval_T *rettv);
171static void f_getline(typval_T *argvars, typval_T *rettv);
172static void f_getmatches(typval_T *argvars, typval_T *rettv);
173static void f_getpid(typval_T *argvars, typval_T *rettv);
174static void f_getcurpos(typval_T *argvars, typval_T *rettv);
175static void f_getpos(typval_T *argvars, typval_T *rettv);
176static void f_getqflist(typval_T *argvars, typval_T *rettv);
177static void f_getreg(typval_T *argvars, typval_T *rettv);
178static void f_getregtype(typval_T *argvars, typval_T *rettv);
179static void f_gettabvar(typval_T *argvars, typval_T *rettv);
180static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
181static void f_getwinposx(typval_T *argvars, typval_T *rettv);
182static void f_getwinposy(typval_T *argvars, typval_T *rettv);
183static void f_getwinvar(typval_T *argvars, typval_T *rettv);
184static void f_glob(typval_T *argvars, typval_T *rettv);
185static void f_globpath(typval_T *argvars, typval_T *rettv);
186static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
187static void f_has(typval_T *argvars, typval_T *rettv);
188static void f_has_key(typval_T *argvars, typval_T *rettv);
189static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
190static void f_hasmapto(typval_T *argvars, typval_T *rettv);
191static void f_histadd(typval_T *argvars, typval_T *rettv);
192static void f_histdel(typval_T *argvars, typval_T *rettv);
193static void f_histget(typval_T *argvars, typval_T *rettv);
194static void f_histnr(typval_T *argvars, typval_T *rettv);
195static void f_hlID(typval_T *argvars, typval_T *rettv);
196static void f_hlexists(typval_T *argvars, typval_T *rettv);
197static void f_hostname(typval_T *argvars, typval_T *rettv);
198static void f_iconv(typval_T *argvars, typval_T *rettv);
199static void f_indent(typval_T *argvars, typval_T *rettv);
200static void f_index(typval_T *argvars, typval_T *rettv);
201static void f_input(typval_T *argvars, typval_T *rettv);
202static void f_inputdialog(typval_T *argvars, typval_T *rettv);
203static void f_inputlist(typval_T *argvars, typval_T *rettv);
204static void f_inputrestore(typval_T *argvars, typval_T *rettv);
205static void f_inputsave(typval_T *argvars, typval_T *rettv);
206static void f_inputsecret(typval_T *argvars, typval_T *rettv);
207static void f_insert(typval_T *argvars, typval_T *rettv);
208static void f_invert(typval_T *argvars, typval_T *rettv);
209static void f_isdirectory(typval_T *argvars, typval_T *rettv);
210static void f_islocked(typval_T *argvars, typval_T *rettv);
211#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
212static void f_isnan(typval_T *argvars, typval_T *rettv);
213#endif
214static void f_items(typval_T *argvars, typval_T *rettv);
215#ifdef FEAT_JOB_CHANNEL
216static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
217static void f_job_info(typval_T *argvars, typval_T *rettv);
218static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
219static void f_job_start(typval_T *argvars, typval_T *rettv);
220static void f_job_stop(typval_T *argvars, typval_T *rettv);
221static void f_job_status(typval_T *argvars, typval_T *rettv);
222#endif
223static void f_join(typval_T *argvars, typval_T *rettv);
224static void f_js_decode(typval_T *argvars, typval_T *rettv);
225static void f_js_encode(typval_T *argvars, typval_T *rettv);
226static void f_json_decode(typval_T *argvars, typval_T *rettv);
227static void f_json_encode(typval_T *argvars, typval_T *rettv);
228static void f_keys(typval_T *argvars, typval_T *rettv);
229static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
230static void f_len(typval_T *argvars, typval_T *rettv);
231static void f_libcall(typval_T *argvars, typval_T *rettv);
232static void f_libcallnr(typval_T *argvars, typval_T *rettv);
233static void f_line(typval_T *argvars, typval_T *rettv);
234static void f_line2byte(typval_T *argvars, typval_T *rettv);
235static void f_lispindent(typval_T *argvars, typval_T *rettv);
236static void f_localtime(typval_T *argvars, typval_T *rettv);
237#ifdef FEAT_FLOAT
238static void f_log(typval_T *argvars, typval_T *rettv);
239static void f_log10(typval_T *argvars, typval_T *rettv);
240#endif
241#ifdef FEAT_LUA
242static void f_luaeval(typval_T *argvars, typval_T *rettv);
243#endif
244static void f_map(typval_T *argvars, typval_T *rettv);
245static void f_maparg(typval_T *argvars, typval_T *rettv);
246static void f_mapcheck(typval_T *argvars, typval_T *rettv);
247static void f_match(typval_T *argvars, typval_T *rettv);
248static void f_matchadd(typval_T *argvars, typval_T *rettv);
249static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
250static void f_matcharg(typval_T *argvars, typval_T *rettv);
251static void f_matchdelete(typval_T *argvars, typval_T *rettv);
252static void f_matchend(typval_T *argvars, typval_T *rettv);
253static void f_matchlist(typval_T *argvars, typval_T *rettv);
254static void f_matchstr(typval_T *argvars, typval_T *rettv);
255static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
256static void f_max(typval_T *argvars, typval_T *rettv);
257static void f_min(typval_T *argvars, typval_T *rettv);
258#ifdef vim_mkdir
259static void f_mkdir(typval_T *argvars, typval_T *rettv);
260#endif
261static void f_mode(typval_T *argvars, typval_T *rettv);
262#ifdef FEAT_MZSCHEME
263static void f_mzeval(typval_T *argvars, typval_T *rettv);
264#endif
265static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
266static void f_nr2char(typval_T *argvars, typval_T *rettv);
267static void f_or(typval_T *argvars, typval_T *rettv);
268static void f_pathshorten(typval_T *argvars, typval_T *rettv);
269#ifdef FEAT_PERL
270static void f_perleval(typval_T *argvars, typval_T *rettv);
271#endif
272#ifdef FEAT_FLOAT
273static void f_pow(typval_T *argvars, typval_T *rettv);
274#endif
275static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
276static void f_printf(typval_T *argvars, typval_T *rettv);
277static void f_pumvisible(typval_T *argvars, typval_T *rettv);
278#ifdef FEAT_PYTHON3
279static void f_py3eval(typval_T *argvars, typval_T *rettv);
280#endif
281#ifdef FEAT_PYTHON
282static void f_pyeval(typval_T *argvars, typval_T *rettv);
283#endif
284static void f_range(typval_T *argvars, typval_T *rettv);
285static void f_readfile(typval_T *argvars, typval_T *rettv);
286static void f_reltime(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_FLOAT
288static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_reltimestr(typval_T *argvars, typval_T *rettv);
291static void f_remote_expr(typval_T *argvars, typval_T *rettv);
292static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
293static void f_remote_peek(typval_T *argvars, typval_T *rettv);
294static void f_remote_read(typval_T *argvars, typval_T *rettv);
295static void f_remote_send(typval_T *argvars, typval_T *rettv);
296static void f_remove(typval_T *argvars, typval_T *rettv);
297static void f_rename(typval_T *argvars, typval_T *rettv);
298static void f_repeat(typval_T *argvars, typval_T *rettv);
299static void f_resolve(typval_T *argvars, typval_T *rettv);
300static void f_reverse(typval_T *argvars, typval_T *rettv);
301#ifdef FEAT_FLOAT
302static void f_round(typval_T *argvars, typval_T *rettv);
303#endif
304static void f_screenattr(typval_T *argvars, typval_T *rettv);
305static void f_screenchar(typval_T *argvars, typval_T *rettv);
306static void f_screencol(typval_T *argvars, typval_T *rettv);
307static void f_screenrow(typval_T *argvars, typval_T *rettv);
308static void f_search(typval_T *argvars, typval_T *rettv);
309static void f_searchdecl(typval_T *argvars, typval_T *rettv);
310static void f_searchpair(typval_T *argvars, typval_T *rettv);
311static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
312static void f_searchpos(typval_T *argvars, typval_T *rettv);
313static void f_server2client(typval_T *argvars, typval_T *rettv);
314static void f_serverlist(typval_T *argvars, typval_T *rettv);
315static void f_setbufvar(typval_T *argvars, typval_T *rettv);
316static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
317static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
318static void f_setfperm(typval_T *argvars, typval_T *rettv);
319static void f_setline(typval_T *argvars, typval_T *rettv);
320static void f_setloclist(typval_T *argvars, typval_T *rettv);
321static void f_setmatches(typval_T *argvars, typval_T *rettv);
322static void f_setpos(typval_T *argvars, typval_T *rettv);
323static void f_setqflist(typval_T *argvars, typval_T *rettv);
324static void f_setreg(typval_T *argvars, typval_T *rettv);
325static void f_settabvar(typval_T *argvars, typval_T *rettv);
326static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
327static void f_setwinvar(typval_T *argvars, typval_T *rettv);
328#ifdef FEAT_CRYPT
329static void f_sha256(typval_T *argvars, typval_T *rettv);
330#endif /* FEAT_CRYPT */
331static void f_shellescape(typval_T *argvars, typval_T *rettv);
332static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
333static void f_simplify(typval_T *argvars, typval_T *rettv);
334#ifdef FEAT_FLOAT
335static void f_sin(typval_T *argvars, typval_T *rettv);
336static void f_sinh(typval_T *argvars, typval_T *rettv);
337#endif
338static void f_sort(typval_T *argvars, typval_T *rettv);
339static void f_soundfold(typval_T *argvars, typval_T *rettv);
340static void f_spellbadword(typval_T *argvars, typval_T *rettv);
341static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
342static void f_split(typval_T *argvars, typval_T *rettv);
343#ifdef FEAT_FLOAT
344static void f_sqrt(typval_T *argvars, typval_T *rettv);
345static void f_str2float(typval_T *argvars, typval_T *rettv);
346#endif
347static void f_str2nr(typval_T *argvars, typval_T *rettv);
348static void f_strchars(typval_T *argvars, typval_T *rettv);
349#ifdef HAVE_STRFTIME
350static void f_strftime(typval_T *argvars, typval_T *rettv);
351#endif
352static void f_strgetchar(typval_T *argvars, typval_T *rettv);
353static void f_stridx(typval_T *argvars, typval_T *rettv);
354static void f_string(typval_T *argvars, typval_T *rettv);
355static void f_strlen(typval_T *argvars, typval_T *rettv);
356static void f_strcharpart(typval_T *argvars, typval_T *rettv);
357static void f_strpart(typval_T *argvars, typval_T *rettv);
358static void f_strridx(typval_T *argvars, typval_T *rettv);
359static void f_strtrans(typval_T *argvars, typval_T *rettv);
360static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
361static void f_strwidth(typval_T *argvars, typval_T *rettv);
362static void f_submatch(typval_T *argvars, typval_T *rettv);
363static void f_substitute(typval_T *argvars, typval_T *rettv);
364static void f_synID(typval_T *argvars, typval_T *rettv);
365static void f_synIDattr(typval_T *argvars, typval_T *rettv);
366static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
367static void f_synstack(typval_T *argvars, typval_T *rettv);
368static void f_synconcealed(typval_T *argvars, typval_T *rettv);
369static void f_system(typval_T *argvars, typval_T *rettv);
370static void f_systemlist(typval_T *argvars, typval_T *rettv);
371static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
372static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
373static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
374static void f_taglist(typval_T *argvars, typval_T *rettv);
375static void f_tagfiles(typval_T *argvars, typval_T *rettv);
376static void f_tempname(typval_T *argvars, typval_T *rettv);
377static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
378static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
379static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
380static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
381#ifdef FEAT_JOB_CHANNEL
382static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
385#ifdef FEAT_JOB_CHANNEL
386static void f_test_null_job(typval_T *argvars, typval_T *rettv);
387#endif
388static void f_test_null_list(typval_T *argvars, typval_T *rettv);
389static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
390static void f_test_null_string(typval_T *argvars, typval_T *rettv);
391static void f_test_settime(typval_T *argvars, typval_T *rettv);
392#ifdef FEAT_FLOAT
393static void f_tan(typval_T *argvars, typval_T *rettv);
394static void f_tanh(typval_T *argvars, typval_T *rettv);
395#endif
396#ifdef FEAT_TIMERS
397static void f_timer_start(typval_T *argvars, typval_T *rettv);
398static void f_timer_stop(typval_T *argvars, typval_T *rettv);
399#endif
400static void f_tolower(typval_T *argvars, typval_T *rettv);
401static void f_toupper(typval_T *argvars, typval_T *rettv);
402static void f_tr(typval_T *argvars, typval_T *rettv);
403#ifdef FEAT_FLOAT
404static void f_trunc(typval_T *argvars, typval_T *rettv);
405#endif
406static void f_type(typval_T *argvars, typval_T *rettv);
407static void f_undofile(typval_T *argvars, typval_T *rettv);
408static void f_undotree(typval_T *argvars, typval_T *rettv);
409static void f_uniq(typval_T *argvars, typval_T *rettv);
410static void f_values(typval_T *argvars, typval_T *rettv);
411static void f_virtcol(typval_T *argvars, typval_T *rettv);
412static void f_visualmode(typval_T *argvars, typval_T *rettv);
413static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
414static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
415static void f_win_getid(typval_T *argvars, typval_T *rettv);
416static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
417static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
418static void f_win_id2win(typval_T *argvars, typval_T *rettv);
419static void f_winbufnr(typval_T *argvars, typval_T *rettv);
420static void f_wincol(typval_T *argvars, typval_T *rettv);
421static void f_winheight(typval_T *argvars, typval_T *rettv);
422static void f_winline(typval_T *argvars, typval_T *rettv);
423static void f_winnr(typval_T *argvars, typval_T *rettv);
424static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
425static void f_winrestview(typval_T *argvars, typval_T *rettv);
426static void f_winsaveview(typval_T *argvars, typval_T *rettv);
427static void f_winwidth(typval_T *argvars, typval_T *rettv);
428static void f_writefile(typval_T *argvars, typval_T *rettv);
429static void f_wordcount(typval_T *argvars, typval_T *rettv);
430static void f_xor(typval_T *argvars, typval_T *rettv);
431
432/*
433 * Array with names and number of arguments of all internal functions
434 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
435 */
436static struct fst
437{
438 char *f_name; /* function name */
439 char f_min_argc; /* minimal number of arguments */
440 char f_max_argc; /* maximal number of arguments */
441 void (*f_func)(typval_T *args, typval_T *rvar);
442 /* implementation of function */
443} functions[] =
444{
445#ifdef FEAT_FLOAT
446 {"abs", 1, 1, f_abs},
447 {"acos", 1, 1, f_acos}, /* WJMc */
448#endif
449 {"add", 2, 2, f_add},
450 {"and", 2, 2, f_and},
451 {"append", 2, 2, f_append},
452 {"argc", 0, 0, f_argc},
453 {"argidx", 0, 0, f_argidx},
454 {"arglistid", 0, 2, f_arglistid},
455 {"argv", 0, 1, f_argv},
456#ifdef FEAT_FLOAT
457 {"asin", 1, 1, f_asin}, /* WJMc */
458#endif
459 {"assert_equal", 2, 3, f_assert_equal},
460 {"assert_exception", 1, 2, f_assert_exception},
461 {"assert_fails", 1, 2, f_assert_fails},
462 {"assert_false", 1, 2, f_assert_false},
463 {"assert_match", 2, 3, f_assert_match},
464 {"assert_notequal", 2, 3, f_assert_notequal},
465 {"assert_notmatch", 2, 3, f_assert_notmatch},
466 {"assert_true", 1, 2, f_assert_true},
467#ifdef FEAT_FLOAT
468 {"atan", 1, 1, f_atan},
469 {"atan2", 2, 2, f_atan2},
470#endif
471 {"browse", 4, 4, f_browse},
472 {"browsedir", 2, 2, f_browsedir},
473 {"bufexists", 1, 1, f_bufexists},
474 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
475 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
476 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
477 {"buflisted", 1, 1, f_buflisted},
478 {"bufloaded", 1, 1, f_bufloaded},
479 {"bufname", 1, 1, f_bufname},
480 {"bufnr", 1, 2, f_bufnr},
481 {"bufwinid", 1, 1, f_bufwinid},
482 {"bufwinnr", 1, 1, f_bufwinnr},
483 {"byte2line", 1, 1, f_byte2line},
484 {"byteidx", 2, 2, f_byteidx},
485 {"byteidxcomp", 2, 2, f_byteidxcomp},
486 {"call", 2, 3, f_call},
487#ifdef FEAT_FLOAT
488 {"ceil", 1, 1, f_ceil},
489#endif
490#ifdef FEAT_JOB_CHANNEL
491 {"ch_close", 1, 1, f_ch_close},
492 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
493 {"ch_evalraw", 2, 3, f_ch_evalraw},
494 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
495 {"ch_getjob", 1, 1, f_ch_getjob},
496 {"ch_info", 1, 1, f_ch_info},
497 {"ch_log", 1, 2, f_ch_log},
498 {"ch_logfile", 1, 2, f_ch_logfile},
499 {"ch_open", 1, 2, f_ch_open},
500 {"ch_read", 1, 2, f_ch_read},
501 {"ch_readraw", 1, 2, f_ch_readraw},
502 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
503 {"ch_sendraw", 2, 3, f_ch_sendraw},
504 {"ch_setoptions", 2, 2, f_ch_setoptions},
505 {"ch_status", 1, 1, f_ch_status},
506#endif
507 {"changenr", 0, 0, f_changenr},
508 {"char2nr", 1, 2, f_char2nr},
509 {"cindent", 1, 1, f_cindent},
510 {"clearmatches", 0, 0, f_clearmatches},
511 {"col", 1, 1, f_col},
512#if defined(FEAT_INS_EXPAND)
513 {"complete", 2, 2, f_complete},
514 {"complete_add", 1, 1, f_complete_add},
515 {"complete_check", 0, 0, f_complete_check},
516#endif
517 {"confirm", 1, 4, f_confirm},
518 {"copy", 1, 1, f_copy},
519#ifdef FEAT_FLOAT
520 {"cos", 1, 1, f_cos},
521 {"cosh", 1, 1, f_cosh},
522#endif
523 {"count", 2, 4, f_count},
524 {"cscope_connection",0,3, f_cscope_connection},
525 {"cursor", 1, 3, f_cursor},
526 {"deepcopy", 1, 2, f_deepcopy},
527 {"delete", 1, 2, f_delete},
528 {"did_filetype", 0, 0, f_did_filetype},
529 {"diff_filler", 1, 1, f_diff_filler},
530 {"diff_hlID", 2, 2, f_diff_hlID},
531 {"empty", 1, 1, f_empty},
532 {"escape", 2, 2, f_escape},
533 {"eval", 1, 1, f_eval},
534 {"eventhandler", 0, 0, f_eventhandler},
535 {"executable", 1, 1, f_executable},
536 {"execute", 1, 2, f_execute},
537 {"exepath", 1, 1, f_exepath},
538 {"exists", 1, 1, f_exists},
539#ifdef FEAT_FLOAT
540 {"exp", 1, 1, f_exp},
541#endif
542 {"expand", 1, 3, f_expand},
543 {"extend", 2, 3, f_extend},
544 {"feedkeys", 1, 2, f_feedkeys},
545 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
546 {"filereadable", 1, 1, f_filereadable},
547 {"filewritable", 1, 1, f_filewritable},
548 {"filter", 2, 2, f_filter},
549 {"finddir", 1, 3, f_finddir},
550 {"findfile", 1, 3, f_findfile},
551#ifdef FEAT_FLOAT
552 {"float2nr", 1, 1, f_float2nr},
553 {"floor", 1, 1, f_floor},
554 {"fmod", 2, 2, f_fmod},
555#endif
556 {"fnameescape", 1, 1, f_fnameescape},
557 {"fnamemodify", 2, 2, f_fnamemodify},
558 {"foldclosed", 1, 1, f_foldclosed},
559 {"foldclosedend", 1, 1, f_foldclosedend},
560 {"foldlevel", 1, 1, f_foldlevel},
561 {"foldtext", 0, 0, f_foldtext},
562 {"foldtextresult", 1, 1, f_foldtextresult},
563 {"foreground", 0, 0, f_foreground},
564 {"function", 1, 3, f_function},
565 {"garbagecollect", 0, 1, f_garbagecollect},
566 {"get", 2, 3, f_get},
567 {"getbufline", 2, 3, f_getbufline},
568 {"getbufvar", 2, 3, f_getbufvar},
569 {"getchar", 0, 1, f_getchar},
570 {"getcharmod", 0, 0, f_getcharmod},
571 {"getcharsearch", 0, 0, f_getcharsearch},
572 {"getcmdline", 0, 0, f_getcmdline},
573 {"getcmdpos", 0, 0, f_getcmdpos},
574 {"getcmdtype", 0, 0, f_getcmdtype},
575 {"getcmdwintype", 0, 0, f_getcmdwintype},
576#if defined(FEAT_CMDL_COMPL)
577 {"getcompletion", 2, 2, f_getcompletion},
578#endif
579 {"getcurpos", 0, 0, f_getcurpos},
580 {"getcwd", 0, 2, f_getcwd},
581 {"getfontname", 0, 1, f_getfontname},
582 {"getfperm", 1, 1, f_getfperm},
583 {"getfsize", 1, 1, f_getfsize},
584 {"getftime", 1, 1, f_getftime},
585 {"getftype", 1, 1, f_getftype},
586 {"getline", 1, 2, f_getline},
587 {"getloclist", 1, 1, f_getqflist},
588 {"getmatches", 0, 0, f_getmatches},
589 {"getpid", 0, 0, f_getpid},
590 {"getpos", 1, 1, f_getpos},
591 {"getqflist", 0, 0, f_getqflist},
592 {"getreg", 0, 3, f_getreg},
593 {"getregtype", 0, 1, f_getregtype},
594 {"gettabvar", 2, 3, f_gettabvar},
595 {"gettabwinvar", 3, 4, f_gettabwinvar},
596 {"getwinposx", 0, 0, f_getwinposx},
597 {"getwinposy", 0, 0, f_getwinposy},
598 {"getwinvar", 2, 3, f_getwinvar},
599 {"glob", 1, 4, f_glob},
600 {"glob2regpat", 1, 1, f_glob2regpat},
601 {"globpath", 2, 5, f_globpath},
602 {"has", 1, 1, f_has},
603 {"has_key", 2, 2, f_has_key},
604 {"haslocaldir", 0, 2, f_haslocaldir},
605 {"hasmapto", 1, 3, f_hasmapto},
606 {"highlightID", 1, 1, f_hlID}, /* obsolete */
607 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
608 {"histadd", 2, 2, f_histadd},
609 {"histdel", 1, 2, f_histdel},
610 {"histget", 1, 2, f_histget},
611 {"histnr", 1, 1, f_histnr},
612 {"hlID", 1, 1, f_hlID},
613 {"hlexists", 1, 1, f_hlexists},
614 {"hostname", 0, 0, f_hostname},
615 {"iconv", 3, 3, f_iconv},
616 {"indent", 1, 1, f_indent},
617 {"index", 2, 4, f_index},
618 {"input", 1, 3, f_input},
619 {"inputdialog", 1, 3, f_inputdialog},
620 {"inputlist", 1, 1, f_inputlist},
621 {"inputrestore", 0, 0, f_inputrestore},
622 {"inputsave", 0, 0, f_inputsave},
623 {"inputsecret", 1, 2, f_inputsecret},
624 {"insert", 2, 3, f_insert},
625 {"invert", 1, 1, f_invert},
626 {"isdirectory", 1, 1, f_isdirectory},
627 {"islocked", 1, 1, f_islocked},
628#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
629 {"isnan", 1, 1, f_isnan},
630#endif
631 {"items", 1, 1, f_items},
632#ifdef FEAT_JOB_CHANNEL
633 {"job_getchannel", 1, 1, f_job_getchannel},
634 {"job_info", 1, 1, f_job_info},
635 {"job_setoptions", 2, 2, f_job_setoptions},
636 {"job_start", 1, 2, f_job_start},
637 {"job_status", 1, 1, f_job_status},
638 {"job_stop", 1, 2, f_job_stop},
639#endif
640 {"join", 1, 2, f_join},
641 {"js_decode", 1, 1, f_js_decode},
642 {"js_encode", 1, 1, f_js_encode},
643 {"json_decode", 1, 1, f_json_decode},
644 {"json_encode", 1, 1, f_json_encode},
645 {"keys", 1, 1, f_keys},
646 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
647 {"len", 1, 1, f_len},
648 {"libcall", 3, 3, f_libcall},
649 {"libcallnr", 3, 3, f_libcallnr},
650 {"line", 1, 1, f_line},
651 {"line2byte", 1, 1, f_line2byte},
652 {"lispindent", 1, 1, f_lispindent},
653 {"localtime", 0, 0, f_localtime},
654#ifdef FEAT_FLOAT
655 {"log", 1, 1, f_log},
656 {"log10", 1, 1, f_log10},
657#endif
658#ifdef FEAT_LUA
659 {"luaeval", 1, 2, f_luaeval},
660#endif
661 {"map", 2, 2, f_map},
662 {"maparg", 1, 4, f_maparg},
663 {"mapcheck", 1, 3, f_mapcheck},
664 {"match", 2, 4, f_match},
665 {"matchadd", 2, 5, f_matchadd},
666 {"matchaddpos", 2, 5, f_matchaddpos},
667 {"matcharg", 1, 1, f_matcharg},
668 {"matchdelete", 1, 1, f_matchdelete},
669 {"matchend", 2, 4, f_matchend},
670 {"matchlist", 2, 4, f_matchlist},
671 {"matchstr", 2, 4, f_matchstr},
672 {"matchstrpos", 2, 4, f_matchstrpos},
673 {"max", 1, 1, f_max},
674 {"min", 1, 1, f_min},
675#ifdef vim_mkdir
676 {"mkdir", 1, 3, f_mkdir},
677#endif
678 {"mode", 0, 1, f_mode},
679#ifdef FEAT_MZSCHEME
680 {"mzeval", 1, 1, f_mzeval},
681#endif
682 {"nextnonblank", 1, 1, f_nextnonblank},
683 {"nr2char", 1, 2, f_nr2char},
684 {"or", 2, 2, f_or},
685 {"pathshorten", 1, 1, f_pathshorten},
686#ifdef FEAT_PERL
687 {"perleval", 1, 1, f_perleval},
688#endif
689#ifdef FEAT_FLOAT
690 {"pow", 2, 2, f_pow},
691#endif
692 {"prevnonblank", 1, 1, f_prevnonblank},
693 {"printf", 2, 19, f_printf},
694 {"pumvisible", 0, 0, f_pumvisible},
695#ifdef FEAT_PYTHON3
696 {"py3eval", 1, 1, f_py3eval},
697#endif
698#ifdef FEAT_PYTHON
699 {"pyeval", 1, 1, f_pyeval},
700#endif
701 {"range", 1, 3, f_range},
702 {"readfile", 1, 3, f_readfile},
703 {"reltime", 0, 2, f_reltime},
704#ifdef FEAT_FLOAT
705 {"reltimefloat", 1, 1, f_reltimefloat},
706#endif
707 {"reltimestr", 1, 1, f_reltimestr},
708 {"remote_expr", 2, 3, f_remote_expr},
709 {"remote_foreground", 1, 1, f_remote_foreground},
710 {"remote_peek", 1, 2, f_remote_peek},
711 {"remote_read", 1, 1, f_remote_read},
712 {"remote_send", 2, 3, f_remote_send},
713 {"remove", 2, 3, f_remove},
714 {"rename", 2, 2, f_rename},
715 {"repeat", 2, 2, f_repeat},
716 {"resolve", 1, 1, f_resolve},
717 {"reverse", 1, 1, f_reverse},
718#ifdef FEAT_FLOAT
719 {"round", 1, 1, f_round},
720#endif
721 {"screenattr", 2, 2, f_screenattr},
722 {"screenchar", 2, 2, f_screenchar},
723 {"screencol", 0, 0, f_screencol},
724 {"screenrow", 0, 0, f_screenrow},
725 {"search", 1, 4, f_search},
726 {"searchdecl", 1, 3, f_searchdecl},
727 {"searchpair", 3, 7, f_searchpair},
728 {"searchpairpos", 3, 7, f_searchpairpos},
729 {"searchpos", 1, 4, f_searchpos},
730 {"server2client", 2, 2, f_server2client},
731 {"serverlist", 0, 0, f_serverlist},
732 {"setbufvar", 3, 3, f_setbufvar},
733 {"setcharsearch", 1, 1, f_setcharsearch},
734 {"setcmdpos", 1, 1, f_setcmdpos},
735 {"setfperm", 2, 2, f_setfperm},
736 {"setline", 2, 2, f_setline},
737 {"setloclist", 2, 3, f_setloclist},
738 {"setmatches", 1, 1, f_setmatches},
739 {"setpos", 2, 2, f_setpos},
740 {"setqflist", 1, 2, f_setqflist},
741 {"setreg", 2, 3, f_setreg},
742 {"settabvar", 3, 3, f_settabvar},
743 {"settabwinvar", 4, 4, f_settabwinvar},
744 {"setwinvar", 3, 3, f_setwinvar},
745#ifdef FEAT_CRYPT
746 {"sha256", 1, 1, f_sha256},
747#endif
748 {"shellescape", 1, 2, f_shellescape},
749 {"shiftwidth", 0, 0, f_shiftwidth},
750 {"simplify", 1, 1, f_simplify},
751#ifdef FEAT_FLOAT
752 {"sin", 1, 1, f_sin},
753 {"sinh", 1, 1, f_sinh},
754#endif
755 {"sort", 1, 3, f_sort},
756 {"soundfold", 1, 1, f_soundfold},
757 {"spellbadword", 0, 1, f_spellbadword},
758 {"spellsuggest", 1, 3, f_spellsuggest},
759 {"split", 1, 3, f_split},
760#ifdef FEAT_FLOAT
761 {"sqrt", 1, 1, f_sqrt},
762 {"str2float", 1, 1, f_str2float},
763#endif
764 {"str2nr", 1, 2, f_str2nr},
765 {"strcharpart", 2, 3, f_strcharpart},
766 {"strchars", 1, 2, f_strchars},
767 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
768#ifdef HAVE_STRFTIME
769 {"strftime", 1, 2, f_strftime},
770#endif
771 {"strgetchar", 2, 2, f_strgetchar},
772 {"stridx", 2, 3, f_stridx},
773 {"string", 1, 1, f_string},
774 {"strlen", 1, 1, f_strlen},
775 {"strpart", 2, 3, f_strpart},
776 {"strridx", 2, 3, f_strridx},
777 {"strtrans", 1, 1, f_strtrans},
778 {"strwidth", 1, 1, f_strwidth},
779 {"submatch", 1, 2, f_submatch},
780 {"substitute", 4, 4, f_substitute},
781 {"synID", 3, 3, f_synID},
782 {"synIDattr", 2, 3, f_synIDattr},
783 {"synIDtrans", 1, 1, f_synIDtrans},
784 {"synconcealed", 2, 2, f_synconcealed},
785 {"synstack", 2, 2, f_synstack},
786 {"system", 1, 2, f_system},
787 {"systemlist", 1, 2, f_systemlist},
788 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
789 {"tabpagenr", 0, 1, f_tabpagenr},
790 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
791 {"tagfiles", 0, 0, f_tagfiles},
792 {"taglist", 1, 1, f_taglist},
793#ifdef FEAT_FLOAT
794 {"tan", 1, 1, f_tan},
795 {"tanh", 1, 1, f_tanh},
796#endif
797 {"tempname", 0, 0, f_tempname},
798 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
799 {"test_autochdir", 0, 0, f_test_autochdir},
800 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
801 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
802#ifdef FEAT_JOB_CHANNEL
803 {"test_null_channel", 0, 0, f_test_null_channel},
804#endif
805 {"test_null_dict", 0, 0, f_test_null_dict},
806#ifdef FEAT_JOB_CHANNEL
807 {"test_null_job", 0, 0, f_test_null_job},
808#endif
809 {"test_null_list", 0, 0, f_test_null_list},
810 {"test_null_partial", 0, 0, f_test_null_partial},
811 {"test_null_string", 0, 0, f_test_null_string},
812 {"test_settime", 1, 1, f_test_settime},
813#ifdef FEAT_TIMERS
814 {"timer_start", 2, 3, f_timer_start},
815 {"timer_stop", 1, 1, f_timer_stop},
816#endif
817 {"tolower", 1, 1, f_tolower},
818 {"toupper", 1, 1, f_toupper},
819 {"tr", 3, 3, f_tr},
820#ifdef FEAT_FLOAT
821 {"trunc", 1, 1, f_trunc},
822#endif
823 {"type", 1, 1, f_type},
824 {"undofile", 1, 1, f_undofile},
825 {"undotree", 0, 0, f_undotree},
826 {"uniq", 1, 3, f_uniq},
827 {"values", 1, 1, f_values},
828 {"virtcol", 1, 1, f_virtcol},
829 {"visualmode", 0, 1, f_visualmode},
830 {"wildmenumode", 0, 0, f_wildmenumode},
831 {"win_findbuf", 1, 1, f_win_findbuf},
832 {"win_getid", 0, 2, f_win_getid},
833 {"win_gotoid", 1, 1, f_win_gotoid},
834 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
835 {"win_id2win", 1, 1, f_win_id2win},
836 {"winbufnr", 1, 1, f_winbufnr},
837 {"wincol", 0, 0, f_wincol},
838 {"winheight", 1, 1, f_winheight},
839 {"winline", 0, 0, f_winline},
840 {"winnr", 0, 1, f_winnr},
841 {"winrestcmd", 0, 0, f_winrestcmd},
842 {"winrestview", 1, 1, f_winrestview},
843 {"winsaveview", 0, 0, f_winsaveview},
844 {"winwidth", 1, 1, f_winwidth},
845 {"wordcount", 0, 0, f_wordcount},
846 {"writefile", 2, 3, f_writefile},
847 {"xor", 2, 2, f_xor},
848};
849
850#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
851
852/*
853 * Function given to ExpandGeneric() to obtain the list of internal
854 * or user defined function names.
855 */
856 char_u *
857get_function_name(expand_T *xp, int idx)
858{
859 static int intidx = -1;
860 char_u *name;
861
862 if (idx == 0)
863 intidx = -1;
864 if (intidx < 0)
865 {
866 name = get_user_func_name(xp, idx);
867 if (name != NULL)
868 return name;
869 }
870 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
871 {
872 STRCPY(IObuff, functions[intidx].f_name);
873 STRCAT(IObuff, "(");
874 if (functions[intidx].f_max_argc == 0)
875 STRCAT(IObuff, ")");
876 return IObuff;
877 }
878
879 return NULL;
880}
881
882/*
883 * Function given to ExpandGeneric() to obtain the list of internal or
884 * user defined variable or function names.
885 */
886 char_u *
887get_expr_name(expand_T *xp, int idx)
888{
889 static int intidx = -1;
890 char_u *name;
891
892 if (idx == 0)
893 intidx = -1;
894 if (intidx < 0)
895 {
896 name = get_function_name(xp, idx);
897 if (name != NULL)
898 return name;
899 }
900 return get_user_var_name(xp, ++intidx);
901}
902
903#endif /* FEAT_CMDL_COMPL */
904
905#if defined(EBCDIC) || defined(PROTO)
906/*
907 * Compare struct fst by function name.
908 */
909 static int
910compare_func_name(const void *s1, const void *s2)
911{
912 struct fst *p1 = (struct fst *)s1;
913 struct fst *p2 = (struct fst *)s2;
914
915 return STRCMP(p1->f_name, p2->f_name);
916}
917
918/*
919 * Sort the function table by function name.
920 * The sorting of the table above is ASCII dependant.
921 * On machines using EBCDIC we have to sort it.
922 */
923 static void
924sortFunctions(void)
925{
926 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
927
928 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
929}
930#endif
931
932
933/*
934 * Find internal function in table above.
935 * Return index, or -1 if not found
936 */
937 int
938find_internal_func(
939 char_u *name) /* name of the function */
940{
941 int first = 0;
942 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
943 int cmp;
944 int x;
945
946 /*
947 * Find the function name in the table. Binary search.
948 */
949 while (first <= last)
950 {
951 x = first + ((unsigned)(last - first) >> 1);
952 cmp = STRCMP(name, functions[x].f_name);
953 if (cmp < 0)
954 last = x - 1;
955 else if (cmp > 0)
956 first = x + 1;
957 else
958 return x;
959 }
960 return -1;
961}
962
963 int
964call_internal_func(
965 char_u *name,
966 int argcount,
967 typval_T *argvars,
968 typval_T *rettv)
969{
970 int i;
971
972 i = find_internal_func(name);
973 if (i < 0)
974 return ERROR_UNKNOWN;
975 if (argcount < functions[i].f_min_argc)
976 return ERROR_TOOFEW;
977 if (argcount > functions[i].f_max_argc)
978 return ERROR_TOOMANY;
979 argvars[argcount].v_type = VAR_UNKNOWN;
980 functions[i].f_func(argvars, rettv);
981 return ERROR_NONE;
982}
983
984/*
985 * Return TRUE for a non-zero Number and a non-empty String.
986 */
987 static int
988non_zero_arg(typval_T *argvars)
989{
990 return ((argvars[0].v_type == VAR_NUMBER
991 && argvars[0].vval.v_number != 0)
992 || (argvars[0].v_type == VAR_SPECIAL
993 && argvars[0].vval.v_number == VVAL_TRUE)
994 || (argvars[0].v_type == VAR_STRING
995 && argvars[0].vval.v_string != NULL
996 && *argvars[0].vval.v_string != NUL));
997}
998
999/*
1000 * Get the lnum from the first argument.
1001 * Also accepts ".", "$", etc., but that only works for the current buffer.
1002 * Returns -1 on error.
1003 */
1004 static linenr_T
1005get_tv_lnum(typval_T *argvars)
1006{
1007 typval_T rettv;
1008 linenr_T lnum;
1009
1010 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1011 if (lnum == 0) /* no valid number, try using line() */
1012 {
1013 rettv.v_type = VAR_NUMBER;
1014 f_line(argvars, &rettv);
1015 lnum = (linenr_T)rettv.vval.v_number;
1016 clear_tv(&rettv);
1017 }
1018 return lnum;
1019}
1020
1021#ifdef FEAT_FLOAT
1022static int get_float_arg(typval_T *argvars, float_T *f);
1023
1024/*
1025 * Get the float value of "argvars[0]" into "f".
1026 * Returns FAIL when the argument is not a Number or Float.
1027 */
1028 static int
1029get_float_arg(typval_T *argvars, float_T *f)
1030{
1031 if (argvars[0].v_type == VAR_FLOAT)
1032 {
1033 *f = argvars[0].vval.v_float;
1034 return OK;
1035 }
1036 if (argvars[0].v_type == VAR_NUMBER)
1037 {
1038 *f = (float_T)argvars[0].vval.v_number;
1039 return OK;
1040 }
1041 EMSG(_("E808: Number or Float required"));
1042 return FAIL;
1043}
1044
1045/*
1046 * "abs(expr)" function
1047 */
1048 static void
1049f_abs(typval_T *argvars, typval_T *rettv)
1050{
1051 if (argvars[0].v_type == VAR_FLOAT)
1052 {
1053 rettv->v_type = VAR_FLOAT;
1054 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1055 }
1056 else
1057 {
1058 varnumber_T n;
1059 int error = FALSE;
1060
1061 n = get_tv_number_chk(&argvars[0], &error);
1062 if (error)
1063 rettv->vval.v_number = -1;
1064 else if (n > 0)
1065 rettv->vval.v_number = n;
1066 else
1067 rettv->vval.v_number = -n;
1068 }
1069}
1070
1071/*
1072 * "acos()" function
1073 */
1074 static void
1075f_acos(typval_T *argvars, typval_T *rettv)
1076{
1077 float_T f = 0.0;
1078
1079 rettv->v_type = VAR_FLOAT;
1080 if (get_float_arg(argvars, &f) == OK)
1081 rettv->vval.v_float = acos(f);
1082 else
1083 rettv->vval.v_float = 0.0;
1084}
1085#endif
1086
1087/*
1088 * "add(list, item)" function
1089 */
1090 static void
1091f_add(typval_T *argvars, typval_T *rettv)
1092{
1093 list_T *l;
1094
1095 rettv->vval.v_number = 1; /* Default: Failed */
1096 if (argvars[0].v_type == VAR_LIST)
1097 {
1098 if ((l = argvars[0].vval.v_list) != NULL
1099 && !tv_check_lock(l->lv_lock,
1100 (char_u *)N_("add() argument"), TRUE)
1101 && list_append_tv(l, &argvars[1]) == OK)
1102 copy_tv(&argvars[0], rettv);
1103 }
1104 else
1105 EMSG(_(e_listreq));
1106}
1107
1108/*
1109 * "and(expr, expr)" function
1110 */
1111 static void
1112f_and(typval_T *argvars, typval_T *rettv)
1113{
1114 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1115 & get_tv_number_chk(&argvars[1], NULL);
1116}
1117
1118/*
1119 * "append(lnum, string/list)" function
1120 */
1121 static void
1122f_append(typval_T *argvars, typval_T *rettv)
1123{
1124 long lnum;
1125 char_u *line;
1126 list_T *l = NULL;
1127 listitem_T *li = NULL;
1128 typval_T *tv;
1129 long added = 0;
1130
1131 /* When coming here from Insert mode, sync undo, so that this can be
1132 * undone separately from what was previously inserted. */
1133 if (u_sync_once == 2)
1134 {
1135 u_sync_once = 1; /* notify that u_sync() was called */
1136 u_sync(TRUE);
1137 }
1138
1139 lnum = get_tv_lnum(argvars);
1140 if (lnum >= 0
1141 && lnum <= curbuf->b_ml.ml_line_count
1142 && u_save(lnum, lnum + 1) == OK)
1143 {
1144 if (argvars[1].v_type == VAR_LIST)
1145 {
1146 l = argvars[1].vval.v_list;
1147 if (l == NULL)
1148 return;
1149 li = l->lv_first;
1150 }
1151 for (;;)
1152 {
1153 if (l == NULL)
1154 tv = &argvars[1]; /* append a string */
1155 else if (li == NULL)
1156 break; /* end of list */
1157 else
1158 tv = &li->li_tv; /* append item from list */
1159 line = get_tv_string_chk(tv);
1160 if (line == NULL) /* type error */
1161 {
1162 rettv->vval.v_number = 1; /* Failed */
1163 break;
1164 }
1165 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1166 ++added;
1167 if (l == NULL)
1168 break;
1169 li = li->li_next;
1170 }
1171
1172 appended_lines_mark(lnum, added);
1173 if (curwin->w_cursor.lnum > lnum)
1174 curwin->w_cursor.lnum += added;
1175 }
1176 else
1177 rettv->vval.v_number = 1; /* Failed */
1178}
1179
1180/*
1181 * "argc()" function
1182 */
1183 static void
1184f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1185{
1186 rettv->vval.v_number = ARGCOUNT;
1187}
1188
1189/*
1190 * "argidx()" function
1191 */
1192 static void
1193f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1194{
1195 rettv->vval.v_number = curwin->w_arg_idx;
1196}
1197
1198/*
1199 * "arglistid()" function
1200 */
1201 static void
1202f_arglistid(typval_T *argvars, typval_T *rettv)
1203{
1204 win_T *wp;
1205
1206 rettv->vval.v_number = -1;
1207 wp = find_tabwin(&argvars[0], &argvars[1]);
1208 if (wp != NULL)
1209 rettv->vval.v_number = wp->w_alist->id;
1210}
1211
1212/*
1213 * "argv(nr)" function
1214 */
1215 static void
1216f_argv(typval_T *argvars, typval_T *rettv)
1217{
1218 int idx;
1219
1220 if (argvars[0].v_type != VAR_UNKNOWN)
1221 {
1222 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1223 if (idx >= 0 && idx < ARGCOUNT)
1224 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1225 else
1226 rettv->vval.v_string = NULL;
1227 rettv->v_type = VAR_STRING;
1228 }
1229 else if (rettv_list_alloc(rettv) == OK)
1230 for (idx = 0; idx < ARGCOUNT; ++idx)
1231 list_append_string(rettv->vval.v_list,
1232 alist_name(&ARGLIST[idx]), -1);
1233}
1234
1235/*
1236 * "assert_equal(expected, actual[, msg])" function
1237 */
1238 static void
1239f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1240{
1241 assert_equal_common(argvars, ASSERT_EQUAL);
1242}
1243
1244/*
1245 * "assert_notequal(expected, actual[, msg])" function
1246 */
1247 static void
1248f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1249{
1250 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1251}
1252
1253/*
1254 * "assert_exception(string[, msg])" function
1255 */
1256 static void
1257f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1258{
1259 assert_exception(argvars);
1260}
1261
1262/*
1263 * "assert_fails(cmd [, error])" function
1264 */
1265 static void
1266f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1267{
1268 assert_fails(argvars);
1269}
1270
1271/*
1272 * "assert_false(actual[, msg])" function
1273 */
1274 static void
1275f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1276{
1277 assert_bool(argvars, FALSE);
1278}
1279
1280/*
1281 * "assert_match(pattern, actual[, msg])" function
1282 */
1283 static void
1284f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1285{
1286 assert_match_common(argvars, ASSERT_MATCH);
1287}
1288
1289/*
1290 * "assert_notmatch(pattern, actual[, msg])" function
1291 */
1292 static void
1293f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1294{
1295 assert_match_common(argvars, ASSERT_NOTMATCH);
1296}
1297
1298/*
1299 * "assert_true(actual[, msg])" function
1300 */
1301 static void
1302f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1303{
1304 assert_bool(argvars, TRUE);
1305}
1306
1307#ifdef FEAT_FLOAT
1308/*
1309 * "asin()" function
1310 */
1311 static void
1312f_asin(typval_T *argvars, typval_T *rettv)
1313{
1314 float_T f = 0.0;
1315
1316 rettv->v_type = VAR_FLOAT;
1317 if (get_float_arg(argvars, &f) == OK)
1318 rettv->vval.v_float = asin(f);
1319 else
1320 rettv->vval.v_float = 0.0;
1321}
1322
1323/*
1324 * "atan()" function
1325 */
1326 static void
1327f_atan(typval_T *argvars, typval_T *rettv)
1328{
1329 float_T f = 0.0;
1330
1331 rettv->v_type = VAR_FLOAT;
1332 if (get_float_arg(argvars, &f) == OK)
1333 rettv->vval.v_float = atan(f);
1334 else
1335 rettv->vval.v_float = 0.0;
1336}
1337
1338/*
1339 * "atan2()" function
1340 */
1341 static void
1342f_atan2(typval_T *argvars, typval_T *rettv)
1343{
1344 float_T fx = 0.0, fy = 0.0;
1345
1346 rettv->v_type = VAR_FLOAT;
1347 if (get_float_arg(argvars, &fx) == OK
1348 && get_float_arg(&argvars[1], &fy) == OK)
1349 rettv->vval.v_float = atan2(fx, fy);
1350 else
1351 rettv->vval.v_float = 0.0;
1352}
1353#endif
1354
1355/*
1356 * "browse(save, title, initdir, default)" function
1357 */
1358 static void
1359f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1360{
1361#ifdef FEAT_BROWSE
1362 int save;
1363 char_u *title;
1364 char_u *initdir;
1365 char_u *defname;
1366 char_u buf[NUMBUFLEN];
1367 char_u buf2[NUMBUFLEN];
1368 int error = FALSE;
1369
1370 save = (int)get_tv_number_chk(&argvars[0], &error);
1371 title = get_tv_string_chk(&argvars[1]);
1372 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1373 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1374
1375 if (error || title == NULL || initdir == NULL || defname == NULL)
1376 rettv->vval.v_string = NULL;
1377 else
1378 rettv->vval.v_string =
1379 do_browse(save ? BROWSE_SAVE : 0,
1380 title, defname, NULL, initdir, NULL, curbuf);
1381#else
1382 rettv->vval.v_string = NULL;
1383#endif
1384 rettv->v_type = VAR_STRING;
1385}
1386
1387/*
1388 * "browsedir(title, initdir)" function
1389 */
1390 static void
1391f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1392{
1393#ifdef FEAT_BROWSE
1394 char_u *title;
1395 char_u *initdir;
1396 char_u buf[NUMBUFLEN];
1397
1398 title = get_tv_string_chk(&argvars[0]);
1399 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1400
1401 if (title == NULL || initdir == NULL)
1402 rettv->vval.v_string = NULL;
1403 else
1404 rettv->vval.v_string = do_browse(BROWSE_DIR,
1405 title, NULL, NULL, initdir, NULL, curbuf);
1406#else
1407 rettv->vval.v_string = NULL;
1408#endif
1409 rettv->v_type = VAR_STRING;
1410}
1411
1412static buf_T *find_buffer(typval_T *avar);
1413
1414/*
1415 * Find a buffer by number or exact name.
1416 */
1417 static buf_T *
1418find_buffer(typval_T *avar)
1419{
1420 buf_T *buf = NULL;
1421
1422 if (avar->v_type == VAR_NUMBER)
1423 buf = buflist_findnr((int)avar->vval.v_number);
1424 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1425 {
1426 buf = buflist_findname_exp(avar->vval.v_string);
1427 if (buf == NULL)
1428 {
1429 /* No full path name match, try a match with a URL or a "nofile"
1430 * buffer, these don't use the full path. */
1431 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1432 if (buf->b_fname != NULL
1433 && (path_with_url(buf->b_fname)
1434#ifdef FEAT_QUICKFIX
1435 || bt_nofile(buf)
1436#endif
1437 )
1438 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1439 break;
1440 }
1441 }
1442 return buf;
1443}
1444
1445/*
1446 * "bufexists(expr)" function
1447 */
1448 static void
1449f_bufexists(typval_T *argvars, typval_T *rettv)
1450{
1451 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1452}
1453
1454/*
1455 * "buflisted(expr)" function
1456 */
1457 static void
1458f_buflisted(typval_T *argvars, typval_T *rettv)
1459{
1460 buf_T *buf;
1461
1462 buf = find_buffer(&argvars[0]);
1463 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1464}
1465
1466/*
1467 * "bufloaded(expr)" function
1468 */
1469 static void
1470f_bufloaded(typval_T *argvars, typval_T *rettv)
1471{
1472 buf_T *buf;
1473
1474 buf = find_buffer(&argvars[0]);
1475 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1476}
1477
1478 buf_T *
1479buflist_find_by_name(char_u *name, int curtab_only)
1480{
1481 int save_magic;
1482 char_u *save_cpo;
1483 buf_T *buf;
1484
1485 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1486 save_magic = p_magic;
1487 p_magic = TRUE;
1488 save_cpo = p_cpo;
1489 p_cpo = (char_u *)"";
1490
1491 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1492 TRUE, FALSE, curtab_only));
1493
1494 p_magic = save_magic;
1495 p_cpo = save_cpo;
1496 return buf;
1497}
1498
1499/*
1500 * Get buffer by number or pattern.
1501 */
1502 static buf_T *
1503get_buf_tv(typval_T *tv, int curtab_only)
1504{
1505 char_u *name = tv->vval.v_string;
1506 buf_T *buf;
1507
1508 if (tv->v_type == VAR_NUMBER)
1509 return buflist_findnr((int)tv->vval.v_number);
1510 if (tv->v_type != VAR_STRING)
1511 return NULL;
1512 if (name == NULL || *name == NUL)
1513 return curbuf;
1514 if (name[0] == '$' && name[1] == NUL)
1515 return lastbuf;
1516
1517 buf = buflist_find_by_name(name, curtab_only);
1518
1519 /* If not found, try expanding the name, like done for bufexists(). */
1520 if (buf == NULL)
1521 buf = find_buffer(tv);
1522
1523 return buf;
1524}
1525
1526/*
1527 * "bufname(expr)" function
1528 */
1529 static void
1530f_bufname(typval_T *argvars, typval_T *rettv)
1531{
1532 buf_T *buf;
1533
1534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1535 ++emsg_off;
1536 buf = get_buf_tv(&argvars[0], FALSE);
1537 rettv->v_type = VAR_STRING;
1538 if (buf != NULL && buf->b_fname != NULL)
1539 rettv->vval.v_string = vim_strsave(buf->b_fname);
1540 else
1541 rettv->vval.v_string = NULL;
1542 --emsg_off;
1543}
1544
1545/*
1546 * "bufnr(expr)" function
1547 */
1548 static void
1549f_bufnr(typval_T *argvars, typval_T *rettv)
1550{
1551 buf_T *buf;
1552 int error = FALSE;
1553 char_u *name;
1554
1555 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1556 ++emsg_off;
1557 buf = get_buf_tv(&argvars[0], FALSE);
1558 --emsg_off;
1559
1560 /* If the buffer isn't found and the second argument is not zero create a
1561 * new buffer. */
1562 if (buf == NULL
1563 && argvars[1].v_type != VAR_UNKNOWN
1564 && get_tv_number_chk(&argvars[1], &error) != 0
1565 && !error
1566 && (name = get_tv_string_chk(&argvars[0])) != NULL
1567 && !error)
1568 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1569
1570 if (buf != NULL)
1571 rettv->vval.v_number = buf->b_fnum;
1572 else
1573 rettv->vval.v_number = -1;
1574}
1575
1576 static void
1577buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1578{
1579#ifdef FEAT_WINDOWS
1580 win_T *wp;
1581 int winnr = 0;
1582#endif
1583 buf_T *buf;
1584
1585 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1586 ++emsg_off;
1587 buf = get_buf_tv(&argvars[0], TRUE);
1588#ifdef FEAT_WINDOWS
1589 for (wp = firstwin; wp; wp = wp->w_next)
1590 {
1591 ++winnr;
1592 if (wp->w_buffer == buf)
1593 break;
1594 }
1595 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
1596#else
1597 rettv->vval.v_number = (curwin->w_buffer == buf
1598 ? (get_nr ? 1 : curwin->w_id) : -1);
1599#endif
1600 --emsg_off;
1601}
1602
1603/*
1604 * "bufwinid(nr)" function
1605 */
1606 static void
1607f_bufwinid(typval_T *argvars, typval_T *rettv)
1608{
1609 buf_win_common(argvars, rettv, FALSE);
1610}
1611
1612/*
1613 * "bufwinnr(nr)" function
1614 */
1615 static void
1616f_bufwinnr(typval_T *argvars, typval_T *rettv)
1617{
1618 buf_win_common(argvars, rettv, TRUE);
1619}
1620
1621/*
1622 * "byte2line(byte)" function
1623 */
1624 static void
1625f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1626{
1627#ifndef FEAT_BYTEOFF
1628 rettv->vval.v_number = -1;
1629#else
1630 long boff = 0;
1631
1632 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1633 if (boff < 0)
1634 rettv->vval.v_number = -1;
1635 else
1636 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1637 (linenr_T)0, &boff);
1638#endif
1639}
1640
1641 static void
1642byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1643{
1644#ifdef FEAT_MBYTE
1645 char_u *t;
1646#endif
1647 char_u *str;
1648 varnumber_T idx;
1649
1650 str = get_tv_string_chk(&argvars[0]);
1651 idx = get_tv_number_chk(&argvars[1], NULL);
1652 rettv->vval.v_number = -1;
1653 if (str == NULL || idx < 0)
1654 return;
1655
1656#ifdef FEAT_MBYTE
1657 t = str;
1658 for ( ; idx > 0; idx--)
1659 {
1660 if (*t == NUL) /* EOL reached */
1661 return;
1662 if (enc_utf8 && comp)
1663 t += utf_ptr2len(t);
1664 else
1665 t += (*mb_ptr2len)(t);
1666 }
1667 rettv->vval.v_number = (varnumber_T)(t - str);
1668#else
1669 if ((size_t)idx <= STRLEN(str))
1670 rettv->vval.v_number = idx;
1671#endif
1672}
1673
1674/*
1675 * "byteidx()" function
1676 */
1677 static void
1678f_byteidx(typval_T *argvars, typval_T *rettv)
1679{
1680 byteidx(argvars, rettv, FALSE);
1681}
1682
1683/*
1684 * "byteidxcomp()" function
1685 */
1686 static void
1687f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1688{
1689 byteidx(argvars, rettv, TRUE);
1690}
1691
1692/*
1693 * "call(func, arglist [, dict])" function
1694 */
1695 static void
1696f_call(typval_T *argvars, typval_T *rettv)
1697{
1698 char_u *func;
1699 partial_T *partial = NULL;
1700 dict_T *selfdict = NULL;
1701
1702 if (argvars[1].v_type != VAR_LIST)
1703 {
1704 EMSG(_(e_listreq));
1705 return;
1706 }
1707 if (argvars[1].vval.v_list == NULL)
1708 return;
1709
1710 if (argvars[0].v_type == VAR_FUNC)
1711 func = argvars[0].vval.v_string;
1712 else if (argvars[0].v_type == VAR_PARTIAL)
1713 {
1714 partial = argvars[0].vval.v_partial;
1715 func = partial->pt_name;
1716 }
1717 else
1718 func = get_tv_string(&argvars[0]);
1719 if (*func == NUL)
1720 return; /* type error or empty name */
1721
1722 if (argvars[2].v_type != VAR_UNKNOWN)
1723 {
1724 if (argvars[2].v_type != VAR_DICT)
1725 {
1726 EMSG(_(e_dictreq));
1727 return;
1728 }
1729 selfdict = argvars[2].vval.v_dict;
1730 }
1731
1732 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1733}
1734
1735#ifdef FEAT_FLOAT
1736/*
1737 * "ceil({float})" function
1738 */
1739 static void
1740f_ceil(typval_T *argvars, typval_T *rettv)
1741{
1742 float_T f = 0.0;
1743
1744 rettv->v_type = VAR_FLOAT;
1745 if (get_float_arg(argvars, &f) == OK)
1746 rettv->vval.v_float = ceil(f);
1747 else
1748 rettv->vval.v_float = 0.0;
1749}
1750#endif
1751
1752#ifdef FEAT_JOB_CHANNEL
1753/*
1754 * "ch_close()" function
1755 */
1756 static void
1757f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1758{
1759 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1760
1761 if (channel != NULL)
1762 {
1763 channel_close(channel, FALSE);
1764 channel_clear(channel);
1765 }
1766}
1767
1768/*
1769 * "ch_getbufnr()" function
1770 */
1771 static void
1772f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1773{
1774 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1775
1776 rettv->vval.v_number = -1;
1777 if (channel != NULL)
1778 {
1779 char_u *what = get_tv_string(&argvars[1]);
1780 int part;
1781
1782 if (STRCMP(what, "err") == 0)
1783 part = PART_ERR;
1784 else if (STRCMP(what, "out") == 0)
1785 part = PART_OUT;
1786 else if (STRCMP(what, "in") == 0)
1787 part = PART_IN;
1788 else
1789 part = PART_SOCK;
1790 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1791 rettv->vval.v_number =
1792 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1793 }
1794}
1795
1796/*
1797 * "ch_getjob()" function
1798 */
1799 static void
1800f_ch_getjob(typval_T *argvars, typval_T *rettv)
1801{
1802 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1803
1804 if (channel != NULL)
1805 {
1806 rettv->v_type = VAR_JOB;
1807 rettv->vval.v_job = channel->ch_job;
1808 if (channel->ch_job != NULL)
1809 ++channel->ch_job->jv_refcount;
1810 }
1811}
1812
1813/*
1814 * "ch_info()" function
1815 */
1816 static void
1817f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1818{
1819 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1820
1821 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1822 channel_info(channel, rettv->vval.v_dict);
1823}
1824
1825/*
1826 * "ch_log()" function
1827 */
1828 static void
1829f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1830{
1831 char_u *msg = get_tv_string(&argvars[0]);
1832 channel_T *channel = NULL;
1833
1834 if (argvars[1].v_type != VAR_UNKNOWN)
1835 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1836
1837 ch_log(channel, (char *)msg);
1838}
1839
1840/*
1841 * "ch_logfile()" function
1842 */
1843 static void
1844f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1845{
1846 char_u *fname;
1847 char_u *opt = (char_u *)"";
1848 char_u buf[NUMBUFLEN];
1849
1850 fname = get_tv_string(&argvars[0]);
1851 if (argvars[1].v_type == VAR_STRING)
1852 opt = get_tv_string_buf(&argvars[1], buf);
1853 ch_logfile(fname, opt);
1854}
1855
1856/*
1857 * "ch_open()" function
1858 */
1859 static void
1860f_ch_open(typval_T *argvars, typval_T *rettv)
1861{
1862 rettv->v_type = VAR_CHANNEL;
1863 if (check_restricted() || check_secure())
1864 return;
1865 rettv->vval.v_channel = channel_open_func(argvars);
1866}
1867
1868/*
1869 * "ch_read()" function
1870 */
1871 static void
1872f_ch_read(typval_T *argvars, typval_T *rettv)
1873{
1874 common_channel_read(argvars, rettv, FALSE);
1875}
1876
1877/*
1878 * "ch_readraw()" function
1879 */
1880 static void
1881f_ch_readraw(typval_T *argvars, typval_T *rettv)
1882{
1883 common_channel_read(argvars, rettv, TRUE);
1884}
1885
1886/*
1887 * "ch_evalexpr()" function
1888 */
1889 static void
1890f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
1891{
1892 ch_expr_common(argvars, rettv, TRUE);
1893}
1894
1895/*
1896 * "ch_sendexpr()" function
1897 */
1898 static void
1899f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
1900{
1901 ch_expr_common(argvars, rettv, FALSE);
1902}
1903
1904/*
1905 * "ch_evalraw()" function
1906 */
1907 static void
1908f_ch_evalraw(typval_T *argvars, typval_T *rettv)
1909{
1910 ch_raw_common(argvars, rettv, TRUE);
1911}
1912
1913/*
1914 * "ch_sendraw()" function
1915 */
1916 static void
1917f_ch_sendraw(typval_T *argvars, typval_T *rettv)
1918{
1919 ch_raw_common(argvars, rettv, FALSE);
1920}
1921
1922/*
1923 * "ch_setoptions()" function
1924 */
1925 static void
1926f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1927{
1928 channel_T *channel;
1929 jobopt_T opt;
1930
1931 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1932 if (channel == NULL)
1933 return;
1934 clear_job_options(&opt);
1935 if (get_job_options(&argvars[1], &opt,
1936 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
1937 channel_set_options(channel, &opt);
1938 free_job_options(&opt);
1939}
1940
1941/*
1942 * "ch_status()" function
1943 */
1944 static void
1945f_ch_status(typval_T *argvars, typval_T *rettv)
1946{
1947 channel_T *channel;
1948
1949 /* return an empty string by default */
1950 rettv->v_type = VAR_STRING;
1951 rettv->vval.v_string = NULL;
1952
1953 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1954 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
1955}
1956#endif
1957
1958/*
1959 * "changenr()" function
1960 */
1961 static void
1962f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1963{
1964 rettv->vval.v_number = curbuf->b_u_seq_cur;
1965}
1966
1967/*
1968 * "char2nr(string)" function
1969 */
1970 static void
1971f_char2nr(typval_T *argvars, typval_T *rettv)
1972{
1973#ifdef FEAT_MBYTE
1974 if (has_mbyte)
1975 {
1976 int utf8 = 0;
1977
1978 if (argvars[1].v_type != VAR_UNKNOWN)
1979 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
1980
1981 if (utf8)
1982 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
1983 else
1984 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
1985 }
1986 else
1987#endif
1988 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
1989}
1990
1991/*
1992 * "cindent(lnum)" function
1993 */
1994 static void
1995f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
1996{
1997#ifdef FEAT_CINDENT
1998 pos_T pos;
1999 linenr_T lnum;
2000
2001 pos = curwin->w_cursor;
2002 lnum = get_tv_lnum(argvars);
2003 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2004 {
2005 curwin->w_cursor.lnum = lnum;
2006 rettv->vval.v_number = get_c_indent();
2007 curwin->w_cursor = pos;
2008 }
2009 else
2010#endif
2011 rettv->vval.v_number = -1;
2012}
2013
2014/*
2015 * "clearmatches()" function
2016 */
2017 static void
2018f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2019{
2020#ifdef FEAT_SEARCH_EXTRA
2021 clear_matches(curwin);
2022#endif
2023}
2024
2025/*
2026 * "col(string)" function
2027 */
2028 static void
2029f_col(typval_T *argvars, typval_T *rettv)
2030{
2031 colnr_T col = 0;
2032 pos_T *fp;
2033 int fnum = curbuf->b_fnum;
2034
2035 fp = var2fpos(&argvars[0], FALSE, &fnum);
2036 if (fp != NULL && fnum == curbuf->b_fnum)
2037 {
2038 if (fp->col == MAXCOL)
2039 {
2040 /* '> can be MAXCOL, get the length of the line then */
2041 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2042 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2043 else
2044 col = MAXCOL;
2045 }
2046 else
2047 {
2048 col = fp->col + 1;
2049#ifdef FEAT_VIRTUALEDIT
2050 /* col(".") when the cursor is on the NUL at the end of the line
2051 * because of "coladd" can be seen as an extra column. */
2052 if (virtual_active() && fp == &curwin->w_cursor)
2053 {
2054 char_u *p = ml_get_cursor();
2055
2056 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2057 curwin->w_virtcol - curwin->w_cursor.coladd))
2058 {
2059# ifdef FEAT_MBYTE
2060 int l;
2061
2062 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2063 col += l;
2064# else
2065 if (*p != NUL && p[1] == NUL)
2066 ++col;
2067# endif
2068 }
2069 }
2070#endif
2071 }
2072 }
2073 rettv->vval.v_number = col;
2074}
2075
2076#if defined(FEAT_INS_EXPAND)
2077/*
2078 * "complete()" function
2079 */
2080 static void
2081f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2082{
2083 int startcol;
2084
2085 if ((State & INSERT) == 0)
2086 {
2087 EMSG(_("E785: complete() can only be used in Insert mode"));
2088 return;
2089 }
2090
2091 /* Check for undo allowed here, because if something was already inserted
2092 * the line was already saved for undo and this check isn't done. */
2093 if (!undo_allowed())
2094 return;
2095
2096 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2097 {
2098 EMSG(_(e_invarg));
2099 return;
2100 }
2101
2102 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2103 if (startcol <= 0)
2104 return;
2105
2106 set_completion(startcol - 1, argvars[1].vval.v_list);
2107}
2108
2109/*
2110 * "complete_add()" function
2111 */
2112 static void
2113f_complete_add(typval_T *argvars, typval_T *rettv)
2114{
2115 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2116}
2117
2118/*
2119 * "complete_check()" function
2120 */
2121 static void
2122f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2123{
2124 int saved = RedrawingDisabled;
2125
2126 RedrawingDisabled = 0;
2127 ins_compl_check_keys(0);
2128 rettv->vval.v_number = compl_interrupted;
2129 RedrawingDisabled = saved;
2130}
2131#endif
2132
2133/*
2134 * "confirm(message, buttons[, default [, type]])" function
2135 */
2136 static void
2137f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2138{
2139#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2140 char_u *message;
2141 char_u *buttons = NULL;
2142 char_u buf[NUMBUFLEN];
2143 char_u buf2[NUMBUFLEN];
2144 int def = 1;
2145 int type = VIM_GENERIC;
2146 char_u *typestr;
2147 int error = FALSE;
2148
2149 message = get_tv_string_chk(&argvars[0]);
2150 if (message == NULL)
2151 error = TRUE;
2152 if (argvars[1].v_type != VAR_UNKNOWN)
2153 {
2154 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2155 if (buttons == NULL)
2156 error = TRUE;
2157 if (argvars[2].v_type != VAR_UNKNOWN)
2158 {
2159 def = (int)get_tv_number_chk(&argvars[2], &error);
2160 if (argvars[3].v_type != VAR_UNKNOWN)
2161 {
2162 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2163 if (typestr == NULL)
2164 error = TRUE;
2165 else
2166 {
2167 switch (TOUPPER_ASC(*typestr))
2168 {
2169 case 'E': type = VIM_ERROR; break;
2170 case 'Q': type = VIM_QUESTION; break;
2171 case 'I': type = VIM_INFO; break;
2172 case 'W': type = VIM_WARNING; break;
2173 case 'G': type = VIM_GENERIC; break;
2174 }
2175 }
2176 }
2177 }
2178 }
2179
2180 if (buttons == NULL || *buttons == NUL)
2181 buttons = (char_u *)_("&Ok");
2182
2183 if (!error)
2184 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2185 def, NULL, FALSE);
2186#endif
2187}
2188
2189/*
2190 * "copy()" function
2191 */
2192 static void
2193f_copy(typval_T *argvars, typval_T *rettv)
2194{
2195 item_copy(&argvars[0], rettv, FALSE, 0);
2196}
2197
2198#ifdef FEAT_FLOAT
2199/*
2200 * "cos()" function
2201 */
2202 static void
2203f_cos(typval_T *argvars, typval_T *rettv)
2204{
2205 float_T f = 0.0;
2206
2207 rettv->v_type = VAR_FLOAT;
2208 if (get_float_arg(argvars, &f) == OK)
2209 rettv->vval.v_float = cos(f);
2210 else
2211 rettv->vval.v_float = 0.0;
2212}
2213
2214/*
2215 * "cosh()" function
2216 */
2217 static void
2218f_cosh(typval_T *argvars, typval_T *rettv)
2219{
2220 float_T f = 0.0;
2221
2222 rettv->v_type = VAR_FLOAT;
2223 if (get_float_arg(argvars, &f) == OK)
2224 rettv->vval.v_float = cosh(f);
2225 else
2226 rettv->vval.v_float = 0.0;
2227}
2228#endif
2229
2230/*
2231 * "count()" function
2232 */
2233 static void
2234f_count(typval_T *argvars, typval_T *rettv)
2235{
2236 long n = 0;
2237 int ic = FALSE;
2238
2239 if (argvars[0].v_type == VAR_LIST)
2240 {
2241 listitem_T *li;
2242 list_T *l;
2243 long idx;
2244
2245 if ((l = argvars[0].vval.v_list) != NULL)
2246 {
2247 li = l->lv_first;
2248 if (argvars[2].v_type != VAR_UNKNOWN)
2249 {
2250 int error = FALSE;
2251
2252 ic = (int)get_tv_number_chk(&argvars[2], &error);
2253 if (argvars[3].v_type != VAR_UNKNOWN)
2254 {
2255 idx = (long)get_tv_number_chk(&argvars[3], &error);
2256 if (!error)
2257 {
2258 li = list_find(l, idx);
2259 if (li == NULL)
2260 EMSGN(_(e_listidx), idx);
2261 }
2262 }
2263 if (error)
2264 li = NULL;
2265 }
2266
2267 for ( ; li != NULL; li = li->li_next)
2268 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2269 ++n;
2270 }
2271 }
2272 else if (argvars[0].v_type == VAR_DICT)
2273 {
2274 int todo;
2275 dict_T *d;
2276 hashitem_T *hi;
2277
2278 if ((d = argvars[0].vval.v_dict) != NULL)
2279 {
2280 int error = FALSE;
2281
2282 if (argvars[2].v_type != VAR_UNKNOWN)
2283 {
2284 ic = (int)get_tv_number_chk(&argvars[2], &error);
2285 if (argvars[3].v_type != VAR_UNKNOWN)
2286 EMSG(_(e_invarg));
2287 }
2288
2289 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2290 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2291 {
2292 if (!HASHITEM_EMPTY(hi))
2293 {
2294 --todo;
2295 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2296 ++n;
2297 }
2298 }
2299 }
2300 }
2301 else
2302 EMSG2(_(e_listdictarg), "count()");
2303 rettv->vval.v_number = n;
2304}
2305
2306/*
2307 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2308 *
2309 * Checks the existence of a cscope connection.
2310 */
2311 static void
2312f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2313{
2314#ifdef FEAT_CSCOPE
2315 int num = 0;
2316 char_u *dbpath = NULL;
2317 char_u *prepend = NULL;
2318 char_u buf[NUMBUFLEN];
2319
2320 if (argvars[0].v_type != VAR_UNKNOWN
2321 && argvars[1].v_type != VAR_UNKNOWN)
2322 {
2323 num = (int)get_tv_number(&argvars[0]);
2324 dbpath = get_tv_string(&argvars[1]);
2325 if (argvars[2].v_type != VAR_UNKNOWN)
2326 prepend = get_tv_string_buf(&argvars[2], buf);
2327 }
2328
2329 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2330#endif
2331}
2332
2333/*
2334 * "cursor(lnum, col)" function, or
2335 * "cursor(list)"
2336 *
2337 * Moves the cursor to the specified line and column.
2338 * Returns 0 when the position could be set, -1 otherwise.
2339 */
2340 static void
2341f_cursor(typval_T *argvars, typval_T *rettv)
2342{
2343 long line, col;
2344#ifdef FEAT_VIRTUALEDIT
2345 long coladd = 0;
2346#endif
2347 int set_curswant = TRUE;
2348
2349 rettv->vval.v_number = -1;
2350 if (argvars[1].v_type == VAR_UNKNOWN)
2351 {
2352 pos_T pos;
2353 colnr_T curswant = -1;
2354
2355 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2356 {
2357 EMSG(_(e_invarg));
2358 return;
2359 }
2360 line = pos.lnum;
2361 col = pos.col;
2362#ifdef FEAT_VIRTUALEDIT
2363 coladd = pos.coladd;
2364#endif
2365 if (curswant >= 0)
2366 {
2367 curwin->w_curswant = curswant - 1;
2368 set_curswant = FALSE;
2369 }
2370 }
2371 else
2372 {
2373 line = get_tv_lnum(argvars);
2374 col = (long)get_tv_number_chk(&argvars[1], NULL);
2375#ifdef FEAT_VIRTUALEDIT
2376 if (argvars[2].v_type != VAR_UNKNOWN)
2377 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2378#endif
2379 }
2380 if (line < 0 || col < 0
2381#ifdef FEAT_VIRTUALEDIT
2382 || coladd < 0
2383#endif
2384 )
2385 return; /* type error; errmsg already given */
2386 if (line > 0)
2387 curwin->w_cursor.lnum = line;
2388 if (col > 0)
2389 curwin->w_cursor.col = col - 1;
2390#ifdef FEAT_VIRTUALEDIT
2391 curwin->w_cursor.coladd = coladd;
2392#endif
2393
2394 /* Make sure the cursor is in a valid position. */
2395 check_cursor();
2396#ifdef FEAT_MBYTE
2397 /* Correct cursor for multi-byte character. */
2398 if (has_mbyte)
2399 mb_adjust_cursor();
2400#endif
2401
2402 curwin->w_set_curswant = set_curswant;
2403 rettv->vval.v_number = 0;
2404}
2405
2406/*
2407 * "deepcopy()" function
2408 */
2409 static void
2410f_deepcopy(typval_T *argvars, typval_T *rettv)
2411{
2412 int noref = 0;
2413 int copyID;
2414
2415 if (argvars[1].v_type != VAR_UNKNOWN)
2416 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2417 if (noref < 0 || noref > 1)
2418 EMSG(_(e_invarg));
2419 else
2420 {
2421 copyID = get_copyID();
2422 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2423 }
2424}
2425
2426/*
2427 * "delete()" function
2428 */
2429 static void
2430f_delete(typval_T *argvars, typval_T *rettv)
2431{
2432 char_u nbuf[NUMBUFLEN];
2433 char_u *name;
2434 char_u *flags;
2435
2436 rettv->vval.v_number = -1;
2437 if (check_restricted() || check_secure())
2438 return;
2439
2440 name = get_tv_string(&argvars[0]);
2441 if (name == NULL || *name == NUL)
2442 {
2443 EMSG(_(e_invarg));
2444 return;
2445 }
2446
2447 if (argvars[1].v_type != VAR_UNKNOWN)
2448 flags = get_tv_string_buf(&argvars[1], nbuf);
2449 else
2450 flags = (char_u *)"";
2451
2452 if (*flags == NUL)
2453 /* delete a file */
2454 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2455 else if (STRCMP(flags, "d") == 0)
2456 /* delete an empty directory */
2457 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2458 else if (STRCMP(flags, "rf") == 0)
2459 /* delete a directory recursively */
2460 rettv->vval.v_number = delete_recursive(name);
2461 else
2462 EMSG2(_(e_invexpr2), flags);
2463}
2464
2465/*
2466 * "did_filetype()" function
2467 */
2468 static void
2469f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2470{
2471#ifdef FEAT_AUTOCMD
2472 rettv->vval.v_number = did_filetype;
2473#endif
2474}
2475
2476/*
2477 * "diff_filler()" function
2478 */
2479 static void
2480f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2481{
2482#ifdef FEAT_DIFF
2483 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2484#endif
2485}
2486
2487/*
2488 * "diff_hlID()" function
2489 */
2490 static void
2491f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2492{
2493#ifdef FEAT_DIFF
2494 linenr_T lnum = get_tv_lnum(argvars);
2495 static linenr_T prev_lnum = 0;
2496 static int changedtick = 0;
2497 static int fnum = 0;
2498 static int change_start = 0;
2499 static int change_end = 0;
2500 static hlf_T hlID = (hlf_T)0;
2501 int filler_lines;
2502 int col;
2503
2504 if (lnum < 0) /* ignore type error in {lnum} arg */
2505 lnum = 0;
2506 if (lnum != prev_lnum
2507 || changedtick != curbuf->b_changedtick
2508 || fnum != curbuf->b_fnum)
2509 {
2510 /* New line, buffer, change: need to get the values. */
2511 filler_lines = diff_check(curwin, lnum);
2512 if (filler_lines < 0)
2513 {
2514 if (filler_lines == -1)
2515 {
2516 change_start = MAXCOL;
2517 change_end = -1;
2518 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2519 hlID = HLF_ADD; /* added line */
2520 else
2521 hlID = HLF_CHD; /* changed line */
2522 }
2523 else
2524 hlID = HLF_ADD; /* added line */
2525 }
2526 else
2527 hlID = (hlf_T)0;
2528 prev_lnum = lnum;
2529 changedtick = curbuf->b_changedtick;
2530 fnum = curbuf->b_fnum;
2531 }
2532
2533 if (hlID == HLF_CHD || hlID == HLF_TXD)
2534 {
2535 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2536 if (col >= change_start && col <= change_end)
2537 hlID = HLF_TXD; /* changed text */
2538 else
2539 hlID = HLF_CHD; /* changed line */
2540 }
2541 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2542#endif
2543}
2544
2545/*
2546 * "empty({expr})" function
2547 */
2548 static void
2549f_empty(typval_T *argvars, typval_T *rettv)
2550{
2551 int n = FALSE;
2552
2553 switch (argvars[0].v_type)
2554 {
2555 case VAR_STRING:
2556 case VAR_FUNC:
2557 n = argvars[0].vval.v_string == NULL
2558 || *argvars[0].vval.v_string == NUL;
2559 break;
2560 case VAR_PARTIAL:
2561 n = FALSE;
2562 break;
2563 case VAR_NUMBER:
2564 n = argvars[0].vval.v_number == 0;
2565 break;
2566 case VAR_FLOAT:
2567#ifdef FEAT_FLOAT
2568 n = argvars[0].vval.v_float == 0.0;
2569 break;
2570#endif
2571 case VAR_LIST:
2572 n = argvars[0].vval.v_list == NULL
2573 || argvars[0].vval.v_list->lv_first == NULL;
2574 break;
2575 case VAR_DICT:
2576 n = argvars[0].vval.v_dict == NULL
2577 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2578 break;
2579 case VAR_SPECIAL:
2580 n = argvars[0].vval.v_number != VVAL_TRUE;
2581 break;
2582
2583 case VAR_JOB:
2584#ifdef FEAT_JOB_CHANNEL
2585 n = argvars[0].vval.v_job == NULL
2586 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2587 break;
2588#endif
2589 case VAR_CHANNEL:
2590#ifdef FEAT_JOB_CHANNEL
2591 n = argvars[0].vval.v_channel == NULL
2592 || !channel_is_open(argvars[0].vval.v_channel);
2593 break;
2594#endif
2595 case VAR_UNKNOWN:
2596 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
2597 n = TRUE;
2598 break;
2599 }
2600
2601 rettv->vval.v_number = n;
2602}
2603
2604/*
2605 * "escape({string}, {chars})" function
2606 */
2607 static void
2608f_escape(typval_T *argvars, typval_T *rettv)
2609{
2610 char_u buf[NUMBUFLEN];
2611
2612 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2613 get_tv_string_buf(&argvars[1], buf));
2614 rettv->v_type = VAR_STRING;
2615}
2616
2617/*
2618 * "eval()" function
2619 */
2620 static void
2621f_eval(typval_T *argvars, typval_T *rettv)
2622{
2623 char_u *s, *p;
2624
2625 s = get_tv_string_chk(&argvars[0]);
2626 if (s != NULL)
2627 s = skipwhite(s);
2628
2629 p = s;
2630 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2631 {
2632 if (p != NULL && !aborting())
2633 EMSG2(_(e_invexpr2), p);
2634 need_clr_eos = FALSE;
2635 rettv->v_type = VAR_NUMBER;
2636 rettv->vval.v_number = 0;
2637 }
2638 else if (*s != NUL)
2639 EMSG(_(e_trailing));
2640}
2641
2642/*
2643 * "eventhandler()" function
2644 */
2645 static void
2646f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2647{
2648 rettv->vval.v_number = vgetc_busy;
2649}
2650
2651/*
2652 * "executable()" function
2653 */
2654 static void
2655f_executable(typval_T *argvars, typval_T *rettv)
2656{
2657 char_u *name = get_tv_string(&argvars[0]);
2658
2659 /* Check in $PATH and also check directly if there is a directory name. */
2660 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2661 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2662}
2663
2664static garray_T redir_execute_ga;
2665
2666/*
2667 * Append "value[value_len]" to the execute() output.
2668 */
2669 void
2670execute_redir_str(char_u *value, int value_len)
2671{
2672 int len;
2673
2674 if (value_len == -1)
2675 len = (int)STRLEN(value); /* Append the entire string */
2676 else
2677 len = value_len; /* Append only "value_len" characters */
2678 if (ga_grow(&redir_execute_ga, len) == OK)
2679 {
2680 mch_memmove((char *)redir_execute_ga.ga_data
2681 + redir_execute_ga.ga_len, value, len);
2682 redir_execute_ga.ga_len += len;
2683 }
2684}
2685
2686/*
2687 * Get next line from a list.
2688 * Called by do_cmdline() to get the next line.
2689 * Returns allocated string, or NULL for end of function.
2690 */
2691
2692 static char_u *
2693get_list_line(
2694 int c UNUSED,
2695 void *cookie,
2696 int indent UNUSED)
2697{
2698 listitem_T **p = (listitem_T **)cookie;
2699 listitem_T *item = *p;
2700 char_u buf[NUMBUFLEN];
2701 char_u *s;
2702
2703 if (item == NULL)
2704 return NULL;
2705 s = get_tv_string_buf_chk(&item->li_tv, buf);
2706 *p = item->li_next;
2707 return s == NULL ? NULL : vim_strsave(s);
2708}
2709
2710/*
2711 * "execute()" function
2712 */
2713 static void
2714f_execute(typval_T *argvars, typval_T *rettv)
2715{
2716 char_u *cmd = NULL;
2717 list_T *list = NULL;
2718 int save_msg_silent = msg_silent;
2719 int save_emsg_silent = emsg_silent;
2720 int save_emsg_noredir = emsg_noredir;
2721 int save_redir_execute = redir_execute;
2722 garray_T save_ga;
2723
2724 rettv->vval.v_string = NULL;
2725 rettv->v_type = VAR_STRING;
2726
2727 if (argvars[0].v_type == VAR_LIST)
2728 {
2729 list = argvars[0].vval.v_list;
2730 if (list == NULL || list->lv_first == NULL)
2731 /* empty list, no commands, empty output */
2732 return;
2733 ++list->lv_refcount;
2734 }
2735 else
2736 {
2737 cmd = get_tv_string_chk(&argvars[0]);
2738 if (cmd == NULL)
2739 return;
2740 }
2741
2742 if (argvars[1].v_type != VAR_UNKNOWN)
2743 {
2744 char_u buf[NUMBUFLEN];
2745 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2746
2747 if (s == NULL)
2748 return;
2749 if (STRNCMP(s, "silent", 6) == 0)
2750 ++msg_silent;
2751 if (STRCMP(s, "silent!") == 0)
2752 {
2753 emsg_silent = TRUE;
2754 emsg_noredir = TRUE;
2755 }
2756 }
2757 else
2758 ++msg_silent;
2759
2760 if (redir_execute)
2761 save_ga = redir_execute_ga;
2762 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2763 redir_execute = TRUE;
2764
2765 if (cmd != NULL)
2766 do_cmdline_cmd(cmd);
2767 else
2768 {
2769 listitem_T *item = list->lv_first;
2770
2771 do_cmdline(NULL, get_list_line, (void *)&item,
2772 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2773 --list->lv_refcount;
2774 }
2775
2776 rettv->vval.v_string = redir_execute_ga.ga_data;
2777 msg_silent = save_msg_silent;
2778 emsg_silent = save_emsg_silent;
2779 emsg_noredir = save_emsg_noredir;
2780
2781 redir_execute = save_redir_execute;
2782 if (redir_execute)
2783 redir_execute_ga = save_ga;
2784
2785 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2786 * line. Put it back in the first column. */
2787 msg_col = 0;
2788}
2789
2790/*
2791 * "exepath()" function
2792 */
2793 static void
2794f_exepath(typval_T *argvars, typval_T *rettv)
2795{
2796 char_u *p = NULL;
2797
2798 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2799 rettv->v_type = VAR_STRING;
2800 rettv->vval.v_string = p;
2801}
2802
2803/*
2804 * "exists()" function
2805 */
2806 static void
2807f_exists(typval_T *argvars, typval_T *rettv)
2808{
2809 char_u *p;
2810 char_u *name;
2811 int n = FALSE;
2812 int len = 0;
2813
2814 p = get_tv_string(&argvars[0]);
2815 if (*p == '$') /* environment variable */
2816 {
2817 /* first try "normal" environment variables (fast) */
2818 if (mch_getenv(p + 1) != NULL)
2819 n = TRUE;
2820 else
2821 {
2822 /* try expanding things like $VIM and ${HOME} */
2823 p = expand_env_save(p);
2824 if (p != NULL && *p != '$')
2825 n = TRUE;
2826 vim_free(p);
2827 }
2828 }
2829 else if (*p == '&' || *p == '+') /* option */
2830 {
2831 n = (get_option_tv(&p, NULL, TRUE) == OK);
2832 if (*skipwhite(p) != NUL)
2833 n = FALSE; /* trailing garbage */
2834 }
2835 else if (*p == '*') /* internal or user defined function */
2836 {
2837 n = function_exists(p + 1);
2838 }
2839 else if (*p == ':')
2840 {
2841 n = cmd_exists(p + 1);
2842 }
2843 else if (*p == '#')
2844 {
2845#ifdef FEAT_AUTOCMD
2846 if (p[1] == '#')
2847 n = autocmd_supported(p + 2);
2848 else
2849 n = au_exists(p + 1);
2850#endif
2851 }
2852 else /* internal variable */
2853 {
2854 char_u *tofree;
2855 typval_T tv;
2856
2857 /* get_name_len() takes care of expanding curly braces */
2858 name = p;
2859 len = get_name_len(&p, &tofree, TRUE, FALSE);
2860 if (len > 0)
2861 {
2862 if (tofree != NULL)
2863 name = tofree;
2864 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
2865 if (n)
2866 {
2867 /* handle d.key, l[idx], f(expr) */
2868 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
2869 if (n)
2870 clear_tv(&tv);
2871 }
2872 }
2873 if (*p != NUL)
2874 n = FALSE;
2875
2876 vim_free(tofree);
2877 }
2878
2879 rettv->vval.v_number = n;
2880}
2881
2882#ifdef FEAT_FLOAT
2883/*
2884 * "exp()" function
2885 */
2886 static void
2887f_exp(typval_T *argvars, typval_T *rettv)
2888{
2889 float_T f = 0.0;
2890
2891 rettv->v_type = VAR_FLOAT;
2892 if (get_float_arg(argvars, &f) == OK)
2893 rettv->vval.v_float = exp(f);
2894 else
2895 rettv->vval.v_float = 0.0;
2896}
2897#endif
2898
2899/*
2900 * "expand()" function
2901 */
2902 static void
2903f_expand(typval_T *argvars, typval_T *rettv)
2904{
2905 char_u *s;
2906 int len;
2907 char_u *errormsg;
2908 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2909 expand_T xpc;
2910 int error = FALSE;
2911 char_u *result;
2912
2913 rettv->v_type = VAR_STRING;
2914 if (argvars[1].v_type != VAR_UNKNOWN
2915 && argvars[2].v_type != VAR_UNKNOWN
2916 && get_tv_number_chk(&argvars[2], &error)
2917 && !error)
2918 {
2919 rettv->v_type = VAR_LIST;
2920 rettv->vval.v_list = NULL;
2921 }
2922
2923 s = get_tv_string(&argvars[0]);
2924 if (*s == '%' || *s == '#' || *s == '<')
2925 {
2926 ++emsg_off;
2927 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2928 --emsg_off;
2929 if (rettv->v_type == VAR_LIST)
2930 {
2931 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2932 list_append_string(rettv->vval.v_list, result, -1);
2933 else
2934 vim_free(result);
2935 }
2936 else
2937 rettv->vval.v_string = result;
2938 }
2939 else
2940 {
2941 /* When the optional second argument is non-zero, don't remove matches
2942 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
2943 if (argvars[1].v_type != VAR_UNKNOWN
2944 && get_tv_number_chk(&argvars[1], &error))
2945 options |= WILD_KEEP_ALL;
2946 if (!error)
2947 {
2948 ExpandInit(&xpc);
2949 xpc.xp_context = EXPAND_FILES;
2950 if (p_wic)
2951 options += WILD_ICASE;
2952 if (rettv->v_type == VAR_STRING)
2953 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2954 options, WILD_ALL);
2955 else if (rettv_list_alloc(rettv) != FAIL)
2956 {
2957 int i;
2958
2959 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2960 for (i = 0; i < xpc.xp_numfiles; i++)
2961 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2962 ExpandCleanup(&xpc);
2963 }
2964 }
2965 else
2966 rettv->vval.v_string = NULL;
2967 }
2968}
2969
2970/*
2971 * "extend(list, list [, idx])" function
2972 * "extend(dict, dict [, action])" function
2973 */
2974 static void
2975f_extend(typval_T *argvars, typval_T *rettv)
2976{
2977 char_u *arg_errmsg = (char_u *)N_("extend() argument");
2978
2979 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2980 {
2981 list_T *l1, *l2;
2982 listitem_T *item;
2983 long before;
2984 int error = FALSE;
2985
2986 l1 = argvars[0].vval.v_list;
2987 l2 = argvars[1].vval.v_list;
2988 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
2989 && l2 != NULL)
2990 {
2991 if (argvars[2].v_type != VAR_UNKNOWN)
2992 {
2993 before = (long)get_tv_number_chk(&argvars[2], &error);
2994 if (error)
2995 return; /* type error; errmsg already given */
2996
2997 if (before == l1->lv_len)
2998 item = NULL;
2999 else
3000 {
3001 item = list_find(l1, before);
3002 if (item == NULL)
3003 {
3004 EMSGN(_(e_listidx), before);
3005 return;
3006 }
3007 }
3008 }
3009 else
3010 item = NULL;
3011 list_extend(l1, l2, item);
3012
3013 copy_tv(&argvars[0], rettv);
3014 }
3015 }
3016 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3017 {
3018 dict_T *d1, *d2;
3019 char_u *action;
3020 int i;
3021
3022 d1 = argvars[0].vval.v_dict;
3023 d2 = argvars[1].vval.v_dict;
3024 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3025 && d2 != NULL)
3026 {
3027 /* Check the third argument. */
3028 if (argvars[2].v_type != VAR_UNKNOWN)
3029 {
3030 static char *(av[]) = {"keep", "force", "error"};
3031
3032 action = get_tv_string_chk(&argvars[2]);
3033 if (action == NULL)
3034 return; /* type error; errmsg already given */
3035 for (i = 0; i < 3; ++i)
3036 if (STRCMP(action, av[i]) == 0)
3037 break;
3038 if (i == 3)
3039 {
3040 EMSG2(_(e_invarg2), action);
3041 return;
3042 }
3043 }
3044 else
3045 action = (char_u *)"force";
3046
3047 dict_extend(d1, d2, action);
3048
3049 copy_tv(&argvars[0], rettv);
3050 }
3051 }
3052 else
3053 EMSG2(_(e_listdictarg), "extend()");
3054}
3055
3056/*
3057 * "feedkeys()" function
3058 */
3059 static void
3060f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3061{
3062 int remap = TRUE;
3063 int insert = FALSE;
3064 char_u *keys, *flags;
3065 char_u nbuf[NUMBUFLEN];
3066 int typed = FALSE;
3067 int execute = FALSE;
3068 int dangerous = FALSE;
3069 char_u *keys_esc;
3070
3071 /* This is not allowed in the sandbox. If the commands would still be
3072 * executed in the sandbox it would be OK, but it probably happens later,
3073 * when "sandbox" is no longer set. */
3074 if (check_secure())
3075 return;
3076
3077 keys = get_tv_string(&argvars[0]);
3078
3079 if (argvars[1].v_type != VAR_UNKNOWN)
3080 {
3081 flags = get_tv_string_buf(&argvars[1], nbuf);
3082 for ( ; *flags != NUL; ++flags)
3083 {
3084 switch (*flags)
3085 {
3086 case 'n': remap = FALSE; break;
3087 case 'm': remap = TRUE; break;
3088 case 't': typed = TRUE; break;
3089 case 'i': insert = TRUE; break;
3090 case 'x': execute = TRUE; break;
3091 case '!': dangerous = TRUE; break;
3092 }
3093 }
3094 }
3095
3096 if (*keys != NUL || execute)
3097 {
3098 /* Need to escape K_SPECIAL and CSI before putting the string in the
3099 * typeahead buffer. */
3100 keys_esc = vim_strsave_escape_csi(keys);
3101 if (keys_esc != NULL)
3102 {
3103 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3104 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3105 vim_free(keys_esc);
3106 if (vgetc_busy)
3107 typebuf_was_filled = TRUE;
3108 if (execute)
3109 {
3110 int save_msg_scroll = msg_scroll;
3111
3112 /* Avoid a 1 second delay when the keys start Insert mode. */
3113 msg_scroll = FALSE;
3114
3115 if (!dangerous)
3116 ++ex_normal_busy;
3117 exec_normal(TRUE);
3118 if (!dangerous)
3119 --ex_normal_busy;
3120 msg_scroll |= save_msg_scroll;
3121 }
3122 }
3123 }
3124}
3125
3126/*
3127 * "filereadable()" function
3128 */
3129 static void
3130f_filereadable(typval_T *argvars, typval_T *rettv)
3131{
3132 int fd;
3133 char_u *p;
3134 int n;
3135
3136#ifndef O_NONBLOCK
3137# define O_NONBLOCK 0
3138#endif
3139 p = get_tv_string(&argvars[0]);
3140 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3141 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3142 {
3143 n = TRUE;
3144 close(fd);
3145 }
3146 else
3147 n = FALSE;
3148
3149 rettv->vval.v_number = n;
3150}
3151
3152/*
3153 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3154 * rights to write into.
3155 */
3156 static void
3157f_filewritable(typval_T *argvars, typval_T *rettv)
3158{
3159 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3160}
3161
3162 static void
3163findfilendir(
3164 typval_T *argvars UNUSED,
3165 typval_T *rettv,
3166 int find_what UNUSED)
3167{
3168#ifdef FEAT_SEARCHPATH
3169 char_u *fname;
3170 char_u *fresult = NULL;
3171 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3172 char_u *p;
3173 char_u pathbuf[NUMBUFLEN];
3174 int count = 1;
3175 int first = TRUE;
3176 int error = FALSE;
3177#endif
3178
3179 rettv->vval.v_string = NULL;
3180 rettv->v_type = VAR_STRING;
3181
3182#ifdef FEAT_SEARCHPATH
3183 fname = get_tv_string(&argvars[0]);
3184
3185 if (argvars[1].v_type != VAR_UNKNOWN)
3186 {
3187 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3188 if (p == NULL)
3189 error = TRUE;
3190 else
3191 {
3192 if (*p != NUL)
3193 path = p;
3194
3195 if (argvars[2].v_type != VAR_UNKNOWN)
3196 count = (int)get_tv_number_chk(&argvars[2], &error);
3197 }
3198 }
3199
3200 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3201 error = TRUE;
3202
3203 if (*fname != NUL && !error)
3204 {
3205 do
3206 {
3207 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3208 vim_free(fresult);
3209 fresult = find_file_in_path_option(first ? fname : NULL,
3210 first ? (int)STRLEN(fname) : 0,
3211 0, first, path,
3212 find_what,
3213 curbuf->b_ffname,
3214 find_what == FINDFILE_DIR
3215 ? (char_u *)"" : curbuf->b_p_sua);
3216 first = FALSE;
3217
3218 if (fresult != NULL && rettv->v_type == VAR_LIST)
3219 list_append_string(rettv->vval.v_list, fresult, -1);
3220
3221 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3222 }
3223
3224 if (rettv->v_type == VAR_STRING)
3225 rettv->vval.v_string = fresult;
3226#endif
3227}
3228
3229/*
3230 * "filter()" function
3231 */
3232 static void
3233f_filter(typval_T *argvars, typval_T *rettv)
3234{
3235 filter_map(argvars, rettv, FALSE);
3236}
3237
3238/*
3239 * "finddir({fname}[, {path}[, {count}]])" function
3240 */
3241 static void
3242f_finddir(typval_T *argvars, typval_T *rettv)
3243{
3244 findfilendir(argvars, rettv, FINDFILE_DIR);
3245}
3246
3247/*
3248 * "findfile({fname}[, {path}[, {count}]])" function
3249 */
3250 static void
3251f_findfile(typval_T *argvars, typval_T *rettv)
3252{
3253 findfilendir(argvars, rettv, FINDFILE_FILE);
3254}
3255
3256#ifdef FEAT_FLOAT
3257/*
3258 * "float2nr({float})" function
3259 */
3260 static void
3261f_float2nr(typval_T *argvars, typval_T *rettv)
3262{
3263 float_T f = 0.0;
3264
3265 if (get_float_arg(argvars, &f) == OK)
3266 {
3267# ifdef FEAT_NUM64
3268 if (f < -0x7fffffffffffffff)
3269 rettv->vval.v_number = -0x7fffffffffffffff;
3270 else if (f > 0x7fffffffffffffff)
3271 rettv->vval.v_number = 0x7fffffffffffffff;
3272 else
3273 rettv->vval.v_number = (varnumber_T)f;
3274# else
3275 if (f < -0x7fffffff)
3276 rettv->vval.v_number = -0x7fffffff;
3277 else if (f > 0x7fffffff)
3278 rettv->vval.v_number = 0x7fffffff;
3279 else
3280 rettv->vval.v_number = (varnumber_T)f;
3281# endif
3282 }
3283}
3284
3285/*
3286 * "floor({float})" function
3287 */
3288 static void
3289f_floor(typval_T *argvars, typval_T *rettv)
3290{
3291 float_T f = 0.0;
3292
3293 rettv->v_type = VAR_FLOAT;
3294 if (get_float_arg(argvars, &f) == OK)
3295 rettv->vval.v_float = floor(f);
3296 else
3297 rettv->vval.v_float = 0.0;
3298}
3299
3300/*
3301 * "fmod()" function
3302 */
3303 static void
3304f_fmod(typval_T *argvars, typval_T *rettv)
3305{
3306 float_T fx = 0.0, fy = 0.0;
3307
3308 rettv->v_type = VAR_FLOAT;
3309 if (get_float_arg(argvars, &fx) == OK
3310 && get_float_arg(&argvars[1], &fy) == OK)
3311 rettv->vval.v_float = fmod(fx, fy);
3312 else
3313 rettv->vval.v_float = 0.0;
3314}
3315#endif
3316
3317/*
3318 * "fnameescape({string})" function
3319 */
3320 static void
3321f_fnameescape(typval_T *argvars, typval_T *rettv)
3322{
3323 rettv->vval.v_string = vim_strsave_fnameescape(
3324 get_tv_string(&argvars[0]), FALSE);
3325 rettv->v_type = VAR_STRING;
3326}
3327
3328/*
3329 * "fnamemodify({fname}, {mods})" function
3330 */
3331 static void
3332f_fnamemodify(typval_T *argvars, typval_T *rettv)
3333{
3334 char_u *fname;
3335 char_u *mods;
3336 int usedlen = 0;
3337 int len;
3338 char_u *fbuf = NULL;
3339 char_u buf[NUMBUFLEN];
3340
3341 fname = get_tv_string_chk(&argvars[0]);
3342 mods = get_tv_string_buf_chk(&argvars[1], buf);
3343 if (fname == NULL || mods == NULL)
3344 fname = NULL;
3345 else
3346 {
3347 len = (int)STRLEN(fname);
3348 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3349 }
3350
3351 rettv->v_type = VAR_STRING;
3352 if (fname == NULL)
3353 rettv->vval.v_string = NULL;
3354 else
3355 rettv->vval.v_string = vim_strnsave(fname, len);
3356 vim_free(fbuf);
3357}
3358
3359static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3360
3361/*
3362 * "foldclosed()" function
3363 */
3364 static void
3365foldclosed_both(
3366 typval_T *argvars UNUSED,
3367 typval_T *rettv,
3368 int end UNUSED)
3369{
3370#ifdef FEAT_FOLDING
3371 linenr_T lnum;
3372 linenr_T first, last;
3373
3374 lnum = get_tv_lnum(argvars);
3375 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3376 {
3377 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3378 {
3379 if (end)
3380 rettv->vval.v_number = (varnumber_T)last;
3381 else
3382 rettv->vval.v_number = (varnumber_T)first;
3383 return;
3384 }
3385 }
3386#endif
3387 rettv->vval.v_number = -1;
3388}
3389
3390/*
3391 * "foldclosed()" function
3392 */
3393 static void
3394f_foldclosed(typval_T *argvars, typval_T *rettv)
3395{
3396 foldclosed_both(argvars, rettv, FALSE);
3397}
3398
3399/*
3400 * "foldclosedend()" function
3401 */
3402 static void
3403f_foldclosedend(typval_T *argvars, typval_T *rettv)
3404{
3405 foldclosed_both(argvars, rettv, TRUE);
3406}
3407
3408/*
3409 * "foldlevel()" function
3410 */
3411 static void
3412f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3413{
3414#ifdef FEAT_FOLDING
3415 linenr_T lnum;
3416
3417 lnum = get_tv_lnum(argvars);
3418 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3419 rettv->vval.v_number = foldLevel(lnum);
3420#endif
3421}
3422
3423/*
3424 * "foldtext()" function
3425 */
3426 static void
3427f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3428{
3429#ifdef FEAT_FOLDING
3430 linenr_T foldstart;
3431 linenr_T foldend;
3432 char_u *dashes;
3433 linenr_T lnum;
3434 char_u *s;
3435 char_u *r;
3436 int len;
3437 char *txt;
3438#endif
3439
3440 rettv->v_type = VAR_STRING;
3441 rettv->vval.v_string = NULL;
3442#ifdef FEAT_FOLDING
3443 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3444 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3445 dashes = get_vim_var_str(VV_FOLDDASHES);
3446 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3447 && dashes != NULL)
3448 {
3449 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003450 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003451 if (!linewhite(lnum))
3452 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003453
3454 /* Find interesting text in this line. */
3455 s = skipwhite(ml_get(lnum));
3456 /* skip C comment-start */
3457 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3458 {
3459 s = skipwhite(s + 2);
3460 if (*skipwhite(s) == NUL
3461 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3462 {
3463 s = skipwhite(ml_get(lnum + 1));
3464 if (*s == '*')
3465 s = skipwhite(s + 1);
3466 }
3467 }
3468 txt = _("+-%s%3ld lines: ");
3469 r = alloc((unsigned)(STRLEN(txt)
3470 + STRLEN(dashes) /* for %s */
3471 + 20 /* for %3ld */
3472 + STRLEN(s))); /* concatenated */
3473 if (r != NULL)
3474 {
3475 sprintf((char *)r, txt, dashes, (long)(foldend - foldstart + 1));
3476 len = (int)STRLEN(r);
3477 STRCAT(r, s);
3478 /* remove 'foldmarker' and 'commentstring' */
3479 foldtext_cleanup(r + len);
3480 rettv->vval.v_string = r;
3481 }
3482 }
3483#endif
3484}
3485
3486/*
3487 * "foldtextresult(lnum)" function
3488 */
3489 static void
3490f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3491{
3492#ifdef FEAT_FOLDING
3493 linenr_T lnum;
3494 char_u *text;
3495 char_u buf[51];
3496 foldinfo_T foldinfo;
3497 int fold_count;
3498#endif
3499
3500 rettv->v_type = VAR_STRING;
3501 rettv->vval.v_string = NULL;
3502#ifdef FEAT_FOLDING
3503 lnum = get_tv_lnum(argvars);
3504 /* treat illegal types and illegal string values for {lnum} the same */
3505 if (lnum < 0)
3506 lnum = 0;
3507 fold_count = foldedCount(curwin, lnum, &foldinfo);
3508 if (fold_count > 0)
3509 {
3510 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3511 &foldinfo, buf);
3512 if (text == buf)
3513 text = vim_strsave(text);
3514 rettv->vval.v_string = text;
3515 }
3516#endif
3517}
3518
3519/*
3520 * "foreground()" function
3521 */
3522 static void
3523f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3524{
3525#ifdef FEAT_GUI
3526 if (gui.in_use)
3527 gui_mch_set_foreground();
3528#else
3529# ifdef WIN32
3530 win32_set_foreground();
3531# endif
3532#endif
3533}
3534
3535/*
3536 * "function()" function
3537 */
3538 static void
3539f_function(typval_T *argvars, typval_T *rettv)
3540{
3541 char_u *s;
3542 char_u *name;
3543 int use_string = FALSE;
3544 partial_T *arg_pt = NULL;
3545
3546 if (argvars[0].v_type == VAR_FUNC)
3547 {
3548 /* function(MyFunc, [arg], dict) */
3549 s = argvars[0].vval.v_string;
3550 }
3551 else if (argvars[0].v_type == VAR_PARTIAL
3552 && argvars[0].vval.v_partial != NULL)
3553 {
3554 /* function(dict.MyFunc, [arg]) */
3555 arg_pt = argvars[0].vval.v_partial;
3556 s = arg_pt->pt_name;
3557 }
3558 else
3559 {
3560 /* function('MyFunc', [arg], dict) */
3561 s = get_tv_string(&argvars[0]);
3562 use_string = TRUE;
3563 }
3564
3565 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
3566 EMSG2(_(e_invarg2), s);
3567 /* Don't check an autoload name for existence here. */
3568 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
3569 && !function_exists(s))
3570 EMSG2(_("E700: Unknown function: %s"), s);
3571 else
3572 {
3573 int dict_idx = 0;
3574 int arg_idx = 0;
3575 list_T *list = NULL;
3576
3577 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3578 {
3579 char sid_buf[25];
3580 int off = *s == 's' ? 2 : 5;
3581
3582 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3583 * also be called from another script. Using trans_function_name()
3584 * would also work, but some plugins depend on the name being
3585 * printable text. */
3586 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3587 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3588 if (name != NULL)
3589 {
3590 STRCPY(name, sid_buf);
3591 STRCAT(name, s + off);
3592 }
3593 }
3594 else
3595 name = vim_strsave(s);
3596
3597 if (argvars[1].v_type != VAR_UNKNOWN)
3598 {
3599 if (argvars[2].v_type != VAR_UNKNOWN)
3600 {
3601 /* function(name, [args], dict) */
3602 arg_idx = 1;
3603 dict_idx = 2;
3604 }
3605 else if (argvars[1].v_type == VAR_DICT)
3606 /* function(name, dict) */
3607 dict_idx = 1;
3608 else
3609 /* function(name, [args]) */
3610 arg_idx = 1;
3611 if (dict_idx > 0)
3612 {
3613 if (argvars[dict_idx].v_type != VAR_DICT)
3614 {
3615 EMSG(_("E922: expected a dict"));
3616 vim_free(name);
3617 return;
3618 }
3619 if (argvars[dict_idx].vval.v_dict == NULL)
3620 dict_idx = 0;
3621 }
3622 if (arg_idx > 0)
3623 {
3624 if (argvars[arg_idx].v_type != VAR_LIST)
3625 {
3626 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3627 vim_free(name);
3628 return;
3629 }
3630 list = argvars[arg_idx].vval.v_list;
3631 if (list == NULL || list->lv_len == 0)
3632 arg_idx = 0;
3633 }
3634 }
3635 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
3636 {
3637 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3638
3639 /* result is a VAR_PARTIAL */
3640 if (pt == NULL)
3641 vim_free(name);
3642 else
3643 {
3644 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3645 {
3646 listitem_T *li;
3647 int i = 0;
3648 int arg_len = 0;
3649 int lv_len = 0;
3650
3651 if (arg_pt != NULL)
3652 arg_len = arg_pt->pt_argc;
3653 if (list != NULL)
3654 lv_len = list->lv_len;
3655 pt->pt_argc = arg_len + lv_len;
3656 pt->pt_argv = (typval_T *)alloc(
3657 sizeof(typval_T) * pt->pt_argc);
3658 if (pt->pt_argv == NULL)
3659 {
3660 vim_free(pt);
3661 vim_free(name);
3662 return;
3663 }
3664 else
3665 {
3666 for (i = 0; i < arg_len; i++)
3667 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3668 if (lv_len > 0)
3669 for (li = list->lv_first; li != NULL;
3670 li = li->li_next)
3671 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
3672 }
3673 }
3674
3675 /* For "function(dict.func, [], dict)" and "func" is a partial
3676 * use "dict". That is backwards compatible. */
3677 if (dict_idx > 0)
3678 {
3679 /* The dict is bound explicitly, pt_auto is FALSE. */
3680 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3681 ++pt->pt_dict->dv_refcount;
3682 }
3683 else if (arg_pt != NULL)
3684 {
3685 /* If the dict was bound automatically the result is also
3686 * bound automatically. */
3687 pt->pt_dict = arg_pt->pt_dict;
3688 pt->pt_auto = arg_pt->pt_auto;
3689 if (pt->pt_dict != NULL)
3690 ++pt->pt_dict->dv_refcount;
3691 }
3692
3693 pt->pt_refcount = 1;
3694 pt->pt_name = name;
3695 func_ref(pt->pt_name);
3696 }
3697 rettv->v_type = VAR_PARTIAL;
3698 rettv->vval.v_partial = pt;
3699 }
3700 else
3701 {
3702 /* result is a VAR_FUNC */
3703 rettv->v_type = VAR_FUNC;
3704 rettv->vval.v_string = name;
3705 func_ref(name);
3706 }
3707 }
3708}
3709
3710/*
3711 * "garbagecollect()" function
3712 */
3713 static void
3714f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3715{
3716 /* This is postponed until we are back at the toplevel, because we may be
3717 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3718 want_garbage_collect = TRUE;
3719
3720 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3721 garbage_collect_at_exit = TRUE;
3722}
3723
3724/*
3725 * "get()" function
3726 */
3727 static void
3728f_get(typval_T *argvars, typval_T *rettv)
3729{
3730 listitem_T *li;
3731 list_T *l;
3732 dictitem_T *di;
3733 dict_T *d;
3734 typval_T *tv = NULL;
3735
3736 if (argvars[0].v_type == VAR_LIST)
3737 {
3738 if ((l = argvars[0].vval.v_list) != NULL)
3739 {
3740 int error = FALSE;
3741
3742 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3743 if (!error && li != NULL)
3744 tv = &li->li_tv;
3745 }
3746 }
3747 else if (argvars[0].v_type == VAR_DICT)
3748 {
3749 if ((d = argvars[0].vval.v_dict) != NULL)
3750 {
3751 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3752 if (di != NULL)
3753 tv = &di->di_tv;
3754 }
3755 }
3756 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3757 {
3758 partial_T *pt;
3759 partial_T fref_pt;
3760
3761 if (argvars[0].v_type == VAR_PARTIAL)
3762 pt = argvars[0].vval.v_partial;
3763 else
3764 {
3765 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3766 fref_pt.pt_name = argvars[0].vval.v_string;
3767 pt = &fref_pt;
3768 }
3769
3770 if (pt != NULL)
3771 {
3772 char_u *what = get_tv_string(&argvars[1]);
3773
3774 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3775 {
3776 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
3777 if (pt->pt_name == NULL)
3778 rettv->vval.v_string = NULL;
3779 else
3780 rettv->vval.v_string = vim_strsave(pt->pt_name);
3781 }
3782 else if (STRCMP(what, "dict") == 0)
3783 {
3784 rettv->v_type = VAR_DICT;
3785 rettv->vval.v_dict = pt->pt_dict;
3786 if (pt->pt_dict != NULL)
3787 ++pt->pt_dict->dv_refcount;
3788 }
3789 else if (STRCMP(what, "args") == 0)
3790 {
3791 rettv->v_type = VAR_LIST;
3792 if (rettv_list_alloc(rettv) == OK)
3793 {
3794 int i;
3795
3796 for (i = 0; i < pt->pt_argc; ++i)
3797 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3798 }
3799 }
3800 else
3801 EMSG2(_(e_invarg2), what);
3802 return;
3803 }
3804 }
3805 else
3806 EMSG2(_(e_listdictarg), "get()");
3807
3808 if (tv == NULL)
3809 {
3810 if (argvars[2].v_type != VAR_UNKNOWN)
3811 copy_tv(&argvars[2], rettv);
3812 }
3813 else
3814 copy_tv(tv, rettv);
3815}
3816
3817static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
3818
3819/*
3820 * Get line or list of lines from buffer "buf" into "rettv".
3821 * Return a range (from start to end) of lines in rettv from the specified
3822 * buffer.
3823 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
3824 */
3825 static void
3826get_buffer_lines(
3827 buf_T *buf,
3828 linenr_T start,
3829 linenr_T end,
3830 int retlist,
3831 typval_T *rettv)
3832{
3833 char_u *p;
3834
3835 rettv->v_type = VAR_STRING;
3836 rettv->vval.v_string = NULL;
3837 if (retlist && rettv_list_alloc(rettv) == FAIL)
3838 return;
3839
3840 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
3841 return;
3842
3843 if (!retlist)
3844 {
3845 if (start >= 1 && start <= buf->b_ml.ml_line_count)
3846 p = ml_get_buf(buf, start, FALSE);
3847 else
3848 p = (char_u *)"";
3849 rettv->vval.v_string = vim_strsave(p);
3850 }
3851 else
3852 {
3853 if (end < start)
3854 return;
3855
3856 if (start < 1)
3857 start = 1;
3858 if (end > buf->b_ml.ml_line_count)
3859 end = buf->b_ml.ml_line_count;
3860 while (start <= end)
3861 if (list_append_string(rettv->vval.v_list,
3862 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
3863 break;
3864 }
3865}
3866
3867/*
3868 * Get the lnum from the first argument.
3869 * Also accepts "$", then "buf" is used.
3870 * Returns 0 on error.
3871 */
3872 static linenr_T
3873get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
3874{
3875 if (argvars[0].v_type == VAR_STRING
3876 && argvars[0].vval.v_string != NULL
3877 && argvars[0].vval.v_string[0] == '$'
3878 && buf != NULL)
3879 return buf->b_ml.ml_line_count;
3880 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
3881}
3882
3883/*
3884 * "getbufline()" function
3885 */
3886 static void
3887f_getbufline(typval_T *argvars, typval_T *rettv)
3888{
3889 linenr_T lnum;
3890 linenr_T end;
3891 buf_T *buf;
3892
3893 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
3894 ++emsg_off;
3895 buf = get_buf_tv(&argvars[0], FALSE);
3896 --emsg_off;
3897
3898 lnum = get_tv_lnum_buf(&argvars[1], buf);
3899 if (argvars[2].v_type == VAR_UNKNOWN)
3900 end = lnum;
3901 else
3902 end = get_tv_lnum_buf(&argvars[2], buf);
3903
3904 get_buffer_lines(buf, lnum, end, TRUE, rettv);
3905}
3906
3907/*
3908 * "getbufvar()" function
3909 */
3910 static void
3911f_getbufvar(typval_T *argvars, typval_T *rettv)
3912{
3913 buf_T *buf;
3914 buf_T *save_curbuf;
3915 char_u *varname;
3916 dictitem_T *v;
3917 int done = FALSE;
3918
3919 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
3920 varname = get_tv_string_chk(&argvars[1]);
3921 ++emsg_off;
3922 buf = get_buf_tv(&argvars[0], FALSE);
3923
3924 rettv->v_type = VAR_STRING;
3925 rettv->vval.v_string = NULL;
3926
3927 if (buf != NULL && varname != NULL)
3928 {
3929 /* set curbuf to be our buf, temporarily */
3930 save_curbuf = curbuf;
3931 curbuf = buf;
3932
3933 if (*varname == '&') /* buffer-local-option */
3934 {
3935 if (get_option_tv(&varname, rettv, TRUE) == OK)
3936 done = TRUE;
3937 }
3938 else if (STRCMP(varname, "changedtick") == 0)
3939 {
3940 rettv->v_type = VAR_NUMBER;
3941 rettv->vval.v_number = curbuf->b_changedtick;
3942 done = TRUE;
3943 }
3944 else
3945 {
3946 /* Look up the variable. */
3947 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
3948 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
3949 'b', varname, FALSE);
3950 if (v != NULL)
3951 {
3952 copy_tv(&v->di_tv, rettv);
3953 done = TRUE;
3954 }
3955 }
3956
3957 /* restore previous notion of curbuf */
3958 curbuf = save_curbuf;
3959 }
3960
3961 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3962 /* use the default value */
3963 copy_tv(&argvars[2], rettv);
3964
3965 --emsg_off;
3966}
3967
3968/*
3969 * "getchar()" function
3970 */
3971 static void
3972f_getchar(typval_T *argvars, typval_T *rettv)
3973{
3974 varnumber_T n;
3975 int error = FALSE;
3976
3977 /* Position the cursor. Needed after a message that ends in a space. */
3978 windgoto(msg_row, msg_col);
3979
3980 ++no_mapping;
3981 ++allow_keys;
3982 for (;;)
3983 {
3984 if (argvars[0].v_type == VAR_UNKNOWN)
3985 /* getchar(): blocking wait. */
3986 n = safe_vgetc();
3987 else if (get_tv_number_chk(&argvars[0], &error) == 1)
3988 /* getchar(1): only check if char avail */
3989 n = vpeekc_any();
3990 else if (error || vpeekc_any() == NUL)
3991 /* illegal argument or getchar(0) and no char avail: return zero */
3992 n = 0;
3993 else
3994 /* getchar(0) and char avail: return char */
3995 n = safe_vgetc();
3996
3997 if (n == K_IGNORE)
3998 continue;
3999 break;
4000 }
4001 --no_mapping;
4002 --allow_keys;
4003
4004 set_vim_var_nr(VV_MOUSE_WIN, 0);
4005 set_vim_var_nr(VV_MOUSE_WINID, 0);
4006 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4007 set_vim_var_nr(VV_MOUSE_COL, 0);
4008
4009 rettv->vval.v_number = n;
4010 if (IS_SPECIAL(n) || mod_mask != 0)
4011 {
4012 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4013 int i = 0;
4014
4015 /* Turn a special key into three bytes, plus modifier. */
4016 if (mod_mask != 0)
4017 {
4018 temp[i++] = K_SPECIAL;
4019 temp[i++] = KS_MODIFIER;
4020 temp[i++] = mod_mask;
4021 }
4022 if (IS_SPECIAL(n))
4023 {
4024 temp[i++] = K_SPECIAL;
4025 temp[i++] = K_SECOND(n);
4026 temp[i++] = K_THIRD(n);
4027 }
4028#ifdef FEAT_MBYTE
4029 else if (has_mbyte)
4030 i += (*mb_char2bytes)(n, temp + i);
4031#endif
4032 else
4033 temp[i++] = n;
4034 temp[i++] = NUL;
4035 rettv->v_type = VAR_STRING;
4036 rettv->vval.v_string = vim_strsave(temp);
4037
4038#ifdef FEAT_MOUSE
4039 if (is_mouse_key(n))
4040 {
4041 int row = mouse_row;
4042 int col = mouse_col;
4043 win_T *win;
4044 linenr_T lnum;
4045# ifdef FEAT_WINDOWS
4046 win_T *wp;
4047# endif
4048 int winnr = 1;
4049
4050 if (row >= 0 && col >= 0)
4051 {
4052 /* Find the window at the mouse coordinates and compute the
4053 * text position. */
4054 win = mouse_find_win(&row, &col);
4055 (void)mouse_comp_pos(win, &row, &col, &lnum);
4056# ifdef FEAT_WINDOWS
4057 for (wp = firstwin; wp != win; wp = wp->w_next)
4058 ++winnr;
4059# endif
4060 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4061 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4062 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4063 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4064 }
4065 }
4066#endif
4067 }
4068}
4069
4070/*
4071 * "getcharmod()" function
4072 */
4073 static void
4074f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4075{
4076 rettv->vval.v_number = mod_mask;
4077}
4078
4079/*
4080 * "getcharsearch()" function
4081 */
4082 static void
4083f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4084{
4085 if (rettv_dict_alloc(rettv) != FAIL)
4086 {
4087 dict_T *dict = rettv->vval.v_dict;
4088
4089 dict_add_nr_str(dict, "char", 0L, last_csearch());
4090 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4091 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4092 }
4093}
4094
4095/*
4096 * "getcmdline()" function
4097 */
4098 static void
4099f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4100{
4101 rettv->v_type = VAR_STRING;
4102 rettv->vval.v_string = get_cmdline_str();
4103}
4104
4105/*
4106 * "getcmdpos()" function
4107 */
4108 static void
4109f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4110{
4111 rettv->vval.v_number = get_cmdline_pos() + 1;
4112}
4113
4114/*
4115 * "getcmdtype()" function
4116 */
4117 static void
4118f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4119{
4120 rettv->v_type = VAR_STRING;
4121 rettv->vval.v_string = alloc(2);
4122 if (rettv->vval.v_string != NULL)
4123 {
4124 rettv->vval.v_string[0] = get_cmdline_type();
4125 rettv->vval.v_string[1] = NUL;
4126 }
4127}
4128
4129/*
4130 * "getcmdwintype()" function
4131 */
4132 static void
4133f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4134{
4135 rettv->v_type = VAR_STRING;
4136 rettv->vval.v_string = NULL;
4137#ifdef FEAT_CMDWIN
4138 rettv->vval.v_string = alloc(2);
4139 if (rettv->vval.v_string != NULL)
4140 {
4141 rettv->vval.v_string[0] = cmdwin_type;
4142 rettv->vval.v_string[1] = NUL;
4143 }
4144#endif
4145}
4146
4147#if defined(FEAT_CMDL_COMPL)
4148/*
4149 * "getcompletion()" function
4150 */
4151 static void
4152f_getcompletion(typval_T *argvars, typval_T *rettv)
4153{
4154 char_u *pat;
4155 expand_T xpc;
4156 int options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
4157 | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
4158
4159 if (p_wic)
4160 options |= WILD_ICASE;
4161
4162 ExpandInit(&xpc);
4163 xpc.xp_pattern = get_tv_string(&argvars[0]);
4164 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4165 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4166 if (xpc.xp_context == EXPAND_NOTHING)
4167 {
4168 if (argvars[1].v_type == VAR_STRING)
4169 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4170 else
4171 EMSG(_(e_invarg));
4172 return;
4173 }
4174
4175# if defined(FEAT_MENU)
4176 if (xpc.xp_context == EXPAND_MENUS)
4177 {
4178 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4179 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4180 }
4181# endif
4182
4183 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4184 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4185 {
4186 int i;
4187
4188 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4189
4190 for (i = 0; i < xpc.xp_numfiles; i++)
4191 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4192 }
4193 vim_free(pat);
4194 ExpandCleanup(&xpc);
4195}
4196#endif
4197
4198/*
4199 * "getcwd()" function
4200 */
4201 static void
4202f_getcwd(typval_T *argvars, typval_T *rettv)
4203{
4204 win_T *wp = NULL;
4205 char_u *cwd;
4206
4207 rettv->v_type = VAR_STRING;
4208 rettv->vval.v_string = NULL;
4209
4210 wp = find_tabwin(&argvars[0], &argvars[1]);
4211 if (wp != NULL)
4212 {
4213 if (wp->w_localdir != NULL)
4214 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4215 else if (globaldir != NULL)
4216 rettv->vval.v_string = vim_strsave(globaldir);
4217 else
4218 {
4219 cwd = alloc(MAXPATHL);
4220 if (cwd != NULL)
4221 {
4222 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4223 rettv->vval.v_string = vim_strsave(cwd);
4224 vim_free(cwd);
4225 }
4226 }
4227#ifdef BACKSLASH_IN_FILENAME
4228 if (rettv->vval.v_string != NULL)
4229 slash_adjust(rettv->vval.v_string);
4230#endif
4231 }
4232}
4233
4234/*
4235 * "getfontname()" function
4236 */
4237 static void
4238f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4239{
4240 rettv->v_type = VAR_STRING;
4241 rettv->vval.v_string = NULL;
4242#ifdef FEAT_GUI
4243 if (gui.in_use)
4244 {
4245 GuiFont font;
4246 char_u *name = NULL;
4247
4248 if (argvars[0].v_type == VAR_UNKNOWN)
4249 {
4250 /* Get the "Normal" font. Either the name saved by
4251 * hl_set_font_name() or from the font ID. */
4252 font = gui.norm_font;
4253 name = hl_get_font_name();
4254 }
4255 else
4256 {
4257 name = get_tv_string(&argvars[0]);
4258 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4259 return;
4260 font = gui_mch_get_font(name, FALSE);
4261 if (font == NOFONT)
4262 return; /* Invalid font name, return empty string. */
4263 }
4264 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4265 if (argvars[0].v_type != VAR_UNKNOWN)
4266 gui_mch_free_font(font);
4267 }
4268#endif
4269}
4270
4271/*
4272 * "getfperm({fname})" function
4273 */
4274 static void
4275f_getfperm(typval_T *argvars, typval_T *rettv)
4276{
4277 char_u *fname;
4278 stat_T st;
4279 char_u *perm = NULL;
4280 char_u flags[] = "rwx";
4281 int i;
4282
4283 fname = get_tv_string(&argvars[0]);
4284
4285 rettv->v_type = VAR_STRING;
4286 if (mch_stat((char *)fname, &st) >= 0)
4287 {
4288 perm = vim_strsave((char_u *)"---------");
4289 if (perm != NULL)
4290 {
4291 for (i = 0; i < 9; i++)
4292 {
4293 if (st.st_mode & (1 << (8 - i)))
4294 perm[i] = flags[i % 3];
4295 }
4296 }
4297 }
4298 rettv->vval.v_string = perm;
4299}
4300
4301/*
4302 * "getfsize({fname})" function
4303 */
4304 static void
4305f_getfsize(typval_T *argvars, typval_T *rettv)
4306{
4307 char_u *fname;
4308 stat_T st;
4309
4310 fname = get_tv_string(&argvars[0]);
4311
4312 rettv->v_type = VAR_NUMBER;
4313
4314 if (mch_stat((char *)fname, &st) >= 0)
4315 {
4316 if (mch_isdir(fname))
4317 rettv->vval.v_number = 0;
4318 else
4319 {
4320 rettv->vval.v_number = (varnumber_T)st.st_size;
4321
4322 /* non-perfect check for overflow */
4323 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4324 rettv->vval.v_number = -2;
4325 }
4326 }
4327 else
4328 rettv->vval.v_number = -1;
4329}
4330
4331/*
4332 * "getftime({fname})" function
4333 */
4334 static void
4335f_getftime(typval_T *argvars, typval_T *rettv)
4336{
4337 char_u *fname;
4338 stat_T st;
4339
4340 fname = get_tv_string(&argvars[0]);
4341
4342 if (mch_stat((char *)fname, &st) >= 0)
4343 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4344 else
4345 rettv->vval.v_number = -1;
4346}
4347
4348/*
4349 * "getftype({fname})" function
4350 */
4351 static void
4352f_getftype(typval_T *argvars, typval_T *rettv)
4353{
4354 char_u *fname;
4355 stat_T st;
4356 char_u *type = NULL;
4357 char *t;
4358
4359 fname = get_tv_string(&argvars[0]);
4360
4361 rettv->v_type = VAR_STRING;
4362 if (mch_lstat((char *)fname, &st) >= 0)
4363 {
4364#ifdef S_ISREG
4365 if (S_ISREG(st.st_mode))
4366 t = "file";
4367 else if (S_ISDIR(st.st_mode))
4368 t = "dir";
4369# ifdef S_ISLNK
4370 else if (S_ISLNK(st.st_mode))
4371 t = "link";
4372# endif
4373# ifdef S_ISBLK
4374 else if (S_ISBLK(st.st_mode))
4375 t = "bdev";
4376# endif
4377# ifdef S_ISCHR
4378 else if (S_ISCHR(st.st_mode))
4379 t = "cdev";
4380# endif
4381# ifdef S_ISFIFO
4382 else if (S_ISFIFO(st.st_mode))
4383 t = "fifo";
4384# endif
4385# ifdef S_ISSOCK
4386 else if (S_ISSOCK(st.st_mode))
4387 t = "fifo";
4388# endif
4389 else
4390 t = "other";
4391#else
4392# ifdef S_IFMT
4393 switch (st.st_mode & S_IFMT)
4394 {
4395 case S_IFREG: t = "file"; break;
4396 case S_IFDIR: t = "dir"; break;
4397# ifdef S_IFLNK
4398 case S_IFLNK: t = "link"; break;
4399# endif
4400# ifdef S_IFBLK
4401 case S_IFBLK: t = "bdev"; break;
4402# endif
4403# ifdef S_IFCHR
4404 case S_IFCHR: t = "cdev"; break;
4405# endif
4406# ifdef S_IFIFO
4407 case S_IFIFO: t = "fifo"; break;
4408# endif
4409# ifdef S_IFSOCK
4410 case S_IFSOCK: t = "socket"; break;
4411# endif
4412 default: t = "other";
4413 }
4414# else
4415 if (mch_isdir(fname))
4416 t = "dir";
4417 else
4418 t = "file";
4419# endif
4420#endif
4421 type = vim_strsave((char_u *)t);
4422 }
4423 rettv->vval.v_string = type;
4424}
4425
4426/*
4427 * "getline(lnum, [end])" function
4428 */
4429 static void
4430f_getline(typval_T *argvars, typval_T *rettv)
4431{
4432 linenr_T lnum;
4433 linenr_T end;
4434 int retlist;
4435
4436 lnum = get_tv_lnum(argvars);
4437 if (argvars[1].v_type == VAR_UNKNOWN)
4438 {
4439 end = 0;
4440 retlist = FALSE;
4441 }
4442 else
4443 {
4444 end = get_tv_lnum(&argvars[1]);
4445 retlist = TRUE;
4446 }
4447
4448 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4449}
4450
4451/*
4452 * "getmatches()" function
4453 */
4454 static void
4455f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4456{
4457#ifdef FEAT_SEARCH_EXTRA
4458 dict_T *dict;
4459 matchitem_T *cur = curwin->w_match_head;
4460 int i;
4461
4462 if (rettv_list_alloc(rettv) == OK)
4463 {
4464 while (cur != NULL)
4465 {
4466 dict = dict_alloc();
4467 if (dict == NULL)
4468 return;
4469 if (cur->match.regprog == NULL)
4470 {
4471 /* match added with matchaddpos() */
4472 for (i = 0; i < MAXPOSMATCH; ++i)
4473 {
4474 llpos_T *llpos;
4475 char buf[6];
4476 list_T *l;
4477
4478 llpos = &cur->pos.pos[i];
4479 if (llpos->lnum == 0)
4480 break;
4481 l = list_alloc();
4482 if (l == NULL)
4483 break;
4484 list_append_number(l, (varnumber_T)llpos->lnum);
4485 if (llpos->col > 0)
4486 {
4487 list_append_number(l, (varnumber_T)llpos->col);
4488 list_append_number(l, (varnumber_T)llpos->len);
4489 }
4490 sprintf(buf, "pos%d", i + 1);
4491 dict_add_list(dict, buf, l);
4492 }
4493 }
4494 else
4495 {
4496 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4497 }
4498 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4499 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4500 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4501# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4502 if (cur->conceal_char)
4503 {
4504 char_u buf[MB_MAXBYTES + 1];
4505
4506 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4507 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4508 }
4509# endif
4510 list_append_dict(rettv->vval.v_list, dict);
4511 cur = cur->next;
4512 }
4513 }
4514#endif
4515}
4516
4517/*
4518 * "getpid()" function
4519 */
4520 static void
4521f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4522{
4523 rettv->vval.v_number = mch_get_pid();
4524}
4525
4526 static void
4527getpos_both(
4528 typval_T *argvars,
4529 typval_T *rettv,
4530 int getcurpos)
4531{
4532 pos_T *fp;
4533 list_T *l;
4534 int fnum = -1;
4535
4536 if (rettv_list_alloc(rettv) == OK)
4537 {
4538 l = rettv->vval.v_list;
4539 if (getcurpos)
4540 fp = &curwin->w_cursor;
4541 else
4542 fp = var2fpos(&argvars[0], TRUE, &fnum);
4543 if (fnum != -1)
4544 list_append_number(l, (varnumber_T)fnum);
4545 else
4546 list_append_number(l, (varnumber_T)0);
4547 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4548 : (varnumber_T)0);
4549 list_append_number(l, (fp != NULL)
4550 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4551 : (varnumber_T)0);
4552 list_append_number(l,
4553#ifdef FEAT_VIRTUALEDIT
4554 (fp != NULL) ? (varnumber_T)fp->coladd :
4555#endif
4556 (varnumber_T)0);
4557 if (getcurpos)
4558 {
4559 update_curswant();
4560 list_append_number(l, curwin->w_curswant == MAXCOL ?
4561 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4562 }
4563 }
4564 else
4565 rettv->vval.v_number = FALSE;
4566}
4567
4568
4569/*
4570 * "getcurpos()" function
4571 */
4572 static void
4573f_getcurpos(typval_T *argvars, typval_T *rettv)
4574{
4575 getpos_both(argvars, rettv, TRUE);
4576}
4577
4578/*
4579 * "getpos(string)" function
4580 */
4581 static void
4582f_getpos(typval_T *argvars, typval_T *rettv)
4583{
4584 getpos_both(argvars, rettv, FALSE);
4585}
4586
4587/*
4588 * "getqflist()" and "getloclist()" functions
4589 */
4590 static void
4591f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4592{
4593#ifdef FEAT_QUICKFIX
4594 win_T *wp;
4595#endif
4596
4597#ifdef FEAT_QUICKFIX
4598 if (rettv_list_alloc(rettv) == OK)
4599 {
4600 wp = NULL;
4601 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
4602 {
4603 wp = find_win_by_nr(&argvars[0], NULL);
4604 if (wp == NULL)
4605 return;
4606 }
4607
4608 (void)get_errorlist(wp, rettv->vval.v_list);
4609 }
4610#endif
4611}
4612
4613/*
4614 * "getreg()" function
4615 */
4616 static void
4617f_getreg(typval_T *argvars, typval_T *rettv)
4618{
4619 char_u *strregname;
4620 int regname;
4621 int arg2 = FALSE;
4622 int return_list = FALSE;
4623 int error = FALSE;
4624
4625 if (argvars[0].v_type != VAR_UNKNOWN)
4626 {
4627 strregname = get_tv_string_chk(&argvars[0]);
4628 error = strregname == NULL;
4629 if (argvars[1].v_type != VAR_UNKNOWN)
4630 {
4631 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4632 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4633 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4634 }
4635 }
4636 else
4637 strregname = get_vim_var_str(VV_REG);
4638
4639 if (error)
4640 return;
4641
4642 regname = (strregname == NULL ? '"' : *strregname);
4643 if (regname == 0)
4644 regname = '"';
4645
4646 if (return_list)
4647 {
4648 rettv->v_type = VAR_LIST;
4649 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4650 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4651 if (rettv->vval.v_list == NULL)
4652 (void)rettv_list_alloc(rettv);
4653 else
4654 ++rettv->vval.v_list->lv_refcount;
4655 }
4656 else
4657 {
4658 rettv->v_type = VAR_STRING;
4659 rettv->vval.v_string = get_reg_contents(regname,
4660 arg2 ? GREG_EXPR_SRC : 0);
4661 }
4662}
4663
4664/*
4665 * "getregtype()" function
4666 */
4667 static void
4668f_getregtype(typval_T *argvars, typval_T *rettv)
4669{
4670 char_u *strregname;
4671 int regname;
4672 char_u buf[NUMBUFLEN + 2];
4673 long reglen = 0;
4674
4675 if (argvars[0].v_type != VAR_UNKNOWN)
4676 {
4677 strregname = get_tv_string_chk(&argvars[0]);
4678 if (strregname == NULL) /* type error; errmsg already given */
4679 {
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = NULL;
4682 return;
4683 }
4684 }
4685 else
4686 /* Default to v:register */
4687 strregname = get_vim_var_str(VV_REG);
4688
4689 regname = (strregname == NULL ? '"' : *strregname);
4690 if (regname == 0)
4691 regname = '"';
4692
4693 buf[0] = NUL;
4694 buf[1] = NUL;
4695 switch (get_reg_type(regname, &reglen))
4696 {
4697 case MLINE: buf[0] = 'V'; break;
4698 case MCHAR: buf[0] = 'v'; break;
4699 case MBLOCK:
4700 buf[0] = Ctrl_V;
4701 sprintf((char *)buf + 1, "%ld", reglen + 1);
4702 break;
4703 }
4704 rettv->v_type = VAR_STRING;
4705 rettv->vval.v_string = vim_strsave(buf);
4706}
4707
4708/*
4709 * "gettabvar()" function
4710 */
4711 static void
4712f_gettabvar(typval_T *argvars, typval_T *rettv)
4713{
4714 win_T *oldcurwin;
4715 tabpage_T *tp, *oldtabpage;
4716 dictitem_T *v;
4717 char_u *varname;
4718 int done = FALSE;
4719
4720 rettv->v_type = VAR_STRING;
4721 rettv->vval.v_string = NULL;
4722
4723 varname = get_tv_string_chk(&argvars[1]);
4724 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
4725 if (tp != NULL && varname != NULL)
4726 {
4727 /* Set tp to be our tabpage, temporarily. Also set the window to the
4728 * first window in the tabpage, otherwise the window is not valid. */
4729 if (switch_win(&oldcurwin, &oldtabpage,
4730 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
4731 == OK)
4732 {
4733 /* look up the variable */
4734 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
4735 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4736 if (v != NULL)
4737 {
4738 copy_tv(&v->di_tv, rettv);
4739 done = TRUE;
4740 }
4741 }
4742
4743 /* restore previous notion of curwin */
4744 restore_win(oldcurwin, oldtabpage, TRUE);
4745 }
4746
4747 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4748 copy_tv(&argvars[2], rettv);
4749}
4750
4751/*
4752 * "gettabwinvar()" function
4753 */
4754 static void
4755f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4756{
4757 getwinvar(argvars, rettv, 1);
4758}
4759
4760/*
4761 * "getwinposx()" function
4762 */
4763 static void
4764f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
4765{
4766 rettv->vval.v_number = -1;
4767#ifdef FEAT_GUI
4768 if (gui.in_use)
4769 {
4770 int x, y;
4771
4772 if (gui_mch_get_winpos(&x, &y) == OK)
4773 rettv->vval.v_number = x;
4774 }
4775#endif
4776}
4777
4778/*
4779 * "win_findbuf()" function
4780 */
4781 static void
4782f_win_findbuf(typval_T *argvars, typval_T *rettv)
4783{
4784 if (rettv_list_alloc(rettv) != FAIL)
4785 win_findbuf(argvars, rettv->vval.v_list);
4786}
4787
4788/*
4789 * "win_getid()" function
4790 */
4791 static void
4792f_win_getid(typval_T *argvars, typval_T *rettv)
4793{
4794 rettv->vval.v_number = win_getid(argvars);
4795}
4796
4797/*
4798 * "win_gotoid()" function
4799 */
4800 static void
4801f_win_gotoid(typval_T *argvars, typval_T *rettv)
4802{
4803 rettv->vval.v_number = win_gotoid(argvars);
4804}
4805
4806/*
4807 * "win_id2tabwin()" function
4808 */
4809 static void
4810f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
4811{
4812 if (rettv_list_alloc(rettv) != FAIL)
4813 win_id2tabwin(argvars, rettv->vval.v_list);
4814}
4815
4816/*
4817 * "win_id2win()" function
4818 */
4819 static void
4820f_win_id2win(typval_T *argvars, typval_T *rettv)
4821{
4822 rettv->vval.v_number = win_id2win(argvars);
4823}
4824
4825/*
4826 * "getwinposy()" function
4827 */
4828 static void
4829f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
4830{
4831 rettv->vval.v_number = -1;
4832#ifdef FEAT_GUI
4833 if (gui.in_use)
4834 {
4835 int x, y;
4836
4837 if (gui_mch_get_winpos(&x, &y) == OK)
4838 rettv->vval.v_number = y;
4839 }
4840#endif
4841}
4842
4843/*
4844 * "getwinvar()" function
4845 */
4846 static void
4847f_getwinvar(typval_T *argvars, typval_T *rettv)
4848{
4849 getwinvar(argvars, rettv, 0);
4850}
4851
4852/*
4853 * "glob()" function
4854 */
4855 static void
4856f_glob(typval_T *argvars, typval_T *rettv)
4857{
4858 int options = WILD_SILENT|WILD_USE_NL;
4859 expand_T xpc;
4860 int error = FALSE;
4861
4862 /* When the optional second argument is non-zero, don't remove matches
4863 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
4864 rettv->v_type = VAR_STRING;
4865 if (argvars[1].v_type != VAR_UNKNOWN)
4866 {
4867 if (get_tv_number_chk(&argvars[1], &error))
4868 options |= WILD_KEEP_ALL;
4869 if (argvars[2].v_type != VAR_UNKNOWN)
4870 {
4871 if (get_tv_number_chk(&argvars[2], &error))
4872 {
4873 rettv->v_type = VAR_LIST;
4874 rettv->vval.v_list = NULL;
4875 }
4876 if (argvars[3].v_type != VAR_UNKNOWN
4877 && get_tv_number_chk(&argvars[3], &error))
4878 options |= WILD_ALLLINKS;
4879 }
4880 }
4881 if (!error)
4882 {
4883 ExpandInit(&xpc);
4884 xpc.xp_context = EXPAND_FILES;
4885 if (p_wic)
4886 options += WILD_ICASE;
4887 if (rettv->v_type == VAR_STRING)
4888 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
4889 NULL, options, WILD_ALL);
4890 else if (rettv_list_alloc(rettv) != FAIL)
4891 {
4892 int i;
4893
4894 ExpandOne(&xpc, get_tv_string(&argvars[0]),
4895 NULL, options, WILD_ALL_KEEP);
4896 for (i = 0; i < xpc.xp_numfiles; i++)
4897 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4898
4899 ExpandCleanup(&xpc);
4900 }
4901 }
4902 else
4903 rettv->vval.v_string = NULL;
4904}
4905
4906/*
4907 * "globpath()" function
4908 */
4909 static void
4910f_globpath(typval_T *argvars, typval_T *rettv)
4911{
4912 int flags = 0;
4913 char_u buf1[NUMBUFLEN];
4914 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
4915 int error = FALSE;
4916 garray_T ga;
4917 int i;
4918
4919 /* When the optional second argument is non-zero, don't remove matches
4920 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
4921 rettv->v_type = VAR_STRING;
4922 if (argvars[2].v_type != VAR_UNKNOWN)
4923 {
4924 if (get_tv_number_chk(&argvars[2], &error))
4925 flags |= WILD_KEEP_ALL;
4926 if (argvars[3].v_type != VAR_UNKNOWN)
4927 {
4928 if (get_tv_number_chk(&argvars[3], &error))
4929 {
4930 rettv->v_type = VAR_LIST;
4931 rettv->vval.v_list = NULL;
4932 }
4933 if (argvars[4].v_type != VAR_UNKNOWN
4934 && get_tv_number_chk(&argvars[4], &error))
4935 flags |= WILD_ALLLINKS;
4936 }
4937 }
4938 if (file != NULL && !error)
4939 {
4940 ga_init2(&ga, (int)sizeof(char_u *), 10);
4941 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
4942 if (rettv->v_type == VAR_STRING)
4943 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
4944 else if (rettv_list_alloc(rettv) != FAIL)
4945 for (i = 0; i < ga.ga_len; ++i)
4946 list_append_string(rettv->vval.v_list,
4947 ((char_u **)(ga.ga_data))[i], -1);
4948 ga_clear_strings(&ga);
4949 }
4950 else
4951 rettv->vval.v_string = NULL;
4952}
4953
4954/*
4955 * "glob2regpat()" function
4956 */
4957 static void
4958f_glob2regpat(typval_T *argvars, typval_T *rettv)
4959{
4960 char_u *pat = get_tv_string_chk(&argvars[0]);
4961
4962 rettv->v_type = VAR_STRING;
4963 rettv->vval.v_string = (pat == NULL)
4964 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
4965}
4966
4967/* for VIM_VERSION_ defines */
4968#include "version.h"
4969
4970/*
4971 * "has()" function
4972 */
4973 static void
4974f_has(typval_T *argvars, typval_T *rettv)
4975{
4976 int i;
4977 char_u *name;
4978 int n = FALSE;
4979 static char *(has_list[]) =
4980 {
4981#ifdef AMIGA
4982 "amiga",
4983# ifdef FEAT_ARP
4984 "arp",
4985# endif
4986#endif
4987#ifdef __BEOS__
4988 "beos",
4989#endif
4990#ifdef MACOS
4991 "mac",
4992#endif
4993#if defined(MACOS_X_UNIX)
4994 "macunix", /* built with 'darwin' enabled */
4995#endif
4996#if defined(__APPLE__) && __APPLE__ == 1
4997 "osx", /* built with or without 'darwin' enabled */
4998#endif
4999#ifdef __QNX__
5000 "qnx",
5001#endif
5002#ifdef UNIX
5003 "unix",
5004#endif
5005#ifdef VMS
5006 "vms",
5007#endif
5008#ifdef WIN32
5009 "win32",
5010#endif
5011#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5012 "win32unix",
5013#endif
5014#if defined(WIN64) || defined(_WIN64)
5015 "win64",
5016#endif
5017#ifdef EBCDIC
5018 "ebcdic",
5019#endif
5020#ifndef CASE_INSENSITIVE_FILENAME
5021 "fname_case",
5022#endif
5023#ifdef HAVE_ACL
5024 "acl",
5025#endif
5026#ifdef FEAT_ARABIC
5027 "arabic",
5028#endif
5029#ifdef FEAT_AUTOCMD
5030 "autocmd",
5031#endif
5032#ifdef FEAT_BEVAL
5033 "balloon_eval",
5034# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5035 "balloon_multiline",
5036# endif
5037#endif
5038#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5039 "builtin_terms",
5040# ifdef ALL_BUILTIN_TCAPS
5041 "all_builtin_terms",
5042# endif
5043#endif
5044#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5045 || defined(FEAT_GUI_W32) \
5046 || defined(FEAT_GUI_MOTIF))
5047 "browsefilter",
5048#endif
5049#ifdef FEAT_BYTEOFF
5050 "byte_offset",
5051#endif
5052#ifdef FEAT_JOB_CHANNEL
5053 "channel",
5054#endif
5055#ifdef FEAT_CINDENT
5056 "cindent",
5057#endif
5058#ifdef FEAT_CLIENTSERVER
5059 "clientserver",
5060#endif
5061#ifdef FEAT_CLIPBOARD
5062 "clipboard",
5063#endif
5064#ifdef FEAT_CMDL_COMPL
5065 "cmdline_compl",
5066#endif
5067#ifdef FEAT_CMDHIST
5068 "cmdline_hist",
5069#endif
5070#ifdef FEAT_COMMENTS
5071 "comments",
5072#endif
5073#ifdef FEAT_CONCEAL
5074 "conceal",
5075#endif
5076#ifdef FEAT_CRYPT
5077 "cryptv",
5078 "crypt-blowfish",
5079 "crypt-blowfish2",
5080#endif
5081#ifdef FEAT_CSCOPE
5082 "cscope",
5083#endif
5084#ifdef FEAT_CURSORBIND
5085 "cursorbind",
5086#endif
5087#ifdef CURSOR_SHAPE
5088 "cursorshape",
5089#endif
5090#ifdef DEBUG
5091 "debug",
5092#endif
5093#ifdef FEAT_CON_DIALOG
5094 "dialog_con",
5095#endif
5096#ifdef FEAT_GUI_DIALOG
5097 "dialog_gui",
5098#endif
5099#ifdef FEAT_DIFF
5100 "diff",
5101#endif
5102#ifdef FEAT_DIGRAPHS
5103 "digraphs",
5104#endif
5105#ifdef FEAT_DIRECTX
5106 "directx",
5107#endif
5108#ifdef FEAT_DND
5109 "dnd",
5110#endif
5111#ifdef FEAT_EMACS_TAGS
5112 "emacs_tags",
5113#endif
5114 "eval", /* always present, of course! */
5115 "ex_extra", /* graduated feature */
5116#ifdef FEAT_SEARCH_EXTRA
5117 "extra_search",
5118#endif
5119#ifdef FEAT_FKMAP
5120 "farsi",
5121#endif
5122#ifdef FEAT_SEARCHPATH
5123 "file_in_path",
5124#endif
5125#ifdef FEAT_FILTERPIPE
5126 "filterpipe",
5127#endif
5128#ifdef FEAT_FIND_ID
5129 "find_in_path",
5130#endif
5131#ifdef FEAT_FLOAT
5132 "float",
5133#endif
5134#ifdef FEAT_FOLDING
5135 "folding",
5136#endif
5137#ifdef FEAT_FOOTER
5138 "footer",
5139#endif
5140#if !defined(USE_SYSTEM) && defined(UNIX)
5141 "fork",
5142#endif
5143#ifdef FEAT_GETTEXT
5144 "gettext",
5145#endif
5146#ifdef FEAT_GUI
5147 "gui",
5148#endif
5149#ifdef FEAT_GUI_ATHENA
5150# ifdef FEAT_GUI_NEXTAW
5151 "gui_neXtaw",
5152# else
5153 "gui_athena",
5154# endif
5155#endif
5156#ifdef FEAT_GUI_GTK
5157 "gui_gtk",
5158# ifdef USE_GTK3
5159 "gui_gtk3",
5160# else
5161 "gui_gtk2",
5162# endif
5163#endif
5164#ifdef FEAT_GUI_GNOME
5165 "gui_gnome",
5166#endif
5167#ifdef FEAT_GUI_MAC
5168 "gui_mac",
5169#endif
5170#ifdef FEAT_GUI_MOTIF
5171 "gui_motif",
5172#endif
5173#ifdef FEAT_GUI_PHOTON
5174 "gui_photon",
5175#endif
5176#ifdef FEAT_GUI_W32
5177 "gui_win32",
5178#endif
5179#ifdef FEAT_HANGULIN
5180 "hangul_input",
5181#endif
5182#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5183 "iconv",
5184#endif
5185#ifdef FEAT_INS_EXPAND
5186 "insert_expand",
5187#endif
5188#ifdef FEAT_JOB_CHANNEL
5189 "job",
5190#endif
5191#ifdef FEAT_JUMPLIST
5192 "jumplist",
5193#endif
5194#ifdef FEAT_KEYMAP
5195 "keymap",
5196#endif
5197#ifdef FEAT_LANGMAP
5198 "langmap",
5199#endif
5200#ifdef FEAT_LIBCALL
5201 "libcall",
5202#endif
5203#ifdef FEAT_LINEBREAK
5204 "linebreak",
5205#endif
5206#ifdef FEAT_LISP
5207 "lispindent",
5208#endif
5209#ifdef FEAT_LISTCMDS
5210 "listcmds",
5211#endif
5212#ifdef FEAT_LOCALMAP
5213 "localmap",
5214#endif
5215#ifdef FEAT_LUA
5216# ifndef DYNAMIC_LUA
5217 "lua",
5218# endif
5219#endif
5220#ifdef FEAT_MENU
5221 "menu",
5222#endif
5223#ifdef FEAT_SESSION
5224 "mksession",
5225#endif
5226#ifdef FEAT_MODIFY_FNAME
5227 "modify_fname",
5228#endif
5229#ifdef FEAT_MOUSE
5230 "mouse",
5231#endif
5232#ifdef FEAT_MOUSESHAPE
5233 "mouseshape",
5234#endif
5235#if defined(UNIX) || defined(VMS)
5236# ifdef FEAT_MOUSE_DEC
5237 "mouse_dec",
5238# endif
5239# ifdef FEAT_MOUSE_GPM
5240 "mouse_gpm",
5241# endif
5242# ifdef FEAT_MOUSE_JSB
5243 "mouse_jsbterm",
5244# endif
5245# ifdef FEAT_MOUSE_NET
5246 "mouse_netterm",
5247# endif
5248# ifdef FEAT_MOUSE_PTERM
5249 "mouse_pterm",
5250# endif
5251# ifdef FEAT_MOUSE_SGR
5252 "mouse_sgr",
5253# endif
5254# ifdef FEAT_SYSMOUSE
5255 "mouse_sysmouse",
5256# endif
5257# ifdef FEAT_MOUSE_URXVT
5258 "mouse_urxvt",
5259# endif
5260# ifdef FEAT_MOUSE_XTERM
5261 "mouse_xterm",
5262# endif
5263#endif
5264#ifdef FEAT_MBYTE
5265 "multi_byte",
5266#endif
5267#ifdef FEAT_MBYTE_IME
5268 "multi_byte_ime",
5269#endif
5270#ifdef FEAT_MULTI_LANG
5271 "multi_lang",
5272#endif
5273#ifdef FEAT_MZSCHEME
5274#ifndef DYNAMIC_MZSCHEME
5275 "mzscheme",
5276#endif
5277#endif
5278#ifdef FEAT_NUM64
5279 "num64",
5280#endif
5281#ifdef FEAT_OLE
5282 "ole",
5283#endif
5284 "packages",
5285#ifdef FEAT_PATH_EXTRA
5286 "path_extra",
5287#endif
5288#ifdef FEAT_PERL
5289#ifndef DYNAMIC_PERL
5290 "perl",
5291#endif
5292#endif
5293#ifdef FEAT_PERSISTENT_UNDO
5294 "persistent_undo",
5295#endif
5296#ifdef FEAT_PYTHON
5297#ifndef DYNAMIC_PYTHON
5298 "python",
5299#endif
5300#endif
5301#ifdef FEAT_PYTHON3
5302#ifndef DYNAMIC_PYTHON3
5303 "python3",
5304#endif
5305#endif
5306#ifdef FEAT_POSTSCRIPT
5307 "postscript",
5308#endif
5309#ifdef FEAT_PRINTER
5310 "printer",
5311#endif
5312#ifdef FEAT_PROFILE
5313 "profile",
5314#endif
5315#ifdef FEAT_RELTIME
5316 "reltime",
5317#endif
5318#ifdef FEAT_QUICKFIX
5319 "quickfix",
5320#endif
5321#ifdef FEAT_RIGHTLEFT
5322 "rightleft",
5323#endif
5324#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5325 "ruby",
5326#endif
5327#ifdef FEAT_SCROLLBIND
5328 "scrollbind",
5329#endif
5330#ifdef FEAT_CMDL_INFO
5331 "showcmd",
5332 "cmdline_info",
5333#endif
5334#ifdef FEAT_SIGNS
5335 "signs",
5336#endif
5337#ifdef FEAT_SMARTINDENT
5338 "smartindent",
5339#endif
5340#ifdef STARTUPTIME
5341 "startuptime",
5342#endif
5343#ifdef FEAT_STL_OPT
5344 "statusline",
5345#endif
5346#ifdef FEAT_SUN_WORKSHOP
5347 "sun_workshop",
5348#endif
5349#ifdef FEAT_NETBEANS_INTG
5350 "netbeans_intg",
5351#endif
5352#ifdef FEAT_SPELL
5353 "spell",
5354#endif
5355#ifdef FEAT_SYN_HL
5356 "syntax",
5357#endif
5358#if defined(USE_SYSTEM) || !defined(UNIX)
5359 "system",
5360#endif
5361#ifdef FEAT_TAG_BINS
5362 "tag_binary",
5363#endif
5364#ifdef FEAT_TAG_OLDSTATIC
5365 "tag_old_static",
5366#endif
5367#ifdef FEAT_TAG_ANYWHITE
5368 "tag_any_white",
5369#endif
5370#ifdef FEAT_TCL
5371# ifndef DYNAMIC_TCL
5372 "tcl",
5373# endif
5374#endif
5375#ifdef FEAT_TERMGUICOLORS
5376 "termguicolors",
5377#endif
5378#ifdef TERMINFO
5379 "terminfo",
5380#endif
5381#ifdef FEAT_TERMRESPONSE
5382 "termresponse",
5383#endif
5384#ifdef FEAT_TEXTOBJ
5385 "textobjects",
5386#endif
5387#ifdef HAVE_TGETENT
5388 "tgetent",
5389#endif
5390#ifdef FEAT_TIMERS
5391 "timers",
5392#endif
5393#ifdef FEAT_TITLE
5394 "title",
5395#endif
5396#ifdef FEAT_TOOLBAR
5397 "toolbar",
5398#endif
5399#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5400 "unnamedplus",
5401#endif
5402#ifdef FEAT_USR_CMDS
5403 "user-commands", /* was accidentally included in 5.4 */
5404 "user_commands",
5405#endif
5406#ifdef FEAT_VIMINFO
5407 "viminfo",
5408#endif
5409#ifdef FEAT_WINDOWS
5410 "vertsplit",
5411#endif
5412#ifdef FEAT_VIRTUALEDIT
5413 "virtualedit",
5414#endif
5415 "visual",
5416#ifdef FEAT_VISUALEXTRA
5417 "visualextra",
5418#endif
5419#ifdef FEAT_VREPLACE
5420 "vreplace",
5421#endif
5422#ifdef FEAT_WILDIGN
5423 "wildignore",
5424#endif
5425#ifdef FEAT_WILDMENU
5426 "wildmenu",
5427#endif
5428#ifdef FEAT_WINDOWS
5429 "windows",
5430#endif
5431#ifdef FEAT_WAK
5432 "winaltkeys",
5433#endif
5434#ifdef FEAT_WRITEBACKUP
5435 "writebackup",
5436#endif
5437#ifdef FEAT_XIM
5438 "xim",
5439#endif
5440#ifdef FEAT_XFONTSET
5441 "xfontset",
5442#endif
5443#ifdef FEAT_XPM_W32
5444 "xpm",
5445 "xpm_w32", /* for backward compatibility */
5446#else
5447# if defined(HAVE_XPM)
5448 "xpm",
5449# endif
5450#endif
5451#ifdef USE_XSMP
5452 "xsmp",
5453#endif
5454#ifdef USE_XSMP_INTERACT
5455 "xsmp_interact",
5456#endif
5457#ifdef FEAT_XCLIPBOARD
5458 "xterm_clipboard",
5459#endif
5460#ifdef FEAT_XTERM_SAVE
5461 "xterm_save",
5462#endif
5463#if defined(UNIX) && defined(FEAT_X11)
5464 "X11",
5465#endif
5466 NULL
5467 };
5468
5469 name = get_tv_string(&argvars[0]);
5470 for (i = 0; has_list[i] != NULL; ++i)
5471 if (STRICMP(name, has_list[i]) == 0)
5472 {
5473 n = TRUE;
5474 break;
5475 }
5476
5477 if (n == FALSE)
5478 {
5479 if (STRNICMP(name, "patch", 5) == 0)
5480 {
5481 if (name[5] == '-'
5482 && STRLEN(name) >= 11
5483 && vim_isdigit(name[6])
5484 && vim_isdigit(name[8])
5485 && vim_isdigit(name[10]))
5486 {
5487 int major = atoi((char *)name + 6);
5488 int minor = atoi((char *)name + 8);
5489
5490 /* Expect "patch-9.9.01234". */
5491 n = (major < VIM_VERSION_MAJOR
5492 || (major == VIM_VERSION_MAJOR
5493 && (minor < VIM_VERSION_MINOR
5494 || (minor == VIM_VERSION_MINOR
5495 && has_patch(atoi((char *)name + 10))))));
5496 }
5497 else
5498 n = has_patch(atoi((char *)name + 5));
5499 }
5500 else if (STRICMP(name, "vim_starting") == 0)
5501 n = (starting != 0);
5502#ifdef FEAT_MBYTE
5503 else if (STRICMP(name, "multi_byte_encoding") == 0)
5504 n = has_mbyte;
5505#endif
5506#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
5507 else if (STRICMP(name, "balloon_multiline") == 0)
5508 n = multiline_balloon_available();
5509#endif
5510#ifdef DYNAMIC_TCL
5511 else if (STRICMP(name, "tcl") == 0)
5512 n = tcl_enabled(FALSE);
5513#endif
5514#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5515 else if (STRICMP(name, "iconv") == 0)
5516 n = iconv_enabled(FALSE);
5517#endif
5518#ifdef DYNAMIC_LUA
5519 else if (STRICMP(name, "lua") == 0)
5520 n = lua_enabled(FALSE);
5521#endif
5522#ifdef DYNAMIC_MZSCHEME
5523 else if (STRICMP(name, "mzscheme") == 0)
5524 n = mzscheme_enabled(FALSE);
5525#endif
5526#ifdef DYNAMIC_RUBY
5527 else if (STRICMP(name, "ruby") == 0)
5528 n = ruby_enabled(FALSE);
5529#endif
5530#ifdef FEAT_PYTHON
5531#ifdef DYNAMIC_PYTHON
5532 else if (STRICMP(name, "python") == 0)
5533 n = python_enabled(FALSE);
5534#endif
5535#endif
5536#ifdef FEAT_PYTHON3
5537#ifdef DYNAMIC_PYTHON3
5538 else if (STRICMP(name, "python3") == 0)
5539 n = python3_enabled(FALSE);
5540#endif
5541#endif
5542#ifdef DYNAMIC_PERL
5543 else if (STRICMP(name, "perl") == 0)
5544 n = perl_enabled(FALSE);
5545#endif
5546#ifdef FEAT_GUI
5547 else if (STRICMP(name, "gui_running") == 0)
5548 n = (gui.in_use || gui.starting);
5549# ifdef FEAT_GUI_W32
5550 else if (STRICMP(name, "gui_win32s") == 0)
5551 n = gui_is_win32s();
5552# endif
5553# ifdef FEAT_BROWSE
5554 else if (STRICMP(name, "browse") == 0)
5555 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
5556# endif
5557#endif
5558#ifdef FEAT_SYN_HL
5559 else if (STRICMP(name, "syntax_items") == 0)
5560 n = syntax_present(curwin);
5561#endif
5562#if defined(WIN3264)
5563 else if (STRICMP(name, "win95") == 0)
5564 n = mch_windows95();
5565#endif
5566#ifdef FEAT_NETBEANS_INTG
5567 else if (STRICMP(name, "netbeans_enabled") == 0)
5568 n = netbeans_active();
5569#endif
5570 }
5571
5572 rettv->vval.v_number = n;
5573}
5574
5575/*
5576 * "has_key()" function
5577 */
5578 static void
5579f_has_key(typval_T *argvars, typval_T *rettv)
5580{
5581 if (argvars[0].v_type != VAR_DICT)
5582 {
5583 EMSG(_(e_dictreq));
5584 return;
5585 }
5586 if (argvars[0].vval.v_dict == NULL)
5587 return;
5588
5589 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
5590 get_tv_string(&argvars[1]), -1) != NULL;
5591}
5592
5593/*
5594 * "haslocaldir()" function
5595 */
5596 static void
5597f_haslocaldir(typval_T *argvars, typval_T *rettv)
5598{
5599 win_T *wp = NULL;
5600
5601 wp = find_tabwin(&argvars[0], &argvars[1]);
5602 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
5603}
5604
5605/*
5606 * "hasmapto()" function
5607 */
5608 static void
5609f_hasmapto(typval_T *argvars, typval_T *rettv)
5610{
5611 char_u *name;
5612 char_u *mode;
5613 char_u buf[NUMBUFLEN];
5614 int abbr = FALSE;
5615
5616 name = get_tv_string(&argvars[0]);
5617 if (argvars[1].v_type == VAR_UNKNOWN)
5618 mode = (char_u *)"nvo";
5619 else
5620 {
5621 mode = get_tv_string_buf(&argvars[1], buf);
5622 if (argvars[2].v_type != VAR_UNKNOWN)
5623 abbr = (int)get_tv_number(&argvars[2]);
5624 }
5625
5626 if (map_to_exists(name, mode, abbr))
5627 rettv->vval.v_number = TRUE;
5628 else
5629 rettv->vval.v_number = FALSE;
5630}
5631
5632/*
5633 * "histadd()" function
5634 */
5635 static void
5636f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
5637{
5638#ifdef FEAT_CMDHIST
5639 int histype;
5640 char_u *str;
5641 char_u buf[NUMBUFLEN];
5642#endif
5643
5644 rettv->vval.v_number = FALSE;
5645 if (check_restricted() || check_secure())
5646 return;
5647#ifdef FEAT_CMDHIST
5648 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
5649 histype = str != NULL ? get_histtype(str) : -1;
5650 if (histype >= 0)
5651 {
5652 str = get_tv_string_buf(&argvars[1], buf);
5653 if (*str != NUL)
5654 {
5655 init_history();
5656 add_to_history(histype, str, FALSE, NUL);
5657 rettv->vval.v_number = TRUE;
5658 return;
5659 }
5660 }
5661#endif
5662}
5663
5664/*
5665 * "histdel()" function
5666 */
5667 static void
5668f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5669{
5670#ifdef FEAT_CMDHIST
5671 int n;
5672 char_u buf[NUMBUFLEN];
5673 char_u *str;
5674
5675 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
5676 if (str == NULL)
5677 n = 0;
5678 else if (argvars[1].v_type == VAR_UNKNOWN)
5679 /* only one argument: clear entire history */
5680 n = clr_history(get_histtype(str));
5681 else if (argvars[1].v_type == VAR_NUMBER)
5682 /* index given: remove that entry */
5683 n = del_history_idx(get_histtype(str),
5684 (int)get_tv_number(&argvars[1]));
5685 else
5686 /* string given: remove all matching entries */
5687 n = del_history_entry(get_histtype(str),
5688 get_tv_string_buf(&argvars[1], buf));
5689 rettv->vval.v_number = n;
5690#endif
5691}
5692
5693/*
5694 * "histget()" function
5695 */
5696 static void
5697f_histget(typval_T *argvars UNUSED, typval_T *rettv)
5698{
5699#ifdef FEAT_CMDHIST
5700 int type;
5701 int idx;
5702 char_u *str;
5703
5704 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
5705 if (str == NULL)
5706 rettv->vval.v_string = NULL;
5707 else
5708 {
5709 type = get_histtype(str);
5710 if (argvars[1].v_type == VAR_UNKNOWN)
5711 idx = get_history_idx(type);
5712 else
5713 idx = (int)get_tv_number_chk(&argvars[1], NULL);
5714 /* -1 on type error */
5715 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
5716 }
5717#else
5718 rettv->vval.v_string = NULL;
5719#endif
5720 rettv->v_type = VAR_STRING;
5721}
5722
5723/*
5724 * "histnr()" function
5725 */
5726 static void
5727f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
5728{
5729 int i;
5730
5731#ifdef FEAT_CMDHIST
5732 char_u *history = get_tv_string_chk(&argvars[0]);
5733
5734 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
5735 if (i >= HIST_CMD && i < HIST_COUNT)
5736 i = get_history_idx(i);
5737 else
5738#endif
5739 i = -1;
5740 rettv->vval.v_number = i;
5741}
5742
5743/*
5744 * "highlightID(name)" function
5745 */
5746 static void
5747f_hlID(typval_T *argvars, typval_T *rettv)
5748{
5749 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
5750}
5751
5752/*
5753 * "highlight_exists()" function
5754 */
5755 static void
5756f_hlexists(typval_T *argvars, typval_T *rettv)
5757{
5758 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
5759}
5760
5761/*
5762 * "hostname()" function
5763 */
5764 static void
5765f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5766{
5767 char_u hostname[256];
5768
5769 mch_get_host_name(hostname, 256);
5770 rettv->v_type = VAR_STRING;
5771 rettv->vval.v_string = vim_strsave(hostname);
5772}
5773
5774/*
5775 * iconv() function
5776 */
5777 static void
5778f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5779{
5780#ifdef FEAT_MBYTE
5781 char_u buf1[NUMBUFLEN];
5782 char_u buf2[NUMBUFLEN];
5783 char_u *from, *to, *str;
5784 vimconv_T vimconv;
5785#endif
5786
5787 rettv->v_type = VAR_STRING;
5788 rettv->vval.v_string = NULL;
5789
5790#ifdef FEAT_MBYTE
5791 str = get_tv_string(&argvars[0]);
5792 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
5793 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
5794 vimconv.vc_type = CONV_NONE;
5795 convert_setup(&vimconv, from, to);
5796
5797 /* If the encodings are equal, no conversion needed. */
5798 if (vimconv.vc_type == CONV_NONE)
5799 rettv->vval.v_string = vim_strsave(str);
5800 else
5801 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5802
5803 convert_setup(&vimconv, NULL, NULL);
5804 vim_free(from);
5805 vim_free(to);
5806#endif
5807}
5808
5809/*
5810 * "indent()" function
5811 */
5812 static void
5813f_indent(typval_T *argvars, typval_T *rettv)
5814{
5815 linenr_T lnum;
5816
5817 lnum = get_tv_lnum(argvars);
5818 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5819 rettv->vval.v_number = get_indent_lnum(lnum);
5820 else
5821 rettv->vval.v_number = -1;
5822}
5823
5824/*
5825 * "index()" function
5826 */
5827 static void
5828f_index(typval_T *argvars, typval_T *rettv)
5829{
5830 list_T *l;
5831 listitem_T *item;
5832 long idx = 0;
5833 int ic = FALSE;
5834
5835 rettv->vval.v_number = -1;
5836 if (argvars[0].v_type != VAR_LIST)
5837 {
5838 EMSG(_(e_listreq));
5839 return;
5840 }
5841 l = argvars[0].vval.v_list;
5842 if (l != NULL)
5843 {
5844 item = l->lv_first;
5845 if (argvars[2].v_type != VAR_UNKNOWN)
5846 {
5847 int error = FALSE;
5848
5849 /* Start at specified item. Use the cached index that list_find()
5850 * sets, so that a negative number also works. */
5851 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
5852 idx = l->lv_idx;
5853 if (argvars[3].v_type != VAR_UNKNOWN)
5854 ic = (int)get_tv_number_chk(&argvars[3], &error);
5855 if (error)
5856 item = NULL;
5857 }
5858
5859 for ( ; item != NULL; item = item->li_next, ++idx)
5860 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5861 {
5862 rettv->vval.v_number = idx;
5863 break;
5864 }
5865 }
5866}
5867
5868static int inputsecret_flag = 0;
5869
5870/*
5871 * "input()" function
5872 * Also handles inputsecret() when inputsecret is set.
5873 */
5874 static void
5875f_input(typval_T *argvars, typval_T *rettv)
5876{
5877 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5878}
5879
5880/*
5881 * "inputdialog()" function
5882 */
5883 static void
5884f_inputdialog(typval_T *argvars, typval_T *rettv)
5885{
5886#if defined(FEAT_GUI_TEXTDIALOG)
5887 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
5888 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5889 {
5890 char_u *message;
5891 char_u buf[NUMBUFLEN];
5892 char_u *defstr = (char_u *)"";
5893
5894 message = get_tv_string_chk(&argvars[0]);
5895 if (argvars[1].v_type != VAR_UNKNOWN
5896 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
5897 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5898 else
5899 IObuff[0] = NUL;
5900 if (message != NULL && defstr != NULL
5901 && do_dialog(VIM_QUESTION, NULL, message,
5902 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5903 rettv->vval.v_string = vim_strsave(IObuff);
5904 else
5905 {
5906 if (message != NULL && defstr != NULL
5907 && argvars[1].v_type != VAR_UNKNOWN
5908 && argvars[2].v_type != VAR_UNKNOWN)
5909 rettv->vval.v_string = vim_strsave(
5910 get_tv_string_buf(&argvars[2], buf));
5911 else
5912 rettv->vval.v_string = NULL;
5913 }
5914 rettv->v_type = VAR_STRING;
5915 }
5916 else
5917#endif
5918 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5919}
5920
5921/*
5922 * "inputlist()" function
5923 */
5924 static void
5925f_inputlist(typval_T *argvars, typval_T *rettv)
5926{
5927 listitem_T *li;
5928 int selected;
5929 int mouse_used;
5930
5931#ifdef NO_CONSOLE_INPUT
5932 /* While starting up, there is no place to enter text. */
5933 if (no_console_input())
5934 return;
5935#endif
5936 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5937 {
5938 EMSG2(_(e_listarg), "inputlist()");
5939 return;
5940 }
5941
5942 msg_start();
5943 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
5944 lines_left = Rows; /* avoid more prompt */
5945 msg_scroll = TRUE;
5946 msg_clr_eos();
5947
5948 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
5949 {
5950 msg_puts(get_tv_string(&li->li_tv));
5951 msg_putchar('\n');
5952 }
5953
5954 /* Ask for choice. */
5955 selected = prompt_for_number(&mouse_used);
5956 if (mouse_used)
5957 selected -= lines_left;
5958
5959 rettv->vval.v_number = selected;
5960}
5961
5962
5963static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5964
5965/*
5966 * "inputrestore()" function
5967 */
5968 static void
5969f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5970{
5971 if (ga_userinput.ga_len > 0)
5972 {
5973 --ga_userinput.ga_len;
5974 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5975 + ga_userinput.ga_len);
5976 /* default return is zero == OK */
5977 }
5978 else if (p_verbose > 1)
5979 {
5980 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
5981 rettv->vval.v_number = 1; /* Failed */
5982 }
5983}
5984
5985/*
5986 * "inputsave()" function
5987 */
5988 static void
5989f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5990{
5991 /* Add an entry to the stack of typeahead storage. */
5992 if (ga_grow(&ga_userinput, 1) == OK)
5993 {
5994 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5995 + ga_userinput.ga_len);
5996 ++ga_userinput.ga_len;
5997 /* default return is zero == OK */
5998 }
5999 else
6000 rettv->vval.v_number = 1; /* Failed */
6001}
6002
6003/*
6004 * "inputsecret()" function
6005 */
6006 static void
6007f_inputsecret(typval_T *argvars, typval_T *rettv)
6008{
6009 ++cmdline_star;
6010 ++inputsecret_flag;
6011 f_input(argvars, rettv);
6012 --cmdline_star;
6013 --inputsecret_flag;
6014}
6015
6016/*
6017 * "insert()" function
6018 */
6019 static void
6020f_insert(typval_T *argvars, typval_T *rettv)
6021{
6022 long before = 0;
6023 listitem_T *item;
6024 list_T *l;
6025 int error = FALSE;
6026
6027 if (argvars[0].v_type != VAR_LIST)
6028 EMSG2(_(e_listarg), "insert()");
6029 else if ((l = argvars[0].vval.v_list) != NULL
6030 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6031 {
6032 if (argvars[2].v_type != VAR_UNKNOWN)
6033 before = (long)get_tv_number_chk(&argvars[2], &error);
6034 if (error)
6035 return; /* type error; errmsg already given */
6036
6037 if (before == l->lv_len)
6038 item = NULL;
6039 else
6040 {
6041 item = list_find(l, before);
6042 if (item == NULL)
6043 {
6044 EMSGN(_(e_listidx), before);
6045 l = NULL;
6046 }
6047 }
6048 if (l != NULL)
6049 {
6050 list_insert_tv(l, &argvars[1], item);
6051 copy_tv(&argvars[0], rettv);
6052 }
6053 }
6054}
6055
6056/*
6057 * "invert(expr)" function
6058 */
6059 static void
6060f_invert(typval_T *argvars, typval_T *rettv)
6061{
6062 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6063}
6064
6065/*
6066 * "isdirectory()" function
6067 */
6068 static void
6069f_isdirectory(typval_T *argvars, typval_T *rettv)
6070{
6071 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6072}
6073
6074/*
6075 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6076 * or it refers to a List or Dictionary that is locked.
6077 */
6078 static int
6079tv_islocked(typval_T *tv)
6080{
6081 return (tv->v_lock & VAR_LOCKED)
6082 || (tv->v_type == VAR_LIST
6083 && tv->vval.v_list != NULL
6084 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6085 || (tv->v_type == VAR_DICT
6086 && tv->vval.v_dict != NULL
6087 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6088}
6089
6090/*
6091 * "islocked()" function
6092 */
6093 static void
6094f_islocked(typval_T *argvars, typval_T *rettv)
6095{
6096 lval_T lv;
6097 char_u *end;
6098 dictitem_T *di;
6099
6100 rettv->vval.v_number = -1;
6101 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
6102 GLV_NO_AUTOLOAD, FNE_CHECK_START);
6103 if (end != NULL && lv.ll_name != NULL)
6104 {
6105 if (*end != NUL)
6106 EMSG(_(e_trailing));
6107 else
6108 {
6109 if (lv.ll_tv == NULL)
6110 {
6111 if (check_changedtick(lv.ll_name))
6112 rettv->vval.v_number = 1; /* always locked */
6113 else
6114 {
6115 di = find_var(lv.ll_name, NULL, TRUE);
6116 if (di != NULL)
6117 {
6118 /* Consider a variable locked when:
6119 * 1. the variable itself is locked
6120 * 2. the value of the variable is locked.
6121 * 3. the List or Dict value is locked.
6122 */
6123 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6124 || tv_islocked(&di->di_tv));
6125 }
6126 }
6127 }
6128 else if (lv.ll_range)
6129 EMSG(_("E786: Range not allowed"));
6130 else if (lv.ll_newkey != NULL)
6131 EMSG2(_(e_dictkey), lv.ll_newkey);
6132 else if (lv.ll_list != NULL)
6133 /* List item. */
6134 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6135 else
6136 /* Dictionary item. */
6137 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6138 }
6139 }
6140
6141 clear_lval(&lv);
6142}
6143
6144#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6145/*
6146 * "isnan()" function
6147 */
6148 static void
6149f_isnan(typval_T *argvars, typval_T *rettv)
6150{
6151 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6152 && isnan(argvars[0].vval.v_float);
6153}
6154#endif
6155
6156/*
6157 * "items(dict)" function
6158 */
6159 static void
6160f_items(typval_T *argvars, typval_T *rettv)
6161{
6162 dict_list(argvars, rettv, 2);
6163}
6164
6165#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6166/*
6167 * Get the job from the argument.
6168 * Returns NULL if the job is invalid.
6169 */
6170 static job_T *
6171get_job_arg(typval_T *tv)
6172{
6173 job_T *job;
6174
6175 if (tv->v_type != VAR_JOB)
6176 {
6177 EMSG2(_(e_invarg2), get_tv_string(tv));
6178 return NULL;
6179 }
6180 job = tv->vval.v_job;
6181
6182 if (job == NULL)
6183 EMSG(_("E916: not a valid job"));
6184 return job;
6185}
6186
6187/*
6188 * "job_getchannel()" function
6189 */
6190 static void
6191f_job_getchannel(typval_T *argvars, typval_T *rettv)
6192{
6193 job_T *job = get_job_arg(&argvars[0]);
6194
6195 if (job != NULL)
6196 {
6197 rettv->v_type = VAR_CHANNEL;
6198 rettv->vval.v_channel = job->jv_channel;
6199 if (job->jv_channel != NULL)
6200 ++job->jv_channel->ch_refcount;
6201 }
6202}
6203
6204/*
6205 * "job_info()" function
6206 */
6207 static void
6208f_job_info(typval_T *argvars, typval_T *rettv)
6209{
6210 job_T *job = get_job_arg(&argvars[0]);
6211
6212 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6213 job_info(job, rettv->vval.v_dict);
6214}
6215
6216/*
6217 * "job_setoptions()" function
6218 */
6219 static void
6220f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6221{
6222 job_T *job = get_job_arg(&argvars[0]);
6223 jobopt_T opt;
6224
6225 if (job == NULL)
6226 return;
6227 clear_job_options(&opt);
6228 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6229 job_set_options(job, &opt);
6230 free_job_options(&opt);
6231}
6232
6233/*
6234 * "job_start()" function
6235 */
6236 static void
6237f_job_start(typval_T *argvars, typval_T *rettv)
6238{
6239 rettv->v_type = VAR_JOB;
6240 if (check_restricted() || check_secure())
6241 return;
6242 rettv->vval.v_job = job_start(argvars);
6243}
6244
6245/*
6246 * "job_status()" function
6247 */
6248 static void
6249f_job_status(typval_T *argvars, typval_T *rettv)
6250{
6251 job_T *job = get_job_arg(&argvars[0]);
6252
6253 if (job != NULL)
6254 {
6255 rettv->v_type = VAR_STRING;
6256 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6257 }
6258}
6259
6260/*
6261 * "job_stop()" function
6262 */
6263 static void
6264f_job_stop(typval_T *argvars, typval_T *rettv)
6265{
6266 job_T *job = get_job_arg(&argvars[0]);
6267
6268 if (job != NULL)
6269 rettv->vval.v_number = job_stop(job, argvars);
6270}
6271#endif
6272
6273/*
6274 * "join()" function
6275 */
6276 static void
6277f_join(typval_T *argvars, typval_T *rettv)
6278{
6279 garray_T ga;
6280 char_u *sep;
6281
6282 if (argvars[0].v_type != VAR_LIST)
6283 {
6284 EMSG(_(e_listreq));
6285 return;
6286 }
6287 if (argvars[0].vval.v_list == NULL)
6288 return;
6289 if (argvars[1].v_type == VAR_UNKNOWN)
6290 sep = (char_u *)" ";
6291 else
6292 sep = get_tv_string_chk(&argvars[1]);
6293
6294 rettv->v_type = VAR_STRING;
6295
6296 if (sep != NULL)
6297 {
6298 ga_init2(&ga, (int)sizeof(char), 80);
6299 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6300 ga_append(&ga, NUL);
6301 rettv->vval.v_string = (char_u *)ga.ga_data;
6302 }
6303 else
6304 rettv->vval.v_string = NULL;
6305}
6306
6307/*
6308 * "js_decode()" function
6309 */
6310 static void
6311f_js_decode(typval_T *argvars, typval_T *rettv)
6312{
6313 js_read_T reader;
6314
6315 reader.js_buf = get_tv_string(&argvars[0]);
6316 reader.js_fill = NULL;
6317 reader.js_used = 0;
6318 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6319 EMSG(_(e_invarg));
6320}
6321
6322/*
6323 * "js_encode()" function
6324 */
6325 static void
6326f_js_encode(typval_T *argvars, typval_T *rettv)
6327{
6328 rettv->v_type = VAR_STRING;
6329 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6330}
6331
6332/*
6333 * "json_decode()" function
6334 */
6335 static void
6336f_json_decode(typval_T *argvars, typval_T *rettv)
6337{
6338 js_read_T reader;
6339
6340 reader.js_buf = get_tv_string(&argvars[0]);
6341 reader.js_fill = NULL;
6342 reader.js_used = 0;
6343 if (json_decode_all(&reader, rettv, 0) != OK)
6344 EMSG(_(e_invarg));
6345}
6346
6347/*
6348 * "json_encode()" function
6349 */
6350 static void
6351f_json_encode(typval_T *argvars, typval_T *rettv)
6352{
6353 rettv->v_type = VAR_STRING;
6354 rettv->vval.v_string = json_encode(&argvars[0], 0);
6355}
6356
6357/*
6358 * "keys()" function
6359 */
6360 static void
6361f_keys(typval_T *argvars, typval_T *rettv)
6362{
6363 dict_list(argvars, rettv, 0);
6364}
6365
6366/*
6367 * "last_buffer_nr()" function.
6368 */
6369 static void
6370f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6371{
6372 int n = 0;
6373 buf_T *buf;
6374
6375 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6376 if (n < buf->b_fnum)
6377 n = buf->b_fnum;
6378
6379 rettv->vval.v_number = n;
6380}
6381
6382/*
6383 * "len()" function
6384 */
6385 static void
6386f_len(typval_T *argvars, typval_T *rettv)
6387{
6388 switch (argvars[0].v_type)
6389 {
6390 case VAR_STRING:
6391 case VAR_NUMBER:
6392 rettv->vval.v_number = (varnumber_T)STRLEN(
6393 get_tv_string(&argvars[0]));
6394 break;
6395 case VAR_LIST:
6396 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6397 break;
6398 case VAR_DICT:
6399 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6400 break;
6401 case VAR_UNKNOWN:
6402 case VAR_SPECIAL:
6403 case VAR_FLOAT:
6404 case VAR_FUNC:
6405 case VAR_PARTIAL:
6406 case VAR_JOB:
6407 case VAR_CHANNEL:
6408 EMSG(_("E701: Invalid type for len()"));
6409 break;
6410 }
6411}
6412
6413static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
6414
6415 static void
6416libcall_common(typval_T *argvars, typval_T *rettv, int type)
6417{
6418#ifdef FEAT_LIBCALL
6419 char_u *string_in;
6420 char_u **string_result;
6421 int nr_result;
6422#endif
6423
6424 rettv->v_type = type;
6425 if (type != VAR_NUMBER)
6426 rettv->vval.v_string = NULL;
6427
6428 if (check_restricted() || check_secure())
6429 return;
6430
6431#ifdef FEAT_LIBCALL
6432 /* The first two args must be strings, otherwise its meaningless */
6433 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6434 {
6435 string_in = NULL;
6436 if (argvars[2].v_type == VAR_STRING)
6437 string_in = argvars[2].vval.v_string;
6438 if (type == VAR_NUMBER)
6439 string_result = NULL;
6440 else
6441 string_result = &rettv->vval.v_string;
6442 if (mch_libcall(argvars[0].vval.v_string,
6443 argvars[1].vval.v_string,
6444 string_in,
6445 argvars[2].vval.v_number,
6446 string_result,
6447 &nr_result) == OK
6448 && type == VAR_NUMBER)
6449 rettv->vval.v_number = nr_result;
6450 }
6451#endif
6452}
6453
6454/*
6455 * "libcall()" function
6456 */
6457 static void
6458f_libcall(typval_T *argvars, typval_T *rettv)
6459{
6460 libcall_common(argvars, rettv, VAR_STRING);
6461}
6462
6463/*
6464 * "libcallnr()" function
6465 */
6466 static void
6467f_libcallnr(typval_T *argvars, typval_T *rettv)
6468{
6469 libcall_common(argvars, rettv, VAR_NUMBER);
6470}
6471
6472/*
6473 * "line(string)" function
6474 */
6475 static void
6476f_line(typval_T *argvars, typval_T *rettv)
6477{
6478 linenr_T lnum = 0;
6479 pos_T *fp;
6480 int fnum;
6481
6482 fp = var2fpos(&argvars[0], TRUE, &fnum);
6483 if (fp != NULL)
6484 lnum = fp->lnum;
6485 rettv->vval.v_number = lnum;
6486}
6487
6488/*
6489 * "line2byte(lnum)" function
6490 */
6491 static void
6492f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6493{
6494#ifndef FEAT_BYTEOFF
6495 rettv->vval.v_number = -1;
6496#else
6497 linenr_T lnum;
6498
6499 lnum = get_tv_lnum(argvars);
6500 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6501 rettv->vval.v_number = -1;
6502 else
6503 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6504 if (rettv->vval.v_number >= 0)
6505 ++rettv->vval.v_number;
6506#endif
6507}
6508
6509/*
6510 * "lispindent(lnum)" function
6511 */
6512 static void
6513f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
6514{
6515#ifdef FEAT_LISP
6516 pos_T pos;
6517 linenr_T lnum;
6518
6519 pos = curwin->w_cursor;
6520 lnum = get_tv_lnum(argvars);
6521 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6522 {
6523 curwin->w_cursor.lnum = lnum;
6524 rettv->vval.v_number = get_lisp_indent();
6525 curwin->w_cursor = pos;
6526 }
6527 else
6528#endif
6529 rettv->vval.v_number = -1;
6530}
6531
6532/*
6533 * "localtime()" function
6534 */
6535 static void
6536f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
6537{
6538 rettv->vval.v_number = (varnumber_T)time(NULL);
6539}
6540
6541static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
6542
6543 static void
6544get_maparg(typval_T *argvars, typval_T *rettv, int exact)
6545{
6546 char_u *keys;
6547 char_u *which;
6548 char_u buf[NUMBUFLEN];
6549 char_u *keys_buf = NULL;
6550 char_u *rhs;
6551 int mode;
6552 int abbr = FALSE;
6553 int get_dict = FALSE;
6554 mapblock_T *mp;
6555 int buffer_local;
6556
6557 /* return empty string for failure */
6558 rettv->v_type = VAR_STRING;
6559 rettv->vval.v_string = NULL;
6560
6561 keys = get_tv_string(&argvars[0]);
6562 if (*keys == NUL)
6563 return;
6564
6565 if (argvars[1].v_type != VAR_UNKNOWN)
6566 {
6567 which = get_tv_string_buf_chk(&argvars[1], buf);
6568 if (argvars[2].v_type != VAR_UNKNOWN)
6569 {
6570 abbr = (int)get_tv_number(&argvars[2]);
6571 if (argvars[3].v_type != VAR_UNKNOWN)
6572 get_dict = (int)get_tv_number(&argvars[3]);
6573 }
6574 }
6575 else
6576 which = (char_u *)"";
6577 if (which == NULL)
6578 return;
6579
6580 mode = get_map_mode(&which, 0);
6581
6582 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
6583 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
6584 vim_free(keys_buf);
6585
6586 if (!get_dict)
6587 {
6588 /* Return a string. */
6589 if (rhs != NULL)
6590 rettv->vval.v_string = str2special_save(rhs, FALSE);
6591
6592 }
6593 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
6594 {
6595 /* Return a dictionary. */
6596 char_u *lhs = str2special_save(mp->m_keys, TRUE);
6597 char_u *mapmode = map_mode_to_chars(mp->m_mode);
6598 dict_T *dict = rettv->vval.v_dict;
6599
6600 dict_add_nr_str(dict, "lhs", 0L, lhs);
6601 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
6602 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
6603 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
6604 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
6605 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
6606 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
6607 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
6608 dict_add_nr_str(dict, "mode", 0L, mapmode);
6609
6610 vim_free(lhs);
6611 vim_free(mapmode);
6612 }
6613}
6614
6615#ifdef FEAT_FLOAT
6616/*
6617 * "log()" function
6618 */
6619 static void
6620f_log(typval_T *argvars, typval_T *rettv)
6621{
6622 float_T f = 0.0;
6623
6624 rettv->v_type = VAR_FLOAT;
6625 if (get_float_arg(argvars, &f) == OK)
6626 rettv->vval.v_float = log(f);
6627 else
6628 rettv->vval.v_float = 0.0;
6629}
6630
6631/*
6632 * "log10()" function
6633 */
6634 static void
6635f_log10(typval_T *argvars, typval_T *rettv)
6636{
6637 float_T f = 0.0;
6638
6639 rettv->v_type = VAR_FLOAT;
6640 if (get_float_arg(argvars, &f) == OK)
6641 rettv->vval.v_float = log10(f);
6642 else
6643 rettv->vval.v_float = 0.0;
6644}
6645#endif
6646
6647#ifdef FEAT_LUA
6648/*
6649 * "luaeval()" function
6650 */
6651 static void
6652f_luaeval(typval_T *argvars, typval_T *rettv)
6653{
6654 char_u *str;
6655 char_u buf[NUMBUFLEN];
6656
6657 str = get_tv_string_buf(&argvars[0], buf);
6658 do_luaeval(str, argvars + 1, rettv);
6659}
6660#endif
6661
6662/*
6663 * "map()" function
6664 */
6665 static void
6666f_map(typval_T *argvars, typval_T *rettv)
6667{
6668 filter_map(argvars, rettv, TRUE);
6669}
6670
6671/*
6672 * "maparg()" function
6673 */
6674 static void
6675f_maparg(typval_T *argvars, typval_T *rettv)
6676{
6677 get_maparg(argvars, rettv, TRUE);
6678}
6679
6680/*
6681 * "mapcheck()" function
6682 */
6683 static void
6684f_mapcheck(typval_T *argvars, typval_T *rettv)
6685{
6686 get_maparg(argvars, rettv, FALSE);
6687}
6688
6689static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
6690
6691 static void
6692find_some_match(typval_T *argvars, typval_T *rettv, int type)
6693{
6694 char_u *str = NULL;
6695 long len = 0;
6696 char_u *expr = NULL;
6697 char_u *pat;
6698 regmatch_T regmatch;
6699 char_u patbuf[NUMBUFLEN];
6700 char_u strbuf[NUMBUFLEN];
6701 char_u *save_cpo;
6702 long start = 0;
6703 long nth = 1;
6704 colnr_T startcol = 0;
6705 int match = 0;
6706 list_T *l = NULL;
6707 listitem_T *li = NULL;
6708 long idx = 0;
6709 char_u *tofree = NULL;
6710
6711 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
6712 save_cpo = p_cpo;
6713 p_cpo = (char_u *)"";
6714
6715 rettv->vval.v_number = -1;
6716 if (type == 3 || type == 4)
6717 {
6718 /* type 3: return empty list when there are no matches.
6719 * type 4: return ["", -1, -1, -1] */
6720 if (rettv_list_alloc(rettv) == FAIL)
6721 goto theend;
6722 if (type == 4
6723 && (list_append_string(rettv->vval.v_list,
6724 (char_u *)"", 0) == FAIL
6725 || list_append_number(rettv->vval.v_list,
6726 (varnumber_T)-1) == FAIL
6727 || list_append_number(rettv->vval.v_list,
6728 (varnumber_T)-1) == FAIL
6729 || list_append_number(rettv->vval.v_list,
6730 (varnumber_T)-1) == FAIL))
6731 {
6732 list_free(rettv->vval.v_list);
6733 rettv->vval.v_list = NULL;
6734 goto theend;
6735 }
6736 }
6737 else if (type == 2)
6738 {
6739 rettv->v_type = VAR_STRING;
6740 rettv->vval.v_string = NULL;
6741 }
6742
6743 if (argvars[0].v_type == VAR_LIST)
6744 {
6745 if ((l = argvars[0].vval.v_list) == NULL)
6746 goto theend;
6747 li = l->lv_first;
6748 }
6749 else
6750 {
6751 expr = str = get_tv_string(&argvars[0]);
6752 len = (long)STRLEN(str);
6753 }
6754
6755 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
6756 if (pat == NULL)
6757 goto theend;
6758
6759 if (argvars[2].v_type != VAR_UNKNOWN)
6760 {
6761 int error = FALSE;
6762
6763 start = (long)get_tv_number_chk(&argvars[2], &error);
6764 if (error)
6765 goto theend;
6766 if (l != NULL)
6767 {
6768 li = list_find(l, start);
6769 if (li == NULL)
6770 goto theend;
6771 idx = l->lv_idx; /* use the cached index */
6772 }
6773 else
6774 {
6775 if (start < 0)
6776 start = 0;
6777 if (start > len)
6778 goto theend;
6779 /* When "count" argument is there ignore matches before "start",
6780 * otherwise skip part of the string. Differs when pattern is "^"
6781 * or "\<". */
6782 if (argvars[3].v_type != VAR_UNKNOWN)
6783 startcol = start;
6784 else
6785 {
6786 str += start;
6787 len -= start;
6788 }
6789 }
6790
6791 if (argvars[3].v_type != VAR_UNKNOWN)
6792 nth = (long)get_tv_number_chk(&argvars[3], &error);
6793 if (error)
6794 goto theend;
6795 }
6796
6797 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6798 if (regmatch.regprog != NULL)
6799 {
6800 regmatch.rm_ic = p_ic;
6801
6802 for (;;)
6803 {
6804 if (l != NULL)
6805 {
6806 if (li == NULL)
6807 {
6808 match = FALSE;
6809 break;
6810 }
6811 vim_free(tofree);
6812 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6813 if (str == NULL)
6814 break;
6815 }
6816
6817 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6818
6819 if (match && --nth <= 0)
6820 break;
6821 if (l == NULL && !match)
6822 break;
6823
6824 /* Advance to just after the match. */
6825 if (l != NULL)
6826 {
6827 li = li->li_next;
6828 ++idx;
6829 }
6830 else
6831 {
6832#ifdef FEAT_MBYTE
6833 startcol = (colnr_T)(regmatch.startp[0]
6834 + (*mb_ptr2len)(regmatch.startp[0]) - str);
6835#else
6836 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
6837#endif
6838 if (startcol > (colnr_T)len
6839 || str + startcol <= regmatch.startp[0])
6840 {
6841 match = FALSE;
6842 break;
6843 }
6844 }
6845 }
6846
6847 if (match)
6848 {
6849 if (type == 4)
6850 {
6851 listitem_T *li1 = rettv->vval.v_list->lv_first;
6852 listitem_T *li2 = li1->li_next;
6853 listitem_T *li3 = li2->li_next;
6854 listitem_T *li4 = li3->li_next;
6855
6856 vim_free(li1->li_tv.vval.v_string);
6857 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
6858 (int)(regmatch.endp[0] - regmatch.startp[0]));
6859 li3->li_tv.vval.v_number =
6860 (varnumber_T)(regmatch.startp[0] - expr);
6861 li4->li_tv.vval.v_number =
6862 (varnumber_T)(regmatch.endp[0] - expr);
6863 if (l != NULL)
6864 li2->li_tv.vval.v_number = (varnumber_T)idx;
6865 }
6866 else if (type == 3)
6867 {
6868 int i;
6869
6870 /* return list with matched string and submatches */
6871 for (i = 0; i < NSUBEXP; ++i)
6872 {
6873 if (regmatch.endp[i] == NULL)
6874 {
6875 if (list_append_string(rettv->vval.v_list,
6876 (char_u *)"", 0) == FAIL)
6877 break;
6878 }
6879 else if (list_append_string(rettv->vval.v_list,
6880 regmatch.startp[i],
6881 (int)(regmatch.endp[i] - regmatch.startp[i]))
6882 == FAIL)
6883 break;
6884 }
6885 }
6886 else if (type == 2)
6887 {
6888 /* return matched string */
6889 if (l != NULL)
6890 copy_tv(&li->li_tv, rettv);
6891 else
6892 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
6893 (int)(regmatch.endp[0] - regmatch.startp[0]));
6894 }
6895 else if (l != NULL)
6896 rettv->vval.v_number = idx;
6897 else
6898 {
6899 if (type != 0)
6900 rettv->vval.v_number =
6901 (varnumber_T)(regmatch.startp[0] - str);
6902 else
6903 rettv->vval.v_number =
6904 (varnumber_T)(regmatch.endp[0] - str);
6905 rettv->vval.v_number += (varnumber_T)(str - expr);
6906 }
6907 }
6908 vim_regfree(regmatch.regprog);
6909 }
6910
6911 if (type == 4 && l == NULL)
6912 /* matchstrpos() without a list: drop the second item. */
6913 listitem_remove(rettv->vval.v_list,
6914 rettv->vval.v_list->lv_first->li_next);
6915
6916theend:
6917 vim_free(tofree);
6918 p_cpo = save_cpo;
6919}
6920
6921/*
6922 * "match()" function
6923 */
6924 static void
6925f_match(typval_T *argvars, typval_T *rettv)
6926{
6927 find_some_match(argvars, rettv, 1);
6928}
6929
6930/*
6931 * "matchadd()" function
6932 */
6933 static void
6934f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6935{
6936#ifdef FEAT_SEARCH_EXTRA
6937 char_u buf[NUMBUFLEN];
6938 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
6939 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
6940 int prio = 10; /* default priority */
6941 int id = -1;
6942 int error = FALSE;
6943 char_u *conceal_char = NULL;
6944
6945 rettv->vval.v_number = -1;
6946
6947 if (grp == NULL || pat == NULL)
6948 return;
6949 if (argvars[2].v_type != VAR_UNKNOWN)
6950 {
6951 prio = (int)get_tv_number_chk(&argvars[2], &error);
6952 if (argvars[3].v_type != VAR_UNKNOWN)
6953 {
6954 id = (int)get_tv_number_chk(&argvars[3], &error);
6955 if (argvars[4].v_type != VAR_UNKNOWN)
6956 {
6957 if (argvars[4].v_type != VAR_DICT)
6958 {
6959 EMSG(_(e_dictreq));
6960 return;
6961 }
6962 if (dict_find(argvars[4].vval.v_dict,
6963 (char_u *)"conceal", -1) != NULL)
6964 conceal_char = get_dict_string(argvars[4].vval.v_dict,
6965 (char_u *)"conceal", FALSE);
6966 }
6967 }
6968 }
6969 if (error == TRUE)
6970 return;
6971 if (id >= 1 && id <= 3)
6972 {
6973 EMSGN("E798: ID is reserved for \":match\": %ld", id);
6974 return;
6975 }
6976
6977 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
6978 conceal_char);
6979#endif
6980}
6981
6982/*
6983 * "matchaddpos()" function
6984 */
6985 static void
6986f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6987{
6988#ifdef FEAT_SEARCH_EXTRA
6989 char_u buf[NUMBUFLEN];
6990 char_u *group;
6991 int prio = 10;
6992 int id = -1;
6993 int error = FALSE;
6994 list_T *l;
6995 char_u *conceal_char = NULL;
6996
6997 rettv->vval.v_number = -1;
6998
6999 group = get_tv_string_buf_chk(&argvars[0], buf);
7000 if (group == NULL)
7001 return;
7002
7003 if (argvars[1].v_type != VAR_LIST)
7004 {
7005 EMSG2(_(e_listarg), "matchaddpos()");
7006 return;
7007 }
7008 l = argvars[1].vval.v_list;
7009 if (l == NULL)
7010 return;
7011
7012 if (argvars[2].v_type != VAR_UNKNOWN)
7013 {
7014 prio = (int)get_tv_number_chk(&argvars[2], &error);
7015 if (argvars[3].v_type != VAR_UNKNOWN)
7016 {
7017 id = (int)get_tv_number_chk(&argvars[3], &error);
7018 if (argvars[4].v_type != VAR_UNKNOWN)
7019 {
7020 if (argvars[4].v_type != VAR_DICT)
7021 {
7022 EMSG(_(e_dictreq));
7023 return;
7024 }
7025 if (dict_find(argvars[4].vval.v_dict,
7026 (char_u *)"conceal", -1) != NULL)
7027 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7028 (char_u *)"conceal", FALSE);
7029 }
7030 }
7031 }
7032 if (error == TRUE)
7033 return;
7034
7035 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7036 if (id == 1 || id == 2)
7037 {
7038 EMSGN("E798: ID is reserved for \":match\": %ld", id);
7039 return;
7040 }
7041
7042 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7043 conceal_char);
7044#endif
7045}
7046
7047/*
7048 * "matcharg()" function
7049 */
7050 static void
7051f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7052{
7053 if (rettv_list_alloc(rettv) == OK)
7054 {
7055#ifdef FEAT_SEARCH_EXTRA
7056 int id = (int)get_tv_number(&argvars[0]);
7057 matchitem_T *m;
7058
7059 if (id >= 1 && id <= 3)
7060 {
7061 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7062 {
7063 list_append_string(rettv->vval.v_list,
7064 syn_id2name(m->hlg_id), -1);
7065 list_append_string(rettv->vval.v_list, m->pattern, -1);
7066 }
7067 else
7068 {
7069 list_append_string(rettv->vval.v_list, NULL, -1);
7070 list_append_string(rettv->vval.v_list, NULL, -1);
7071 }
7072 }
7073#endif
7074 }
7075}
7076
7077/*
7078 * "matchdelete()" function
7079 */
7080 static void
7081f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7082{
7083#ifdef FEAT_SEARCH_EXTRA
7084 rettv->vval.v_number = match_delete(curwin,
7085 (int)get_tv_number(&argvars[0]), TRUE);
7086#endif
7087}
7088
7089/*
7090 * "matchend()" function
7091 */
7092 static void
7093f_matchend(typval_T *argvars, typval_T *rettv)
7094{
7095 find_some_match(argvars, rettv, 0);
7096}
7097
7098/*
7099 * "matchlist()" function
7100 */
7101 static void
7102f_matchlist(typval_T *argvars, typval_T *rettv)
7103{
7104 find_some_match(argvars, rettv, 3);
7105}
7106
7107/*
7108 * "matchstr()" function
7109 */
7110 static void
7111f_matchstr(typval_T *argvars, typval_T *rettv)
7112{
7113 find_some_match(argvars, rettv, 2);
7114}
7115
7116/*
7117 * "matchstrpos()" function
7118 */
7119 static void
7120f_matchstrpos(typval_T *argvars, typval_T *rettv)
7121{
7122 find_some_match(argvars, rettv, 4);
7123}
7124
7125static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7126
7127 static void
7128max_min(typval_T *argvars, typval_T *rettv, int domax)
7129{
7130 varnumber_T n = 0;
7131 varnumber_T i;
7132 int error = FALSE;
7133
7134 if (argvars[0].v_type == VAR_LIST)
7135 {
7136 list_T *l;
7137 listitem_T *li;
7138
7139 l = argvars[0].vval.v_list;
7140 if (l != NULL)
7141 {
7142 li = l->lv_first;
7143 if (li != NULL)
7144 {
7145 n = get_tv_number_chk(&li->li_tv, &error);
7146 for (;;)
7147 {
7148 li = li->li_next;
7149 if (li == NULL)
7150 break;
7151 i = get_tv_number_chk(&li->li_tv, &error);
7152 if (domax ? i > n : i < n)
7153 n = i;
7154 }
7155 }
7156 }
7157 }
7158 else if (argvars[0].v_type == VAR_DICT)
7159 {
7160 dict_T *d;
7161 int first = TRUE;
7162 hashitem_T *hi;
7163 int todo;
7164
7165 d = argvars[0].vval.v_dict;
7166 if (d != NULL)
7167 {
7168 todo = (int)d->dv_hashtab.ht_used;
7169 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7170 {
7171 if (!HASHITEM_EMPTY(hi))
7172 {
7173 --todo;
7174 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7175 if (first)
7176 {
7177 n = i;
7178 first = FALSE;
7179 }
7180 else if (domax ? i > n : i < n)
7181 n = i;
7182 }
7183 }
7184 }
7185 }
7186 else
7187 EMSG(_(e_listdictarg));
7188 rettv->vval.v_number = error ? 0 : n;
7189}
7190
7191/*
7192 * "max()" function
7193 */
7194 static void
7195f_max(typval_T *argvars, typval_T *rettv)
7196{
7197 max_min(argvars, rettv, TRUE);
7198}
7199
7200/*
7201 * "min()" function
7202 */
7203 static void
7204f_min(typval_T *argvars, typval_T *rettv)
7205{
7206 max_min(argvars, rettv, FALSE);
7207}
7208
7209static int mkdir_recurse(char_u *dir, int prot);
7210
7211/*
7212 * Create the directory in which "dir" is located, and higher levels when
7213 * needed.
7214 */
7215 static int
7216mkdir_recurse(char_u *dir, int prot)
7217{
7218 char_u *p;
7219 char_u *updir;
7220 int r = FAIL;
7221
7222 /* Get end of directory name in "dir".
7223 * We're done when it's "/" or "c:/". */
7224 p = gettail_sep(dir);
7225 if (p <= get_past_head(dir))
7226 return OK;
7227
7228 /* If the directory exists we're done. Otherwise: create it.*/
7229 updir = vim_strnsave(dir, (int)(p - dir));
7230 if (updir == NULL)
7231 return FAIL;
7232 if (mch_isdir(updir))
7233 r = OK;
7234 else if (mkdir_recurse(updir, prot) == OK)
7235 r = vim_mkdir_emsg(updir, prot);
7236 vim_free(updir);
7237 return r;
7238}
7239
7240#ifdef vim_mkdir
7241/*
7242 * "mkdir()" function
7243 */
7244 static void
7245f_mkdir(typval_T *argvars, typval_T *rettv)
7246{
7247 char_u *dir;
7248 char_u buf[NUMBUFLEN];
7249 int prot = 0755;
7250
7251 rettv->vval.v_number = FAIL;
7252 if (check_restricted() || check_secure())
7253 return;
7254
7255 dir = get_tv_string_buf(&argvars[0], buf);
7256 if (*dir == NUL)
7257 rettv->vval.v_number = FAIL;
7258 else
7259 {
7260 if (*gettail(dir) == NUL)
7261 /* remove trailing slashes */
7262 *gettail_sep(dir) = NUL;
7263
7264 if (argvars[1].v_type != VAR_UNKNOWN)
7265 {
7266 if (argvars[2].v_type != VAR_UNKNOWN)
7267 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7268 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7269 mkdir_recurse(dir, prot);
7270 }
7271 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7272 }
7273}
7274#endif
7275
7276/*
7277 * "mode()" function
7278 */
7279 static void
7280f_mode(typval_T *argvars, typval_T *rettv)
7281{
7282 char_u buf[3];
7283
7284 buf[1] = NUL;
7285 buf[2] = NUL;
7286
7287 if (time_for_testing == 93784)
7288 {
7289 /* Testing the two-character code. */
7290 buf[0] = 'x';
7291 buf[1] = '!';
7292 }
7293 else if (VIsual_active)
7294 {
7295 if (VIsual_select)
7296 buf[0] = VIsual_mode + 's' - 'v';
7297 else
7298 buf[0] = VIsual_mode;
7299 }
7300 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7301 || State == CONFIRM)
7302 {
7303 buf[0] = 'r';
7304 if (State == ASKMORE)
7305 buf[1] = 'm';
7306 else if (State == CONFIRM)
7307 buf[1] = '?';
7308 }
7309 else if (State == EXTERNCMD)
7310 buf[0] = '!';
7311 else if (State & INSERT)
7312 {
7313#ifdef FEAT_VREPLACE
7314 if (State & VREPLACE_FLAG)
7315 {
7316 buf[0] = 'R';
7317 buf[1] = 'v';
7318 }
7319 else
7320#endif
7321 if (State & REPLACE_FLAG)
7322 buf[0] = 'R';
7323 else
7324 buf[0] = 'i';
7325 }
7326 else if (State & CMDLINE)
7327 {
7328 buf[0] = 'c';
7329 if (exmode_active)
7330 buf[1] = 'v';
7331 }
7332 else if (exmode_active)
7333 {
7334 buf[0] = 'c';
7335 buf[1] = 'e';
7336 }
7337 else
7338 {
7339 buf[0] = 'n';
7340 if (finish_op)
7341 buf[1] = 'o';
7342 }
7343
7344 /* Clear out the minor mode when the argument is not a non-zero number or
7345 * non-empty string. */
7346 if (!non_zero_arg(&argvars[0]))
7347 buf[1] = NUL;
7348
7349 rettv->vval.v_string = vim_strsave(buf);
7350 rettv->v_type = VAR_STRING;
7351}
7352
7353#if defined(FEAT_MZSCHEME) || defined(PROTO)
7354/*
7355 * "mzeval()" function
7356 */
7357 static void
7358f_mzeval(typval_T *argvars, typval_T *rettv)
7359{
7360 char_u *str;
7361 char_u buf[NUMBUFLEN];
7362
7363 str = get_tv_string_buf(&argvars[0], buf);
7364 do_mzeval(str, rettv);
7365}
7366
7367 void
7368mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7369{
7370 typval_T argvars[3];
7371
7372 argvars[0].v_type = VAR_STRING;
7373 argvars[0].vval.v_string = name;
7374 copy_tv(args, &argvars[1]);
7375 argvars[2].v_type = VAR_UNKNOWN;
7376 f_call(argvars, rettv);
7377 clear_tv(&argvars[1]);
7378}
7379#endif
7380
7381/*
7382 * "nextnonblank()" function
7383 */
7384 static void
7385f_nextnonblank(typval_T *argvars, typval_T *rettv)
7386{
7387 linenr_T lnum;
7388
7389 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7390 {
7391 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7392 {
7393 lnum = 0;
7394 break;
7395 }
7396 if (*skipwhite(ml_get(lnum)) != NUL)
7397 break;
7398 }
7399 rettv->vval.v_number = lnum;
7400}
7401
7402/*
7403 * "nr2char()" function
7404 */
7405 static void
7406f_nr2char(typval_T *argvars, typval_T *rettv)
7407{
7408 char_u buf[NUMBUFLEN];
7409
7410#ifdef FEAT_MBYTE
7411 if (has_mbyte)
7412 {
7413 int utf8 = 0;
7414
7415 if (argvars[1].v_type != VAR_UNKNOWN)
7416 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7417 if (utf8)
7418 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7419 else
7420 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7421 }
7422 else
7423#endif
7424 {
7425 buf[0] = (char_u)get_tv_number(&argvars[0]);
7426 buf[1] = NUL;
7427 }
7428 rettv->v_type = VAR_STRING;
7429 rettv->vval.v_string = vim_strsave(buf);
7430}
7431
7432/*
7433 * "or(expr, expr)" function
7434 */
7435 static void
7436f_or(typval_T *argvars, typval_T *rettv)
7437{
7438 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7439 | get_tv_number_chk(&argvars[1], NULL);
7440}
7441
7442/*
7443 * "pathshorten()" function
7444 */
7445 static void
7446f_pathshorten(typval_T *argvars, typval_T *rettv)
7447{
7448 char_u *p;
7449
7450 rettv->v_type = VAR_STRING;
7451 p = get_tv_string_chk(&argvars[0]);
7452 if (p == NULL)
7453 rettv->vval.v_string = NULL;
7454 else
7455 {
7456 p = vim_strsave(p);
7457 rettv->vval.v_string = p;
7458 if (p != NULL)
7459 shorten_dir(p);
7460 }
7461}
7462
7463#ifdef FEAT_PERL
7464/*
7465 * "perleval()" function
7466 */
7467 static void
7468f_perleval(typval_T *argvars, typval_T *rettv)
7469{
7470 char_u *str;
7471 char_u buf[NUMBUFLEN];
7472
7473 str = get_tv_string_buf(&argvars[0], buf);
7474 do_perleval(str, rettv);
7475}
7476#endif
7477
7478#ifdef FEAT_FLOAT
7479/*
7480 * "pow()" function
7481 */
7482 static void
7483f_pow(typval_T *argvars, typval_T *rettv)
7484{
7485 float_T fx = 0.0, fy = 0.0;
7486
7487 rettv->v_type = VAR_FLOAT;
7488 if (get_float_arg(argvars, &fx) == OK
7489 && get_float_arg(&argvars[1], &fy) == OK)
7490 rettv->vval.v_float = pow(fx, fy);
7491 else
7492 rettv->vval.v_float = 0.0;
7493}
7494#endif
7495
7496/*
7497 * "prevnonblank()" function
7498 */
7499 static void
7500f_prevnonblank(typval_T *argvars, typval_T *rettv)
7501{
7502 linenr_T lnum;
7503
7504 lnum = get_tv_lnum(argvars);
7505 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7506 lnum = 0;
7507 else
7508 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7509 --lnum;
7510 rettv->vval.v_number = lnum;
7511}
7512
7513/* This dummy va_list is here because:
7514 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7515 * - locally in the function results in a "used before set" warning
7516 * - using va_start() to initialize it gives "function with fixed args" error */
7517static va_list ap;
7518
7519/*
7520 * "printf()" function
7521 */
7522 static void
7523f_printf(typval_T *argvars, typval_T *rettv)
7524{
7525 char_u buf[NUMBUFLEN];
7526 int len;
7527 char_u *s;
7528 int saved_did_emsg = did_emsg;
7529 char *fmt;
7530
7531 rettv->v_type = VAR_STRING;
7532 rettv->vval.v_string = NULL;
7533
7534 /* Get the required length, allocate the buffer and do it for real. */
7535 did_emsg = FALSE;
7536 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
7537 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
7538 if (!did_emsg)
7539 {
7540 s = alloc(len + 1);
7541 if (s != NULL)
7542 {
7543 rettv->vval.v_string = s;
7544 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
7545 }
7546 }
7547 did_emsg |= saved_did_emsg;
7548}
7549
7550/*
7551 * "pumvisible()" function
7552 */
7553 static void
7554f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7555{
7556#ifdef FEAT_INS_EXPAND
7557 if (pum_visible())
7558 rettv->vval.v_number = 1;
7559#endif
7560}
7561
7562#ifdef FEAT_PYTHON3
7563/*
7564 * "py3eval()" function
7565 */
7566 static void
7567f_py3eval(typval_T *argvars, typval_T *rettv)
7568{
7569 char_u *str;
7570 char_u buf[NUMBUFLEN];
7571
7572 str = get_tv_string_buf(&argvars[0], buf);
7573 do_py3eval(str, rettv);
7574}
7575#endif
7576
7577#ifdef FEAT_PYTHON
7578/*
7579 * "pyeval()" function
7580 */
7581 static void
7582f_pyeval(typval_T *argvars, typval_T *rettv)
7583{
7584 char_u *str;
7585 char_u buf[NUMBUFLEN];
7586
7587 str = get_tv_string_buf(&argvars[0], buf);
7588 do_pyeval(str, rettv);
7589}
7590#endif
7591
7592/*
7593 * "range()" function
7594 */
7595 static void
7596f_range(typval_T *argvars, typval_T *rettv)
7597{
7598 varnumber_T start;
7599 varnumber_T end;
7600 varnumber_T stride = 1;
7601 varnumber_T i;
7602 int error = FALSE;
7603
7604 start = get_tv_number_chk(&argvars[0], &error);
7605 if (argvars[1].v_type == VAR_UNKNOWN)
7606 {
7607 end = start - 1;
7608 start = 0;
7609 }
7610 else
7611 {
7612 end = get_tv_number_chk(&argvars[1], &error);
7613 if (argvars[2].v_type != VAR_UNKNOWN)
7614 stride = get_tv_number_chk(&argvars[2], &error);
7615 }
7616
7617 if (error)
7618 return; /* type error; errmsg already given */
7619 if (stride == 0)
7620 EMSG(_("E726: Stride is zero"));
7621 else if (stride > 0 ? end + 1 < start : end - 1 > start)
7622 EMSG(_("E727: Start past end"));
7623 else
7624 {
7625 if (rettv_list_alloc(rettv) == OK)
7626 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7627 if (list_append_number(rettv->vval.v_list,
7628 (varnumber_T)i) == FAIL)
7629 break;
7630 }
7631}
7632
7633/*
7634 * "readfile()" function
7635 */
7636 static void
7637f_readfile(typval_T *argvars, typval_T *rettv)
7638{
7639 int binary = FALSE;
7640 int failed = FALSE;
7641 char_u *fname;
7642 FILE *fd;
7643 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
7644 int io_size = sizeof(buf);
7645 int readlen; /* size of last fread() */
7646 char_u *prev = NULL; /* previously read bytes, if any */
7647 long prevlen = 0; /* length of data in prev */
7648 long prevsize = 0; /* size of prev buffer */
7649 long maxline = MAXLNUM;
7650 long cnt = 0;
7651 char_u *p; /* position in buf */
7652 char_u *start; /* start of current line */
7653
7654 if (argvars[1].v_type != VAR_UNKNOWN)
7655 {
7656 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
7657 binary = TRUE;
7658 if (argvars[2].v_type != VAR_UNKNOWN)
7659 maxline = (long)get_tv_number(&argvars[2]);
7660 }
7661
7662 if (rettv_list_alloc(rettv) == FAIL)
7663 return;
7664
7665 /* Always open the file in binary mode, library functions have a mind of
7666 * their own about CR-LF conversion. */
7667 fname = get_tv_string(&argvars[0]);
7668 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
7669 {
7670 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
7671 return;
7672 }
7673
7674 while (cnt < maxline || maxline < 0)
7675 {
7676 readlen = (int)fread(buf, 1, io_size, fd);
7677
7678 /* This for loop processes what was read, but is also entered at end
7679 * of file so that either:
7680 * - an incomplete line gets written
7681 * - a "binary" file gets an empty line at the end if it ends in a
7682 * newline. */
7683 for (p = buf, start = buf;
7684 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
7685 ++p)
7686 {
7687 if (*p == '\n' || readlen <= 0)
7688 {
7689 listitem_T *li;
7690 char_u *s = NULL;
7691 long_u len = p - start;
7692
7693 /* Finished a line. Remove CRs before NL. */
7694 if (readlen > 0 && !binary)
7695 {
7696 while (len > 0 && start[len - 1] == '\r')
7697 --len;
7698 /* removal may cross back to the "prev" string */
7699 if (len == 0)
7700 while (prevlen > 0 && prev[prevlen - 1] == '\r')
7701 --prevlen;
7702 }
7703 if (prevlen == 0)
7704 s = vim_strnsave(start, (int)len);
7705 else
7706 {
7707 /* Change "prev" buffer to be the right size. This way
7708 * the bytes are only copied once, and very long lines are
7709 * allocated only once. */
7710 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
7711 {
7712 mch_memmove(s + prevlen, start, len);
7713 s[prevlen + len] = NUL;
7714 prev = NULL; /* the list will own the string */
7715 prevlen = prevsize = 0;
7716 }
7717 }
7718 if (s == NULL)
7719 {
7720 do_outofmem_msg((long_u) prevlen + len + 1);
7721 failed = TRUE;
7722 break;
7723 }
7724
7725 if ((li = listitem_alloc()) == NULL)
7726 {
7727 vim_free(s);
7728 failed = TRUE;
7729 break;
7730 }
7731 li->li_tv.v_type = VAR_STRING;
7732 li->li_tv.v_lock = 0;
7733 li->li_tv.vval.v_string = s;
7734 list_append(rettv->vval.v_list, li);
7735
7736 start = p + 1; /* step over newline */
7737 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
7738 break;
7739 }
7740 else if (*p == NUL)
7741 *p = '\n';
7742#ifdef FEAT_MBYTE
7743 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
7744 * when finding the BF and check the previous two bytes. */
7745 else if (*p == 0xbf && enc_utf8 && !binary)
7746 {
7747 /* Find the two bytes before the 0xbf. If p is at buf, or buf
7748 * + 1, these may be in the "prev" string. */
7749 char_u back1 = p >= buf + 1 ? p[-1]
7750 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
7751 char_u back2 = p >= buf + 2 ? p[-2]
7752 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
7753 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
7754
7755 if (back2 == 0xef && back1 == 0xbb)
7756 {
7757 char_u *dest = p - 2;
7758
7759 /* Usually a BOM is at the beginning of a file, and so at
7760 * the beginning of a line; then we can just step over it.
7761 */
7762 if (start == dest)
7763 start = p + 1;
7764 else
7765 {
7766 /* have to shuffle buf to close gap */
7767 int adjust_prevlen = 0;
7768
7769 if (dest < buf)
7770 {
7771 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
7772 dest = buf;
7773 }
7774 if (readlen > p - buf + 1)
7775 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
7776 readlen -= 3 - adjust_prevlen;
7777 prevlen -= adjust_prevlen;
7778 p = dest - 1;
7779 }
7780 }
7781 }
7782#endif
7783 } /* for */
7784
7785 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
7786 break;
7787 if (start < p)
7788 {
7789 /* There's part of a line in buf, store it in "prev". */
7790 if (p - start + prevlen >= prevsize)
7791 {
7792 /* need bigger "prev" buffer */
7793 char_u *newprev;
7794
7795 /* A common use case is ordinary text files and "prev" gets a
7796 * fragment of a line, so the first allocation is made
7797 * small, to avoid repeatedly 'allocing' large and
7798 * 'reallocing' small. */
7799 if (prevsize == 0)
7800 prevsize = (long)(p - start);
7801 else
7802 {
7803 long grow50pc = (prevsize * 3) / 2;
7804 long growmin = (long)((p - start) * 2 + prevlen);
7805 prevsize = grow50pc > growmin ? grow50pc : growmin;
7806 }
7807 newprev = prev == NULL ? alloc(prevsize)
7808 : vim_realloc(prev, prevsize);
7809 if (newprev == NULL)
7810 {
7811 do_outofmem_msg((long_u)prevsize);
7812 failed = TRUE;
7813 break;
7814 }
7815 prev = newprev;
7816 }
7817 /* Add the line part to end of "prev". */
7818 mch_memmove(prev + prevlen, start, p - start);
7819 prevlen += (long)(p - start);
7820 }
7821 } /* while */
7822
7823 /*
7824 * For a negative line count use only the lines at the end of the file,
7825 * free the rest.
7826 */
7827 if (!failed && maxline < 0)
7828 while (cnt > -maxline)
7829 {
7830 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
7831 --cnt;
7832 }
7833
7834 if (failed)
7835 {
7836 list_free(rettv->vval.v_list);
7837 /* readfile doc says an empty list is returned on error */
7838 rettv->vval.v_list = list_alloc();
7839 }
7840
7841 vim_free(prev);
7842 fclose(fd);
7843}
7844
7845#if defined(FEAT_RELTIME)
7846static int list2proftime(typval_T *arg, proftime_T *tm);
7847
7848/*
7849 * Convert a List to proftime_T.
7850 * Return FAIL when there is something wrong.
7851 */
7852 static int
7853list2proftime(typval_T *arg, proftime_T *tm)
7854{
7855 long n1, n2;
7856 int error = FALSE;
7857
7858 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
7859 || arg->vval.v_list->lv_len != 2)
7860 return FAIL;
7861 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
7862 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
7863# ifdef WIN3264
7864 tm->HighPart = n1;
7865 tm->LowPart = n2;
7866# else
7867 tm->tv_sec = n1;
7868 tm->tv_usec = n2;
7869# endif
7870 return error ? FAIL : OK;
7871}
7872#endif /* FEAT_RELTIME */
7873
7874/*
7875 * "reltime()" function
7876 */
7877 static void
7878f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7879{
7880#ifdef FEAT_RELTIME
7881 proftime_T res;
7882 proftime_T start;
7883
7884 if (argvars[0].v_type == VAR_UNKNOWN)
7885 {
7886 /* No arguments: get current time. */
7887 profile_start(&res);
7888 }
7889 else if (argvars[1].v_type == VAR_UNKNOWN)
7890 {
7891 if (list2proftime(&argvars[0], &res) == FAIL)
7892 return;
7893 profile_end(&res);
7894 }
7895 else
7896 {
7897 /* Two arguments: compute the difference. */
7898 if (list2proftime(&argvars[0], &start) == FAIL
7899 || list2proftime(&argvars[1], &res) == FAIL)
7900 return;
7901 profile_sub(&res, &start);
7902 }
7903
7904 if (rettv_list_alloc(rettv) == OK)
7905 {
7906 long n1, n2;
7907
7908# ifdef WIN3264
7909 n1 = res.HighPart;
7910 n2 = res.LowPart;
7911# else
7912 n1 = res.tv_sec;
7913 n2 = res.tv_usec;
7914# endif
7915 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
7916 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
7917 }
7918#endif
7919}
7920
7921#ifdef FEAT_FLOAT
7922/*
7923 * "reltimefloat()" function
7924 */
7925 static void
7926f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
7927{
7928# ifdef FEAT_RELTIME
7929 proftime_T tm;
7930# endif
7931
7932 rettv->v_type = VAR_FLOAT;
7933 rettv->vval.v_float = 0;
7934# ifdef FEAT_RELTIME
7935 if (list2proftime(&argvars[0], &tm) == OK)
7936 rettv->vval.v_float = profile_float(&tm);
7937# endif
7938}
7939#endif
7940
7941/*
7942 * "reltimestr()" function
7943 */
7944 static void
7945f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
7946{
7947#ifdef FEAT_RELTIME
7948 proftime_T tm;
7949#endif
7950
7951 rettv->v_type = VAR_STRING;
7952 rettv->vval.v_string = NULL;
7953#ifdef FEAT_RELTIME
7954 if (list2proftime(&argvars[0], &tm) == OK)
7955 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
7956#endif
7957}
7958
7959#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
7960static void make_connection(void);
7961static int check_connection(void);
7962
7963 static void
7964make_connection(void)
7965{
7966 if (X_DISPLAY == NULL
7967# ifdef FEAT_GUI
7968 && !gui.in_use
7969# endif
7970 )
7971 {
7972 x_force_connect = TRUE;
7973 setup_term_clip();
7974 x_force_connect = FALSE;
7975 }
7976}
7977
7978 static int
7979check_connection(void)
7980{
7981 make_connection();
7982 if (X_DISPLAY == NULL)
7983 {
7984 EMSG(_("E240: No connection to Vim server"));
7985 return FAIL;
7986 }
7987 return OK;
7988}
7989#endif
7990
7991#ifdef FEAT_CLIENTSERVER
7992 static void
7993remote_common(typval_T *argvars, typval_T *rettv, int expr)
7994{
7995 char_u *server_name;
7996 char_u *keys;
7997 char_u *r = NULL;
7998 char_u buf[NUMBUFLEN];
7999# ifdef WIN32
8000 HWND w;
8001# else
8002 Window w;
8003# endif
8004
8005 if (check_restricted() || check_secure())
8006 return;
8007
8008# ifdef FEAT_X11
8009 if (check_connection() == FAIL)
8010 return;
8011# endif
8012
8013 server_name = get_tv_string_chk(&argvars[0]);
8014 if (server_name == NULL)
8015 return; /* type error; errmsg already given */
8016 keys = get_tv_string_buf(&argvars[1], buf);
8017# ifdef WIN32
8018 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8019# else
8020 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8021 < 0)
8022# endif
8023 {
8024 if (r != NULL)
8025 EMSG(r); /* sending worked but evaluation failed */
8026 else
8027 EMSG2(_("E241: Unable to send to %s"), server_name);
8028 return;
8029 }
8030
8031 rettv->vval.v_string = r;
8032
8033 if (argvars[2].v_type != VAR_UNKNOWN)
8034 {
8035 dictitem_T v;
8036 char_u str[30];
8037 char_u *idvar;
8038
8039 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8040 v.di_tv.v_type = VAR_STRING;
8041 v.di_tv.vval.v_string = vim_strsave(str);
8042 idvar = get_tv_string_chk(&argvars[2]);
8043 if (idvar != NULL)
8044 set_var(idvar, &v.di_tv, FALSE);
8045 vim_free(v.di_tv.vval.v_string);
8046 }
8047}
8048#endif
8049
8050/*
8051 * "remote_expr()" function
8052 */
8053 static void
8054f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8055{
8056 rettv->v_type = VAR_STRING;
8057 rettv->vval.v_string = NULL;
8058#ifdef FEAT_CLIENTSERVER
8059 remote_common(argvars, rettv, TRUE);
8060#endif
8061}
8062
8063/*
8064 * "remote_foreground()" function
8065 */
8066 static void
8067f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8068{
8069#ifdef FEAT_CLIENTSERVER
8070# ifdef WIN32
8071 /* On Win32 it's done in this application. */
8072 {
8073 char_u *server_name = get_tv_string_chk(&argvars[0]);
8074
8075 if (server_name != NULL)
8076 serverForeground(server_name);
8077 }
8078# else
8079 /* Send a foreground() expression to the server. */
8080 argvars[1].v_type = VAR_STRING;
8081 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8082 argvars[2].v_type = VAR_UNKNOWN;
8083 remote_common(argvars, rettv, TRUE);
8084 vim_free(argvars[1].vval.v_string);
8085# endif
8086#endif
8087}
8088
8089 static void
8090f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8091{
8092#ifdef FEAT_CLIENTSERVER
8093 dictitem_T v;
8094 char_u *s = NULL;
8095# ifdef WIN32
8096 long_u n = 0;
8097# endif
8098 char_u *serverid;
8099
8100 if (check_restricted() || check_secure())
8101 {
8102 rettv->vval.v_number = -1;
8103 return;
8104 }
8105 serverid = get_tv_string_chk(&argvars[0]);
8106 if (serverid == NULL)
8107 {
8108 rettv->vval.v_number = -1;
8109 return; /* type error; errmsg already given */
8110 }
8111# ifdef WIN32
8112 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8113 if (n == 0)
8114 rettv->vval.v_number = -1;
8115 else
8116 {
8117 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8118 rettv->vval.v_number = (s != NULL);
8119 }
8120# else
8121 if (check_connection() == FAIL)
8122 return;
8123
8124 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8125 serverStrToWin(serverid), &s);
8126# endif
8127
8128 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8129 {
8130 char_u *retvar;
8131
8132 v.di_tv.v_type = VAR_STRING;
8133 v.di_tv.vval.v_string = vim_strsave(s);
8134 retvar = get_tv_string_chk(&argvars[1]);
8135 if (retvar != NULL)
8136 set_var(retvar, &v.di_tv, FALSE);
8137 vim_free(v.di_tv.vval.v_string);
8138 }
8139#else
8140 rettv->vval.v_number = -1;
8141#endif
8142}
8143
8144 static void
8145f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8146{
8147 char_u *r = NULL;
8148
8149#ifdef FEAT_CLIENTSERVER
8150 char_u *serverid = get_tv_string_chk(&argvars[0]);
8151
8152 if (serverid != NULL && !check_restricted() && !check_secure())
8153 {
8154# ifdef WIN32
8155 /* The server's HWND is encoded in the 'id' parameter */
8156 long_u n = 0;
8157
8158 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8159 if (n != 0)
8160 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8161 if (r == NULL)
8162# else
8163 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8164 serverStrToWin(serverid), &r, FALSE) < 0)
8165# endif
8166 EMSG(_("E277: Unable to read a server reply"));
8167 }
8168#endif
8169 rettv->v_type = VAR_STRING;
8170 rettv->vval.v_string = r;
8171}
8172
8173/*
8174 * "remote_send()" function
8175 */
8176 static void
8177f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8178{
8179 rettv->v_type = VAR_STRING;
8180 rettv->vval.v_string = NULL;
8181#ifdef FEAT_CLIENTSERVER
8182 remote_common(argvars, rettv, FALSE);
8183#endif
8184}
8185
8186/*
8187 * "remove()" function
8188 */
8189 static void
8190f_remove(typval_T *argvars, typval_T *rettv)
8191{
8192 list_T *l;
8193 listitem_T *item, *item2;
8194 listitem_T *li;
8195 long idx;
8196 long end;
8197 char_u *key;
8198 dict_T *d;
8199 dictitem_T *di;
8200 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8201
8202 if (argvars[0].v_type == VAR_DICT)
8203 {
8204 if (argvars[2].v_type != VAR_UNKNOWN)
8205 EMSG2(_(e_toomanyarg), "remove()");
8206 else if ((d = argvars[0].vval.v_dict) != NULL
8207 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8208 {
8209 key = get_tv_string_chk(&argvars[1]);
8210 if (key != NULL)
8211 {
8212 di = dict_find(d, key, -1);
8213 if (di == NULL)
8214 EMSG2(_(e_dictkey), key);
8215 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8216 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8217 {
8218 *rettv = di->di_tv;
8219 init_tv(&di->di_tv);
8220 dictitem_remove(d, di);
8221 }
8222 }
8223 }
8224 }
8225 else if (argvars[0].v_type != VAR_LIST)
8226 EMSG2(_(e_listdictarg), "remove()");
8227 else if ((l = argvars[0].vval.v_list) != NULL
8228 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8229 {
8230 int error = FALSE;
8231
8232 idx = (long)get_tv_number_chk(&argvars[1], &error);
8233 if (error)
8234 ; /* type error: do nothing, errmsg already given */
8235 else if ((item = list_find(l, idx)) == NULL)
8236 EMSGN(_(e_listidx), idx);
8237 else
8238 {
8239 if (argvars[2].v_type == VAR_UNKNOWN)
8240 {
8241 /* Remove one item, return its value. */
8242 vimlist_remove(l, item, item);
8243 *rettv = item->li_tv;
8244 vim_free(item);
8245 }
8246 else
8247 {
8248 /* Remove range of items, return list with values. */
8249 end = (long)get_tv_number_chk(&argvars[2], &error);
8250 if (error)
8251 ; /* type error: do nothing */
8252 else if ((item2 = list_find(l, end)) == NULL)
8253 EMSGN(_(e_listidx), end);
8254 else
8255 {
8256 int cnt = 0;
8257
8258 for (li = item; li != NULL; li = li->li_next)
8259 {
8260 ++cnt;
8261 if (li == item2)
8262 break;
8263 }
8264 if (li == NULL) /* didn't find "item2" after "item" */
8265 EMSG(_(e_invrange));
8266 else
8267 {
8268 vimlist_remove(l, item, item2);
8269 if (rettv_list_alloc(rettv) == OK)
8270 {
8271 l = rettv->vval.v_list;
8272 l->lv_first = item;
8273 l->lv_last = item2;
8274 item->li_prev = NULL;
8275 item2->li_next = NULL;
8276 l->lv_len = cnt;
8277 }
8278 }
8279 }
8280 }
8281 }
8282 }
8283}
8284
8285/*
8286 * "rename({from}, {to})" function
8287 */
8288 static void
8289f_rename(typval_T *argvars, typval_T *rettv)
8290{
8291 char_u buf[NUMBUFLEN];
8292
8293 if (check_restricted() || check_secure())
8294 rettv->vval.v_number = -1;
8295 else
8296 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8297 get_tv_string_buf(&argvars[1], buf));
8298}
8299
8300/*
8301 * "repeat()" function
8302 */
8303 static void
8304f_repeat(typval_T *argvars, typval_T *rettv)
8305{
8306 char_u *p;
8307 int n;
8308 int slen;
8309 int len;
8310 char_u *r;
8311 int i;
8312
8313 n = (int)get_tv_number(&argvars[1]);
8314 if (argvars[0].v_type == VAR_LIST)
8315 {
8316 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8317 while (n-- > 0)
8318 if (list_extend(rettv->vval.v_list,
8319 argvars[0].vval.v_list, NULL) == FAIL)
8320 break;
8321 }
8322 else
8323 {
8324 p = get_tv_string(&argvars[0]);
8325 rettv->v_type = VAR_STRING;
8326 rettv->vval.v_string = NULL;
8327
8328 slen = (int)STRLEN(p);
8329 len = slen * n;
8330 if (len <= 0)
8331 return;
8332
8333 r = alloc(len + 1);
8334 if (r != NULL)
8335 {
8336 for (i = 0; i < n; i++)
8337 mch_memmove(r + i * slen, p, (size_t)slen);
8338 r[len] = NUL;
8339 }
8340
8341 rettv->vval.v_string = r;
8342 }
8343}
8344
8345/*
8346 * "resolve()" function
8347 */
8348 static void
8349f_resolve(typval_T *argvars, typval_T *rettv)
8350{
8351 char_u *p;
8352#ifdef HAVE_READLINK
8353 char_u *buf = NULL;
8354#endif
8355
8356 p = get_tv_string(&argvars[0]);
8357#ifdef FEAT_SHORTCUT
8358 {
8359 char_u *v = NULL;
8360
8361 v = mch_resolve_shortcut(p);
8362 if (v != NULL)
8363 rettv->vval.v_string = v;
8364 else
8365 rettv->vval.v_string = vim_strsave(p);
8366 }
8367#else
8368# ifdef HAVE_READLINK
8369 {
8370 char_u *cpy;
8371 int len;
8372 char_u *remain = NULL;
8373 char_u *q;
8374 int is_relative_to_current = FALSE;
8375 int has_trailing_pathsep = FALSE;
8376 int limit = 100;
8377
8378 p = vim_strsave(p);
8379
8380 if (p[0] == '.' && (vim_ispathsep(p[1])
8381 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8382 is_relative_to_current = TRUE;
8383
8384 len = STRLEN(p);
8385 if (len > 0 && after_pathsep(p, p + len))
8386 {
8387 has_trailing_pathsep = TRUE;
8388 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8389 }
8390
8391 q = getnextcomp(p);
8392 if (*q != NUL)
8393 {
8394 /* Separate the first path component in "p", and keep the
8395 * remainder (beginning with the path separator). */
8396 remain = vim_strsave(q - 1);
8397 q[-1] = NUL;
8398 }
8399
8400 buf = alloc(MAXPATHL + 1);
8401 if (buf == NULL)
8402 goto fail;
8403
8404 for (;;)
8405 {
8406 for (;;)
8407 {
8408 len = readlink((char *)p, (char *)buf, MAXPATHL);
8409 if (len <= 0)
8410 break;
8411 buf[len] = NUL;
8412
8413 if (limit-- == 0)
8414 {
8415 vim_free(p);
8416 vim_free(remain);
8417 EMSG(_("E655: Too many symbolic links (cycle?)"));
8418 rettv->vval.v_string = NULL;
8419 goto fail;
8420 }
8421
8422 /* Ensure that the result will have a trailing path separator
8423 * if the argument has one. */
8424 if (remain == NULL && has_trailing_pathsep)
8425 add_pathsep(buf);
8426
8427 /* Separate the first path component in the link value and
8428 * concatenate the remainders. */
8429 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8430 if (*q != NUL)
8431 {
8432 if (remain == NULL)
8433 remain = vim_strsave(q - 1);
8434 else
8435 {
8436 cpy = concat_str(q - 1, remain);
8437 if (cpy != NULL)
8438 {
8439 vim_free(remain);
8440 remain = cpy;
8441 }
8442 }
8443 q[-1] = NUL;
8444 }
8445
8446 q = gettail(p);
8447 if (q > p && *q == NUL)
8448 {
8449 /* Ignore trailing path separator. */
8450 q[-1] = NUL;
8451 q = gettail(p);
8452 }
8453 if (q > p && !mch_isFullName(buf))
8454 {
8455 /* symlink is relative to directory of argument */
8456 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8457 if (cpy != NULL)
8458 {
8459 STRCPY(cpy, p);
8460 STRCPY(gettail(cpy), buf);
8461 vim_free(p);
8462 p = cpy;
8463 }
8464 }
8465 else
8466 {
8467 vim_free(p);
8468 p = vim_strsave(buf);
8469 }
8470 }
8471
8472 if (remain == NULL)
8473 break;
8474
8475 /* Append the first path component of "remain" to "p". */
8476 q = getnextcomp(remain + 1);
8477 len = q - remain - (*q != NUL);
8478 cpy = vim_strnsave(p, STRLEN(p) + len);
8479 if (cpy != NULL)
8480 {
8481 STRNCAT(cpy, remain, len);
8482 vim_free(p);
8483 p = cpy;
8484 }
8485 /* Shorten "remain". */
8486 if (*q != NUL)
8487 STRMOVE(remain, q - 1);
8488 else
8489 {
8490 vim_free(remain);
8491 remain = NULL;
8492 }
8493 }
8494
8495 /* If the result is a relative path name, make it explicitly relative to
8496 * the current directory if and only if the argument had this form. */
8497 if (!vim_ispathsep(*p))
8498 {
8499 if (is_relative_to_current
8500 && *p != NUL
8501 && !(p[0] == '.'
8502 && (p[1] == NUL
8503 || vim_ispathsep(p[1])
8504 || (p[1] == '.'
8505 && (p[2] == NUL
8506 || vim_ispathsep(p[2]))))))
8507 {
8508 /* Prepend "./". */
8509 cpy = concat_str((char_u *)"./", p);
8510 if (cpy != NULL)
8511 {
8512 vim_free(p);
8513 p = cpy;
8514 }
8515 }
8516 else if (!is_relative_to_current)
8517 {
8518 /* Strip leading "./". */
8519 q = p;
8520 while (q[0] == '.' && vim_ispathsep(q[1]))
8521 q += 2;
8522 if (q > p)
8523 STRMOVE(p, p + 2);
8524 }
8525 }
8526
8527 /* Ensure that the result will have no trailing path separator
8528 * if the argument had none. But keep "/" or "//". */
8529 if (!has_trailing_pathsep)
8530 {
8531 q = p + STRLEN(p);
8532 if (after_pathsep(p, q))
8533 *gettail_sep(p) = NUL;
8534 }
8535
8536 rettv->vval.v_string = p;
8537 }
8538# else
8539 rettv->vval.v_string = vim_strsave(p);
8540# endif
8541#endif
8542
8543 simplify_filename(rettv->vval.v_string);
8544
8545#ifdef HAVE_READLINK
8546fail:
8547 vim_free(buf);
8548#endif
8549 rettv->v_type = VAR_STRING;
8550}
8551
8552/*
8553 * "reverse({list})" function
8554 */
8555 static void
8556f_reverse(typval_T *argvars, typval_T *rettv)
8557{
8558 list_T *l;
8559 listitem_T *li, *ni;
8560
8561 if (argvars[0].v_type != VAR_LIST)
8562 EMSG2(_(e_listarg), "reverse()");
8563 else if ((l = argvars[0].vval.v_list) != NULL
8564 && !tv_check_lock(l->lv_lock,
8565 (char_u *)N_("reverse() argument"), TRUE))
8566 {
8567 li = l->lv_last;
8568 l->lv_first = l->lv_last = NULL;
8569 l->lv_len = 0;
8570 while (li != NULL)
8571 {
8572 ni = li->li_prev;
8573 list_append(l, li);
8574 li = ni;
8575 }
8576 rettv->vval.v_list = l;
8577 rettv->v_type = VAR_LIST;
8578 ++l->lv_refcount;
8579 l->lv_idx = l->lv_len - l->lv_idx - 1;
8580 }
8581}
8582
8583#define SP_NOMOVE 0x01 /* don't move cursor */
8584#define SP_REPEAT 0x02 /* repeat to find outer pair */
8585#define SP_RETCOUNT 0x04 /* return matchcount */
8586#define SP_SETPCMARK 0x08 /* set previous context mark */
8587#define SP_START 0x10 /* accept match at start position */
8588#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
8589#define SP_END 0x40 /* leave cursor at end of match */
8590#define SP_COLUMN 0x80 /* start at cursor column */
8591
8592static int get_search_arg(typval_T *varp, int *flagsp);
8593
8594/*
8595 * Get flags for a search function.
8596 * Possibly sets "p_ws".
8597 * Returns BACKWARD, FORWARD or zero (for an error).
8598 */
8599 static int
8600get_search_arg(typval_T *varp, int *flagsp)
8601{
8602 int dir = FORWARD;
8603 char_u *flags;
8604 char_u nbuf[NUMBUFLEN];
8605 int mask;
8606
8607 if (varp->v_type != VAR_UNKNOWN)
8608 {
8609 flags = get_tv_string_buf_chk(varp, nbuf);
8610 if (flags == NULL)
8611 return 0; /* type error; errmsg already given */
8612 while (*flags != NUL)
8613 {
8614 switch (*flags)
8615 {
8616 case 'b': dir = BACKWARD; break;
8617 case 'w': p_ws = TRUE; break;
8618 case 'W': p_ws = FALSE; break;
8619 default: mask = 0;
8620 if (flagsp != NULL)
8621 switch (*flags)
8622 {
8623 case 'c': mask = SP_START; break;
8624 case 'e': mask = SP_END; break;
8625 case 'm': mask = SP_RETCOUNT; break;
8626 case 'n': mask = SP_NOMOVE; break;
8627 case 'p': mask = SP_SUBPAT; break;
8628 case 'r': mask = SP_REPEAT; break;
8629 case 's': mask = SP_SETPCMARK; break;
8630 case 'z': mask = SP_COLUMN; break;
8631 }
8632 if (mask == 0)
8633 {
8634 EMSG2(_(e_invarg2), flags);
8635 dir = 0;
8636 }
8637 else
8638 *flagsp |= mask;
8639 }
8640 if (dir == 0)
8641 break;
8642 ++flags;
8643 }
8644 }
8645 return dir;
8646}
8647
8648/*
8649 * Shared by search() and searchpos() functions.
8650 */
8651 static int
8652search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
8653{
8654 int flags;
8655 char_u *pat;
8656 pos_T pos;
8657 pos_T save_cursor;
8658 int save_p_ws = p_ws;
8659 int dir;
8660 int retval = 0; /* default: FAIL */
8661 long lnum_stop = 0;
8662 proftime_T tm;
8663#ifdef FEAT_RELTIME
8664 long time_limit = 0;
8665#endif
8666 int options = SEARCH_KEEP;
8667 int subpatnum;
8668
8669 pat = get_tv_string(&argvars[0]);
8670 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
8671 if (dir == 0)
8672 goto theend;
8673 flags = *flagsp;
8674 if (flags & SP_START)
8675 options |= SEARCH_START;
8676 if (flags & SP_END)
8677 options |= SEARCH_END;
8678 if (flags & SP_COLUMN)
8679 options |= SEARCH_COL;
8680
8681 /* Optional arguments: line number to stop searching and timeout. */
8682 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
8683 {
8684 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
8685 if (lnum_stop < 0)
8686 goto theend;
8687#ifdef FEAT_RELTIME
8688 if (argvars[3].v_type != VAR_UNKNOWN)
8689 {
8690 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
8691 if (time_limit < 0)
8692 goto theend;
8693 }
8694#endif
8695 }
8696
8697#ifdef FEAT_RELTIME
8698 /* Set the time limit, if there is one. */
8699 profile_setlimit(time_limit, &tm);
8700#endif
8701
8702 /*
8703 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
8704 * Check to make sure only those flags are set.
8705 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
8706 * flags cannot be set. Check for that condition also.
8707 */
8708 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
8709 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
8710 {
8711 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
8712 goto theend;
8713 }
8714
8715 pos = save_cursor = curwin->w_cursor;
8716 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
8717 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
8718 if (subpatnum != FAIL)
8719 {
8720 if (flags & SP_SUBPAT)
8721 retval = subpatnum;
8722 else
8723 retval = pos.lnum;
8724 if (flags & SP_SETPCMARK)
8725 setpcmark();
8726 curwin->w_cursor = pos;
8727 if (match_pos != NULL)
8728 {
8729 /* Store the match cursor position */
8730 match_pos->lnum = pos.lnum;
8731 match_pos->col = pos.col + 1;
8732 }
8733 /* "/$" will put the cursor after the end of the line, may need to
8734 * correct that here */
8735 check_cursor();
8736 }
8737
8738 /* If 'n' flag is used: restore cursor position. */
8739 if (flags & SP_NOMOVE)
8740 curwin->w_cursor = save_cursor;
8741 else
8742 curwin->w_set_curswant = TRUE;
8743theend:
8744 p_ws = save_p_ws;
8745
8746 return retval;
8747}
8748
8749#ifdef FEAT_FLOAT
8750
8751/*
8752 * round() is not in C90, use ceil() or floor() instead.
8753 */
8754 float_T
8755vim_round(float_T f)
8756{
8757 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
8758}
8759
8760/*
8761 * "round({float})" function
8762 */
8763 static void
8764f_round(typval_T *argvars, typval_T *rettv)
8765{
8766 float_T f = 0.0;
8767
8768 rettv->v_type = VAR_FLOAT;
8769 if (get_float_arg(argvars, &f) == OK)
8770 rettv->vval.v_float = vim_round(f);
8771 else
8772 rettv->vval.v_float = 0.0;
8773}
8774#endif
8775
8776/*
8777 * "screenattr()" function
8778 */
8779 static void
8780f_screenattr(typval_T *argvars, typval_T *rettv)
8781{
8782 int row;
8783 int col;
8784 int c;
8785
8786 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
8787 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
8788 if (row < 0 || row >= screen_Rows
8789 || col < 0 || col >= screen_Columns)
8790 c = -1;
8791 else
8792 c = ScreenAttrs[LineOffset[row] + col];
8793 rettv->vval.v_number = c;
8794}
8795
8796/*
8797 * "screenchar()" function
8798 */
8799 static void
8800f_screenchar(typval_T *argvars, typval_T *rettv)
8801{
8802 int row;
8803 int col;
8804 int off;
8805 int c;
8806
8807 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
8808 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
8809 if (row < 0 || row >= screen_Rows
8810 || col < 0 || col >= screen_Columns)
8811 c = -1;
8812 else
8813 {
8814 off = LineOffset[row] + col;
8815#ifdef FEAT_MBYTE
8816 if (enc_utf8 && ScreenLinesUC[off] != 0)
8817 c = ScreenLinesUC[off];
8818 else
8819#endif
8820 c = ScreenLines[off];
8821 }
8822 rettv->vval.v_number = c;
8823}
8824
8825/*
8826 * "screencol()" function
8827 *
8828 * First column is 1 to be consistent with virtcol().
8829 */
8830 static void
8831f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
8832{
8833 rettv->vval.v_number = screen_screencol() + 1;
8834}
8835
8836/*
8837 * "screenrow()" function
8838 */
8839 static void
8840f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
8841{
8842 rettv->vval.v_number = screen_screenrow() + 1;
8843}
8844
8845/*
8846 * "search()" function
8847 */
8848 static void
8849f_search(typval_T *argvars, typval_T *rettv)
8850{
8851 int flags = 0;
8852
8853 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
8854}
8855
8856/*
8857 * "searchdecl()" function
8858 */
8859 static void
8860f_searchdecl(typval_T *argvars, typval_T *rettv)
8861{
8862 int locally = 1;
8863 int thisblock = 0;
8864 int error = FALSE;
8865 char_u *name;
8866
8867 rettv->vval.v_number = 1; /* default: FAIL */
8868
8869 name = get_tv_string_chk(&argvars[0]);
8870 if (argvars[1].v_type != VAR_UNKNOWN)
8871 {
8872 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
8873 if (!error && argvars[2].v_type != VAR_UNKNOWN)
8874 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
8875 }
8876 if (!error && name != NULL)
8877 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
8878 locally, thisblock, SEARCH_KEEP) == FAIL;
8879}
8880
8881/*
8882 * Used by searchpair() and searchpairpos()
8883 */
8884 static int
8885searchpair_cmn(typval_T *argvars, pos_T *match_pos)
8886{
8887 char_u *spat, *mpat, *epat;
8888 char_u *skip;
8889 int save_p_ws = p_ws;
8890 int dir;
8891 int flags = 0;
8892 char_u nbuf1[NUMBUFLEN];
8893 char_u nbuf2[NUMBUFLEN];
8894 char_u nbuf3[NUMBUFLEN];
8895 int retval = 0; /* default: FAIL */
8896 long lnum_stop = 0;
8897 long time_limit = 0;
8898
8899 /* Get the three pattern arguments: start, middle, end. */
8900 spat = get_tv_string_chk(&argvars[0]);
8901 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
8902 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
8903 if (spat == NULL || mpat == NULL || epat == NULL)
8904 goto theend; /* type error */
8905
8906 /* Handle the optional fourth argument: flags */
8907 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
8908 if (dir == 0)
8909 goto theend;
8910
8911 /* Don't accept SP_END or SP_SUBPAT.
8912 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
8913 */
8914 if ((flags & (SP_END | SP_SUBPAT)) != 0
8915 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
8916 {
8917 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
8918 goto theend;
8919 }
8920
8921 /* Using 'r' implies 'W', otherwise it doesn't work. */
8922 if (flags & SP_REPEAT)
8923 p_ws = FALSE;
8924
8925 /* Optional fifth argument: skip expression */
8926 if (argvars[3].v_type == VAR_UNKNOWN
8927 || argvars[4].v_type == VAR_UNKNOWN)
8928 skip = (char_u *)"";
8929 else
8930 {
8931 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
8932 if (argvars[5].v_type != VAR_UNKNOWN)
8933 {
8934 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
8935 if (lnum_stop < 0)
8936 goto theend;
8937#ifdef FEAT_RELTIME
8938 if (argvars[6].v_type != VAR_UNKNOWN)
8939 {
8940 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
8941 if (time_limit < 0)
8942 goto theend;
8943 }
8944#endif
8945 }
8946 }
8947 if (skip == NULL)
8948 goto theend; /* type error */
8949
8950 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
8951 match_pos, lnum_stop, time_limit);
8952
8953theend:
8954 p_ws = save_p_ws;
8955
8956 return retval;
8957}
8958
8959/*
8960 * "searchpair()" function
8961 */
8962 static void
8963f_searchpair(typval_T *argvars, typval_T *rettv)
8964{
8965 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
8966}
8967
8968/*
8969 * "searchpairpos()" function
8970 */
8971 static void
8972f_searchpairpos(typval_T *argvars, typval_T *rettv)
8973{
8974 pos_T match_pos;
8975 int lnum = 0;
8976 int col = 0;
8977
8978 if (rettv_list_alloc(rettv) == FAIL)
8979 return;
8980
8981 if (searchpair_cmn(argvars, &match_pos) > 0)
8982 {
8983 lnum = match_pos.lnum;
8984 col = match_pos.col;
8985 }
8986
8987 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
8988 list_append_number(rettv->vval.v_list, (varnumber_T)col);
8989}
8990
8991/*
8992 * Search for a start/middle/end thing.
8993 * Used by searchpair(), see its documentation for the details.
8994 * Returns 0 or -1 for no match,
8995 */
8996 long
8997do_searchpair(
8998 char_u *spat, /* start pattern */
8999 char_u *mpat, /* middle pattern */
9000 char_u *epat, /* end pattern */
9001 int dir, /* BACKWARD or FORWARD */
9002 char_u *skip, /* skip expression */
9003 int flags, /* SP_SETPCMARK and other SP_ values */
9004 pos_T *match_pos,
9005 linenr_T lnum_stop, /* stop at this line if not zero */
9006 long time_limit UNUSED) /* stop after this many msec */
9007{
9008 char_u *save_cpo;
9009 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9010 long retval = 0;
9011 pos_T pos;
9012 pos_T firstpos;
9013 pos_T foundpos;
9014 pos_T save_cursor;
9015 pos_T save_pos;
9016 int n;
9017 int r;
9018 int nest = 1;
9019 int err;
9020 int options = SEARCH_KEEP;
9021 proftime_T tm;
9022
9023 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9024 save_cpo = p_cpo;
9025 p_cpo = empty_option;
9026
9027#ifdef FEAT_RELTIME
9028 /* Set the time limit, if there is one. */
9029 profile_setlimit(time_limit, &tm);
9030#endif
9031
9032 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9033 * start/middle/end (pat3, for the top pair). */
9034 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
9035 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
9036 if (pat2 == NULL || pat3 == NULL)
9037 goto theend;
9038 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
9039 if (*mpat == NUL)
9040 STRCPY(pat3, pat2);
9041 else
9042 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
9043 spat, epat, mpat);
9044 if (flags & SP_START)
9045 options |= SEARCH_START;
9046
9047 save_cursor = curwin->w_cursor;
9048 pos = curwin->w_cursor;
9049 clearpos(&firstpos);
9050 clearpos(&foundpos);
9051 pat = pat3;
9052 for (;;)
9053 {
9054 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9055 options, RE_SEARCH, lnum_stop, &tm);
9056 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9057 /* didn't find it or found the first match again: FAIL */
9058 break;
9059
9060 if (firstpos.lnum == 0)
9061 firstpos = pos;
9062 if (equalpos(pos, foundpos))
9063 {
9064 /* Found the same position again. Can happen with a pattern that
9065 * has "\zs" at the end and searching backwards. Advance one
9066 * character and try again. */
9067 if (dir == BACKWARD)
9068 decl(&pos);
9069 else
9070 incl(&pos);
9071 }
9072 foundpos = pos;
9073
9074 /* clear the start flag to avoid getting stuck here */
9075 options &= ~SEARCH_START;
9076
9077 /* If the skip pattern matches, ignore this match. */
9078 if (*skip != NUL)
9079 {
9080 save_pos = curwin->w_cursor;
9081 curwin->w_cursor = pos;
9082 r = eval_to_bool(skip, &err, NULL, FALSE);
9083 curwin->w_cursor = save_pos;
9084 if (err)
9085 {
9086 /* Evaluating {skip} caused an error, break here. */
9087 curwin->w_cursor = save_cursor;
9088 retval = -1;
9089 break;
9090 }
9091 if (r)
9092 continue;
9093 }
9094
9095 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9096 {
9097 /* Found end when searching backwards or start when searching
9098 * forward: nested pair. */
9099 ++nest;
9100 pat = pat2; /* nested, don't search for middle */
9101 }
9102 else
9103 {
9104 /* Found end when searching forward or start when searching
9105 * backward: end of (nested) pair; or found middle in outer pair. */
9106 if (--nest == 1)
9107 pat = pat3; /* outer level, search for middle */
9108 }
9109
9110 if (nest == 0)
9111 {
9112 /* Found the match: return matchcount or line number. */
9113 if (flags & SP_RETCOUNT)
9114 ++retval;
9115 else
9116 retval = pos.lnum;
9117 if (flags & SP_SETPCMARK)
9118 setpcmark();
9119 curwin->w_cursor = pos;
9120 if (!(flags & SP_REPEAT))
9121 break;
9122 nest = 1; /* search for next unmatched */
9123 }
9124 }
9125
9126 if (match_pos != NULL)
9127 {
9128 /* Store the match cursor position */
9129 match_pos->lnum = curwin->w_cursor.lnum;
9130 match_pos->col = curwin->w_cursor.col + 1;
9131 }
9132
9133 /* If 'n' flag is used or search failed: restore cursor position. */
9134 if ((flags & SP_NOMOVE) || retval == 0)
9135 curwin->w_cursor = save_cursor;
9136
9137theend:
9138 vim_free(pat2);
9139 vim_free(pat3);
9140 if (p_cpo == empty_option)
9141 p_cpo = save_cpo;
9142 else
9143 /* Darn, evaluating the {skip} expression changed the value. */
9144 free_string_option(save_cpo);
9145
9146 return retval;
9147}
9148
9149/*
9150 * "searchpos()" function
9151 */
9152 static void
9153f_searchpos(typval_T *argvars, typval_T *rettv)
9154{
9155 pos_T match_pos;
9156 int lnum = 0;
9157 int col = 0;
9158 int n;
9159 int flags = 0;
9160
9161 if (rettv_list_alloc(rettv) == FAIL)
9162 return;
9163
9164 n = search_cmn(argvars, &match_pos, &flags);
9165 if (n > 0)
9166 {
9167 lnum = match_pos.lnum;
9168 col = match_pos.col;
9169 }
9170
9171 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9172 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9173 if (flags & SP_SUBPAT)
9174 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9175}
9176
9177 static void
9178f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9179{
9180#ifdef FEAT_CLIENTSERVER
9181 char_u buf[NUMBUFLEN];
9182 char_u *server = get_tv_string_chk(&argvars[0]);
9183 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9184
9185 rettv->vval.v_number = -1;
9186 if (server == NULL || reply == NULL)
9187 return;
9188 if (check_restricted() || check_secure())
9189 return;
9190# ifdef FEAT_X11
9191 if (check_connection() == FAIL)
9192 return;
9193# endif
9194
9195 if (serverSendReply(server, reply) < 0)
9196 {
9197 EMSG(_("E258: Unable to send to client"));
9198 return;
9199 }
9200 rettv->vval.v_number = 0;
9201#else
9202 rettv->vval.v_number = -1;
9203#endif
9204}
9205
9206 static void
9207f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9208{
9209 char_u *r = NULL;
9210
9211#ifdef FEAT_CLIENTSERVER
9212# ifdef WIN32
9213 r = serverGetVimNames();
9214# else
9215 make_connection();
9216 if (X_DISPLAY != NULL)
9217 r = serverGetVimNames(X_DISPLAY);
9218# endif
9219#endif
9220 rettv->v_type = VAR_STRING;
9221 rettv->vval.v_string = r;
9222}
9223
9224/*
9225 * "setbufvar()" function
9226 */
9227 static void
9228f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9229{
9230 buf_T *buf;
9231 char_u *varname, *bufvarname;
9232 typval_T *varp;
9233 char_u nbuf[NUMBUFLEN];
9234
9235 if (check_restricted() || check_secure())
9236 return;
9237 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9238 varname = get_tv_string_chk(&argvars[1]);
9239 buf = get_buf_tv(&argvars[0], FALSE);
9240 varp = &argvars[2];
9241
9242 if (buf != NULL && varname != NULL && varp != NULL)
9243 {
9244 if (*varname == '&')
9245 {
9246 long numval;
9247 char_u *strval;
9248 int error = FALSE;
9249 aco_save_T aco;
9250
9251 /* set curbuf to be our buf, temporarily */
9252 aucmd_prepbuf(&aco, buf);
9253
9254 ++varname;
9255 numval = (long)get_tv_number_chk(varp, &error);
9256 strval = get_tv_string_buf_chk(varp, nbuf);
9257 if (!error && strval != NULL)
9258 set_option_value(varname, numval, strval, OPT_LOCAL);
9259
9260 /* reset notion of buffer */
9261 aucmd_restbuf(&aco);
9262 }
9263 else
9264 {
9265 buf_T *save_curbuf = curbuf;
9266
9267 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9268 if (bufvarname != NULL)
9269 {
9270 curbuf = buf;
9271 STRCPY(bufvarname, "b:");
9272 STRCPY(bufvarname + 2, varname);
9273 set_var(bufvarname, varp, TRUE);
9274 vim_free(bufvarname);
9275 curbuf = save_curbuf;
9276 }
9277 }
9278 }
9279}
9280
9281 static void
9282f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9283{
9284 dict_T *d;
9285 dictitem_T *di;
9286 char_u *csearch;
9287
9288 if (argvars[0].v_type != VAR_DICT)
9289 {
9290 EMSG(_(e_dictreq));
9291 return;
9292 }
9293
9294 if ((d = argvars[0].vval.v_dict) != NULL)
9295 {
9296 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9297 if (csearch != NULL)
9298 {
9299#ifdef FEAT_MBYTE
9300 if (enc_utf8)
9301 {
9302 int pcc[MAX_MCO];
9303 int c = utfc_ptr2char(csearch, pcc);
9304
9305 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9306 }
9307 else
9308#endif
9309 set_last_csearch(PTR2CHAR(csearch),
9310 csearch, MB_PTR2LEN(csearch));
9311 }
9312
9313 di = dict_find(d, (char_u *)"forward", -1);
9314 if (di != NULL)
9315 set_csearch_direction((int)get_tv_number(&di->di_tv)
9316 ? FORWARD : BACKWARD);
9317
9318 di = dict_find(d, (char_u *)"until", -1);
9319 if (di != NULL)
9320 set_csearch_until(!!get_tv_number(&di->di_tv));
9321 }
9322}
9323
9324/*
9325 * "setcmdpos()" function
9326 */
9327 static void
9328f_setcmdpos(typval_T *argvars, typval_T *rettv)
9329{
9330 int pos = (int)get_tv_number(&argvars[0]) - 1;
9331
9332 if (pos >= 0)
9333 rettv->vval.v_number = set_cmdline_pos(pos);
9334}
9335
9336/*
9337 * "setfperm({fname}, {mode})" function
9338 */
9339 static void
9340f_setfperm(typval_T *argvars, typval_T *rettv)
9341{
9342 char_u *fname;
9343 char_u modebuf[NUMBUFLEN];
9344 char_u *mode_str;
9345 int i;
9346 int mask;
9347 int mode = 0;
9348
9349 rettv->vval.v_number = 0;
9350 fname = get_tv_string_chk(&argvars[0]);
9351 if (fname == NULL)
9352 return;
9353 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9354 if (mode_str == NULL)
9355 return;
9356 if (STRLEN(mode_str) != 9)
9357 {
9358 EMSG2(_(e_invarg2), mode_str);
9359 return;
9360 }
9361
9362 mask = 1;
9363 for (i = 8; i >= 0; --i)
9364 {
9365 if (mode_str[i] != '-')
9366 mode |= mask;
9367 mask = mask << 1;
9368 }
9369 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9370}
9371
9372/*
9373 * "setline()" function
9374 */
9375 static void
9376f_setline(typval_T *argvars, typval_T *rettv)
9377{
9378 linenr_T lnum;
9379 char_u *line = NULL;
9380 list_T *l = NULL;
9381 listitem_T *li = NULL;
9382 long added = 0;
9383 linenr_T lcount = curbuf->b_ml.ml_line_count;
9384
9385 lnum = get_tv_lnum(&argvars[0]);
9386 if (argvars[1].v_type == VAR_LIST)
9387 {
9388 l = argvars[1].vval.v_list;
9389 li = l->lv_first;
9390 }
9391 else
9392 line = get_tv_string_chk(&argvars[1]);
9393
9394 /* default result is zero == OK */
9395 for (;;)
9396 {
9397 if (l != NULL)
9398 {
9399 /* list argument, get next string */
9400 if (li == NULL)
9401 break;
9402 line = get_tv_string_chk(&li->li_tv);
9403 li = li->li_next;
9404 }
9405
9406 rettv->vval.v_number = 1; /* FAIL */
9407 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9408 break;
9409
9410 /* When coming here from Insert mode, sync undo, so that this can be
9411 * undone separately from what was previously inserted. */
9412 if (u_sync_once == 2)
9413 {
9414 u_sync_once = 1; /* notify that u_sync() was called */
9415 u_sync(TRUE);
9416 }
9417
9418 if (lnum <= curbuf->b_ml.ml_line_count)
9419 {
9420 /* existing line, replace it */
9421 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9422 {
9423 changed_bytes(lnum, 0);
9424 if (lnum == curwin->w_cursor.lnum)
9425 check_cursor_col();
9426 rettv->vval.v_number = 0; /* OK */
9427 }
9428 }
9429 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
9430 {
9431 /* lnum is one past the last line, append the line */
9432 ++added;
9433 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
9434 rettv->vval.v_number = 0; /* OK */
9435 }
9436
9437 if (l == NULL) /* only one string argument */
9438 break;
9439 ++lnum;
9440 }
9441
9442 if (added > 0)
9443 appended_lines_mark(lcount, added);
9444}
9445
9446static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv);
9447
9448/*
9449 * Used by "setqflist()" and "setloclist()" functions
9450 */
9451 static void
9452set_qf_ll_list(
9453 win_T *wp UNUSED,
9454 typval_T *list_arg UNUSED,
9455 typval_T *action_arg UNUSED,
9456 typval_T *rettv)
9457{
9458#ifdef FEAT_QUICKFIX
9459 static char *e_invact = N_("E927: Invalid action: '%s'");
9460 char_u *act;
9461 int action = 0;
9462#endif
9463
9464 rettv->vval.v_number = -1;
9465
9466#ifdef FEAT_QUICKFIX
9467 if (list_arg->v_type != VAR_LIST)
9468 EMSG(_(e_listreq));
9469 else
9470 {
9471 list_T *l = list_arg->vval.v_list;
9472
9473 if (action_arg->v_type == VAR_STRING)
9474 {
9475 act = get_tv_string_chk(action_arg);
9476 if (act == NULL)
9477 return; /* type error; errmsg already given */
9478 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
9479 action = *act;
9480 else
9481 EMSG2(_(e_invact), act);
9482 }
9483 else if (action_arg->v_type == VAR_UNKNOWN)
9484 action = ' ';
9485 else
9486 EMSG(_(e_stringreq));
9487
9488 if (l != NULL && action && set_errorlist(wp, l, action,
9489 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
9490 rettv->vval.v_number = 0;
9491 }
9492#endif
9493}
9494
9495/*
9496 * "setloclist()" function
9497 */
9498 static void
9499f_setloclist(typval_T *argvars, typval_T *rettv)
9500{
9501 win_T *win;
9502
9503 rettv->vval.v_number = -1;
9504
9505 win = find_win_by_nr(&argvars[0], NULL);
9506 if (win != NULL)
9507 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
9508}
9509
9510/*
9511 * "setmatches()" function
9512 */
9513 static void
9514f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9515{
9516#ifdef FEAT_SEARCH_EXTRA
9517 list_T *l;
9518 listitem_T *li;
9519 dict_T *d;
9520 list_T *s = NULL;
9521
9522 rettv->vval.v_number = -1;
9523 if (argvars[0].v_type != VAR_LIST)
9524 {
9525 EMSG(_(e_listreq));
9526 return;
9527 }
9528 if ((l = argvars[0].vval.v_list) != NULL)
9529 {
9530
9531 /* To some extent make sure that we are dealing with a list from
9532 * "getmatches()". */
9533 li = l->lv_first;
9534 while (li != NULL)
9535 {
9536 if (li->li_tv.v_type != VAR_DICT
9537 || (d = li->li_tv.vval.v_dict) == NULL)
9538 {
9539 EMSG(_(e_invarg));
9540 return;
9541 }
9542 if (!(dict_find(d, (char_u *)"group", -1) != NULL
9543 && (dict_find(d, (char_u *)"pattern", -1) != NULL
9544 || dict_find(d, (char_u *)"pos1", -1) != NULL)
9545 && dict_find(d, (char_u *)"priority", -1) != NULL
9546 && dict_find(d, (char_u *)"id", -1) != NULL))
9547 {
9548 EMSG(_(e_invarg));
9549 return;
9550 }
9551 li = li->li_next;
9552 }
9553
9554 clear_matches(curwin);
9555 li = l->lv_first;
9556 while (li != NULL)
9557 {
9558 int i = 0;
9559 char_u buf[5];
9560 dictitem_T *di;
9561 char_u *group;
9562 int priority;
9563 int id;
9564 char_u *conceal;
9565
9566 d = li->li_tv.vval.v_dict;
9567 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
9568 {
9569 if (s == NULL)
9570 {
9571 s = list_alloc();
9572 if (s == NULL)
9573 return;
9574 }
9575
9576 /* match from matchaddpos() */
9577 for (i = 1; i < 9; i++)
9578 {
9579 sprintf((char *)buf, (char *)"pos%d", i);
9580 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
9581 {
9582 if (di->di_tv.v_type != VAR_LIST)
9583 return;
9584
9585 list_append_tv(s, &di->di_tv);
9586 s->lv_refcount++;
9587 }
9588 else
9589 break;
9590 }
9591 }
9592
9593 group = get_dict_string(d, (char_u *)"group", FALSE);
9594 priority = (int)get_dict_number(d, (char_u *)"priority");
9595 id = (int)get_dict_number(d, (char_u *)"id");
9596 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
9597 ? get_dict_string(d, (char_u *)"conceal", FALSE)
9598 : NULL;
9599 if (i == 0)
9600 {
9601 match_add(curwin, group,
9602 get_dict_string(d, (char_u *)"pattern", FALSE),
9603 priority, id, NULL, conceal);
9604 }
9605 else
9606 {
9607 match_add(curwin, group, NULL, priority, id, s, conceal);
9608 list_unref(s);
9609 s = NULL;
9610 }
9611
9612 li = li->li_next;
9613 }
9614 rettv->vval.v_number = 0;
9615 }
9616#endif
9617}
9618
9619/*
9620 * "setpos()" function
9621 */
9622 static void
9623f_setpos(typval_T *argvars, typval_T *rettv)
9624{
9625 pos_T pos;
9626 int fnum;
9627 char_u *name;
9628 colnr_T curswant = -1;
9629
9630 rettv->vval.v_number = -1;
9631 name = get_tv_string_chk(argvars);
9632 if (name != NULL)
9633 {
9634 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
9635 {
9636 if (--pos.col < 0)
9637 pos.col = 0;
9638 if (name[0] == '.' && name[1] == NUL)
9639 {
9640 /* set cursor */
9641 if (fnum == curbuf->b_fnum)
9642 {
9643 curwin->w_cursor = pos;
9644 if (curswant >= 0)
9645 {
9646 curwin->w_curswant = curswant - 1;
9647 curwin->w_set_curswant = FALSE;
9648 }
9649 check_cursor();
9650 rettv->vval.v_number = 0;
9651 }
9652 else
9653 EMSG(_(e_invarg));
9654 }
9655 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
9656 {
9657 /* set mark */
9658 if (setmark_pos(name[1], &pos, fnum) == OK)
9659 rettv->vval.v_number = 0;
9660 }
9661 else
9662 EMSG(_(e_invarg));
9663 }
9664 }
9665}
9666
9667/*
9668 * "setqflist()" function
9669 */
9670 static void
9671f_setqflist(typval_T *argvars, typval_T *rettv)
9672{
9673 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
9674}
9675
9676/*
9677 * "setreg()" function
9678 */
9679 static void
9680f_setreg(typval_T *argvars, typval_T *rettv)
9681{
9682 int regname;
9683 char_u *strregname;
9684 char_u *stropt;
9685 char_u *strval;
9686 int append;
9687 char_u yank_type;
9688 long block_len;
9689
9690 block_len = -1;
9691 yank_type = MAUTO;
9692 append = FALSE;
9693
9694 strregname = get_tv_string_chk(argvars);
9695 rettv->vval.v_number = 1; /* FAIL is default */
9696
9697 if (strregname == NULL)
9698 return; /* type error; errmsg already given */
9699 regname = *strregname;
9700 if (regname == 0 || regname == '@')
9701 regname = '"';
9702
9703 if (argvars[2].v_type != VAR_UNKNOWN)
9704 {
9705 stropt = get_tv_string_chk(&argvars[2]);
9706 if (stropt == NULL)
9707 return; /* type error */
9708 for (; *stropt != NUL; ++stropt)
9709 switch (*stropt)
9710 {
9711 case 'a': case 'A': /* append */
9712 append = TRUE;
9713 break;
9714 case 'v': case 'c': /* character-wise selection */
9715 yank_type = MCHAR;
9716 break;
9717 case 'V': case 'l': /* line-wise selection */
9718 yank_type = MLINE;
9719 break;
9720 case 'b': case Ctrl_V: /* block-wise selection */
9721 yank_type = MBLOCK;
9722 if (VIM_ISDIGIT(stropt[1]))
9723 {
9724 ++stropt;
9725 block_len = getdigits(&stropt) - 1;
9726 --stropt;
9727 }
9728 break;
9729 }
9730 }
9731
9732 if (argvars[1].v_type == VAR_LIST)
9733 {
9734 char_u **lstval;
9735 char_u **allocval;
9736 char_u buf[NUMBUFLEN];
9737 char_u **curval;
9738 char_u **curallocval;
9739 list_T *ll = argvars[1].vval.v_list;
9740 listitem_T *li;
9741 int len;
9742
9743 /* If the list is NULL handle like an empty list. */
9744 len = ll == NULL ? 0 : ll->lv_len;
9745
9746 /* First half: use for pointers to result lines; second half: use for
9747 * pointers to allocated copies. */
9748 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
9749 if (lstval == NULL)
9750 return;
9751 curval = lstval;
9752 allocval = lstval + len + 2;
9753 curallocval = allocval;
9754
9755 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
9756 li = li->li_next)
9757 {
9758 strval = get_tv_string_buf_chk(&li->li_tv, buf);
9759 if (strval == NULL)
9760 goto free_lstval;
9761 if (strval == buf)
9762 {
9763 /* Need to make a copy, next get_tv_string_buf_chk() will
9764 * overwrite the string. */
9765 strval = vim_strsave(buf);
9766 if (strval == NULL)
9767 goto free_lstval;
9768 *curallocval++ = strval;
9769 }
9770 *curval++ = strval;
9771 }
9772 *curval++ = NULL;
9773
9774 write_reg_contents_lst(regname, lstval, -1,
9775 append, yank_type, block_len);
9776free_lstval:
9777 while (curallocval > allocval)
9778 vim_free(*--curallocval);
9779 vim_free(lstval);
9780 }
9781 else
9782 {
9783 strval = get_tv_string_chk(&argvars[1]);
9784 if (strval == NULL)
9785 return;
9786 write_reg_contents_ex(regname, strval, -1,
9787 append, yank_type, block_len);
9788 }
9789 rettv->vval.v_number = 0;
9790}
9791
9792/*
9793 * "settabvar()" function
9794 */
9795 static void
9796f_settabvar(typval_T *argvars, typval_T *rettv)
9797{
9798#ifdef FEAT_WINDOWS
9799 tabpage_T *save_curtab;
9800 tabpage_T *tp;
9801#endif
9802 char_u *varname, *tabvarname;
9803 typval_T *varp;
9804
9805 rettv->vval.v_number = 0;
9806
9807 if (check_restricted() || check_secure())
9808 return;
9809
9810#ifdef FEAT_WINDOWS
9811 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
9812#endif
9813 varname = get_tv_string_chk(&argvars[1]);
9814 varp = &argvars[2];
9815
9816 if (varname != NULL && varp != NULL
9817#ifdef FEAT_WINDOWS
9818 && tp != NULL
9819#endif
9820 )
9821 {
9822#ifdef FEAT_WINDOWS
9823 save_curtab = curtab;
9824 goto_tabpage_tp(tp, FALSE, FALSE);
9825#endif
9826
9827 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
9828 if (tabvarname != NULL)
9829 {
9830 STRCPY(tabvarname, "t:");
9831 STRCPY(tabvarname + 2, varname);
9832 set_var(tabvarname, varp, TRUE);
9833 vim_free(tabvarname);
9834 }
9835
9836#ifdef FEAT_WINDOWS
9837 /* Restore current tabpage */
9838 if (valid_tabpage(save_curtab))
9839 goto_tabpage_tp(save_curtab, FALSE, FALSE);
9840#endif
9841 }
9842}
9843
9844/*
9845 * "settabwinvar()" function
9846 */
9847 static void
9848f_settabwinvar(typval_T *argvars, typval_T *rettv)
9849{
9850 setwinvar(argvars, rettv, 1);
9851}
9852
9853/*
9854 * "setwinvar()" function
9855 */
9856 static void
9857f_setwinvar(typval_T *argvars, typval_T *rettv)
9858{
9859 setwinvar(argvars, rettv, 0);
9860}
9861
9862#ifdef FEAT_CRYPT
9863/*
9864 * "sha256({string})" function
9865 */
9866 static void
9867f_sha256(typval_T *argvars, typval_T *rettv)
9868{
9869 char_u *p;
9870
9871 p = get_tv_string(&argvars[0]);
9872 rettv->vval.v_string = vim_strsave(
9873 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
9874 rettv->v_type = VAR_STRING;
9875}
9876#endif /* FEAT_CRYPT */
9877
9878/*
9879 * "shellescape({string})" function
9880 */
9881 static void
9882f_shellescape(typval_T *argvars, typval_T *rettv)
9883{
9884 rettv->vval.v_string = vim_strsave_shellescape(
9885 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
9886 rettv->v_type = VAR_STRING;
9887}
9888
9889/*
9890 * shiftwidth() function
9891 */
9892 static void
9893f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
9894{
9895 rettv->vval.v_number = get_sw_value(curbuf);
9896}
9897
9898/*
9899 * "simplify()" function
9900 */
9901 static void
9902f_simplify(typval_T *argvars, typval_T *rettv)
9903{
9904 char_u *p;
9905
9906 p = get_tv_string(&argvars[0]);
9907 rettv->vval.v_string = vim_strsave(p);
9908 simplify_filename(rettv->vval.v_string); /* simplify in place */
9909 rettv->v_type = VAR_STRING;
9910}
9911
9912#ifdef FEAT_FLOAT
9913/*
9914 * "sin()" function
9915 */
9916 static void
9917f_sin(typval_T *argvars, typval_T *rettv)
9918{
9919 float_T f = 0.0;
9920
9921 rettv->v_type = VAR_FLOAT;
9922 if (get_float_arg(argvars, &f) == OK)
9923 rettv->vval.v_float = sin(f);
9924 else
9925 rettv->vval.v_float = 0.0;
9926}
9927
9928/*
9929 * "sinh()" function
9930 */
9931 static void
9932f_sinh(typval_T *argvars, typval_T *rettv)
9933{
9934 float_T f = 0.0;
9935
9936 rettv->v_type = VAR_FLOAT;
9937 if (get_float_arg(argvars, &f) == OK)
9938 rettv->vval.v_float = sinh(f);
9939 else
9940 rettv->vval.v_float = 0.0;
9941}
9942#endif
9943
9944static int
9945#ifdef __BORLANDC__
9946 _RTLENTRYF
9947#endif
9948 item_compare(const void *s1, const void *s2);
9949static int
9950#ifdef __BORLANDC__
9951 _RTLENTRYF
9952#endif
9953 item_compare2(const void *s1, const void *s2);
9954
9955/* struct used in the array that's given to qsort() */
9956typedef struct
9957{
9958 listitem_T *item;
9959 int idx;
9960} sortItem_T;
9961
9962/* struct storing information about current sort */
9963typedef struct
9964{
9965 int item_compare_ic;
9966 int item_compare_numeric;
9967 int item_compare_numbers;
9968#ifdef FEAT_FLOAT
9969 int item_compare_float;
9970#endif
9971 char_u *item_compare_func;
9972 partial_T *item_compare_partial;
9973 dict_T *item_compare_selfdict;
9974 int item_compare_func_err;
9975 int item_compare_keep_zero;
9976} sortinfo_T;
9977static sortinfo_T *sortinfo = NULL;
9978static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
9979#define ITEM_COMPARE_FAIL 999
9980
9981/*
9982 * Compare functions for f_sort() and f_uniq() below.
9983 */
9984 static int
9985#ifdef __BORLANDC__
9986_RTLENTRYF
9987#endif
9988item_compare(const void *s1, const void *s2)
9989{
9990 sortItem_T *si1, *si2;
9991 typval_T *tv1, *tv2;
9992 char_u *p1, *p2;
9993 char_u *tofree1 = NULL, *tofree2 = NULL;
9994 int res;
9995 char_u numbuf1[NUMBUFLEN];
9996 char_u numbuf2[NUMBUFLEN];
9997
9998 si1 = (sortItem_T *)s1;
9999 si2 = (sortItem_T *)s2;
10000 tv1 = &si1->item->li_tv;
10001 tv2 = &si2->item->li_tv;
10002
10003 if (sortinfo->item_compare_numbers)
10004 {
10005 varnumber_T v1 = get_tv_number(tv1);
10006 varnumber_T v2 = get_tv_number(tv2);
10007
10008 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10009 }
10010
10011#ifdef FEAT_FLOAT
10012 if (sortinfo->item_compare_float)
10013 {
10014 float_T v1 = get_tv_float(tv1);
10015 float_T v2 = get_tv_float(tv2);
10016
10017 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10018 }
10019#endif
10020
10021 /* tv2string() puts quotes around a string and allocates memory. Don't do
10022 * that for string variables. Use a single quote when comparing with a
10023 * non-string to do what the docs promise. */
10024 if (tv1->v_type == VAR_STRING)
10025 {
10026 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10027 p1 = (char_u *)"'";
10028 else
10029 p1 = tv1->vval.v_string;
10030 }
10031 else
10032 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10033 if (tv2->v_type == VAR_STRING)
10034 {
10035 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10036 p2 = (char_u *)"'";
10037 else
10038 p2 = tv2->vval.v_string;
10039 }
10040 else
10041 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10042 if (p1 == NULL)
10043 p1 = (char_u *)"";
10044 if (p2 == NULL)
10045 p2 = (char_u *)"";
10046 if (!sortinfo->item_compare_numeric)
10047 {
10048 if (sortinfo->item_compare_ic)
10049 res = STRICMP(p1, p2);
10050 else
10051 res = STRCMP(p1, p2);
10052 }
10053 else
10054 {
10055 double n1, n2;
10056 n1 = strtod((char *)p1, (char **)&p1);
10057 n2 = strtod((char *)p2, (char **)&p2);
10058 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10059 }
10060
10061 /* When the result would be zero, compare the item indexes. Makes the
10062 * sort stable. */
10063 if (res == 0 && !sortinfo->item_compare_keep_zero)
10064 res = si1->idx > si2->idx ? 1 : -1;
10065
10066 vim_free(tofree1);
10067 vim_free(tofree2);
10068 return res;
10069}
10070
10071 static int
10072#ifdef __BORLANDC__
10073_RTLENTRYF
10074#endif
10075item_compare2(const void *s1, const void *s2)
10076{
10077 sortItem_T *si1, *si2;
10078 int res;
10079 typval_T rettv;
10080 typval_T argv[3];
10081 int dummy;
10082 char_u *func_name;
10083 partial_T *partial = sortinfo->item_compare_partial;
10084
10085 /* shortcut after failure in previous call; compare all items equal */
10086 if (sortinfo->item_compare_func_err)
10087 return 0;
10088
10089 si1 = (sortItem_T *)s1;
10090 si2 = (sortItem_T *)s2;
10091
10092 if (partial == NULL)
10093 func_name = sortinfo->item_compare_func;
10094 else
10095 func_name = partial->pt_name;
10096
10097 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10098 * in the copy without changing the original list items. */
10099 copy_tv(&si1->item->li_tv, &argv[0]);
10100 copy_tv(&si2->item->li_tv, &argv[1]);
10101
10102 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10103 res = call_func(func_name, (int)STRLEN(func_name),
10104 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
10105 partial, sortinfo->item_compare_selfdict);
10106 clear_tv(&argv[0]);
10107 clear_tv(&argv[1]);
10108
10109 if (res == FAIL)
10110 res = ITEM_COMPARE_FAIL;
10111 else
10112 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10113 if (sortinfo->item_compare_func_err)
10114 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10115 clear_tv(&rettv);
10116
10117 /* When the result would be zero, compare the pointers themselves. Makes
10118 * the sort stable. */
10119 if (res == 0 && !sortinfo->item_compare_keep_zero)
10120 res = si1->idx > si2->idx ? 1 : -1;
10121
10122 return res;
10123}
10124
10125/*
10126 * "sort({list})" function
10127 */
10128 static void
10129do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10130{
10131 list_T *l;
10132 listitem_T *li;
10133 sortItem_T *ptrs;
10134 sortinfo_T *old_sortinfo;
10135 sortinfo_T info;
10136 long len;
10137 long i;
10138
10139 /* Pointer to current info struct used in compare function. Save and
10140 * restore the current one for nested calls. */
10141 old_sortinfo = sortinfo;
10142 sortinfo = &info;
10143
10144 if (argvars[0].v_type != VAR_LIST)
10145 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10146 else
10147 {
10148 l = argvars[0].vval.v_list;
10149 if (l == NULL || tv_check_lock(l->lv_lock,
10150 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10151 TRUE))
10152 goto theend;
10153 rettv->vval.v_list = l;
10154 rettv->v_type = VAR_LIST;
10155 ++l->lv_refcount;
10156
10157 len = list_len(l);
10158 if (len <= 1)
10159 goto theend; /* short list sorts pretty quickly */
10160
10161 info.item_compare_ic = FALSE;
10162 info.item_compare_numeric = FALSE;
10163 info.item_compare_numbers = FALSE;
10164#ifdef FEAT_FLOAT
10165 info.item_compare_float = FALSE;
10166#endif
10167 info.item_compare_func = NULL;
10168 info.item_compare_partial = NULL;
10169 info.item_compare_selfdict = NULL;
10170 if (argvars[1].v_type != VAR_UNKNOWN)
10171 {
10172 /* optional second argument: {func} */
10173 if (argvars[1].v_type == VAR_FUNC)
10174 info.item_compare_func = argvars[1].vval.v_string;
10175 else if (argvars[1].v_type == VAR_PARTIAL)
10176 info.item_compare_partial = argvars[1].vval.v_partial;
10177 else
10178 {
10179 int error = FALSE;
10180
10181 i = (long)get_tv_number_chk(&argvars[1], &error);
10182 if (error)
10183 goto theend; /* type error; errmsg already given */
10184 if (i == 1)
10185 info.item_compare_ic = TRUE;
10186 else if (argvars[1].v_type != VAR_NUMBER)
10187 info.item_compare_func = get_tv_string(&argvars[1]);
10188 else if (i != 0)
10189 {
10190 EMSG(_(e_invarg));
10191 goto theend;
10192 }
10193 if (info.item_compare_func != NULL)
10194 {
10195 if (*info.item_compare_func == NUL)
10196 {
10197 /* empty string means default sort */
10198 info.item_compare_func = NULL;
10199 }
10200 else if (STRCMP(info.item_compare_func, "n") == 0)
10201 {
10202 info.item_compare_func = NULL;
10203 info.item_compare_numeric = TRUE;
10204 }
10205 else if (STRCMP(info.item_compare_func, "N") == 0)
10206 {
10207 info.item_compare_func = NULL;
10208 info.item_compare_numbers = TRUE;
10209 }
10210#ifdef FEAT_FLOAT
10211 else if (STRCMP(info.item_compare_func, "f") == 0)
10212 {
10213 info.item_compare_func = NULL;
10214 info.item_compare_float = TRUE;
10215 }
10216#endif
10217 else if (STRCMP(info.item_compare_func, "i") == 0)
10218 {
10219 info.item_compare_func = NULL;
10220 info.item_compare_ic = TRUE;
10221 }
10222 }
10223 }
10224
10225 if (argvars[2].v_type != VAR_UNKNOWN)
10226 {
10227 /* optional third argument: {dict} */
10228 if (argvars[2].v_type != VAR_DICT)
10229 {
10230 EMSG(_(e_dictreq));
10231 goto theend;
10232 }
10233 info.item_compare_selfdict = argvars[2].vval.v_dict;
10234 }
10235 }
10236
10237 /* Make an array with each entry pointing to an item in the List. */
10238 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10239 if (ptrs == NULL)
10240 goto theend;
10241
10242 i = 0;
10243 if (sort)
10244 {
10245 /* sort(): ptrs will be the list to sort */
10246 for (li = l->lv_first; li != NULL; li = li->li_next)
10247 {
10248 ptrs[i].item = li;
10249 ptrs[i].idx = i;
10250 ++i;
10251 }
10252
10253 info.item_compare_func_err = FALSE;
10254 info.item_compare_keep_zero = FALSE;
10255 /* test the compare function */
10256 if ((info.item_compare_func != NULL
10257 || info.item_compare_partial != NULL)
10258 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10259 == ITEM_COMPARE_FAIL)
10260 EMSG(_("E702: Sort compare function failed"));
10261 else
10262 {
10263 /* Sort the array with item pointers. */
10264 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10265 info.item_compare_func == NULL
10266 && info.item_compare_partial == NULL
10267 ? item_compare : item_compare2);
10268
10269 if (!info.item_compare_func_err)
10270 {
10271 /* Clear the List and append the items in sorted order. */
10272 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10273 l->lv_len = 0;
10274 for (i = 0; i < len; ++i)
10275 list_append(l, ptrs[i].item);
10276 }
10277 }
10278 }
10279 else
10280 {
10281 int (*item_compare_func_ptr)(const void *, const void *);
10282
10283 /* f_uniq(): ptrs will be a stack of items to remove */
10284 info.item_compare_func_err = FALSE;
10285 info.item_compare_keep_zero = TRUE;
10286 item_compare_func_ptr = info.item_compare_func != NULL
10287 || info.item_compare_partial != NULL
10288 ? item_compare2 : item_compare;
10289
10290 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10291 li = li->li_next)
10292 {
10293 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10294 == 0)
10295 ptrs[i++].item = li;
10296 if (info.item_compare_func_err)
10297 {
10298 EMSG(_("E882: Uniq compare function failed"));
10299 break;
10300 }
10301 }
10302
10303 if (!info.item_compare_func_err)
10304 {
10305 while (--i >= 0)
10306 {
10307 li = ptrs[i].item->li_next;
10308 ptrs[i].item->li_next = li->li_next;
10309 if (li->li_next != NULL)
10310 li->li_next->li_prev = ptrs[i].item;
10311 else
10312 l->lv_last = ptrs[i].item;
10313 list_fix_watch(l, li);
10314 listitem_free(li);
10315 l->lv_len--;
10316 }
10317 }
10318 }
10319
10320 vim_free(ptrs);
10321 }
10322theend:
10323 sortinfo = old_sortinfo;
10324}
10325
10326/*
10327 * "sort({list})" function
10328 */
10329 static void
10330f_sort(typval_T *argvars, typval_T *rettv)
10331{
10332 do_sort_uniq(argvars, rettv, TRUE);
10333}
10334
10335/*
10336 * "uniq({list})" function
10337 */
10338 static void
10339f_uniq(typval_T *argvars, typval_T *rettv)
10340{
10341 do_sort_uniq(argvars, rettv, FALSE);
10342}
10343
10344/*
10345 * "soundfold({word})" function
10346 */
10347 static void
10348f_soundfold(typval_T *argvars, typval_T *rettv)
10349{
10350 char_u *s;
10351
10352 rettv->v_type = VAR_STRING;
10353 s = get_tv_string(&argvars[0]);
10354#ifdef FEAT_SPELL
10355 rettv->vval.v_string = eval_soundfold(s);
10356#else
10357 rettv->vval.v_string = vim_strsave(s);
10358#endif
10359}
10360
10361/*
10362 * "spellbadword()" function
10363 */
10364 static void
10365f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10366{
10367 char_u *word = (char_u *)"";
10368 hlf_T attr = HLF_COUNT;
10369 int len = 0;
10370
10371 if (rettv_list_alloc(rettv) == FAIL)
10372 return;
10373
10374#ifdef FEAT_SPELL
10375 if (argvars[0].v_type == VAR_UNKNOWN)
10376 {
10377 /* Find the start and length of the badly spelled word. */
10378 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10379 if (len != 0)
10380 word = ml_get_cursor();
10381 }
10382 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10383 {
10384 char_u *str = get_tv_string_chk(&argvars[0]);
10385 int capcol = -1;
10386
10387 if (str != NULL)
10388 {
10389 /* Check the argument for spelling. */
10390 while (*str != NUL)
10391 {
10392 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10393 if (attr != HLF_COUNT)
10394 {
10395 word = str;
10396 break;
10397 }
10398 str += len;
10399 }
10400 }
10401 }
10402#endif
10403
10404 list_append_string(rettv->vval.v_list, word, len);
10405 list_append_string(rettv->vval.v_list, (char_u *)(
10406 attr == HLF_SPB ? "bad" :
10407 attr == HLF_SPR ? "rare" :
10408 attr == HLF_SPL ? "local" :
10409 attr == HLF_SPC ? "caps" :
10410 ""), -1);
10411}
10412
10413/*
10414 * "spellsuggest()" function
10415 */
10416 static void
10417f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10418{
10419#ifdef FEAT_SPELL
10420 char_u *str;
10421 int typeerr = FALSE;
10422 int maxcount;
10423 garray_T ga;
10424 int i;
10425 listitem_T *li;
10426 int need_capital = FALSE;
10427#endif
10428
10429 if (rettv_list_alloc(rettv) == FAIL)
10430 return;
10431
10432#ifdef FEAT_SPELL
10433 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10434 {
10435 str = get_tv_string(&argvars[0]);
10436 if (argvars[1].v_type != VAR_UNKNOWN)
10437 {
10438 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
10439 if (maxcount <= 0)
10440 return;
10441 if (argvars[2].v_type != VAR_UNKNOWN)
10442 {
10443 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
10444 if (typeerr)
10445 return;
10446 }
10447 }
10448 else
10449 maxcount = 25;
10450
10451 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10452
10453 for (i = 0; i < ga.ga_len; ++i)
10454 {
10455 str = ((char_u **)ga.ga_data)[i];
10456
10457 li = listitem_alloc();
10458 if (li == NULL)
10459 vim_free(str);
10460 else
10461 {
10462 li->li_tv.v_type = VAR_STRING;
10463 li->li_tv.v_lock = 0;
10464 li->li_tv.vval.v_string = str;
10465 list_append(rettv->vval.v_list, li);
10466 }
10467 }
10468 ga_clear(&ga);
10469 }
10470#endif
10471}
10472
10473 static void
10474f_split(typval_T *argvars, typval_T *rettv)
10475{
10476 char_u *str;
10477 char_u *end;
10478 char_u *pat = NULL;
10479 regmatch_T regmatch;
10480 char_u patbuf[NUMBUFLEN];
10481 char_u *save_cpo;
10482 int match;
10483 colnr_T col = 0;
10484 int keepempty = FALSE;
10485 int typeerr = FALSE;
10486
10487 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10488 save_cpo = p_cpo;
10489 p_cpo = (char_u *)"";
10490
10491 str = get_tv_string(&argvars[0]);
10492 if (argvars[1].v_type != VAR_UNKNOWN)
10493 {
10494 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
10495 if (pat == NULL)
10496 typeerr = TRUE;
10497 if (argvars[2].v_type != VAR_UNKNOWN)
10498 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
10499 }
10500 if (pat == NULL || *pat == NUL)
10501 pat = (char_u *)"[\\x01- ]\\+";
10502
10503 if (rettv_list_alloc(rettv) == FAIL)
10504 return;
10505 if (typeerr)
10506 return;
10507
10508 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10509 if (regmatch.regprog != NULL)
10510 {
10511 regmatch.rm_ic = FALSE;
10512 while (*str != NUL || keepempty)
10513 {
10514 if (*str == NUL)
10515 match = FALSE; /* empty item at the end */
10516 else
10517 match = vim_regexec_nl(&regmatch, str, col);
10518 if (match)
10519 end = regmatch.startp[0];
10520 else
10521 end = str + STRLEN(str);
10522 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10523 && *str != NUL && match && end < regmatch.endp[0]))
10524 {
10525 if (list_append_string(rettv->vval.v_list, str,
10526 (int)(end - str)) == FAIL)
10527 break;
10528 }
10529 if (!match)
10530 break;
10531 /* Advance to just after the match. */
10532 if (regmatch.endp[0] > str)
10533 col = 0;
10534 else
10535 {
10536 /* Don't get stuck at the same match. */
10537#ifdef FEAT_MBYTE
10538 col = (*mb_ptr2len)(regmatch.endp[0]);
10539#else
10540 col = 1;
10541#endif
10542 }
10543 str = regmatch.endp[0];
10544 }
10545
10546 vim_regfree(regmatch.regprog);
10547 }
10548
10549 p_cpo = save_cpo;
10550}
10551
10552#ifdef FEAT_FLOAT
10553/*
10554 * "sqrt()" function
10555 */
10556 static void
10557f_sqrt(typval_T *argvars, typval_T *rettv)
10558{
10559 float_T f = 0.0;
10560
10561 rettv->v_type = VAR_FLOAT;
10562 if (get_float_arg(argvars, &f) == OK)
10563 rettv->vval.v_float = sqrt(f);
10564 else
10565 rettv->vval.v_float = 0.0;
10566}
10567
10568/*
10569 * "str2float()" function
10570 */
10571 static void
10572f_str2float(typval_T *argvars, typval_T *rettv)
10573{
10574 char_u *p = skipwhite(get_tv_string(&argvars[0]));
10575
10576 if (*p == '+')
10577 p = skipwhite(p + 1);
10578 (void)string2float(p, &rettv->vval.v_float);
10579 rettv->v_type = VAR_FLOAT;
10580}
10581#endif
10582
10583/*
10584 * "str2nr()" function
10585 */
10586 static void
10587f_str2nr(typval_T *argvars, typval_T *rettv)
10588{
10589 int base = 10;
10590 char_u *p;
10591 varnumber_T n;
10592 int what;
10593
10594 if (argvars[1].v_type != VAR_UNKNOWN)
10595 {
10596 base = (int)get_tv_number(&argvars[1]);
10597 if (base != 2 && base != 8 && base != 10 && base != 16)
10598 {
10599 EMSG(_(e_invarg));
10600 return;
10601 }
10602 }
10603
10604 p = skipwhite(get_tv_string(&argvars[0]));
10605 if (*p == '+')
10606 p = skipwhite(p + 1);
10607 switch (base)
10608 {
10609 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
10610 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
10611 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
10612 default: what = 0;
10613 }
10614 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
10615 rettv->vval.v_number = n;
10616}
10617
10618#ifdef HAVE_STRFTIME
10619/*
10620 * "strftime({format}[, {time}])" function
10621 */
10622 static void
10623f_strftime(typval_T *argvars, typval_T *rettv)
10624{
10625 char_u result_buf[256];
10626 struct tm *curtime;
10627 time_t seconds;
10628 char_u *p;
10629
10630 rettv->v_type = VAR_STRING;
10631
10632 p = get_tv_string(&argvars[0]);
10633 if (argvars[1].v_type == VAR_UNKNOWN)
10634 seconds = time(NULL);
10635 else
10636 seconds = (time_t)get_tv_number(&argvars[1]);
10637 curtime = localtime(&seconds);
10638 /* MSVC returns NULL for an invalid value of seconds. */
10639 if (curtime == NULL)
10640 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
10641 else
10642 {
10643# ifdef FEAT_MBYTE
10644 vimconv_T conv;
10645 char_u *enc;
10646
10647 conv.vc_type = CONV_NONE;
10648 enc = enc_locale();
10649 convert_setup(&conv, p_enc, enc);
10650 if (conv.vc_type != CONV_NONE)
10651 p = string_convert(&conv, p, NULL);
10652# endif
10653 if (p != NULL)
10654 (void)strftime((char *)result_buf, sizeof(result_buf),
10655 (char *)p, curtime);
10656 else
10657 result_buf[0] = NUL;
10658
10659# ifdef FEAT_MBYTE
10660 if (conv.vc_type != CONV_NONE)
10661 vim_free(p);
10662 convert_setup(&conv, enc, p_enc);
10663 if (conv.vc_type != CONV_NONE)
10664 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
10665 else
10666# endif
10667 rettv->vval.v_string = vim_strsave(result_buf);
10668
10669# ifdef FEAT_MBYTE
10670 /* Release conversion descriptors */
10671 convert_setup(&conv, NULL, NULL);
10672 vim_free(enc);
10673# endif
10674 }
10675}
10676#endif
10677
10678/*
10679 * "strgetchar()" function
10680 */
10681 static void
10682f_strgetchar(typval_T *argvars, typval_T *rettv)
10683{
10684 char_u *str;
10685 int len;
10686 int error = FALSE;
10687 int charidx;
10688
10689 rettv->vval.v_number = -1;
10690 str = get_tv_string_chk(&argvars[0]);
10691 if (str == NULL)
10692 return;
10693 len = (int)STRLEN(str);
10694 charidx = (int)get_tv_number_chk(&argvars[1], &error);
10695 if (error)
10696 return;
10697#ifdef FEAT_MBYTE
10698 {
10699 int byteidx = 0;
10700
10701 while (charidx >= 0 && byteidx < len)
10702 {
10703 if (charidx == 0)
10704 {
10705 rettv->vval.v_number = mb_ptr2char(str + byteidx);
10706 break;
10707 }
10708 --charidx;
10709 byteidx += mb_cptr2len(str + byteidx);
10710 }
10711 }
10712#else
10713 if (charidx < len)
10714 rettv->vval.v_number = str[charidx];
10715#endif
10716}
10717
10718/*
10719 * "stridx()" function
10720 */
10721 static void
10722f_stridx(typval_T *argvars, typval_T *rettv)
10723{
10724 char_u buf[NUMBUFLEN];
10725 char_u *needle;
10726 char_u *haystack;
10727 char_u *save_haystack;
10728 char_u *pos;
10729 int start_idx;
10730
10731 needle = get_tv_string_chk(&argvars[1]);
10732 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
10733 rettv->vval.v_number = -1;
10734 if (needle == NULL || haystack == NULL)
10735 return; /* type error; errmsg already given */
10736
10737 if (argvars[2].v_type != VAR_UNKNOWN)
10738 {
10739 int error = FALSE;
10740
10741 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
10742 if (error || start_idx >= (int)STRLEN(haystack))
10743 return;
10744 if (start_idx >= 0)
10745 haystack += start_idx;
10746 }
10747
10748 pos = (char_u *)strstr((char *)haystack, (char *)needle);
10749 if (pos != NULL)
10750 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
10751}
10752
10753/*
10754 * "string()" function
10755 */
10756 static void
10757f_string(typval_T *argvars, typval_T *rettv)
10758{
10759 char_u *tofree;
10760 char_u numbuf[NUMBUFLEN];
10761
10762 rettv->v_type = VAR_STRING;
10763 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
10764 get_copyID());
10765 /* Make a copy if we have a value but it's not in allocated memory. */
10766 if (rettv->vval.v_string != NULL && tofree == NULL)
10767 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
10768}
10769
10770/*
10771 * "strlen()" function
10772 */
10773 static void
10774f_strlen(typval_T *argvars, typval_T *rettv)
10775{
10776 rettv->vval.v_number = (varnumber_T)(STRLEN(
10777 get_tv_string(&argvars[0])));
10778}
10779
10780/*
10781 * "strchars()" function
10782 */
10783 static void
10784f_strchars(typval_T *argvars, typval_T *rettv)
10785{
10786 char_u *s = get_tv_string(&argvars[0]);
10787 int skipcc = 0;
10788#ifdef FEAT_MBYTE
10789 varnumber_T len = 0;
10790 int (*func_mb_ptr2char_adv)(char_u **pp);
10791#endif
10792
10793 if (argvars[1].v_type != VAR_UNKNOWN)
10794 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
10795 if (skipcc < 0 || skipcc > 1)
10796 EMSG(_(e_invarg));
10797 else
10798 {
10799#ifdef FEAT_MBYTE
10800 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
10801 while (*s != NUL)
10802 {
10803 func_mb_ptr2char_adv(&s);
10804 ++len;
10805 }
10806 rettv->vval.v_number = len;
10807#else
10808 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
10809#endif
10810 }
10811}
10812
10813/*
10814 * "strdisplaywidth()" function
10815 */
10816 static void
10817f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
10818{
10819 char_u *s = get_tv_string(&argvars[0]);
10820 int col = 0;
10821
10822 if (argvars[1].v_type != VAR_UNKNOWN)
10823 col = (int)get_tv_number(&argvars[1]);
10824
10825 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
10826}
10827
10828/*
10829 * "strwidth()" function
10830 */
10831 static void
10832f_strwidth(typval_T *argvars, typval_T *rettv)
10833{
10834 char_u *s = get_tv_string(&argvars[0]);
10835
10836 rettv->vval.v_number = (varnumber_T)(
10837#ifdef FEAT_MBYTE
10838 mb_string2cells(s, -1)
10839#else
10840 STRLEN(s)
10841#endif
10842 );
10843}
10844
10845/*
10846 * "strcharpart()" function
10847 */
10848 static void
10849f_strcharpart(typval_T *argvars, typval_T *rettv)
10850{
10851#ifdef FEAT_MBYTE
10852 char_u *p;
10853 int nchar;
10854 int nbyte = 0;
10855 int charlen;
10856 int len = 0;
10857 int slen;
10858 int error = FALSE;
10859
10860 p = get_tv_string(&argvars[0]);
10861 slen = (int)STRLEN(p);
10862
10863 nchar = (int)get_tv_number_chk(&argvars[1], &error);
10864 if (!error)
10865 {
10866 if (nchar > 0)
10867 while (nchar > 0 && nbyte < slen)
10868 {
10869 nbyte += mb_cptr2len(p + nbyte);
10870 --nchar;
10871 }
10872 else
10873 nbyte = nchar;
10874 if (argvars[2].v_type != VAR_UNKNOWN)
10875 {
10876 charlen = (int)get_tv_number(&argvars[2]);
10877 while (charlen > 0 && nbyte + len < slen)
10878 {
10879 int off = nbyte + len;
10880
10881 if (off < 0)
10882 len += 1;
10883 else
10884 len += mb_cptr2len(p + off);
10885 --charlen;
10886 }
10887 }
10888 else
10889 len = slen - nbyte; /* default: all bytes that are available. */
10890 }
10891
10892 /*
10893 * Only return the overlap between the specified part and the actual
10894 * string.
10895 */
10896 if (nbyte < 0)
10897 {
10898 len += nbyte;
10899 nbyte = 0;
10900 }
10901 else if (nbyte > slen)
10902 nbyte = slen;
10903 if (len < 0)
10904 len = 0;
10905 else if (nbyte + len > slen)
10906 len = slen - nbyte;
10907
10908 rettv->v_type = VAR_STRING;
10909 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
10910#else
10911 f_strpart(argvars, rettv);
10912#endif
10913}
10914
10915/*
10916 * "strpart()" function
10917 */
10918 static void
10919f_strpart(typval_T *argvars, typval_T *rettv)
10920{
10921 char_u *p;
10922 int n;
10923 int len;
10924 int slen;
10925 int error = FALSE;
10926
10927 p = get_tv_string(&argvars[0]);
10928 slen = (int)STRLEN(p);
10929
10930 n = (int)get_tv_number_chk(&argvars[1], &error);
10931 if (error)
10932 len = 0;
10933 else if (argvars[2].v_type != VAR_UNKNOWN)
10934 len = (int)get_tv_number(&argvars[2]);
10935 else
10936 len = slen - n; /* default len: all bytes that are available. */
10937
10938 /*
10939 * Only return the overlap between the specified part and the actual
10940 * string.
10941 */
10942 if (n < 0)
10943 {
10944 len += n;
10945 n = 0;
10946 }
10947 else if (n > slen)
10948 n = slen;
10949 if (len < 0)
10950 len = 0;
10951 else if (n + len > slen)
10952 len = slen - n;
10953
10954 rettv->v_type = VAR_STRING;
10955 rettv->vval.v_string = vim_strnsave(p + n, len);
10956}
10957
10958/*
10959 * "strridx()" function
10960 */
10961 static void
10962f_strridx(typval_T *argvars, typval_T *rettv)
10963{
10964 char_u buf[NUMBUFLEN];
10965 char_u *needle;
10966 char_u *haystack;
10967 char_u *rest;
10968 char_u *lastmatch = NULL;
10969 int haystack_len, end_idx;
10970
10971 needle = get_tv_string_chk(&argvars[1]);
10972 haystack = get_tv_string_buf_chk(&argvars[0], buf);
10973
10974 rettv->vval.v_number = -1;
10975 if (needle == NULL || haystack == NULL)
10976 return; /* type error; errmsg already given */
10977
10978 haystack_len = (int)STRLEN(haystack);
10979 if (argvars[2].v_type != VAR_UNKNOWN)
10980 {
10981 /* Third argument: upper limit for index */
10982 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
10983 if (end_idx < 0)
10984 return; /* can never find a match */
10985 }
10986 else
10987 end_idx = haystack_len;
10988
10989 if (*needle == NUL)
10990 {
10991 /* Empty string matches past the end. */
10992 lastmatch = haystack + end_idx;
10993 }
10994 else
10995 {
10996 for (rest = haystack; *rest != '\0'; ++rest)
10997 {
10998 rest = (char_u *)strstr((char *)rest, (char *)needle);
10999 if (rest == NULL || rest > haystack + end_idx)
11000 break;
11001 lastmatch = rest;
11002 }
11003 }
11004
11005 if (lastmatch == NULL)
11006 rettv->vval.v_number = -1;
11007 else
11008 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11009}
11010
11011/*
11012 * "strtrans()" function
11013 */
11014 static void
11015f_strtrans(typval_T *argvars, typval_T *rettv)
11016{
11017 rettv->v_type = VAR_STRING;
11018 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11019}
11020
11021/*
11022 * "submatch()" function
11023 */
11024 static void
11025f_submatch(typval_T *argvars, typval_T *rettv)
11026{
11027 int error = FALSE;
11028 int no;
11029 int retList = 0;
11030
11031 no = (int)get_tv_number_chk(&argvars[0], &error);
11032 if (error)
11033 return;
11034 error = FALSE;
11035 if (argvars[1].v_type != VAR_UNKNOWN)
11036 retList = (int)get_tv_number_chk(&argvars[1], &error);
11037 if (error)
11038 return;
11039
11040 if (retList == 0)
11041 {
11042 rettv->v_type = VAR_STRING;
11043 rettv->vval.v_string = reg_submatch(no);
11044 }
11045 else
11046 {
11047 rettv->v_type = VAR_LIST;
11048 rettv->vval.v_list = reg_submatch_list(no);
11049 }
11050}
11051
11052/*
11053 * "substitute()" function
11054 */
11055 static void
11056f_substitute(typval_T *argvars, typval_T *rettv)
11057{
11058 char_u patbuf[NUMBUFLEN];
11059 char_u subbuf[NUMBUFLEN];
11060 char_u flagsbuf[NUMBUFLEN];
11061
11062 char_u *str = get_tv_string_chk(&argvars[0]);
11063 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011064 char_u *sub = NULL;
11065 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11067
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011068 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11069 expr = &argvars[2];
11070 else
11071 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11072
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011074 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11075 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011076 rettv->vval.v_string = NULL;
11077 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011078 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079}
11080
11081/*
11082 * "synID(lnum, col, trans)" function
11083 */
11084 static void
11085f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11086{
11087 int id = 0;
11088#ifdef FEAT_SYN_HL
11089 linenr_T lnum;
11090 colnr_T col;
11091 int trans;
11092 int transerr = FALSE;
11093
11094 lnum = get_tv_lnum(argvars); /* -1 on type error */
11095 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11096 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11097
11098 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11099 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11100 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11101#endif
11102
11103 rettv->vval.v_number = id;
11104}
11105
11106/*
11107 * "synIDattr(id, what [, mode])" function
11108 */
11109 static void
11110f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11111{
11112 char_u *p = NULL;
11113#ifdef FEAT_SYN_HL
11114 int id;
11115 char_u *what;
11116 char_u *mode;
11117 char_u modebuf[NUMBUFLEN];
11118 int modec;
11119
11120 id = (int)get_tv_number(&argvars[0]);
11121 what = get_tv_string(&argvars[1]);
11122 if (argvars[2].v_type != VAR_UNKNOWN)
11123 {
11124 mode = get_tv_string_buf(&argvars[2], modebuf);
11125 modec = TOLOWER_ASC(mode[0]);
11126 if (modec != 't' && modec != 'c' && modec != 'g')
11127 modec = 0; /* replace invalid with current */
11128 }
11129 else
11130 {
11131#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11132 if (USE_24BIT)
11133 modec = 'g';
11134 else
11135#endif
11136 if (t_colors > 1)
11137 modec = 'c';
11138 else
11139 modec = 't';
11140 }
11141
11142
11143 switch (TOLOWER_ASC(what[0]))
11144 {
11145 case 'b':
11146 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11147 p = highlight_color(id, what, modec);
11148 else /* bold */
11149 p = highlight_has_attr(id, HL_BOLD, modec);
11150 break;
11151
11152 case 'f': /* fg[#] or font */
11153 p = highlight_color(id, what, modec);
11154 break;
11155
11156 case 'i':
11157 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11158 p = highlight_has_attr(id, HL_INVERSE, modec);
11159 else /* italic */
11160 p = highlight_has_attr(id, HL_ITALIC, modec);
11161 break;
11162
11163 case 'n': /* name */
11164 p = get_highlight_name(NULL, id - 1);
11165 break;
11166
11167 case 'r': /* reverse */
11168 p = highlight_has_attr(id, HL_INVERSE, modec);
11169 break;
11170
11171 case 's':
11172 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11173 p = highlight_color(id, what, modec);
11174 else /* standout */
11175 p = highlight_has_attr(id, HL_STANDOUT, modec);
11176 break;
11177
11178 case 'u':
11179 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11180 /* underline */
11181 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11182 else
11183 /* undercurl */
11184 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11185 break;
11186 }
11187
11188 if (p != NULL)
11189 p = vim_strsave(p);
11190#endif
11191 rettv->v_type = VAR_STRING;
11192 rettv->vval.v_string = p;
11193}
11194
11195/*
11196 * "synIDtrans(id)" function
11197 */
11198 static void
11199f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11200{
11201 int id;
11202
11203#ifdef FEAT_SYN_HL
11204 id = (int)get_tv_number(&argvars[0]);
11205
11206 if (id > 0)
11207 id = syn_get_final_id(id);
11208 else
11209#endif
11210 id = 0;
11211
11212 rettv->vval.v_number = id;
11213}
11214
11215/*
11216 * "synconcealed(lnum, col)" function
11217 */
11218 static void
11219f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11220{
11221#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11222 linenr_T lnum;
11223 colnr_T col;
11224 int syntax_flags = 0;
11225 int cchar;
11226 int matchid = 0;
11227 char_u str[NUMBUFLEN];
11228#endif
11229
11230 rettv->v_type = VAR_LIST;
11231 rettv->vval.v_list = NULL;
11232
11233#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11234 lnum = get_tv_lnum(argvars); /* -1 on type error */
11235 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11236
11237 vim_memset(str, NUL, sizeof(str));
11238
11239 if (rettv_list_alloc(rettv) != FAIL)
11240 {
11241 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11242 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11243 && curwin->w_p_cole > 0)
11244 {
11245 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11246 syntax_flags = get_syntax_info(&matchid);
11247
11248 /* get the conceal character */
11249 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11250 {
11251 cchar = syn_get_sub_char();
11252 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
11253 cchar = lcs_conceal;
11254 if (cchar != NUL)
11255 {
11256# ifdef FEAT_MBYTE
11257 if (has_mbyte)
11258 (*mb_char2bytes)(cchar, str);
11259 else
11260# endif
11261 str[0] = cchar;
11262 }
11263 }
11264 }
11265
11266 list_append_number(rettv->vval.v_list,
11267 (syntax_flags & HL_CONCEAL) != 0);
11268 /* -1 to auto-determine strlen */
11269 list_append_string(rettv->vval.v_list, str, -1);
11270 list_append_number(rettv->vval.v_list, matchid);
11271 }
11272#endif
11273}
11274
11275/*
11276 * "synstack(lnum, col)" function
11277 */
11278 static void
11279f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11280{
11281#ifdef FEAT_SYN_HL
11282 linenr_T lnum;
11283 colnr_T col;
11284 int i;
11285 int id;
11286#endif
11287
11288 rettv->v_type = VAR_LIST;
11289 rettv->vval.v_list = NULL;
11290
11291#ifdef FEAT_SYN_HL
11292 lnum = get_tv_lnum(argvars); /* -1 on type error */
11293 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11294
11295 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11296 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11297 && rettv_list_alloc(rettv) != FAIL)
11298 {
11299 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11300 for (i = 0; ; ++i)
11301 {
11302 id = syn_get_stack_item(i);
11303 if (id < 0)
11304 break;
11305 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11306 break;
11307 }
11308 }
11309#endif
11310}
11311
11312 static void
11313get_cmd_output_as_rettv(
11314 typval_T *argvars,
11315 typval_T *rettv,
11316 int retlist)
11317{
11318 char_u *res = NULL;
11319 char_u *p;
11320 char_u *infile = NULL;
11321 char_u buf[NUMBUFLEN];
11322 int err = FALSE;
11323 FILE *fd;
11324 list_T *list = NULL;
11325 int flags = SHELL_SILENT;
11326
11327 rettv->v_type = VAR_STRING;
11328 rettv->vval.v_string = NULL;
11329 if (check_restricted() || check_secure())
11330 goto errret;
11331
11332 if (argvars[1].v_type != VAR_UNKNOWN)
11333 {
11334 /*
11335 * Write the string to a temp file, to be used for input of the shell
11336 * command.
11337 */
11338 if ((infile = vim_tempname('i', TRUE)) == NULL)
11339 {
11340 EMSG(_(e_notmp));
11341 goto errret;
11342 }
11343
11344 fd = mch_fopen((char *)infile, WRITEBIN);
11345 if (fd == NULL)
11346 {
11347 EMSG2(_(e_notopen), infile);
11348 goto errret;
11349 }
11350 if (argvars[1].v_type == VAR_LIST)
11351 {
11352 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11353 err = TRUE;
11354 }
11355 else
11356 {
11357 size_t len;
11358
11359 p = get_tv_string_buf_chk(&argvars[1], buf);
11360 if (p == NULL)
11361 {
11362 fclose(fd);
11363 goto errret; /* type error; errmsg already given */
11364 }
11365 len = STRLEN(p);
11366 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11367 err = TRUE;
11368 }
11369 if (fclose(fd) != 0)
11370 err = TRUE;
11371 if (err)
11372 {
11373 EMSG(_("E677: Error writing temp file"));
11374 goto errret;
11375 }
11376 }
11377
11378 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11379 * echoes typeahead, that messes up the display. */
11380 if (!msg_silent)
11381 flags += SHELL_COOKED;
11382
11383 if (retlist)
11384 {
11385 int len;
11386 listitem_T *li;
11387 char_u *s = NULL;
11388 char_u *start;
11389 char_u *end;
11390 int i;
11391
11392 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
11393 if (res == NULL)
11394 goto errret;
11395
11396 list = list_alloc();
11397 if (list == NULL)
11398 goto errret;
11399
11400 for (i = 0; i < len; ++i)
11401 {
11402 start = res + i;
11403 while (i < len && res[i] != NL)
11404 ++i;
11405 end = res + i;
11406
11407 s = alloc((unsigned)(end - start + 1));
11408 if (s == NULL)
11409 goto errret;
11410
11411 for (p = s; start < end; ++p, ++start)
11412 *p = *start == NUL ? NL : *start;
11413 *p = NUL;
11414
11415 li = listitem_alloc();
11416 if (li == NULL)
11417 {
11418 vim_free(s);
11419 goto errret;
11420 }
11421 li->li_tv.v_type = VAR_STRING;
11422 li->li_tv.v_lock = 0;
11423 li->li_tv.vval.v_string = s;
11424 list_append(list, li);
11425 }
11426
11427 ++list->lv_refcount;
11428 rettv->v_type = VAR_LIST;
11429 rettv->vval.v_list = list;
11430 list = NULL;
11431 }
11432 else
11433 {
11434 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
11435#ifdef USE_CR
11436 /* translate <CR> into <NL> */
11437 if (res != NULL)
11438 {
11439 char_u *s;
11440
11441 for (s = res; *s; ++s)
11442 {
11443 if (*s == CAR)
11444 *s = NL;
11445 }
11446 }
11447#else
11448# ifdef USE_CRNL
11449 /* translate <CR><NL> into <NL> */
11450 if (res != NULL)
11451 {
11452 char_u *s, *d;
11453
11454 d = res;
11455 for (s = res; *s; ++s)
11456 {
11457 if (s[0] == CAR && s[1] == NL)
11458 ++s;
11459 *d++ = *s;
11460 }
11461 *d = NUL;
11462 }
11463# endif
11464#endif
11465 rettv->vval.v_string = res;
11466 res = NULL;
11467 }
11468
11469errret:
11470 if (infile != NULL)
11471 {
11472 mch_remove(infile);
11473 vim_free(infile);
11474 }
11475 if (res != NULL)
11476 vim_free(res);
11477 if (list != NULL)
11478 list_free(list);
11479}
11480
11481/*
11482 * "system()" function
11483 */
11484 static void
11485f_system(typval_T *argvars, typval_T *rettv)
11486{
11487 get_cmd_output_as_rettv(argvars, rettv, FALSE);
11488}
11489
11490/*
11491 * "systemlist()" function
11492 */
11493 static void
11494f_systemlist(typval_T *argvars, typval_T *rettv)
11495{
11496 get_cmd_output_as_rettv(argvars, rettv, TRUE);
11497}
11498
11499/*
11500 * "tabpagebuflist()" function
11501 */
11502 static void
11503f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11504{
11505#ifdef FEAT_WINDOWS
11506 tabpage_T *tp;
11507 win_T *wp = NULL;
11508
11509 if (argvars[0].v_type == VAR_UNKNOWN)
11510 wp = firstwin;
11511 else
11512 {
11513 tp = find_tabpage((int)get_tv_number(&argvars[0]));
11514 if (tp != NULL)
11515 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11516 }
11517 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
11518 {
11519 for (; wp != NULL; wp = wp->w_next)
11520 if (list_append_number(rettv->vval.v_list,
11521 wp->w_buffer->b_fnum) == FAIL)
11522 break;
11523 }
11524#endif
11525}
11526
11527
11528/*
11529 * "tabpagenr()" function
11530 */
11531 static void
11532f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
11533{
11534 int nr = 1;
11535#ifdef FEAT_WINDOWS
11536 char_u *arg;
11537
11538 if (argvars[0].v_type != VAR_UNKNOWN)
11539 {
11540 arg = get_tv_string_chk(&argvars[0]);
11541 nr = 0;
11542 if (arg != NULL)
11543 {
11544 if (STRCMP(arg, "$") == 0)
11545 nr = tabpage_index(NULL) - 1;
11546 else
11547 EMSG2(_(e_invexpr2), arg);
11548 }
11549 }
11550 else
11551 nr = tabpage_index(curtab);
11552#endif
11553 rettv->vval.v_number = nr;
11554}
11555
11556
11557#ifdef FEAT_WINDOWS
11558static int get_winnr(tabpage_T *tp, typval_T *argvar);
11559
11560/*
11561 * Common code for tabpagewinnr() and winnr().
11562 */
11563 static int
11564get_winnr(tabpage_T *tp, typval_T *argvar)
11565{
11566 win_T *twin;
11567 int nr = 1;
11568 win_T *wp;
11569 char_u *arg;
11570
11571 twin = (tp == curtab) ? curwin : tp->tp_curwin;
11572 if (argvar->v_type != VAR_UNKNOWN)
11573 {
11574 arg = get_tv_string_chk(argvar);
11575 if (arg == NULL)
11576 nr = 0; /* type error; errmsg already given */
11577 else if (STRCMP(arg, "$") == 0)
11578 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
11579 else if (STRCMP(arg, "#") == 0)
11580 {
11581 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
11582 if (twin == NULL)
11583 nr = 0;
11584 }
11585 else
11586 {
11587 EMSG2(_(e_invexpr2), arg);
11588 nr = 0;
11589 }
11590 }
11591
11592 if (nr > 0)
11593 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11594 wp != twin; wp = wp->w_next)
11595 {
11596 if (wp == NULL)
11597 {
11598 /* didn't find it in this tabpage */
11599 nr = 0;
11600 break;
11601 }
11602 ++nr;
11603 }
11604 return nr;
11605}
11606#endif
11607
11608/*
11609 * "tabpagewinnr()" function
11610 */
11611 static void
11612f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
11613{
11614 int nr = 1;
11615#ifdef FEAT_WINDOWS
11616 tabpage_T *tp;
11617
11618 tp = find_tabpage((int)get_tv_number(&argvars[0]));
11619 if (tp == NULL)
11620 nr = 0;
11621 else
11622 nr = get_winnr(tp, &argvars[1]);
11623#endif
11624 rettv->vval.v_number = nr;
11625}
11626
11627
11628/*
11629 * "tagfiles()" function
11630 */
11631 static void
11632f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
11633{
11634 char_u *fname;
11635 tagname_T tn;
11636 int first;
11637
11638 if (rettv_list_alloc(rettv) == FAIL)
11639 return;
11640 fname = alloc(MAXPATHL);
11641 if (fname == NULL)
11642 return;
11643
11644 for (first = TRUE; ; first = FALSE)
11645 if (get_tagfname(&tn, first, fname) == FAIL
11646 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
11647 break;
11648 tagname_free(&tn);
11649 vim_free(fname);
11650}
11651
11652/*
11653 * "taglist()" function
11654 */
11655 static void
11656f_taglist(typval_T *argvars, typval_T *rettv)
11657{
11658 char_u *tag_pattern;
11659
11660 tag_pattern = get_tv_string(&argvars[0]);
11661
11662 rettv->vval.v_number = FALSE;
11663 if (*tag_pattern == NUL)
11664 return;
11665
11666 if (rettv_list_alloc(rettv) == OK)
11667 (void)get_tags(rettv->vval.v_list, tag_pattern);
11668}
11669
11670/*
11671 * "tempname()" function
11672 */
11673 static void
11674f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
11675{
11676 static int x = 'A';
11677
11678 rettv->v_type = VAR_STRING;
11679 rettv->vval.v_string = vim_tempname(x, FALSE);
11680
11681 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
11682 * names. Skip 'I' and 'O', they are used for shell redirection. */
11683 do
11684 {
11685 if (x == 'Z')
11686 x = '0';
11687 else if (x == '9')
11688 x = 'A';
11689 else
11690 {
11691#ifdef EBCDIC
11692 if (x == 'I')
11693 x = 'J';
11694 else if (x == 'R')
11695 x = 'S';
11696 else
11697#endif
11698 ++x;
11699 }
11700 } while (x == 'I' || x == 'O');
11701}
11702
11703#ifdef FEAT_FLOAT
11704/*
11705 * "tan()" function
11706 */
11707 static void
11708f_tan(typval_T *argvars, typval_T *rettv)
11709{
11710 float_T f = 0.0;
11711
11712 rettv->v_type = VAR_FLOAT;
11713 if (get_float_arg(argvars, &f) == OK)
11714 rettv->vval.v_float = tan(f);
11715 else
11716 rettv->vval.v_float = 0.0;
11717}
11718
11719/*
11720 * "tanh()" function
11721 */
11722 static void
11723f_tanh(typval_T *argvars, typval_T *rettv)
11724{
11725 float_T f = 0.0;
11726
11727 rettv->v_type = VAR_FLOAT;
11728 if (get_float_arg(argvars, &f) == OK)
11729 rettv->vval.v_float = tanh(f);
11730 else
11731 rettv->vval.v_float = 0.0;
11732}
11733#endif
11734
11735/*
11736 * "test_alloc_fail(id, countdown, repeat)" function
11737 */
11738 static void
11739f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
11740{
11741 if (argvars[0].v_type != VAR_NUMBER
11742 || argvars[0].vval.v_number <= 0
11743 || argvars[1].v_type != VAR_NUMBER
11744 || argvars[1].vval.v_number < 0
11745 || argvars[2].v_type != VAR_NUMBER)
11746 EMSG(_(e_invarg));
11747 else
11748 {
11749 alloc_fail_id = argvars[0].vval.v_number;
11750 if (alloc_fail_id >= aid_last)
11751 EMSG(_(e_invarg));
11752 alloc_fail_countdown = argvars[1].vval.v_number;
11753 alloc_fail_repeat = argvars[2].vval.v_number;
11754 did_outofmem_msg = FALSE;
11755 }
11756}
11757
11758/*
11759 * "test_autochdir()"
11760 */
11761 static void
11762f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11763{
11764#if defined(FEAT_AUTOCHDIR)
11765 test_autochdir = TRUE;
11766#endif
11767}
11768
11769/*
11770 * "test_disable_char_avail({expr})" function
11771 */
11772 static void
11773f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
11774{
11775 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
11776}
11777
11778/*
11779 * "test_garbagecollect_now()" function
11780 */
11781 static void
11782f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11783{
11784 /* This is dangerous, any Lists and Dicts used internally may be freed
11785 * while still in use. */
11786 garbage_collect(TRUE);
11787}
11788
11789#ifdef FEAT_JOB_CHANNEL
11790 static void
11791f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
11792{
11793 rettv->v_type = VAR_CHANNEL;
11794 rettv->vval.v_channel = NULL;
11795}
11796#endif
11797
11798 static void
11799f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
11800{
11801 rettv->v_type = VAR_DICT;
11802 rettv->vval.v_dict = NULL;
11803}
11804
11805#ifdef FEAT_JOB_CHANNEL
11806 static void
11807f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
11808{
11809 rettv->v_type = VAR_JOB;
11810 rettv->vval.v_job = NULL;
11811}
11812#endif
11813
11814 static void
11815f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
11816{
11817 rettv->v_type = VAR_LIST;
11818 rettv->vval.v_list = NULL;
11819}
11820
11821 static void
11822f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
11823{
11824 rettv->v_type = VAR_PARTIAL;
11825 rettv->vval.v_partial = NULL;
11826}
11827
11828 static void
11829f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
11830{
11831 rettv->v_type = VAR_STRING;
11832 rettv->vval.v_string = NULL;
11833}
11834
11835 static void
11836f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
11837{
11838 time_for_testing = (time_t)get_tv_number(&argvars[0]);
11839}
11840
11841#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
11842/*
11843 * Get a callback from "arg". It can be a Funcref or a function name.
11844 * When "arg" is zero return an empty string.
11845 * Return NULL for an invalid argument.
11846 */
11847 char_u *
11848get_callback(typval_T *arg, partial_T **pp)
11849{
11850 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
11851 {
11852 *pp = arg->vval.v_partial;
11853 ++(*pp)->pt_refcount;
11854 return (*pp)->pt_name;
11855 }
11856 *pp = NULL;
11857 if (arg->v_type == VAR_FUNC)
11858 {
11859 func_ref(arg->vval.v_string);
11860 return arg->vval.v_string;
11861 }
11862 if (arg->v_type == VAR_STRING)
11863 return arg->vval.v_string;
11864 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
11865 return (char_u *)"";
11866 EMSG(_("E921: Invalid callback argument"));
11867 return NULL;
11868}
11869
11870/*
11871 * Unref/free "callback" and "partial" retured by get_callback().
11872 */
11873 void
11874free_callback(char_u *callback, partial_T *partial)
11875{
11876 if (partial != NULL)
11877 partial_unref(partial);
11878 else if (callback != NULL)
11879 {
11880 func_unref(callback);
11881 vim_free(callback);
11882 }
11883}
11884#endif
11885
11886#ifdef FEAT_TIMERS
11887/*
11888 * "timer_start(time, callback [, options])" function
11889 */
11890 static void
11891f_timer_start(typval_T *argvars, typval_T *rettv)
11892{
11893 long msec = (long)get_tv_number(&argvars[0]);
11894 timer_T *timer;
11895 int repeat = 0;
11896 char_u *callback;
11897 dict_T *dict;
11898
11899 if (check_secure())
11900 return;
11901 if (argvars[2].v_type != VAR_UNKNOWN)
11902 {
11903 if (argvars[2].v_type != VAR_DICT
11904 || (dict = argvars[2].vval.v_dict) == NULL)
11905 {
11906 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
11907 return;
11908 }
11909 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
11910 repeat = get_dict_number(dict, (char_u *)"repeat");
11911 }
11912
11913 timer = create_timer(msec, repeat);
11914 callback = get_callback(&argvars[1], &timer->tr_partial);
11915 if (callback == NULL)
11916 {
11917 stop_timer(timer);
11918 rettv->vval.v_number = -1;
11919 }
11920 else
11921 {
11922 timer->tr_callback = vim_strsave(callback);
11923 rettv->vval.v_number = timer->tr_id;
11924 }
11925}
11926
11927/*
11928 * "timer_stop(timer)" function
11929 */
11930 static void
11931f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
11932{
11933 timer_T *timer;
11934
11935 if (argvars[0].v_type != VAR_NUMBER)
11936 {
11937 EMSG(_(e_number_exp));
11938 return;
11939 }
11940 timer = find_timer((int)get_tv_number(&argvars[0]));
11941 if (timer != NULL)
11942 stop_timer(timer);
11943}
11944#endif
11945
11946/*
11947 * "tolower(string)" function
11948 */
11949 static void
11950f_tolower(typval_T *argvars, typval_T *rettv)
11951{
11952 char_u *p;
11953
11954 p = vim_strsave(get_tv_string(&argvars[0]));
11955 rettv->v_type = VAR_STRING;
11956 rettv->vval.v_string = p;
11957
11958 if (p != NULL)
11959 while (*p != NUL)
11960 {
11961#ifdef FEAT_MBYTE
11962 int l;
11963
11964 if (enc_utf8)
11965 {
11966 int c, lc;
11967
11968 c = utf_ptr2char(p);
11969 lc = utf_tolower(c);
11970 l = utf_ptr2len(p);
11971 /* TODO: reallocate string when byte count changes. */
11972 if (utf_char2len(lc) == l)
11973 utf_char2bytes(lc, p);
11974 p += l;
11975 }
11976 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
11977 p += l; /* skip multi-byte character */
11978 else
11979#endif
11980 {
11981 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
11982 ++p;
11983 }
11984 }
11985}
11986
11987/*
11988 * "toupper(string)" function
11989 */
11990 static void
11991f_toupper(typval_T *argvars, typval_T *rettv)
11992{
11993 rettv->v_type = VAR_STRING;
11994 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
11995}
11996
11997/*
11998 * "tr(string, fromstr, tostr)" function
11999 */
12000 static void
12001f_tr(typval_T *argvars, typval_T *rettv)
12002{
12003 char_u *in_str;
12004 char_u *fromstr;
12005 char_u *tostr;
12006 char_u *p;
12007#ifdef FEAT_MBYTE
12008 int inlen;
12009 int fromlen;
12010 int tolen;
12011 int idx;
12012 char_u *cpstr;
12013 int cplen;
12014 int first = TRUE;
12015#endif
12016 char_u buf[NUMBUFLEN];
12017 char_u buf2[NUMBUFLEN];
12018 garray_T ga;
12019
12020 in_str = get_tv_string(&argvars[0]);
12021 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12022 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12023
12024 /* Default return value: empty string. */
12025 rettv->v_type = VAR_STRING;
12026 rettv->vval.v_string = NULL;
12027 if (fromstr == NULL || tostr == NULL)
12028 return; /* type error; errmsg already given */
12029 ga_init2(&ga, (int)sizeof(char), 80);
12030
12031#ifdef FEAT_MBYTE
12032 if (!has_mbyte)
12033#endif
12034 /* not multi-byte: fromstr and tostr must be the same length */
12035 if (STRLEN(fromstr) != STRLEN(tostr))
12036 {
12037#ifdef FEAT_MBYTE
12038error:
12039#endif
12040 EMSG2(_(e_invarg2), fromstr);
12041 ga_clear(&ga);
12042 return;
12043 }
12044
12045 /* fromstr and tostr have to contain the same number of chars */
12046 while (*in_str != NUL)
12047 {
12048#ifdef FEAT_MBYTE
12049 if (has_mbyte)
12050 {
12051 inlen = (*mb_ptr2len)(in_str);
12052 cpstr = in_str;
12053 cplen = inlen;
12054 idx = 0;
12055 for (p = fromstr; *p != NUL; p += fromlen)
12056 {
12057 fromlen = (*mb_ptr2len)(p);
12058 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12059 {
12060 for (p = tostr; *p != NUL; p += tolen)
12061 {
12062 tolen = (*mb_ptr2len)(p);
12063 if (idx-- == 0)
12064 {
12065 cplen = tolen;
12066 cpstr = p;
12067 break;
12068 }
12069 }
12070 if (*p == NUL) /* tostr is shorter than fromstr */
12071 goto error;
12072 break;
12073 }
12074 ++idx;
12075 }
12076
12077 if (first && cpstr == in_str)
12078 {
12079 /* Check that fromstr and tostr have the same number of
12080 * (multi-byte) characters. Done only once when a character
12081 * of in_str doesn't appear in fromstr. */
12082 first = FALSE;
12083 for (p = tostr; *p != NUL; p += tolen)
12084 {
12085 tolen = (*mb_ptr2len)(p);
12086 --idx;
12087 }
12088 if (idx != 0)
12089 goto error;
12090 }
12091
12092 (void)ga_grow(&ga, cplen);
12093 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12094 ga.ga_len += cplen;
12095
12096 in_str += inlen;
12097 }
12098 else
12099#endif
12100 {
12101 /* When not using multi-byte chars we can do it faster. */
12102 p = vim_strchr(fromstr, *in_str);
12103 if (p != NULL)
12104 ga_append(&ga, tostr[p - fromstr]);
12105 else
12106 ga_append(&ga, *in_str);
12107 ++in_str;
12108 }
12109 }
12110
12111 /* add a terminating NUL */
12112 (void)ga_grow(&ga, 1);
12113 ga_append(&ga, NUL);
12114
12115 rettv->vval.v_string = ga.ga_data;
12116}
12117
12118#ifdef FEAT_FLOAT
12119/*
12120 * "trunc({float})" function
12121 */
12122 static void
12123f_trunc(typval_T *argvars, typval_T *rettv)
12124{
12125 float_T f = 0.0;
12126
12127 rettv->v_type = VAR_FLOAT;
12128 if (get_float_arg(argvars, &f) == OK)
12129 /* trunc() is not in C90, use floor() or ceil() instead. */
12130 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12131 else
12132 rettv->vval.v_float = 0.0;
12133}
12134#endif
12135
12136/*
12137 * "type(expr)" function
12138 */
12139 static void
12140f_type(typval_T *argvars, typval_T *rettv)
12141{
12142 int n = -1;
12143
12144 switch (argvars[0].v_type)
12145 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012146 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12147 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012148 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012149 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12150 case VAR_LIST: n = VAR_TYPE_LIST; break;
12151 case VAR_DICT: n = VAR_TYPE_DICT; break;
12152 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153 case VAR_SPECIAL:
12154 if (argvars[0].vval.v_number == VVAL_FALSE
12155 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012156 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012157 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012158 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012159 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012160 case VAR_JOB: n = VAR_TYPE_JOB; break;
12161 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012162 case VAR_UNKNOWN:
12163 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
12164 n = -1;
12165 break;
12166 }
12167 rettv->vval.v_number = n;
12168}
12169
12170/*
12171 * "undofile(name)" function
12172 */
12173 static void
12174f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12175{
12176 rettv->v_type = VAR_STRING;
12177#ifdef FEAT_PERSISTENT_UNDO
12178 {
12179 char_u *fname = get_tv_string(&argvars[0]);
12180
12181 if (*fname == NUL)
12182 {
12183 /* If there is no file name there will be no undo file. */
12184 rettv->vval.v_string = NULL;
12185 }
12186 else
12187 {
12188 char_u *ffname = FullName_save(fname, FALSE);
12189
12190 if (ffname != NULL)
12191 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12192 vim_free(ffname);
12193 }
12194 }
12195#else
12196 rettv->vval.v_string = NULL;
12197#endif
12198}
12199
12200/*
12201 * "undotree()" function
12202 */
12203 static void
12204f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12205{
12206 if (rettv_dict_alloc(rettv) == OK)
12207 {
12208 dict_T *dict = rettv->vval.v_dict;
12209 list_T *list;
12210
12211 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12212 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12213 dict_add_nr_str(dict, "save_last",
12214 (long)curbuf->b_u_save_nr_last, NULL);
12215 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12216 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12217 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12218
12219 list = list_alloc();
12220 if (list != NULL)
12221 {
12222 u_eval_tree(curbuf->b_u_oldhead, list);
12223 dict_add_list(dict, "entries", list);
12224 }
12225 }
12226}
12227
12228/*
12229 * "values(dict)" function
12230 */
12231 static void
12232f_values(typval_T *argvars, typval_T *rettv)
12233{
12234 dict_list(argvars, rettv, 1);
12235}
12236
12237/*
12238 * "virtcol(string)" function
12239 */
12240 static void
12241f_virtcol(typval_T *argvars, typval_T *rettv)
12242{
12243 colnr_T vcol = 0;
12244 pos_T *fp;
12245 int fnum = curbuf->b_fnum;
12246
12247 fp = var2fpos(&argvars[0], FALSE, &fnum);
12248 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12249 && fnum == curbuf->b_fnum)
12250 {
12251 getvvcol(curwin, fp, NULL, NULL, &vcol);
12252 ++vcol;
12253 }
12254
12255 rettv->vval.v_number = vcol;
12256}
12257
12258/*
12259 * "visualmode()" function
12260 */
12261 static void
12262f_visualmode(typval_T *argvars, typval_T *rettv)
12263{
12264 char_u str[2];
12265
12266 rettv->v_type = VAR_STRING;
12267 str[0] = curbuf->b_visual_mode_eval;
12268 str[1] = NUL;
12269 rettv->vval.v_string = vim_strsave(str);
12270
12271 /* A non-zero number or non-empty string argument: reset mode. */
12272 if (non_zero_arg(&argvars[0]))
12273 curbuf->b_visual_mode_eval = NUL;
12274}
12275
12276/*
12277 * "wildmenumode()" function
12278 */
12279 static void
12280f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12281{
12282#ifdef FEAT_WILDMENU
12283 if (wild_menu_showing)
12284 rettv->vval.v_number = 1;
12285#endif
12286}
12287
12288/*
12289 * "winbufnr(nr)" function
12290 */
12291 static void
12292f_winbufnr(typval_T *argvars, typval_T *rettv)
12293{
12294 win_T *wp;
12295
12296 wp = find_win_by_nr(&argvars[0], NULL);
12297 if (wp == NULL)
12298 rettv->vval.v_number = -1;
12299 else
12300 rettv->vval.v_number = wp->w_buffer->b_fnum;
12301}
12302
12303/*
12304 * "wincol()" function
12305 */
12306 static void
12307f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12308{
12309 validate_cursor();
12310 rettv->vval.v_number = curwin->w_wcol + 1;
12311}
12312
12313/*
12314 * "winheight(nr)" function
12315 */
12316 static void
12317f_winheight(typval_T *argvars, typval_T *rettv)
12318{
12319 win_T *wp;
12320
12321 wp = find_win_by_nr(&argvars[0], NULL);
12322 if (wp == NULL)
12323 rettv->vval.v_number = -1;
12324 else
12325 rettv->vval.v_number = wp->w_height;
12326}
12327
12328/*
12329 * "winline()" function
12330 */
12331 static void
12332f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12333{
12334 validate_cursor();
12335 rettv->vval.v_number = curwin->w_wrow + 1;
12336}
12337
12338/*
12339 * "winnr()" function
12340 */
12341 static void
12342f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12343{
12344 int nr = 1;
12345
12346#ifdef FEAT_WINDOWS
12347 nr = get_winnr(curtab, &argvars[0]);
12348#endif
12349 rettv->vval.v_number = nr;
12350}
12351
12352/*
12353 * "winrestcmd()" function
12354 */
12355 static void
12356f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12357{
12358#ifdef FEAT_WINDOWS
12359 win_T *wp;
12360 int winnr = 1;
12361 garray_T ga;
12362 char_u buf[50];
12363
12364 ga_init2(&ga, (int)sizeof(char), 70);
12365 for (wp = firstwin; wp != NULL; wp = wp->w_next)
12366 {
12367 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12368 ga_concat(&ga, buf);
12369 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12370 ga_concat(&ga, buf);
12371 ++winnr;
12372 }
12373 ga_append(&ga, NUL);
12374
12375 rettv->vval.v_string = ga.ga_data;
12376#else
12377 rettv->vval.v_string = NULL;
12378#endif
12379 rettv->v_type = VAR_STRING;
12380}
12381
12382/*
12383 * "winrestview()" function
12384 */
12385 static void
12386f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12387{
12388 dict_T *dict;
12389
12390 if (argvars[0].v_type != VAR_DICT
12391 || (dict = argvars[0].vval.v_dict) == NULL)
12392 EMSG(_(e_invarg));
12393 else
12394 {
12395 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
12396 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
12397 if (dict_find(dict, (char_u *)"col", -1) != NULL)
12398 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
12399#ifdef FEAT_VIRTUALEDIT
12400 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
12401 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
12402#endif
12403 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12404 {
12405 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
12406 curwin->w_set_curswant = FALSE;
12407 }
12408
12409 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
12410 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
12411#ifdef FEAT_DIFF
12412 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
12413 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
12414#endif
12415 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
12416 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
12417 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
12418 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
12419
12420 check_cursor();
12421 win_new_height(curwin, curwin->w_height);
12422# ifdef FEAT_WINDOWS
12423 win_new_width(curwin, W_WIDTH(curwin));
12424# endif
12425 changed_window_setting();
12426
12427 if (curwin->w_topline <= 0)
12428 curwin->w_topline = 1;
12429 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
12430 curwin->w_topline = curbuf->b_ml.ml_line_count;
12431#ifdef FEAT_DIFF
12432 check_topfill(curwin, TRUE);
12433#endif
12434 }
12435}
12436
12437/*
12438 * "winsaveview()" function
12439 */
12440 static void
12441f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
12442{
12443 dict_T *dict;
12444
12445 if (rettv_dict_alloc(rettv) == FAIL)
12446 return;
12447 dict = rettv->vval.v_dict;
12448
12449 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
12450 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
12451#ifdef FEAT_VIRTUALEDIT
12452 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
12453#endif
12454 update_curswant();
12455 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
12456
12457 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
12458#ifdef FEAT_DIFF
12459 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
12460#endif
12461 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
12462 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
12463}
12464
12465/*
12466 * "winwidth(nr)" function
12467 */
12468 static void
12469f_winwidth(typval_T *argvars, typval_T *rettv)
12470{
12471 win_T *wp;
12472
12473 wp = find_win_by_nr(&argvars[0], NULL);
12474 if (wp == NULL)
12475 rettv->vval.v_number = -1;
12476 else
12477#ifdef FEAT_WINDOWS
12478 rettv->vval.v_number = wp->w_width;
12479#else
12480 rettv->vval.v_number = Columns;
12481#endif
12482}
12483
12484/*
12485 * "wordcount()" function
12486 */
12487 static void
12488f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
12489{
12490 if (rettv_dict_alloc(rettv) == FAIL)
12491 return;
12492 cursor_pos_info(rettv->vval.v_dict);
12493}
12494
12495/*
12496 * "writefile()" function
12497 */
12498 static void
12499f_writefile(typval_T *argvars, typval_T *rettv)
12500{
12501 int binary = FALSE;
12502 int append = FALSE;
12503 char_u *fname;
12504 FILE *fd;
12505 int ret = 0;
12506
12507 if (check_restricted() || check_secure())
12508 return;
12509
12510 if (argvars[0].v_type != VAR_LIST)
12511 {
12512 EMSG2(_(e_listarg), "writefile()");
12513 return;
12514 }
12515 if (argvars[0].vval.v_list == NULL)
12516 return;
12517
12518 if (argvars[2].v_type != VAR_UNKNOWN)
12519 {
12520 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
12521 binary = TRUE;
12522 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
12523 append = TRUE;
12524 }
12525
12526 /* Always open the file in binary mode, library functions have a mind of
12527 * their own about CR-LF conversion. */
12528 fname = get_tv_string(&argvars[1]);
12529 if (*fname == NUL || (fd = mch_fopen((char *)fname,
12530 append ? APPENDBIN : WRITEBIN)) == NULL)
12531 {
12532 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
12533 ret = -1;
12534 }
12535 else
12536 {
12537 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
12538 ret = -1;
12539 fclose(fd);
12540 }
12541
12542 rettv->vval.v_number = ret;
12543}
12544
12545/*
12546 * "xor(expr, expr)" function
12547 */
12548 static void
12549f_xor(typval_T *argvars, typval_T *rettv)
12550{
12551 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
12552 ^ get_tv_number_chk(&argvars[1], NULL);
12553}
12554
12555
12556#endif /* FEAT_EVAL */