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