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