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