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