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